blob: 407104ff2f33da08656b936570ec05bd60ea5c27 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080061
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/gsm/gsm48.h>
63#include <osmocom/gsm/gsm0480.h>
64#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020065#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/core/msgb.h>
67#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010068#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010069#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000070
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020071#include <assert.h>
72
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020073void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010074void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020075
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020076static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
77
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020078static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080079static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020080 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010081static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080082static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Andreas Eversberga83d5112013-12-07 18:32:28 +010090static int apply_codec_restrictions(struct gsm_bts *bts,
91 struct gsm_mncc_bearer_cap *bcap)
92{
93 int i, j;
94
95 /* remove unsupported speech versions from list */
96 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
103 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
104 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
105 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
106 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
107 }
108 bcap->speech_ver[j] = -1;
109
110 return 0;
111}
112
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200113static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800114
Harald Welte31c00f72011-03-03 23:29:05 +0100115void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
116{
117 net->mncc_recv(net, msg);
118}
119
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
121 struct gsm_trans *trans)
122{
123 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
124
125 /* if we get passed a transaction reference, do some common
126 * work that the caller no longer has to do */
127 if (trans) {
128 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
129 msg->lchan = trans->conn->lchan;
130 }
131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800132 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200133 struct e1inp_sign_link *sign_link =
134 msg->lchan->ts->trx->rsl_link;
135
136 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100137 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800138 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending '%s' to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr & 0xf0,
143 gsm48_cc_msg_name(gh->msg_type));
144 else
145 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200146 "Sending 0x%02x to MS.\n",
147 sign_link->trx->bts->nr,
148 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800149 gh->proto_discr, gh->msg_type);
150 }
151
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100152 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800153}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800155int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
156{
157 struct gsm48_hdr *gh;
158 struct msgb *ss_notify;
159
160 ss_notify = gsm0480_create_notifySS(message);
161 if (!ss_notify)
162 return -1;
163
164 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
165 uint8_t *data = msgb_push(ss_notify, 1);
166 data[0] = ss_notify->len - 1;
167 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
168 gh->msg_type = GSM48_MT_CC_FACILITY;
169 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
170}
171
Harald Weltecf149ee2012-01-23 16:40:24 +0100172void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173{
174 if (!conn->sec_operation)
175 return;
176
177 talloc_free(conn->sec_operation);
178 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800179 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100180}
181
Harald Weltecf149ee2012-01-23 16:40:24 +0100182void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100184 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
185 struct gsm_security_operation);
186}
187
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100188int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
189 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100190{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200191 struct gsm_network *net = conn->network;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800192 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100193 struct gsm_security_operation *op;
194 struct gsm_auth_tuple atuple;
195 int status = -1, rc;
196
197 /* Check if we _can_ enable encryption. Cases where we can't:
198 * - Encryption disabled in config
199 * - Channel already secured (nothing to do)
200 * - Subscriber equipment doesn't support configured encryption
201 */
202 if (!net->a5_encryption) {
203 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800204 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100205 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200206 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100207 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
208 net->a5_encryption)) {
209 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* If not done yet, try to get info for this user */
214 if (status < 0) {
215 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
216 if (rc <= 0)
217 status = GSM_SECURITY_NOAVAIL;
218 }
219
220 /* Are we done yet ? */
221 if (status >= 0)
222 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100224 0;
225
226 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800227 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100228 return -EBUSY;
229
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800230 allocate_security_operation(conn);
231 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100232 op->cb = cb;
233 op->cb_data = cb_data;
234 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
235
236 /* FIXME: Should start a timer for completion ... */
237
238 /* Then do whatever is needed ... */
Neels Hofmeyrd2fa7a52016-03-10 23:30:37 +0100239 if (rc == AUTH_DO_AUTH_THEN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100240 /* Start authentication */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100241 return gsm48_tx_mm_auth_req(conn, op->atuple.vec.rand, NULL,
242 op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100243 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100244 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100245 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +0200246 op->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100247 }
248
249 return -EINVAL; /* not reached */
250}
251
Maxddee01f2016-05-24 14:23:27 +0200252static bool subscr_regexp_check(const struct gsm_network *net, const char *imsi)
253{
254 if (!net->authorized_reg_str)
255 return false;
256
257 if (regexec(&net->authorized_regexp, imsi, 0, NULL, 0) != REG_NOMATCH)
258 return true;
259
260 return false;
261}
262
Holger Freyther73487a22008-12-31 18:53:57 +0000263static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
264 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000265{
266 if (!subscriber)
267 return 0;
268
Holger Freyther73487a22008-12-31 18:53:57 +0000269 /*
270 * Do not send accept yet as more information should arrive. Some
271 * phones will not send us the information and we will have to check
272 * what we want to do with that.
273 */
274 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
275 return 0;
276
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100277 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200278 case GSM_AUTH_POLICY_CLOSED:
279 return subscriber->authorized;
Maxddee01f2016-05-24 14:23:27 +0200280 case GSM_AUTH_POLICY_REGEXP:
281 if (subscriber->authorized)
282 return 1;
283 if (subscr_regexp_check(subscriber->group->net,
284 subscriber->imsi))
285 subscriber->authorized = 1;
286 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200287 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200288 if (subscriber->authorized)
289 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200290 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200291 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000292 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200293 default:
294 return 0;
295 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000296}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000297
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100298static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000299{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100300 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000301 return;
302
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800303 /* No need to keep the connection up */
304 release_anchor(conn);
305
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200306 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100307 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100308 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100309 if (release)
310 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000311}
312
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100313static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000314{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800315 if (conn->loc_operation)
316 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100317 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000318
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100319 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200320 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000321}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000322
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200323static int finish_lu(struct gsm_subscriber_connection *conn)
324{
325 int rc = 0;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200326 int avoid_tmsi = conn->network->avoid_tmsi;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200327
328 /* We're all good */
329 if (avoid_tmsi) {
330 conn->subscr->tmsi = GSM_RESERVED_TMSI;
331 db_sync_subscriber(conn->subscr);
332 } else {
333 db_subscriber_alloc_tmsi(conn->subscr);
334 }
335
336 rc = gsm0408_loc_upd_acc(conn);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200337 if (conn->network->send_mm_info) {
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200338 /* send MM INFO with network name */
339 rc = gsm48_tx_mm_info(conn);
340 }
341
342 /* call subscr_update after putting the loc_upd_acc
343 * in the transmit queue, since S_SUBSCR_ATTACHED might
344 * trigger further action like SMS delivery */
345 subscr_update(conn->subscr, conn->bts,
346 GSM_SUBSCRIBER_UPDATE_ATTACHED);
347
348 /*
349 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
350 * MS needs to respond with a TMSI REALLOCATION COMPLETE
351 * (even if the TMSI is the same).
352 */
353 if (avoid_tmsi)
354 release_loc_updating_req(conn, 1);
355
356 return rc;
357}
358
Sylvain Munaut267fba02009-12-24 00:28:01 +0100359static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
360 struct msgb *msg, void *data, void *param)
361{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800362 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100363 int rc = 0;
364
365 switch (event) {
366 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100367 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 break;
369
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200370 case GSM_SECURITY_ALREADY:
371 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
372 "UPDATING after CM SERVICE REQUEST\n");
373 /* fall through */
374
Sylvain Munaut267fba02009-12-24 00:28:01 +0100375 case GSM_SECURITY_NOAVAIL:
376 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200377 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100378 break;
379
380 default:
381 rc = -EINVAL;
382 };
383
384 return rc;
385}
386
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100387static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000388{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100389 if (!conn->loc_operation)
390 return 0;
391
Sylvain Munaut267fba02009-12-24 00:28:01 +0100392 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800393 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100394 conn->loc_operation->key_seq,
395 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000396 return 0;
397}
398
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800399void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000400{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800401 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800402
403 /* avoid someone issuing a clear */
404 conn->in_release = 1;
405
Holger Freyther7c19f742009-06-06 13:54:35 +0000406 /*
407 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800408 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000409 */
Neels Hofmeyraea28ce2016-05-04 15:30:30 +0200410 release_loc_updating_req(conn, 0);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100411
412 /* We might need to cancel the paging response or such. */
413 if (conn->sec_operation && conn->sec_operation->cb) {
414 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
415 NULL, conn, conn->sec_operation->cb_data);
416 }
417
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800418 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800419 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000420
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100421 /*
422 * Free all transactions that are associated with the released
423 * connection. The transaction code will inform the CC or SMS
424 * facilities that will send the release indications. As part of
425 * the CC REL_IND the remote leg might be released and this will
426 * trigger the call to trans_free. This is something the llist
427 * macro can not handle and we will need to re-iterate the list.
428 *
429 * TODO: Move the trans_list into the subscriber connection and
430 * create a pending list for MT transactions. These exist before
431 * we have a subscriber connection.
432 */
433restart:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200434 llist_for_each_entry_safe(trans, temp, &conn->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100435 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200436 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100437 goto restart;
438 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800439 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000440}
441
Harald Welte371efe52010-12-22 23:17:50 +0100442void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
443{
444 struct gsm_trans *trans, *temp;
445
446 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
447
448 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100449 if (trans->protocol == protocol) {
450 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100451 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100452 }
Harald Welte371efe52010-12-22 23:17:50 +0100453 }
454}
455
Holger Freyther429e7762008-12-30 13:28:30 +0000456/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200457int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000458{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800459 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800460 struct msgb *msg;
461
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200462 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_REJECT]);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800463
464 msg = gsm48_create_loc_upd_rej(cause);
465 if (!msg) {
466 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
467 return -1;
468 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200469
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800470 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000471
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100472 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte7184bd02015-12-28 14:04:36 +0100473 "LAC=%u BTS=%u\n", subscr_name(conn->subscr),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800474 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100475
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800476 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000477}
478
479/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200480static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000481{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100482 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000483 struct gsm48_hdr *gh;
484 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200485 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200486
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800487 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000488
489 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
490 gh->proto_discr = GSM48_PDISC_MM;
491 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
492
493 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200494 gsm48_generate_lai(lai, conn->network->country_code,
495 conn->network->network_code,
496 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000497
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200498 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
499 uint8_t mi[10];
500 int len;
501 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
502 mid = msgb_put(msg, len);
503 memcpy(mid, mi, len);
504 } else {
505 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
506 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
507 }
Harald Welte52b1f982008-12-23 20:25:15 +0000508
509 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
510
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200511 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_RESP_ACCEPT]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100512
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800513 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000514}
515
Harald Weltebf5e8df2009-02-03 12:59:45 +0000516/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000518{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100519 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000520 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000521
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800522 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000523
524 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
525 gh->proto_discr = GSM48_PDISC_MM;
526 gh->msg_type = GSM48_MT_MM_ID_REQ;
527 gh->data[0] = id_type;
528
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800529 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000530}
531
Maxe21cf382016-05-11 11:01:06 +0200532static struct gsm_subscriber *subscr_create(const struct gsm_network *net,
533 const char *imsi)
534{
Maxe6052c42016-06-30 10:25:49 +0200535 if (!net->auto_create_subscr)
Maxddee01f2016-05-24 14:23:27 +0200536 return NULL;
Maxe21cf382016-05-11 11:01:06 +0200537
Maxe6052c42016-06-30 10:25:49 +0200538 if (!subscr_regexp_check(net, imsi))
539 return NULL;
Maxddee01f2016-05-24 14:23:27 +0200540
Maxe6052c42016-06-30 10:25:49 +0200541 return subscr_create_subscriber(net->subscr_group, imsi);
Maxe21cf382016-05-11 11:01:06 +0200542}
Harald Welte231ad4f2008-12-27 11:15:38 +0000543
Harald Weltebf5e8df2009-02-03 12:59:45 +0000544/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800545static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000546{
547 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200548 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200549 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200550 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000551
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200552 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200553 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
554 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000555
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200556 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100557
Harald Welte75a983f2008-12-27 21:34:06 +0000558 switch (mi_type) {
559 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200560 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100561 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100562 conn->subscr = subscr_get_by_imsi(net->subscr_group,
563 mi_string);
Maxe21cf382016-05-11 11:01:06 +0200564 if (!conn->subscr)
565 conn->subscr = subscr_create(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200566 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100567 if (!conn->subscr && conn->loc_operation) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200568 gsm0408_loc_upd_rej(conn, net->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100569 release_loc_updating_req(conn, 1);
570 return 0;
571 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572 if (conn->loc_operation)
573 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000574 break;
575 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000576 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000577 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100578 if (conn->subscr) {
579 db_subscriber_assoc_imei(conn->subscr, mi_string);
580 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200581 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100582 if (conn->loc_operation)
583 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000584 break;
585 }
Holger Freyther73487a22008-12-31 18:53:57 +0000586
587 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100588 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000589}
590
Harald Welte255539c2008-12-28 02:26:27 +0000591
592static void loc_upd_rej_cb(void *data)
593{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100594 struct gsm_subscriber_connection *conn = data;
Harald Welte255539c2008-12-28 02:26:27 +0000595
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100596 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200597 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100598 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000599}
600
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100601static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000602{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
604 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200605 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000606}
607
Harald Welteb9845f92015-08-16 18:07:48 +0200608static const struct value_string lupd_names[] = {
609 { GSM48_LUPD_NORMAL, "NORMAL" },
610 { GSM48_LUPD_PERIODIC, "PERIODIC" },
611 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
612 { 0, NULL }
613};
Harald Welte2a139372009-02-22 21:14:55 +0000614
Harald Weltebf5e8df2009-02-03 12:59:45 +0000615/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800616static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000617{
Harald Welte8470bf22008-12-25 23:28:35 +0000618 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000619 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800620 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200621 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200622 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000623
Harald Welte8470bf22008-12-25 23:28:35 +0000624 lu = (struct gsm48_loc_upd_req *) gh->data;
625
626 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000627
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200628 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000629
Harald Welteb9845f92015-08-16 18:07:48 +0200630 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
631 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000632
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200633 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100634
Harald Welte24ff6ee2009-12-22 00:41:05 +0100635 switch (lu->type) {
636 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200637 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100638 break;
639 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200640 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100641 break;
642 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200643 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100644 break;
645 }
646
Holger Freythereaf04692009-06-06 13:54:44 +0000647 /*
648 * Pseudo Spoof detection: Just drop a second/concurrent
649 * location updating request.
650 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100651 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200652 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100653 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800654 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000655 return 0;
656 }
657
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800658 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000659
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200660 conn->loc_operation->key_seq = lu->key_seq;
661
Harald Welte52b1f982008-12-23 20:25:15 +0000662 switch (mi_type) {
663 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200664 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000665 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200666 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100667 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000668
Jan Luebbe370b41d2009-08-12 10:19:34 +0200669 /* look up subscriber based on IMSI, create if not found */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200670 subscr = subscr_get_by_imsi(conn->network->subscr_group, mi_string);
Maxe21cf382016-05-11 11:01:06 +0200671 if (!subscr)
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200672 subscr = subscr_create(conn->network, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100673 if (!subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200674 gsm0408_loc_upd_rej(conn, conn->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100675 release_loc_updating_req(conn, 0);
676 return 0;
677 }
Harald Welte4b634542008-12-27 01:55:51 +0000678 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000679 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200680 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000681 /* look up the subscriber based on TMSI, request IMSI if it fails */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200682 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200683 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000684 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000685 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200686 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100687 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000688 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200689 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200690 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200691 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000692 break;
693 case GSM_MI_TYPE_IMEI:
694 case GSM_MI_TYPE_IMEISV:
695 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200696 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000697 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200698 default:
Harald Weltea0368542009-06-27 02:58:43 +0200699 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000700 break;
701 }
702
Harald Welte24516ea2009-07-04 10:18:00 +0200703 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100704 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200705
Harald Welte4bfdfe72009-06-10 23:11:52 +0800706 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200707 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800708 /* FIXME: request id? close channel? */
709 return -EINVAL;
710 }
711
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100712 conn->subscr = subscr;
713 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000714
Harald Welte24516ea2009-07-04 10:18:00 +0200715 /* check if we can let the subscriber into our network immediately
716 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100717 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000718}
719
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100720/* Turn int into semi-octet representation: 98 => 0x89 */
721static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800722{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100723 uint8_t ret;
724
725 ret = value / 10;
726 ret |= (value % 10) << 4;
727
728 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800729}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100730
Harald Welte4bfdfe72009-06-10 23:11:52 +0800731
Harald Weltedb253af2008-12-30 17:56:55 +0000732/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800733int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000734{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100735 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000736 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200737 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200738 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200739 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100740
Harald Welte4bfdfe72009-06-10 23:11:52 +0800741 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100742 struct tm* gmt_time;
743 struct tm* local_time;
744 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100745 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000746
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800747 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000748
749 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
750 gh->proto_discr = GSM48_PDISC_MM;
751 gh->msg_type = GSM48_MT_MM_INFO;
752
753 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200754#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000755 name_len = strlen(net->name_long);
756 /* 10.5.3.5a */
757 ptr8 = msgb_put(msg, 3);
758 ptr8[0] = GSM48_IE_NAME_LONG;
759 ptr8[1] = name_len*2 +1;
760 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
761
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200762 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000763 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000764 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000765
766 /* FIXME: Use Cell Broadcast, not UCS-2, since
767 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200768#endif
769 name_len = (strlen(net->name_long)*7)/8;
770 name_pad = (8 - strlen(net->name_long)*7)%8;
771 if (name_pad > 0)
772 name_len++;
773 /* 10.5.3.5a */
774 ptr8 = msgb_put(msg, 3);
775 ptr8[0] = GSM48_IE_NAME_LONG;
776 ptr8[1] = name_len +1;
777 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
778
779 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100780 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200781
Harald Weltedb253af2008-12-30 17:56:55 +0000782 }
783
784 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200785#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000786 name_len = strlen(net->name_short);
787 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200788 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200789 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000790 ptr8[1] = name_len*2 + 1;
791 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
792
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200793 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000794 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000795 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200796#endif
797 name_len = (strlen(net->name_short)*7)/8;
798 name_pad = (8 - strlen(net->name_short)*7)%8;
799 if (name_pad > 0)
800 name_len++;
801 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200802 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200803 ptr8[0] = GSM48_IE_NAME_SHORT;
804 ptr8[1] = name_len +1;
805 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
806
807 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100808 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200809
Harald Weltedb253af2008-12-30 17:56:55 +0000810 }
811
Harald Weltedb253af2008-12-30 17:56:55 +0000812 /* Section 10.5.3.9 */
813 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100814 gmt_time = gmtime(&cur_t);
815
Harald Weltedb253af2008-12-30 17:56:55 +0000816 ptr8 = msgb_put(msg, 8);
817 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100818 ptr8[1] = bcdify(gmt_time->tm_year % 100);
819 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
820 ptr8[3] = bcdify(gmt_time->tm_mday);
821 ptr8[4] = bcdify(gmt_time->tm_hour);
822 ptr8[5] = bcdify(gmt_time->tm_min);
823 ptr8[6] = bcdify(gmt_time->tm_sec);
824
Neels Hofmeyr73983952016-05-10 13:29:33 +0200825 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200826 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200827 if (net->tz.hr < 0) {
828 tzunits = ((net->tz.hr/-1)*4);
829 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100830 ptr8[7] = bcdify(tzunits);
831 /* Set negative time */
832 ptr8[7] |= 0x08;
833 }
834 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200835 tzunits = net->tz.hr*4;
836 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100837 ptr8[7] = bcdify(tzunits);
838 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100839 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200840 if (net->tz.dst >= 0 && net->tz.dst <= 2)
841 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100842 }
843 else {
844 /* Need to get GSM offset and convert into 15 min units */
845 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
846 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200847#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100848 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200849#else
850#warning find a portable way to obtain the timezone offset
851 tzunits = 0;
852#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100853 if (tzunits < 0) {
854 tzunits = tzunits/-1;
855 ptr8[7] = bcdify(tzunits);
856 /* Flip it to negative */
857 ptr8[7] |= 0x08;
858 }
859 else
860 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100861
862 /* Does not support DST +2 */
863 if (local_time->tm_isdst)
864 dst = 1;
865 }
866
867 if (dst) {
868 ptr8 = msgb_put(msg, 3);
869 ptr8[0] = GSM48_IE_NET_DST;
870 ptr8[1] = 1;
871 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100872 }
Harald Weltedb253af2008-12-30 17:56:55 +0000873
Daniel Willmanneea93372009-08-13 03:42:07 +0200874 DEBUGP(DMM, "-> MM INFO\n");
875
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800876 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000877}
878
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100879/*! Send an Authentication Request to MS on the given subscriber connection
880 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
881 * \param[in] conn Subscriber connection to send on.
882 * \param[in] rand Random challenge token to send, must be 16 bytes long.
883 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
884 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
885 * \param[in] key_seq auth tuple's sequence number.
886 */
887int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
888 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200889{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100890 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200892 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200893
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200894 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100895 if (autn)
896 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200897
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800898 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200899 gh->proto_discr = GSM48_PDISC_MM;
900 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
901
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100902 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200903
Harald Welte7984d5c2009-08-12 22:56:50 +0200904 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100905 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200906 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200907 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200908
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100909
910 /* 16 bytes AUTN */
911 if (autn)
912 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
913
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800914 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200915}
916
917/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800918int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200919{
920 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800921 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200922}
923
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100924/*
925 * At the 30C3 phones miss their periodic update
926 * interval a lot and then remain unreachable. In case
927 * we still know the TMSI we can just attach it again.
928 */
929static void implit_attach(struct gsm_subscriber_connection *conn)
930{
931 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
932 return;
933
934 subscr_update(conn->subscr, conn->bts,
935 GSM_SUBSCRIBER_UPDATE_ATTACHED);
936}
937
938
Sylvain Munautba87f452009-12-24 00:28:46 +0100939static int _gsm48_rx_mm_serv_req_sec_cb(
940 unsigned int hooknum, unsigned int event,
941 struct msgb *msg, void *data, void *param)
942{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800943 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100944 int rc = 0;
945
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200946 /* auth failed or succeeded, the timer was stopped */
947 conn->expire_timer_stopped = 1;
948
Sylvain Munautba87f452009-12-24 00:28:46 +0100949 switch (event) {
950 case GSM_SECURITY_AUTH_FAILED:
951 /* Nothing to do */
952 break;
953
954 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200955 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800956 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100957 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100958 break;
959
960 case GSM_SECURITY_SUCCEEDED:
961 /* nothing to do. CIPHER MODE COMMAND is
962 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100963 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100964 break;
965
966 default:
967 rc = -EINVAL;
968 };
969
970 return rc;
971}
972
Harald Welte4ed0e922009-01-10 03:17:30 +0000973/*
974 * Handle CM Service Requests
975 * a) Verify that the packet is long enough to contain the information
976 * we require otherwsie reject with INCORRECT_MESSAGE
977 * b) Try to parse the TMSI. If we do not have one reject
978 * c) Check that we know the subscriber with the TMSI otherwise reject
979 * with a HLR cause
980 * d) Set the subscriber on the gsm_lchan and accept
981 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800982static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000983{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200984 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200985 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000986
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200987 struct gsm_network *network = conn->network;
Harald Welteba4cf162009-01-10 01:49:35 +0000988 struct gsm_subscriber *subscr;
989 struct gsm48_hdr *gh = msgb_l3(msg);
990 struct gsm48_service_request *req =
991 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800992 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200993 uint8_t classmark2_len = gh->data[1];
994 uint8_t *classmark2 = gh->data+2;
995 uint8_t mi_len = *(classmark2 + classmark2_len);
996 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000997
Harald Weltec9e02182009-05-01 19:07:53 +0000998 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000999 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001000 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001001 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001002 GSM48_REJECT_INCORRECT_MESSAGE);
1003 }
1004
1005 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001006 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001007 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001008 GSM48_REJECT_INCORRECT_MESSAGE);
1009 }
1010
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001011 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001012 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001013
1014 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001015 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1016 req->cm_service_type, gsm48_mi_type_name(mi_type),
1017 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001018 subscr = subscr_get_by_imsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001019 mi_string);
1020 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +02001021 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
1022 req->cm_service_type, gsm48_mi_type_name(mi_type),
1023 mi_string);
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001024 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +02001025 tmsi_from_string(mi_string));
1026 } else {
1027 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001028 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +00001029 GSM48_REJECT_INCORRECT_MESSAGE);
1030 }
1031
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001032 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +01001033
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001034 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +02001035 send_siemens_mrpci(msg->lchan, classmark2-1);
1036
Holger Freythereb443982009-06-04 13:58:42 +00001037
Harald Welte2a139372009-02-22 21:14:55 +00001038 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001039 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001041 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001042
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043 if (!conn->subscr)
1044 conn->subscr = subscr;
1045 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001046 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1047 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001048 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1049 subscr_put(subscr);
1050 }
1051
Harald Weltec2e302d2009-07-05 14:08:13 +02001052 subscr->equipment.classmark2_len = classmark2_len;
1053 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1054 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001055
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001056 /* we will send a MM message soon */
1057 conn->expire_timer_stopped = 1;
1058
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001060 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001061}
1062
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001063static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001064{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001065 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +00001066 struct gsm48_hdr *gh = msgb_l3(msg);
1067 struct gsm48_imsi_detach_ind *idi =
1068 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001069 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001070 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001071 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001072
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001073 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001074 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1075 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001076
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001077 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001078
Harald Welte2a139372009-02-22 21:14:55 +00001079 switch (mi_type) {
1080 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001081 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001082 subscr = subscr_get_by_tmsi(network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001083 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001084 break;
1085 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001086 DEBUGPC(DMM, "\n");
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001087 subscr = subscr_get_by_imsi(network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001088 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001089 break;
1090 case GSM_MI_TYPE_IMEI:
1091 case GSM_MI_TYPE_IMEISV:
1092 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001093 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001094 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001095 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001096 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001097 break;
1098 }
1099
Holger Freyther4a49e772009-04-12 05:37:29 +00001100 if (subscr) {
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001101 subscr_update(subscr, conn->bts,
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001102 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001103 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001104
1105 subscr->equipment.classmark1 = idi->classmark1;
1106 db_sync_equipment(&subscr->equipment);
1107
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001108 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001109 } else
Harald Welte2a139372009-02-22 21:14:55 +00001110 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1111
Harald Welte31981a02009-12-20 09:58:40 +01001112 /* FIXME: iterate over all transactions and release them,
1113 * imagine an IMSI DETACH happening during an active call! */
1114
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001115 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001116 return 0;
1117}
1118
Harald Welted2a7f5a2009-06-05 20:08:20 +00001119static int gsm48_rx_mm_status(struct msgb *msg)
1120{
1121 struct gsm48_hdr *gh = msgb_l3(msg);
1122
1123 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1124
1125 return 0;
1126}
1127
Sylvain Munaut30a15382009-12-24 00:27:26 +01001128/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001129static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001130{
1131 struct gsm48_hdr *gh = msgb_l3(msg);
1132 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001133 struct gsm_network *net = conn->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001134
Sylvain Munautc593cf12010-06-10 23:51:41 +02001135 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001136 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001137
1138 /* Safety check */
1139 if (!conn->sec_operation) {
1140 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1141 return -EIO;
1142 }
1143
1144 /* Validate SRES */
Harald Welte121e9a42016-04-20 13:13:19 +02001145 if (memcmp(conn->sec_operation->atuple.vec.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001146 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001147 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001148
1149 DEBUGPC(DMM, "Invalid (expected %s)\n",
Harald Welte121e9a42016-04-20 13:13:19 +02001150 osmo_hexdump(conn->sec_operation->atuple.vec.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001151
Sylvain Munaut30a15382009-12-24 00:27:26 +01001152 if (cb)
1153 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001154 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001155
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001156 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001157 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001158 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001159 }
1160
Sylvain Munautc593cf12010-06-10 23:51:41 +02001161 DEBUGPC(DMM, "OK\n");
1162
Sylvain Munaut30a15382009-12-24 00:27:26 +01001163 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001164 return gsm0808_cipher_mode(conn, net->a5_encryption,
Harald Welte121e9a42016-04-20 13:13:19 +02001165 conn->sec_operation->atuple.vec.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001166}
1167
Harald Weltebf5e8df2009-02-03 12:59:45 +00001168/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001169static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001170{
1171 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001172 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001173
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001174 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001175 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001176 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001177 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001178 break;
1179 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001180 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001181 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001182 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001183 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001184 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001185 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001186 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001187 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001188 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001189 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Harald Welte7184bd02015-12-28 14:04:36 +01001190 subscr_name(conn->subscr));
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001191 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001192 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001193 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001194 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001195 break;
1196 case GSM48_MT_MM_CM_REEST_REQ:
1197 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1198 break;
1199 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001200 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001201 break;
1202 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001203 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001204 gh->msg_type);
1205 break;
1206 }
1207
1208 return rc;
1209}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001210
Harald Welte2d35ae62009-02-06 12:02:13 +00001211/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001212static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001213{
1214 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001215 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001216 uint8_t *classmark2_lv = gh->data + 1;
1217 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001218 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001219 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001220 int rc = 0;
1221
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001222 resp = (struct gsm48_pag_resp *) &gh->data[0];
1223 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1224 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001225 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1226 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001227
Harald Weltefe18d8f2009-02-22 21:14:24 +00001228 switch (mi_type) {
1229 case GSM_MI_TYPE_TMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001230 subscr = subscr_get_by_tmsi(conn->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001231 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001232 break;
1233 case GSM_MI_TYPE_IMSI:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02001234 subscr = subscr_get_by_imsi(conn->network->subscr_group,
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001235 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001236 break;
1237 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001238
1239 if (!subscr) {
1240 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001241 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001242 return -EINVAL;
1243 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001244 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001245 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001246 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001247
Harald Weltec2e302d2009-07-05 14:08:13 +02001248 subscr->equipment.classmark2_len = *classmark2_lv;
1249 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1250 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001251
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001252 /* We received a paging */
1253 conn->expire_timer_stopped = 1;
1254
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001255 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001256 return rc;
1257}
1258
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001259static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001260{
1261 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001262 uint8_t apdu_id_flags;
1263 uint8_t apdu_len;
1264 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001265
1266 apdu_id_flags = gh->data[0];
1267 apdu_len = gh->data[1];
1268 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001269
Harald Welte70f92052012-07-16 13:22:19 +02001270 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001271 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001272
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001273 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001274}
1275
Harald Weltebf5e8df2009-02-03 12:59:45 +00001276/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001277static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001278{
1279 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001280 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001281
1282 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001283 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001284 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001285 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001286 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001287 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001288 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001289 default:
Philippd37981e2016-10-10 15:11:41 +02001290 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1291 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001292 break;
1293 }
1294
Harald Welte2d35ae62009-02-06 12:02:13 +00001295 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001296}
1297
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001298int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1299 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001300{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001301 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001302 struct gsm48_hdr *gh;
1303
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001304 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001305
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001306 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1307 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001308
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001309 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1310 gh->proto_discr = GSM48_PDISC_RR;
1311 gh->msg_type = GSM48_MT_RR_APP_INFO;
1312 gh->data[0] = apdu_id;
1313 gh->data[1] = apdu_len;
1314 memcpy(gh->data+2, apdu, apdu_len);
1315
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001316 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001317}
1318
Alexander Couzens92f552f2016-08-23 07:32:27 +02001319/* FIXME: this count_statistics is a state machine behaviour. we should convert
1320 * the complete call control into a state machine. Afterwards we can move this
1321 * code into state transitions.
1322 */
1323static void count_statistics(struct gsm_trans *trans, int new_state)
1324{
1325 int old_state = trans->cc.state;
1326 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1327
1328 if (old_state == new_state)
1329 return;
1330
1331 /* state incoming */
1332 switch (new_state) {
1333 case GSM_CSTATE_ACTIVE:
1334 osmo_counter_inc(trans->net->active_calls);
1335 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1336 break;
1337 }
1338
1339 /* state outgoing */
1340 switch (old_state) {
1341 case GSM_CSTATE_ACTIVE:
1342 osmo_counter_dec(trans->net->active_calls);
1343 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1344 new_state == GSM_CSTATE_DISCONNECT_IND)
1345 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1346 else
1347 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1348 break;
1349 }
1350}
1351
Harald Welte4bc90a12008-12-27 16:32:52 +00001352/* Call Control */
1353
Harald Welte7584aea2009-02-11 11:44:12 +00001354/* The entire call control code is written in accordance with Figure 7.10c
1355 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1356 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1357 * it for voice */
1358
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359static void new_cc_state(struct gsm_trans *trans, int state)
1360{
1361 if (state > 31 || state < 0)
1362 return;
1363
1364 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001365 gsm48_cc_state_name(trans->cc.state),
1366 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367
Alexander Couzens92f552f2016-08-23 07:32:27 +02001368 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001369 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370}
1371
1372static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001373{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001374 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001375 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001376 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001377
Harald Welte4bc90a12008-12-27 16:32:52 +00001378 gh->msg_type = GSM48_MT_CC_STATUS;
1379
1380 cause = msgb_put(msg, 3);
1381 cause[0] = 2;
1382 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1383 cause[2] = 0x80 | 30; /* response to status inquiry */
1384
1385 call_state = msgb_put(msg, 1);
1386 call_state[0] = 0xc0 | 0x00;
1387
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001388 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001389}
1390
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001391static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001392 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001393{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001394 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001395 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1396
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001397 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001398
Harald Welte6f4b7532008-12-29 00:39:37 +00001399 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001400 gh->msg_type = msg_type;
1401
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001402 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001403}
1404
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1406{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001407 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001408 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001409 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001410 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 }
1412}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001413
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1415 int msg_type, struct gsm_mncc *mncc)
1416{
1417 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001418 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419
1420 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001421 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001422 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001424 trans->conn->lchan->ts->trx->bts->nr,
1425 trans->conn->lchan->ts->trx->nr,
1426 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 (trans->subscr)?(trans->subscr->extension):"-",
1428 get_mncc_name(msg_type));
1429 else
1430 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1431 "Sending '%s' to MNCC.\n",
1432 (trans->subscr)?(trans->subscr->extension):"-",
1433 get_mncc_name(msg_type));
1434 else
1435 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1436 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1437
1438 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001439
Harald Welte966636f2009-06-26 19:39:35 +02001440 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 if (!msg)
1442 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001443
1444 data = msgb_put(msg, sizeof(struct gsm_mncc));
1445 memcpy(data, mncc, sizeof(struct gsm_mncc));
1446
Harald Welte54209c22010-12-22 23:43:29 +01001447 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448
1449 return 0;
1450}
1451
1452int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001453 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001454{
1455 struct gsm_mncc rel;
1456
Harald Welte92f70c52009-06-12 01:54:08 +08001457 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001458 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001459 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001460 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1461 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001462 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1463}
1464
Harald Weltedcaf5652009-07-23 18:56:43 +02001465/* Call Control Specific transaction release.
1466 * gets called by trans_free, DO NOT CALL YOURSELF! */
1467void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001468{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001469 gsm48_stop_cc_timer(trans);
1470
1471 /* send release to L4, if callref still exists */
1472 if (trans->callref) {
1473 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001474 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001475 GSM48_CAUSE_LOC_PRN_S_LU,
1476 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001477 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001478 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001479 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001480 if (trans->conn)
1481 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482}
1483
1484static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001485
Harald Welte09e38af2009-02-16 22:52:23 +00001486/* call-back from paging the B-end of the connection */
1487static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001488 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001489{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001490 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001491 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001492
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001493 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001494
Harald Welte4bfdfe72009-06-10 23:11:52 +08001495 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001496 switch (event) {
1497 case GSM_PAGING_SUCCEEDED:
1498 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001499 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001500 /* Assign lchan */
1501 transt->conn = conn;
1502 /* send SETUP request to called party */
1503 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1504 break;
1505 case GSM_PAGING_EXPIRED:
1506 case GSM_PAGING_BUSY:
1507 DEBUGP(DCC, "Paging subscr %s expired!\n",
1508 transt->subscr->extension);
1509 /* Temporarily out of order */
1510 mncc_release_ind(transt->net, transt,
1511 transt->callref,
1512 GSM48_CAUSE_LOC_PRN_S_LU,
1513 GSM48_CC_CAUSE_DEST_OOO);
1514 transt->callref = 0;
1515 transt->paging_request = NULL;
1516 trans_free(transt);
1517 break;
1518 default:
1519 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1520 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001522
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001523 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001524 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001525}
Harald Welte7584aea2009-02-11 11:44:12 +00001526
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001527static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001528
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001529/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001530static int switch_for_handover(struct gsm_lchan *old_lchan,
1531 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001532{
1533 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001534
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001535 /* Ask the new socket to send to the already known port. */
1536 if (new_lchan->conn->mncc_rtp_bridge) {
1537 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1538 rsl_ipacc_mdcx(new_lchan,
1539 old_lchan->abis_ip.connect_ip,
1540 old_lchan->abis_ip.connect_port, 0);
1541 return 0;
1542 }
1543
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001544 if (ipacc_rtp_direct) {
1545 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1546 return 0;
1547 }
1548
1549 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001550 new_rs = new_lchan->abis_ip.rtp_socket;
1551 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001552
1553 if (!new_rs) {
1554 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1555 return -EIO;
1556 }
1557
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001558 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001559
1560 if (!old_rs) {
1561 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1562 return -EIO;
1563 }
1564
1565 /* copy rx_action and reference to other sock */
1566 new_rs->rx_action = old_rs->rx_action;
1567 new_rs->tx_action = old_rs->tx_action;
1568 new_rs->transmit = old_rs->transmit;
1569
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001570 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001571 case RTP_PROXY:
1572 other_rs = old_rs->proxy.other_sock;
1573 rtp_socket_proxy(new_rs, other_rs);
1574 /* delete reference to other end socket to prevent
1575 * rtp_socket_free() from removing the inverse reference */
1576 old_rs->proxy.other_sock = NULL;
1577 break;
1578 case RTP_RECV_UPSTREAM:
1579 new_rs->receive = old_rs->receive;
1580 break;
1581 case RTP_NONE:
1582 break;
1583 }
1584
1585 return 0;
1586}
1587
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001588static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1589{
1590 struct gsm_lchan *old_lchan;
1591 old_lchan = bsc_handover_pending(lchan);
1592 if (old_lchan)
1593 switch_for_handover(old_lchan, lchan);
1594}
1595
Harald Welte805f6442009-07-28 18:25:29 +02001596/* some other part of the code sends us a signal */
1597static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1598 void *handler_data, void *signal_data)
1599{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001600 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001601 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001602 struct gsm_network *net;
1603 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001604
1605 if (subsys != SS_ABISIP)
1606 return 0;
1607
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001608 /* RTP bridge handling */
1609 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1610 return tch_rtp_signal(lchan, signal);
1611
Harald Welte805f6442009-07-28 18:25:29 +02001612 /* in case we use direct BTS-to-BTS RTP */
1613 if (ipacc_rtp_direct)
1614 return 0;
1615
Harald Welte805f6442009-07-28 18:25:29 +02001616 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001617 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001618 /* in case we don't use direct BTS-to-BTS RTP */
1619 /* the BTS has successfully bound a TCH to a local ip/port,
1620 * which means we can connect our UDP socket to it */
1621 if (lchan->abis_ip.rtp_socket) {
1622 rtp_socket_free(lchan->abis_ip.rtp_socket);
1623 lchan->abis_ip.rtp_socket = NULL;
1624 }
1625
1626 lchan->abis_ip.rtp_socket = rtp_socket_create();
1627 if (!lchan->abis_ip.rtp_socket)
1628 return -EIO;
1629
1630 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1631 lchan->abis_ip.bound_ip,
1632 lchan->abis_ip.bound_port);
1633 if (rc < 0)
1634 return -EIO;
1635
Harald Welteda7ab742009-12-19 22:23:05 +01001636 /* check if any transactions on this lchan still have
1637 * a tch_recv_mncc request pending */
1638 net = lchan->ts->trx->bts->network;
1639 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001640 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001641 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1642 tch_recv_mncc(net, trans->callref, 1);
1643 }
1644 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001645
1646 /*
1647 * TODO: this appears to be too early? Why not until after
1648 * the handover detect or the handover complete?
1649 *
1650 * Do we have a handover pending for this new lchan? In that
1651 * case re-route the audio from the old channel to the new one.
1652 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001653 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001654 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001655 case S_ABISIP_DLCX_IND:
1656 /* the BTS tells us a RTP stream has been disconnected */
1657 if (lchan->abis_ip.rtp_socket) {
1658 rtp_socket_free(lchan->abis_ip.rtp_socket);
1659 lchan->abis_ip.rtp_socket = NULL;
1660 }
1661
1662 break;
Harald Welte805f6442009-07-28 18:25:29 +02001663 }
1664
1665 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001666}
1667
Harald Welte49f48b82009-02-17 15:29:33 +00001668/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001669static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001670{
Harald Welte11fa29c2009-02-19 17:24:39 +00001671 struct gsm_bts *bts = lchan->ts->trx->bts;
1672 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001673 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001674 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001675 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001676
Max8db12e42016-04-18 23:11:18 +02001677 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1678 "(bts=%u,trx=%u,ts=%u,%s)\n",
1679 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1680 get_value_string(gsm_chan_t_names, lt),
1681 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1682 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001683
1684 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001685 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001686 return -EINVAL;
1687 }
Harald Welteda7ab742009-12-19 22:23:05 +01001688
Max3ffce192016-04-25 15:22:00 +02001689 if (lt != rt) {
1690 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1691 " channel types: local = %s, remote = %s\n",
1692 get_value_string(gsm_chan_t_names, lt),
1693 get_value_string(gsm_chan_t_names, rt));
1694 return -EBADSLT;
1695 }
1696
1697 if (lm != rm) {
1698 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1699 " channel modes: local = %s, remote = %s\n",
1700 get_value_string(gsm48_chan_mode_names, lm),
1701 get_value_string(gsm48_chan_mode_names, rm));
1702 return -EMEDIUMTYPE;
1703 }
1704
Harald Welteda7ab742009-12-19 22:23:05 +01001705 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001706 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001707 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001708 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001709 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001710 if (!lchan->abis_ip.rtp_socket) {
1711 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1712 "lchan\n");
1713 return -EIO;
1714 }
1715 if (!remote_lchan->abis_ip.rtp_socket) {
1716 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1717 "remote_lchan\n");
1718 return -EIO;
1719 }
1720
Harald Welte805f6442009-07-28 18:25:29 +02001721 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001722 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001723 if (rc < 0)
1724 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001725 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001726 if (rc < 0)
1727 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001728 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301729 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1730 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001731 } else {
1732 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301733 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1734 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301735 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001736 if (rc < 0)
1737 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301738 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1739 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301740 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001741 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001742 break;
1743 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001744 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001745 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001746 trau_mux_map_lchan(lchan, remote_lchan);
1747 break;
1748 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001749 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001750 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001751 }
Harald Welte49f48b82009-02-17 15:29:33 +00001752
1753 return 0;
1754}
1755
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001757static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001758{
Harald Welte53d51f52015-12-03 14:59:04 +01001759 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1760 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001761
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001763 return -EIO;
1764
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001765 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 return -EIO;
1767
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001768 /* Which subscriber do we want to track trans1 or trans2? */
1769 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1770
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001772 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001773}
1774
Harald Welteda7ab742009-12-19 22:23:05 +01001775/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001776static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777{
1778 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001779 struct gsm_lchan *lchan;
1780 struct gsm_bts *bts;
1781 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001782
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001784 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 if (!trans)
1786 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001787 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001789
1790 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001791 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001792 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001794 /* store receive state */
1795 trans->tch_recv = enable;
1796
Harald Welteda7ab742009-12-19 22:23:05 +01001797 switch (bts->type) {
1798 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001799 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001800 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001801 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001802 return -EINVAL;
1803 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001804 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1805 * will not be connected to our RTP proxy and the socket will
1806 * not be assigned to the application interface. This method
1807 * will be called again, once the audio socket is created and
1808 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001809 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001810 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1811 return 0;
1812 }
1813 if (enable) {
1814 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001815 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001816 if (rc < 0)
1817 return rc;
1818 /* assign socket to application interface */
1819 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1820 net, callref);
1821 } else
1822 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1823 net, 0);
1824 break;
1825 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001826 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001827 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001828 /* In case we don't have a TCH with correct mode, the TRAU muxer
1829 * will not be asigned to the application interface. This is
1830 * performed by switch_trau_mux() after successful handover or
1831 * assignment. */
1832 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1833 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1834 return 0;
1835 }
Harald Welteda7ab742009-12-19 22:23:05 +01001836 if (enable)
1837 return trau_recv_lchan(lchan, callref);
1838 return trau_mux_unmap(NULL, callref);
1839 break;
1840 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001841 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001842 return -EINVAL;
1843 }
1844
1845 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846}
1847
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1849{
1850 DEBUGP(DCC, "-> STATUS ENQ\n");
1851 return gsm48_cc_tx_status(trans, msg);
1852}
1853
1854static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1855static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1856
1857static void gsm48_cc_timeout(void *arg)
1858{
1859 struct gsm_trans *trans = arg;
1860 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001861 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1862 int mo_location = GSM48_CAUSE_LOC_USER;
1863 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1864 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 struct gsm_mncc mo_rel, l4_rel;
1866
1867 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1868 mo_rel.callref = trans->callref;
1869 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1870 l4_rel.callref = trans->callref;
1871
Harald Weltedcaf5652009-07-23 18:56:43 +02001872 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 case 0x303:
1874 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001875 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 break;
1877 case 0x310:
1878 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001879 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 break;
1881 case 0x313:
1882 disconnect = 1;
1883 /* unknown, did not find it in the specs */
1884 break;
1885 case 0x301:
1886 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001887 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 break;
1889 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001890 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001892 gsm48_cc_tx_release(trans, &trans->cc.msg);
1893 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 break; /* stay in release state */
1895 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001896 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 return;
1898// release = 1;
1899// l4_cause = 14;
1900// break;
1901 case 0x306:
1902 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001903 mo_cause = trans->cc.msg.cause.value;
1904 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 break;
1906 case 0x323:
1907 disconnect = 1;
1908 break;
1909 default:
1910 release = 1;
1911 }
1912
1913 if (release && trans->callref) {
1914 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001915 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 l4_location, l4_cause);
1917 trans->callref = 0;
1918 }
1919
1920 if (disconnect && trans->callref) {
1921 /* process disconnect towards layer 4 */
1922 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001923 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 }
1925
1926 /* process disconnect towards mobile station */
1927 if (disconnect || release) {
1928 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001929 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1930 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1931 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 mo_rel.cause.diag_len = 3;
1933
1934 if (disconnect)
1935 gsm48_cc_tx_disconnect(trans, &mo_rel);
1936 if (release)
1937 gsm48_cc_tx_release(trans, &mo_rel);
1938 }
1939
1940}
1941
Max3ffce192016-04-25 15:22:00 +02001942/* disconnect both calls from the bridge */
1943static inline void disconnect_bridge(struct gsm_network *net,
1944 struct gsm_mncc_bridge *bridge, int err)
1945{
1946 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1947 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1948 struct gsm_mncc mx_rel;
1949 if (!trans0 || !trans1)
1950 return;
1951
1952 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1953 trans0->callref, trans1->callref, strerror(err));
1954
1955 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1956 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1957 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1958
1959 mx_rel.callref = trans0->callref;
1960 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1961
1962 mx_rel.callref = trans1->callref;
1963 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1964}
1965
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1967 int sec, int micro)
1968{
1969 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001970 trans->cc.timer.cb = gsm48_cc_timeout;
1971 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001972 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001973 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974}
1975
1976static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1977{
1978 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001979 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1981 struct tlv_parsed tp;
1982 struct gsm_mncc setup;
1983
1984 memset(&setup, 0, sizeof(struct gsm_mncc));
1985 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001986 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001987 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* emergency setup is identified by msg_type */
1989 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1990 setup.emergency = 1;
1991
1992 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001993 setup.fields |= MNCC_F_CALLING;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01001994 osmo_strlcpy(setup.calling.number, trans->subscr->extension,
1995 sizeof(setup.calling.number));
1996 osmo_strlcpy(setup.imsi, trans->subscr->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 /* bearer capability */
1999 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2000 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002001 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002003 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 }
2005 /* facility */
2006 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2007 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2010 }
2011 /* called party bcd number */
2012 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2013 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01002014 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2016 }
2017 /* user-user */
2018 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2019 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002020 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2022 }
2023 /* ss-version */
2024 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2025 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002026 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2028 }
2029 /* CLIR suppression */
2030 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2031 setup.clir.sup = 1;
2032 /* CLIR invocation */
2033 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2034 setup.clir.inv = 1;
2035 /* cc cap */
2036 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2037 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002038 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2040 }
2041
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 new_cc_state(trans, GSM_CSTATE_INITIATED);
2043
Harald Welte (local)47df3992009-12-26 19:45:03 +01002044 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2045 subscr_name(trans->subscr), trans->subscr->extension,
2046 setup.called.number);
2047
Alexander Couzensb847a212016-08-02 11:34:11 +02002048 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002049
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002051 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052
Harald Welte13cac662009-07-29 12:10:35 +02002053 /* MNCC code will modify the channel asynchronously, we should
2054 * ipaccess-bind only after the modification has been made to the
2055 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 return 0;
2057}
2058
2059static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002060{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002061 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00002062 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002064 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002065
Harald Welte7ccf7782009-02-17 01:43:01 +00002066 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002067
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 /* transaction id must not be assigned */
2069 if (trans->transaction_id != 0xff) { /* unasssigned */
2070 DEBUGP(DCC, "TX Setup with assigned transaction. "
2071 "This is not allowed!\n");
2072 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002073 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002074 GSM48_CAUSE_LOC_PRN_S_LU,
2075 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002077 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 return rc;
2079 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002080
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01002082 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
2083 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02002084 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002086 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002087 GSM48_CAUSE_LOC_PRN_S_LU,
2088 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002090 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 return rc;
2092 }
Harald Welte78283ef2009-07-23 21:36:44 +02002093 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002094
Harald Welte65e74cc2008-12-29 01:55:35 +00002095 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002096
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002098
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 /* bearer capability */
2100 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002101 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102 /* facility */
2103 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 /* progress */
2106 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002107 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 /* calling party BCD number */
2109 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 /* called party BCD number */
2112 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01002113 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 /* user-user */
2115 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 /* redirecting party BCD number */
2118 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 /* signal */
2121 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002123
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002125
Alexander Couzensb847a212016-08-02 11:34:11 +02002126 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002127
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002128 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002129}
2130
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2132{
2133 struct gsm48_hdr *gh = msgb_l3(msg);
2134 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2135 struct tlv_parsed tp;
2136 struct gsm_mncc call_conf;
2137
2138 gsm48_stop_cc_timer(trans);
2139 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2140
2141 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2142 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002143 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002144 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145#if 0
2146 /* repeat */
2147 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2148 call_conf.repeat = 1;
2149 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2150 call_conf.repeat = 2;
2151#endif
2152 /* bearer capability */
2153 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2154 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002155 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002157 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 }
2159 /* cause */
2160 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2161 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2164 }
2165 /* cc cap */
2166 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2167 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2170 }
2171
Andreas Eversberg035b8742013-09-19 09:27:06 +02002172 /* IMSI of called subscriber */
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002173 osmo_strlcpy(call_conf.imsi, trans->subscr->imsi,
2174 sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02002175
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2177
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002178 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002179 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180}
2181
2182static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2183{
2184 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002185 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2187
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2189
2190 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2191
2192 /* bearer capability */
2193 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002194 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002195 /* facility */
2196 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002197 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 /* progress */
2199 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002200 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002202 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203}
2204
2205static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2206{
2207 struct gsm48_hdr *gh = msgb_l3(msg);
2208 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2209 struct tlv_parsed tp;
2210 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002211
Harald Welte4bfdfe72009-06-10 23:11:52 +08002212 gsm48_stop_cc_timer(trans);
2213 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2214
2215 memset(&alerting, 0, sizeof(struct gsm_mncc));
2216 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002217 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 /* facility */
2219 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2220 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002221 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2223 }
2224
2225 /* progress */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2227 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2230 }
2231 /* ss-version */
2232 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2233 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002234 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2236 }
2237
2238 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2239
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002240 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002241 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242}
2243
2244static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2245{
2246 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002247 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2249
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 gh->msg_type = GSM48_MT_CC_ALERTING;
2251
2252 /* facility */
2253 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002254 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 /* progress */
2256 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 /* user-user */
2259 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261
2262 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002263
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002264 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265}
2266
2267static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2268{
2269 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002270 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2272
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 gh->msg_type = GSM48_MT_CC_PROGRESS;
2274
2275 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002276 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277 /* user-user */
2278 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002281 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002282}
2283
2284static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2285{
2286 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002287 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2289
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 gh->msg_type = GSM48_MT_CC_CONNECT;
2291
2292 gsm48_stop_cc_timer(trans);
2293 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2294
2295 /* facility */
2296 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002297 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 /* progress */
2299 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002300 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 /* connected number */
2302 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002303 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 /* user-user */
2305 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
2308 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2309
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002310 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311}
2312
2313static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2314{
2315 struct gsm48_hdr *gh = msgb_l3(msg);
2316 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2317 struct tlv_parsed tp;
2318 struct gsm_mncc connect;
2319
2320 gsm48_stop_cc_timer(trans);
2321
2322 memset(&connect, 0, sizeof(struct gsm_mncc));
2323 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002324 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002326 connect.fields |= MNCC_F_CONNECTED;
Neels Hofmeyr93bafb62017-01-13 03:12:08 +01002327 osmo_strlcpy(connect.connected.number, trans->subscr->extension,
2328 sizeof(connect.connected.number));
2329 osmo_strlcpy(connect.imsi, trans->subscr->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 /* facility */
2332 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2333 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002334 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2336 }
2337 /* user-user */
2338 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2339 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002340 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2342 }
2343 /* ss-version */
2344 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2345 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002346 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2348 }
2349
2350 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002351 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002353 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354}
2355
2356
2357static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2358{
2359 struct gsm_mncc connect_ack;
2360
2361 gsm48_stop_cc_timer(trans);
2362
2363 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002364 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002365
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2367 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002368
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002369 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 &connect_ack);
2371}
2372
2373static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2374{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002375 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2377
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2379
2380 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2381
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002382 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383}
2384
2385static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2386{
2387 struct gsm48_hdr *gh = msgb_l3(msg);
2388 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2389 struct tlv_parsed tp;
2390 struct gsm_mncc disc;
2391
2392 gsm48_stop_cc_timer(trans);
2393
2394 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2395
2396 memset(&disc, 0, sizeof(struct gsm_mncc));
2397 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002398 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 /* cause */
2400 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2401 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002402 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2404 }
2405 /* facility */
2406 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2407 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002408 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2410 }
2411 /* user-user */
2412 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2413 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002414 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2416 }
2417 /* ss-version */
2418 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2419 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2422 }
2423
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002424 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425
2426}
2427
Harald Weltec66b71c2009-06-11 14:23:20 +08002428static struct gsm_mncc_cause default_cause = {
2429 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2430 .coding = 0,
2431 .rec = 0,
2432 .rec_val = 0,
2433 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2434 .diag_len = 0,
2435 .diag = { 0 },
2436};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437
2438static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2439{
2440 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002441 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2443
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2445
2446 gsm48_stop_cc_timer(trans);
2447 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2448
2449 /* cause */
2450 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002451 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 else
Harald Welte55c8f352010-03-07 23:40:35 +01002453 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454
2455 /* facility */
2456 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 /* progress */
2459 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002460 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 /* user-user */
2462 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002463 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464
2465 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002466 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467
2468 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2469
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002470 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471}
2472
2473static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2474{
2475 struct gsm48_hdr *gh = msgb_l3(msg);
2476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2477 struct tlv_parsed tp;
2478 struct gsm_mncc rel;
2479 int rc;
2480
2481 gsm48_stop_cc_timer(trans);
2482
2483 memset(&rel, 0, sizeof(struct gsm_mncc));
2484 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002485 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486 /* cause */
2487 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2488 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002489 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2491 }
2492 /* facility */
2493 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2494 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2497 }
2498 /* user-user */
2499 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2500 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002501 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2503 }
2504 /* ss-version */
2505 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2506 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002507 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2509 }
2510
Harald Weltedcaf5652009-07-23 18:56:43 +02002511 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002513 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002515 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002516 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002517 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002518 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 }
2520
2521 new_cc_state(trans, GSM_CSTATE_NULL);
2522
2523 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002524 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525
2526 return rc;
2527}
2528
2529static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2530{
2531 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002532 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2534
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535 gh->msg_type = GSM48_MT_CC_RELEASE;
2536
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 gsm48_stop_cc_timer(trans);
2538 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2539
2540 /* cause */
2541 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002542 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 /* facility */
2544 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002545 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546 /* user-user */
2547 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002548 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549
Harald Weltedcaf5652009-07-23 18:56:43 +02002550 trans->cc.T308_second = 0;
2551 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552
Harald Weltedcaf5652009-07-23 18:56:43 +02002553 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2555
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002556 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2560{
2561 struct gsm48_hdr *gh = msgb_l3(msg);
2562 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2563 struct tlv_parsed tp;
2564 struct gsm_mncc rel;
2565 int rc = 0;
2566
2567 gsm48_stop_cc_timer(trans);
2568
2569 memset(&rel, 0, sizeof(struct gsm_mncc));
2570 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002571 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 /* cause */
2573 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2574 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2577 }
2578 /* facility */
2579 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2580 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002581 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2583 }
2584 /* user-user */
2585 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2586 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002587 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2589 }
2590 /* ss-version */
2591 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2592 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2595 }
2596
2597 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002598 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002600 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 MNCC_REJ_IND, &rel);
2602 break;
2603 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002604 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 MNCC_REL_CNF, &rel);
2606 break;
2607 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002608 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 MNCC_REL_IND, &rel);
2610 }
2611 }
2612
2613 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002614 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615
2616 return rc;
2617}
2618
2619static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2620{
2621 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002622 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002624 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2627
2628 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002629
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630 gsm48_stop_cc_timer(trans);
2631
2632 /* cause */
2633 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002634 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 /* facility */
2636 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002637 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 /* user-user */
2639 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002640 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641
Harald Weltec7782de2010-12-21 19:31:41 +01002642 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2643
Harald Weltedcaf5652009-07-23 18:56:43 +02002644 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645
Harald Weltec7782de2010-12-21 19:31:41 +01002646 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2650{
2651 struct gsm48_hdr *gh = msgb_l3(msg);
2652 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2653 struct tlv_parsed tp;
2654 struct gsm_mncc fac;
2655
2656 memset(&fac, 0, sizeof(struct gsm_mncc));
2657 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002658 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659 /* facility */
2660 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2661 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002662 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2664 }
2665 /* ss-version */
2666 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2667 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002668 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2670 }
2671
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002672 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673}
2674
2675static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2676{
2677 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002678 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2680
Harald Welte4bfdfe72009-06-10 23:11:52 +08002681 gh->msg_type = GSM48_MT_CC_FACILITY;
2682
2683 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002684 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002686 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687}
2688
2689static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2690{
2691 struct gsm_mncc hold;
2692
2693 memset(&hold, 0, sizeof(struct gsm_mncc));
2694 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002695 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2699{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002700 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2702
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2704
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002705 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706}
2707
2708static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2709{
2710 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002711 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2713
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2715
2716 /* cause */
2717 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002718 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 else
Harald Welte55c8f352010-03-07 23:40:35 +01002720 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002722 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723}
2724
2725static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2726{
2727 struct gsm_mncc retrieve;
2728
2729 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2730 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002731 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002732 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733}
2734
2735static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2736{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002737 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2739
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2741
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002742 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743}
2744
2745static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2746{
2747 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002748 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2750
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2752
2753 /* cause */
2754 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002755 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756 else
Harald Welte55c8f352010-03-07 23:40:35 +01002757 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002759 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760}
2761
2762static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2763{
2764 struct gsm48_hdr *gh = msgb_l3(msg);
2765 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2766 struct tlv_parsed tp;
2767 struct gsm_mncc dtmf;
2768
2769 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2770 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002771 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772 /* keypad facility */
2773 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2774 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002775 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2777 }
2778
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002779 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780}
2781
2782static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2783{
2784 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002785 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2787
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2789
2790 /* keypad */
2791 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002792 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002793
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002794 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795}
2796
2797static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2798{
2799 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002800 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2802
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2804
2805 /* cause */
2806 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002807 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 else
Harald Welte55c8f352010-03-07 23:40:35 +01002809 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002811 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812}
2813
2814static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2815{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002816 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2818
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2820
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002821 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822}
2823
2824static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2825{
2826 struct gsm_mncc dtmf;
2827
2828 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2829 dtmf.callref = trans->callref;
2830
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002831 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2835{
2836 struct gsm48_hdr *gh = msgb_l3(msg);
2837 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2838 struct tlv_parsed tp;
2839 struct gsm_mncc modify;
2840
2841 memset(&modify, 0, sizeof(struct gsm_mncc));
2842 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002843 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 /* bearer capability */
2845 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2846 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002847 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002849 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002850 }
2851
2852 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2853
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002854 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855}
2856
2857static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2858{
2859 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002860 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2862
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863 gh->msg_type = GSM48_MT_CC_MODIFY;
2864
2865 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2866
2867 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002868 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869
2870 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2871
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002872 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873}
2874
2875static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2876{
2877 struct gsm48_hdr *gh = msgb_l3(msg);
2878 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2879 struct tlv_parsed tp;
2880 struct gsm_mncc modify;
2881
2882 gsm48_stop_cc_timer(trans);
2883
2884 memset(&modify, 0, sizeof(struct gsm_mncc));
2885 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002886 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 /* bearer capability */
2888 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2889 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002890 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002892 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893 }
2894
2895 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2896
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002897 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898}
2899
2900static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2901{
2902 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002903 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002904 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2905
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2907
2908 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002909 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910
2911 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2912
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002913 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002914}
2915
2916static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2917{
2918 struct gsm48_hdr *gh = msgb_l3(msg);
2919 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2920 struct tlv_parsed tp;
2921 struct gsm_mncc modify;
2922
2923 gsm48_stop_cc_timer(trans);
2924
2925 memset(&modify, 0, sizeof(struct gsm_mncc));
2926 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002927 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928 /* bearer capability */
2929 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2930 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002931 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002933 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 }
2935 /* cause */
2936 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2937 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002938 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2940 }
2941
2942 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2943
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002944 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002945}
2946
2947static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2948{
2949 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002950 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2952
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2954
2955 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002956 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002958 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959
2960 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2961
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002962 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963}
2964
2965static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2966{
2967 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002968 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2970
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 gh->msg_type = GSM48_MT_CC_NOTIFY;
2972
2973 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002974 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002976 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977}
2978
2979static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2980{
2981 struct gsm48_hdr *gh = msgb_l3(msg);
2982 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2983// struct tlv_parsed tp;
2984 struct gsm_mncc notify;
2985
2986 memset(&notify, 0, sizeof(struct gsm_mncc));
2987 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002988// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002990 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002992 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993}
2994
2995static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2996{
2997 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002998 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
3000
Harald Welte4bfdfe72009-06-10 23:11:52 +08003001 gh->msg_type = GSM48_MT_CC_USER_INFO;
3002
3003 /* user-user */
3004 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01003005 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 /* more data */
3007 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01003008 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08003010 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011}
3012
3013static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3014{
3015 struct gsm48_hdr *gh = msgb_l3(msg);
3016 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3017 struct tlv_parsed tp;
3018 struct gsm_mncc user;
3019
3020 memset(&user, 0, sizeof(struct gsm_mncc));
3021 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003022 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 /* user-user */
3024 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3025 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01003026 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3028 }
3029 /* more data */
3030 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3031 user.more = 1;
3032
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01003033 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034}
3035
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003036static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037{
3038 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003039 struct gsm_lchan *lchan = trans->conn->lchan;
3040
3041 /*
3042 * We were forced to make an assignment a lot earlier and
3043 * we should avoid sending another assignment that might
3044 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
3045 * In case of rtp-bridge it is too late to change things
3046 * here.
3047 */
3048 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
3049 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02003051 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
3052 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053}
3054
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003055static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
3056 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
3057 uint32_t payload_msg_type)
3058{
3059 uint8_t data[sizeof(struct gsm_mncc)];
3060 struct gsm_mncc_rtp *rtp;
3061
3062 memset(&data, 0, sizeof(data));
3063 rtp = (struct gsm_mncc_rtp *) &data[0];
3064
3065 rtp->callref = callref;
3066 rtp->msg_type = cmd;
3067 rtp->ip = addr;
3068 rtp->port = port;
3069 rtp->payload_type = payload_type;
3070 rtp->payload_msg_type = payload_msg_type;
3071 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
3072}
3073
3074static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
3075{
3076 struct gsm_lchan *lchan;
3077 int msg_type;
3078
3079 lchan = trans->conn->lchan;
3080 switch (lchan->abis_ip.rtp_payload) {
3081 case RTP_PT_GSM_FULL:
3082 msg_type = GSM_TCHF_FRAME;
3083 break;
3084 case RTP_PT_GSM_EFR:
3085 msg_type = GSM_TCHF_FRAME_EFR;
3086 break;
3087 case RTP_PT_GSM_HALF:
3088 msg_type = GSM_TCHH_FRAME;
3089 break;
3090 case RTP_PT_AMR:
3091 msg_type = GSM_TCH_FRAME_AMR;
3092 break;
3093 default:
3094 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
3095 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
3096 msg_type = 0;
3097 break;
3098 }
3099
3100 return mncc_recv_rtp(net, trans->callref, cmd,
3101 lchan->abis_ip.bound_ip,
3102 lchan->abis_ip.bound_port,
3103 lchan->abis_ip.rtp_payload,
3104 msg_type);
3105}
3106
3107static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
3108{
3109 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
3110}
3111
3112static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
3113{
3114 struct gsm_bts *bts;
3115 struct gsm_lchan *lchan;
3116 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02003117 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003118
3119 /* Find callref */
3120 trans = trans_find_by_callref(net, callref);
3121 if (!trans) {
3122 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3123 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3124 return -EIO;
3125 }
3126 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3127 if (!trans->conn) {
3128 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3129 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3130 return 0;
3131 }
3132
3133 lchan = trans->conn->lchan;
3134 bts = lchan->ts->trx->bts;
3135 if (!is_ipaccess_bts(bts)) {
3136 /*
3137 * I want this to be straight forward and have no audio flow
3138 * through the nitb/osmo-mss system. This currently means that
3139 * this will not work with BS11/Nokia type BTS. We would need
3140 * to have a trau<->rtp bridge for these but still preferable
3141 * in another process.
3142 */
3143 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3144 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3145 return -EINVAL;
3146 }
3147
3148 trans->conn->mncc_rtp_bridge = 1;
3149 /*
3150 * *sigh* we need to pick a codec now. Pick the most generic one
3151 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003152 * similiar to the routine above.
3153 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003154 */
3155 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3156 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003157 m = mncc_codec_for_mode(lchan->type);
3158 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3159 get_value_string(gsm48_chan_mode_names, m),
3160 get_value_string(gsm_chan_t_names, lchan->type));
3161 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003162 lchan->type != GSM_LCHAN_TCH_H);
3163 }
3164
3165 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3166 return 0;
3167}
3168
3169static int tch_rtp_connect(struct gsm_network *net, void *arg)
3170{
3171 struct gsm_lchan *lchan;
3172 struct gsm_trans *trans;
3173 struct gsm_mncc_rtp *rtp = arg;
3174
3175 /* Find callref */
3176 trans = trans_find_by_callref(net, rtp->callref);
3177 if (!trans) {
3178 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3179 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3180 return -EIO;
3181 }
3182 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3183 if (!trans->conn) {
3184 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3185 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3186 return 0;
3187 }
3188
3189 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003190 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3191 get_value_string(gsm48_chan_mode_names,
3192 mncc_codec_for_mode(lchan->type)),
3193 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003194
3195 /* TODO: Check if payload_msg_type is compatible with what we have */
3196 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3197 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3198 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3199 }
3200
3201 /*
3202 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3203 * complains about both rtp and rtp payload2 being present in the
3204 * same package!
3205 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003206 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003207 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3208}
3209
3210static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3211{
3212 struct gsm_network *net;
3213 struct gsm_trans *tmp, *trans = NULL;
3214
3215 net = lchan->ts->trx->bts->network;
3216 llist_for_each_entry(tmp, &net->trans_list, entry) {
3217 if (!tmp->conn)
3218 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003219 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003220 continue;
3221 trans = tmp;
3222 break;
3223 }
3224
3225 if (!trans) {
3226 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3227 gsm_lchan_name(lchan));
3228 return 0;
3229 }
3230
3231 switch (signal) {
3232 case S_ABISIP_CRCX_ACK:
3233 if (lchan->conn->mncc_rtp_create_pending) {
3234 lchan->conn->mncc_rtp_create_pending = 0;
3235 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3236 gsm_lchan_name(lchan));
3237 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3238 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003239 /*
3240 * TODO: this appears to be too early? Why not until after
3241 * the handover detect or the handover complete?
3242 */
3243 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003244 break;
3245 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003246 if (lchan->conn->mncc_rtp_connect_pending) {
3247 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003248 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3249 gsm_lchan_name(lchan));
3250 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3251 }
3252 break;
3253 }
3254
3255 return 0;
3256}
3257
3258
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003260 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261 int type;
3262 int (*rout) (struct gsm_trans *trans, void *arg);
3263} downstatelist[] = {
3264 /* mobile originating call establishment */
3265 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3266 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3267 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3268 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3269 {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 */
3270 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3271 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3272 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3273 /* mobile terminating call establishment */
3274 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3275 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3276 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3277 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3278 /* signalling during call */
3279 {SBIT(GSM_CSTATE_ACTIVE),
3280 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3281 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3282 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3283 {ALL_STATES,
3284 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3285 {ALL_STATES,
3286 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3287 {ALL_STATES,
3288 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3289 {SBIT(GSM_CSTATE_ACTIVE),
3290 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3291 {SBIT(GSM_CSTATE_ACTIVE),
3292 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3293 {SBIT(GSM_CSTATE_ACTIVE),
3294 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3295 {SBIT(GSM_CSTATE_ACTIVE),
3296 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3297 {SBIT(GSM_CSTATE_ACTIVE),
3298 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3299 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3300 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3301 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3302 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3303 {SBIT(GSM_CSTATE_ACTIVE),
3304 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3305 /* clearing */
3306 {SBIT(GSM_CSTATE_INITIATED),
3307 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3308 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3309 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3310 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3311 MNCC_REL_REQ, gsm48_cc_tx_release},
3312 /* special */
3313 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003314 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003315};
3316
3317#define DOWNSLLEN \
3318 (sizeof(downstatelist) / sizeof(struct downstate))
3319
3320
Harald Welte76556372010-12-22 23:57:45 +01003321int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003322{
Harald Welte1a6f7982009-08-09 18:52:33 +02003323 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003324 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003325 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003326 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003327 struct gsm_mncc *data = arg, rel;
3328
Harald Welte04dc88f2010-12-22 21:45:05 +01003329 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3330
Harald Welte4bfdfe72009-06-10 23:11:52 +08003331 /* handle special messages */
3332 switch(msg_type) {
3333 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003334 rc = tch_bridge(net, arg);
3335 if (rc < 0)
3336 disconnect_bridge(net, arg, -rc);
3337 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003338 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003339 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003340 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003341 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003342 case MNCC_RTP_CREATE:
3343 return tch_rtp_create(net, data->callref);
3344 case MNCC_RTP_CONNECT:
3345 return tch_rtp_connect(net, arg);
3346 case MNCC_RTP_FREE:
3347 /* unused right now */
3348 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003349 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003350 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003351 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003352 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003353 /* Find callref */
3354 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003355 if (!trans) {
3356 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003357 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003358 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003359 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003360 if (!trans->conn) {
3361 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003362 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003363 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003364 if (!trans->conn->lchan) {
3365 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3366 return 0;
3367 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003368 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3369 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003370 /* This should be LOGL_ERROR or NOTICE, but
3371 * unfortuantely it happens for a couple of frames at
3372 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003373 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003374 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003375 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003376 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003377 switch (bts->type) {
3378 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003379 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003380 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003381 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003382 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003383 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003384 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003385 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003386 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003387 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003388 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003389 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003390 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003391 }
3392 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003393 }
3394
3395 memset(&rel, 0, sizeof(struct gsm_mncc));
3396 rel.callref = data->callref;
3397
3398 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003399 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003400
3401 /* Callref unknown */
3402 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003403 struct gsm_subscriber *subscr;
3404
Harald Welte4a3464c2009-07-04 10:11:24 +02003405 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003406 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3407 "Received '%s' from MNCC with "
3408 "unknown callref %d\n", data->called.number,
3409 get_mncc_name(msg_type), data->callref);
3410 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003411 return mncc_release_ind(net, NULL, data->callref,
3412 GSM48_CAUSE_LOC_PRN_S_LU,
3413 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003414 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003415 if (!data->called.number[0] && !data->imsi[0]) {
3416 DEBUGP(DCC, "(bts - trx - ts - ti) "
3417 "Received '%s' from MNCC with "
3418 "no number or IMSI\n", get_mncc_name(msg_type));
3419 /* Invalid number */
3420 return mncc_release_ind(net, NULL, data->callref,
3421 GSM48_CAUSE_LOC_PRN_S_LU,
3422 GSM48_CC_CAUSE_INV_NR_FORMAT);
3423 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003424 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003425 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003426 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003427 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003428 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003429 subscr = subscr_get_by_imsi(net->subscr_group,
3430 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003431
3432 /* update the subscriber we deal with */
3433 log_set_context(BSC_CTX_SUBSCR, subscr);
3434
Harald Welte4bfdfe72009-06-10 23:11:52 +08003435 /* If subscriber is not found */
3436 if (!subscr) {
3437 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3438 "Received '%s' from MNCC with "
3439 "unknown subscriber %s\n", data->called.number,
3440 get_mncc_name(msg_type), data->called.number);
3441 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003442 return mncc_release_ind(net, NULL, data->callref,
3443 GSM48_CAUSE_LOC_PRN_S_LU,
3444 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003445 }
3446 /* If subscriber is not "attached" */
3447 if (!subscr->lac) {
3448 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3449 "Received '%s' from MNCC with "
3450 "detached subscriber %s\n", data->called.number,
3451 get_mncc_name(msg_type), data->called.number);
3452 subscr_put(subscr);
3453 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003454 return mncc_release_ind(net, NULL, data->callref,
3455 GSM48_CAUSE_LOC_PRN_S_LU,
3456 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003457 }
3458 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003459 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003460 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003461 DEBUGP(DCC, "No memory for trans.\n");
3462 subscr_put(subscr);
3463 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003464 mncc_release_ind(net, NULL, data->callref,
3465 GSM48_CAUSE_LOC_PRN_S_LU,
3466 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003467 return -ENOMEM;
3468 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003469 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003470 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003471
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003473 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003474 /* find transaction with this subscriber already paging */
3475 llist_for_each_entry(transt, &net->trans_list, entry) {
3476 /* Transaction of our lchan? */
3477 if (transt == trans ||
3478 transt->subscr != subscr)
3479 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003480 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003481 "Received '%s' from MNCC with "
3482 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003483 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003484 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003485 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003486 subscr_put(subscr);
3487 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003488 return 0;
3489 }
3490 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003491 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003492
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003493 /* Request a channel */
3494 trans->paging_request = subscr_request_channel(subscr,
3495 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3496 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003497 if (!trans->paging_request) {
3498 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3499 subscr_put(subscr);
3500 trans_free(trans);
3501 return 0;
3502 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003503 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003504 return 0;
3505 }
3506 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003507 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003508 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003509 } else {
3510 /* update the subscriber we deal with */
3511 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003512 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003513
3514 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003515 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003516
3517 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003518 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003519 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003520 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003521 (trans->subscr)?(trans->subscr->extension):"-",
3522 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003523 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3524 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003525 if (msg_type == MNCC_REL_REQ)
3526 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3527 else
3528 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3529 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003530 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003531 return rc;
3532 }
3533
3534 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3535 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003536 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003537 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003538 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003539 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003540 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003541
3542 /* Find function for current state and message */
3543 for (i = 0; i < DOWNSLLEN; i++)
3544 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003545 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003546 break;
3547 if (i == DOWNSLLEN) {
3548 DEBUGP(DCC, "Message unhandled at this state.\n");
3549 return 0;
3550 }
3551
3552 rc = downstatelist[i].rout(trans, arg);
3553
3554 return rc;
3555}
3556
3557
3558static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003559 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003560 int type;
3561 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3562} datastatelist[] = {
3563 /* mobile originating call establishment */
3564 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3565 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3566 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3567 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3568 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3569 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3570 /* mobile terminating call establishment */
3571 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3572 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3573 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3574 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003575 {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 +08003576 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3577 /* signalling during call */
3578 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3579 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3580 {SBIT(GSM_CSTATE_ACTIVE),
3581 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3582 {ALL_STATES,
3583 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3584 {ALL_STATES,
3585 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3586 {ALL_STATES,
3587 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3588 {SBIT(GSM_CSTATE_ACTIVE),
3589 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3590 {SBIT(GSM_CSTATE_ACTIVE),
3591 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3592 {SBIT(GSM_CSTATE_ACTIVE),
3593 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3594 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3595 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3596 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3597 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3598 {SBIT(GSM_CSTATE_ACTIVE),
3599 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3600 /* clearing */
3601 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3602 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3603 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3604 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3605 {ALL_STATES, /* 5.4.3.4 */
3606 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3607};
3608
3609#define DATASLLEN \
3610 (sizeof(datastatelist) / sizeof(struct datastate))
3611
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003612static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003613{
3614 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003615 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003616 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003617 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003618 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003619
Harald Welte4bfdfe72009-06-10 23:11:52 +08003620 if (msg_type & 0x80) {
3621 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3622 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003623 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003624
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003625 if (!conn->subscr) {
3626 LOGP(DCC, LOGL_ERROR, "Invalid conn, no subscriber\n");
3627 return -EINVAL;
3628 }
3629
Harald Welte4bfdfe72009-06-10 23:11:52 +08003630 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003631 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003632
Harald Welte6f5aee02009-07-23 21:21:14 +02003633 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003634 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003635 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003636 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003637 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3638 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003639
3640 /* Create transaction */
3641 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003642 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003643 "creating new trans.\n", transaction_id);
3644 /* Create transaction */
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +02003645 trans = trans_alloc(conn->network, conn->subscr,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003646 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003647 transaction_id, new_callref++);
3648 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003649 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003650 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003651 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003652 GSM48_MT_CC_RELEASE_COMPL);
3653 return -ENOMEM;
3654 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003655 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003656 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003657 }
3658
3659 /* find function for current state and message */
3660 for (i = 0; i < DATASLLEN; i++)
3661 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003662 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003663 break;
3664 if (i == DATASLLEN) {
3665 DEBUGP(DCC, "Message unhandled at this state.\n");
3666 return 0;
3667 }
3668
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003669 assert(trans->subscr);
3670
Harald Welte4bfdfe72009-06-10 23:11:52 +08003671 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003672
3673 return rc;
3674}
3675
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003676/* Create a dummy to wait five seconds */
3677static void release_anchor(struct gsm_subscriber_connection *conn)
3678{
3679 if (!conn->anch_operation)
3680 return;
3681
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003682 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003683 talloc_free(conn->anch_operation);
3684 conn->anch_operation = NULL;
3685}
3686
3687static void anchor_timeout(void *_data)
3688{
3689 struct gsm_subscriber_connection *con = _data;
3690
3691 release_anchor(con);
3692 msc_release_connection(con);
3693}
3694
3695int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3696{
3697 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3698 if (!conn->anch_operation)
3699 return -1;
3700
3701 conn->anch_operation->timeout.data = conn;
3702 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003703 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003704 return 0;
3705}
3706
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003707struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
3708{
3709 struct gsm_subscriber_connection *conn;
3710
3711 conn = talloc_zero(network, struct gsm_subscriber_connection);
3712 if (!conn)
3713 return NULL;
3714
3715 conn->network = network;
3716 llist_add_tail(&conn->entry, &network->subscr_conns);
3717 return conn;
3718}
3719
3720void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
3721{
3722 if (!conn)
3723 return;
3724
3725 if (conn->subscr) {
3726 subscr_put(conn->subscr);
3727 conn->subscr = NULL;
3728 }
3729
3730 llist_del(&conn->entry);
3731 talloc_free(conn);
3732}
3733
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003734/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003735int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003736{
3737 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003738 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003739 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003740
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003741 OSMO_ASSERT(conn);
3742 OSMO_ASSERT(msg);
3743
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003744 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003745 if (silent_call_reroute(conn, msg))
3746 return silent_call_rx(conn, msg);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003747
Harald Welte52b1f982008-12-23 20:25:15 +00003748 switch (pdisc) {
3749 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003750 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003751 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003752 break;
3753 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003754 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003755 break;
3756 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003757 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003758 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003759 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003760 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003761 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003762 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003763 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003764 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003765 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3766 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003767 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003768 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003769 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003770 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003771 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003772 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003773 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003774 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3775 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003776 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003777 break;
3778 }
3779
3780 return rc;
3781}
Harald Welte8470bf22008-12-25 23:28:35 +00003782
Harald Welte805f6442009-07-28 18:25:29 +02003783/*
Neels Hofmeyrcc7db182016-12-18 23:52:38 +01003784 * This will be run by the linker when loading the DSO. We use it to
Harald Welte805f6442009-07-28 18:25:29 +02003785 * do system initialization, e.g. registration of signal handlers.
3786 */
3787static __attribute__((constructor)) void on_dso_load_0408(void)
3788{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003789 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003790}