blob: 437cb590ce94ea8bceb20a4250961f56bc8cb55e [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 Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 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
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/transaction.h>
47#include <osmocom/msc/ussd.h>
48#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080082static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000084
Harald Welte52b1f982008-12-23 20:25:15 +000085struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020086 uint16_t mcc;
87 uint16_t mnc;
88 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000089};
90
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020091static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080092
Harald Welte71330722017-12-23 19:59:02 +010093/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
94static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
95{
96 switch (a5) {
97 case 0:
98 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
99 return true;
100 case 1:
101 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
102 * "The network shall not provide service to an MS which indicates that it does not
103 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
104 * on policy here */
105 /* See 3GPP TS 24.008 10.5.1.7 */
106 if (!cm->classmark1_set) {
107 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
108 return true;
109 } else {
110 if (cm->classmark1.a5_1)
111 return false; /* Inverted logic for this bit! */
112 else
113 return true;
114 }
115 break;
116 case 2:
117 case 3:
118 /* See 3GPP TS 24.008 10.5.1.6 */
119 if (cm->classmark2_len < 3) {
120 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
121 return false;
122 } else {
123 if (cm->classmark2[2] & (1 << (a5-2)))
124 return true;
125 else
126 return false;
127 }
128 break;
129 case 4:
130 case 5:
131 case 6:
132 case 7:
133 /* See 3GPP TS 24.008 10.5.1.7 */
134 if (cm->classmark3_len < 1) {
135 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
136 return false;
137 } else {
138 if (cm->classmark3[0] & (1 << (a5-4)))
139 return true;
140 else
141 return false;
142 }
143 break;
144 default:
145 return false;
146 }
147}
148
Harald Welte31c00f72011-03-03 23:29:05 +0100149void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
150{
151 net->mncc_recv(net, msg);
152}
153
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800154static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
155 struct gsm_trans *trans)
156{
157 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
158
159 /* if we get passed a transaction reference, do some common
160 * work that the caller no longer has to do */
161 if (trans) {
162 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800163 }
164
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200165 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800166}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100167
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800168int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
169{
170 struct gsm48_hdr *gh;
171 struct msgb *ss_notify;
172
173 ss_notify = gsm0480_create_notifySS(message);
174 if (!ss_notify)
175 return -1;
176
177 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
178 uint8_t *data = msgb_push(ss_notify, 1);
179 data[0] = ss_notify->len - 1;
180 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
181 gh->msg_type = GSM48_MT_CC_FACILITY;
182 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
183}
184
Harald Welte2483f1b2016-06-19 18:06:02 +0200185/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100186void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
187{
188 struct gsm_trans *trans, *temp;
189
190 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
191
192 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100193 if (trans->protocol == protocol) {
194 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100195 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100196 }
Harald Welte371efe52010-12-22 23:17:50 +0100197 }
198}
199
Holger Freyther429e7762008-12-30 13:28:30 +0000200/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200201static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000202{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800203 struct msgb *msg;
204
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800205 msg = gsm48_create_loc_upd_rej(cause);
206 if (!msg) {
207 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
208 return -1;
209 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200210
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200211 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
212 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100213
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800214 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000215}
216
217/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200218static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
219 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000220{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100221 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000222 struct gsm48_hdr *gh;
223 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200224 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100225 struct osmo_location_area_id laid = {
226 .plmn = conn->network->plmn,
227 .lac = conn->lac,
228 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200229
Harald Welte52b1f982008-12-23 20:25:15 +0000230 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
231 gh->proto_discr = GSM48_PDISC_MM;
232 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
233
234 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100235 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000236
Harald Welte2483f1b2016-06-19 18:06:02 +0200237 if (send_tmsi == GSM_RESERVED_TMSI) {
238 /* we did not allocate a TMSI to the MS, so we need to
239 * include the IMSI in order for the MS to delete any
240 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200241 uint8_t mi[10];
242 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200243 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200244 mid = msgb_put(msg, len);
245 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200246 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
247 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200248 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200249 /* Include the TMSI, which means that the MS will send a
250 * TMSI REALLOCATION COMPLETE, and we should wait for
251 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200252 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200253 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200254 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
255 vlr_subscr_name(conn->vsub),
256 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200257 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200258 /* TODO: Follow-on proceed */
259 /* TODO: CTS permission */
260 /* TODO: Equivalent PLMNs */
261 /* TODO: Emergency Number List */
262 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000263
Harald Welte52b1f982008-12-23 20:25:15 +0000264
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800265 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000266}
267
Harald Weltebf5e8df2009-02-03 12:59:45 +0000268/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200269static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000270{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100271 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000272 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000273
Harald Welte231ad4f2008-12-27 11:15:38 +0000274 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
275 gh->proto_discr = GSM48_PDISC_MM;
276 gh->msg_type = GSM48_MT_MM_ID_REQ;
277 gh->data[0] = id_type;
278
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800279 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000280}
281
Harald Weltebf5e8df2009-02-03 12:59:45 +0000282/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800283static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000284{
285 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200286 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200287 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000288
Harald Welte2483f1b2016-06-19 18:06:02 +0200289 if (!conn->vsub) {
290 LOGP(DMM, LOGL_ERROR,
291 "Rx MM Identity Response: invalid: no subscriber\n");
292 return -EINVAL;
293 }
294
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200295 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200296 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
297 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000298
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200299 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100300
Harald Welte2483f1b2016-06-19 18:06:02 +0200301 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000302}
303
Harald Welte2483f1b2016-06-19 18:06:02 +0200304/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200305static const struct value_string lupd_names[] = {
306 { GSM48_LUPD_NORMAL, "NORMAL" },
307 { GSM48_LUPD_PERIODIC, "PERIODIC" },
308 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
309 { 0, NULL }
310};
Harald Welte2a139372009-02-22 21:14:55 +0000311
Harald Welte2483f1b2016-06-19 18:06:02 +0200312/* Chapter 9.2.15: Receive Location Updating Request.
313 * Keep this function non-static for direct invocation by unit tests. */
314int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000315{
Harald Welte2483f1b2016-06-19 18:06:02 +0200316 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
317 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000318 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000319 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200320 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200321 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200323 uint32_t tmsi;
324 char *imsi;
325 struct osmo_location_area_id old_lai, new_lai;
326 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200327 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200328 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000329
Harald Welte8470bf22008-12-25 23:28:35 +0000330 lu = (struct gsm48_loc_upd_req *) gh->data;
331
332 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000333
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200334 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000335
Harald Welte2483f1b2016-06-19 18:06:02 +0200336 rc = msc_create_conn_fsm(conn, mi_string);
337 if (rc)
338 /* logging already happened in msc_create_conn_fsm() */
339 return rc;
340
341 conn->classmark.classmark1 = lu->classmark1;
342 conn->classmark.classmark1_set = true;
343
344 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
345 gsm48_mi_type_name(mi_type), mi_string,
346 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000347
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200348 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100349
Harald Welte24ff6ee2009-12-22 00:41:05 +0100350 switch (lu->type) {
351 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200352 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200353 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100354 break;
355 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200356 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200357 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100358 break;
359 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200360 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200361 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100362 break;
363 }
364
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
366 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
367 /* TODO: 10.5.7.8 Device properties */
368 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200369
Harald Welte52b1f982008-12-23 20:25:15 +0000370 switch (mi_type) {
371 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200372 tmsi = GSM_RESERVED_TMSI;
373 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000374 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000375 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200376 tmsi = tmsi_from_string(mi_string);
377 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000378 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200379 default:
Harald Weltea0368542009-06-27 02:58:43 +0200380 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 tmsi = GSM_RESERVED_TMSI;
382 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000383 break;
384 }
385
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100386 gsm48_decode_lai2(&lu->lai, &old_lai);
387 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200388 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200389 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200390
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200391 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200392 lu_fsm = vlr_loc_update(conn->conn_fsm,
393 SUBSCR_CONN_E_ACCEPTED,
394 SUBSCR_CONN_E_CN_CLOSE,
395 (void*)&conn_from_lu,
396 net->vlr, conn, vlr_lu_type, tmsi, imsi,
397 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200398 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100399 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200400 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200401 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200402 net->vlr->cfg.assign_tmsi);
403 if (!lu_fsm) {
404 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
405 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800406 }
407
Harald Welte2483f1b2016-06-19 18:06:02 +0200408 /* From vlr_loc_update() we expect an implicit dispatch of
409 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
410 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000411
Harald Welte2483f1b2016-06-19 18:06:02 +0200412 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
413 LOGP(DRR, LOGL_ERROR,
414 "%s: internal error during Location Updating attempt\n",
415 mi_string);
416 return -EIO;
417 }
418
419 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000420}
421
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100422/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200423/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100424static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800425{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100426 uint8_t ret;
427
428 ret = value / 10;
429 ret |= (value % 10) << 4;
430
431 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100433
Harald Welte4bfdfe72009-06-10 23:11:52 +0800434
Harald Weltedb253af2008-12-30 17:56:55 +0000435/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800436int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000437{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100438 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000439 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200440 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200441 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200442 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100443
Harald Welte4bfdfe72009-06-10 23:11:52 +0800444 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100445 struct tm* gmt_time;
446 struct tm* local_time;
447 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100448 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000449
Harald Weltedb253af2008-12-30 17:56:55 +0000450 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
451 gh->proto_discr = GSM48_PDISC_MM;
452 gh->msg_type = GSM48_MT_MM_INFO;
453
454 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200455#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000456 name_len = strlen(net->name_long);
457 /* 10.5.3.5a */
458 ptr8 = msgb_put(msg, 3);
459 ptr8[0] = GSM48_IE_NAME_LONG;
460 ptr8[1] = name_len*2 +1;
461 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
462
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200463 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000464 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000465 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000466
467 /* FIXME: Use Cell Broadcast, not UCS-2, since
468 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200469#endif
470 name_len = (strlen(net->name_long)*7)/8;
471 name_pad = (8 - strlen(net->name_long)*7)%8;
472 if (name_pad > 0)
473 name_len++;
474 /* 10.5.3.5a */
475 ptr8 = msgb_put(msg, 3);
476 ptr8[0] = GSM48_IE_NAME_LONG;
477 ptr8[1] = name_len +1;
478 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
479
480 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100481 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200482
Harald Weltedb253af2008-12-30 17:56:55 +0000483 }
484
485 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200486#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000487 name_len = strlen(net->name_short);
488 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200489 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200490 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000491 ptr8[1] = name_len*2 + 1;
492 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
493
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200494 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000495 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000496 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200497#endif
498 name_len = (strlen(net->name_short)*7)/8;
499 name_pad = (8 - strlen(net->name_short)*7)%8;
500 if (name_pad > 0)
501 name_len++;
502 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200503 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200504 ptr8[0] = GSM48_IE_NAME_SHORT;
505 ptr8[1] = name_len +1;
506 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
507
508 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100509 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200510
Harald Weltedb253af2008-12-30 17:56:55 +0000511 }
512
Harald Weltedb253af2008-12-30 17:56:55 +0000513 /* Section 10.5.3.9 */
514 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100515 gmt_time = gmtime(&cur_t);
516
Harald Weltedb253af2008-12-30 17:56:55 +0000517 ptr8 = msgb_put(msg, 8);
518 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100519 ptr8[1] = bcdify(gmt_time->tm_year % 100);
520 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
521 ptr8[3] = bcdify(gmt_time->tm_mday);
522 ptr8[4] = bcdify(gmt_time->tm_hour);
523 ptr8[5] = bcdify(gmt_time->tm_min);
524 ptr8[6] = bcdify(gmt_time->tm_sec);
525
Neels Hofmeyr73983952016-05-10 13:29:33 +0200526 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200527 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200528 if (net->tz.hr < 0) {
529 tzunits = ((net->tz.hr/-1)*4);
530 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100531 ptr8[7] = bcdify(tzunits);
532 /* Set negative time */
533 ptr8[7] |= 0x08;
534 }
535 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200536 tzunits = net->tz.hr*4;
537 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100538 ptr8[7] = bcdify(tzunits);
539 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100540 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200541 if (net->tz.dst >= 0 && net->tz.dst <= 2)
542 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100543 }
544 else {
545 /* Need to get GSM offset and convert into 15 min units */
546 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200547#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200548 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100549 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200550#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200551 /* find timezone offset */
552 time_t utc;
553 double offsetFromUTC;
554 utc = mktime(gmt_time);
555 local_time = localtime(&cur_t);
556 offsetFromUTC = difftime(cur_t, utc);
557 if (local_time->tm_isdst)
558 offsetFromUTC += 3600.0;
559 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200560#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100561 if (tzunits < 0) {
562 tzunits = tzunits/-1;
563 ptr8[7] = bcdify(tzunits);
564 /* Flip it to negative */
565 ptr8[7] |= 0x08;
566 }
567 else
568 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100569
570 /* Does not support DST +2 */
571 if (local_time->tm_isdst)
572 dst = 1;
573 }
574
575 if (dst) {
576 ptr8 = msgb_put(msg, 3);
577 ptr8[0] = GSM48_IE_NET_DST;
578 ptr8[1] = 1;
579 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100580 }
Harald Weltedb253af2008-12-30 17:56:55 +0000581
Daniel Willmanneea93372009-08-13 03:42:07 +0200582 DEBUGP(DMM, "-> MM INFO\n");
583
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800584 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000585}
586
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100587/*! Send an Authentication Request to MS on the given subscriber connection
588 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
589 * \param[in] conn Subscriber connection to send on.
590 * \param[in] rand Random challenge token to send, must be 16 bytes long.
591 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
592 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
593 * \param[in] key_seq auth tuple's sequence number.
594 */
595int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
596 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200597{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100598 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200599 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200600 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200601
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100602 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100603 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100604 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200605
Harald Welte7984d5c2009-08-12 22:56:50 +0200606 gh->proto_discr = GSM48_PDISC_MM;
607 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
608
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100609 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200610
Harald Welte7984d5c2009-08-12 22:56:50 +0200611 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100612 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200613 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200614 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200615
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100616
617 /* 16 bytes AUTN */
618 if (autn)
619 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
620
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800621 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200622}
623
624/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800625int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200626{
627 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800628 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200629}
630
Harald Welte2483f1b2016-06-19 18:06:02 +0200631static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
632static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
633
634static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100635{
Harald Welte2483f1b2016-06-19 18:06:02 +0200636 uint8_t mi_type;
637 char mi_string[GSM48_MI_SIZE];
638 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100639
Harald Welte2483f1b2016-06-19 18:06:02 +0200640 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
641 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100642
Harald Welte2483f1b2016-06-19 18:06:02 +0200643 switch (mi_type) {
644 case GSM_MI_TYPE_IMSI:
645 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
646 goto accept_reuse;
647 break;
648 case GSM_MI_TYPE_TMSI:
649 tmsi = osmo_load32be(mi_lv+2);
650 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
651 goto accept_reuse;
652 break;
653 case GSM_MI_TYPE_IMEI:
654 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
655 goto accept_reuse;
656 break;
657 default:
658 break;
659 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100660
Harald Welte2483f1b2016-06-19 18:06:02 +0200661 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
662 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
663 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
664 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100665
Harald Welte2483f1b2016-06-19 18:06:02 +0200666accept_reuse:
667 DEBUGP(DMM, "%s: re-using already accepted connection\n",
668 vlr_subscr_name(conn->vsub));
669 conn->received_cm_service_request = true;
670 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100671}
672
Harald Welte4ed0e922009-01-10 03:17:30 +0000673/*
674 * Handle CM Service Requests
675 * a) Verify that the packet is long enough to contain the information
676 * we require otherwsie reject with INCORRECT_MESSAGE
677 * b) Try to parse the TMSI. If we do not have one reject
678 * c) Check that we know the subscriber with the TMSI otherwise reject
679 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200680 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200681 *
682 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000683 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200684int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000685{
Harald Welte2483f1b2016-06-19 18:06:02 +0200686 static const enum subscr_conn_from conn_from_cm_service_req =
687 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
688 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200689 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200690 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000691
Harald Welteba4cf162009-01-10 01:49:35 +0000692 struct gsm48_hdr *gh = msgb_l3(msg);
693 struct gsm48_service_request *req =
694 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800695 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200696 uint8_t classmark2_len = gh->data[1];
697 uint8_t *classmark2 = gh->data+2;
698 uint8_t mi_len = *(classmark2 + classmark2_len);
699 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200700 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200701 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200702 int rc;
703
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100704 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200705 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000706
Harald Weltec9e02182009-05-01 19:07:53 +0000707 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000708 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000709 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200710 return msc_gsm48_tx_mm_serv_rej(conn,
711 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000712 }
713
714 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000715 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200716 return msc_gsm48_tx_mm_serv_rej(conn,
717 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000718 }
719
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200720 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000721 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100722 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
723 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200724
Harald Welte3995d2e2018-01-24 11:23:54 +0100725 switch (mi_type) {
726 case GSM_MI_TYPE_IMSI:
727 case GSM_MI_TYPE_TMSI:
728 /* continue below */
729 break;
730 case GSM_MI_TYPE_IMEI:
731 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
732 /* We don't do emergency calls by IMEI */
733 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
734 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
735 }
736 /* fall-through for non-emergency setup */
737 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200738 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200739 return msc_gsm48_tx_mm_serv_rej(conn,
740 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000741 }
742
Harald Welte37382ec2018-01-24 12:49:34 +0100743 switch (req->cm_service_type) {
744 case GSM48_CMSERV_MO_CALL_PACKET:
745 case GSM48_CMSERV_EMERGENCY:
746 case GSM48_CMSERV_SMS:
747 case GSM48_CMSERV_SUP_SERV:
748 /* continue below */
749 break;
750 default:
751 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
752 }
753
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200754 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200755 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
756 conn->classmark.classmark2_len = classmark2_len;
757
758 if (conn->conn_fsm) {
759 if (msc_subscr_conn_is_accepted(conn))
760 return cm_serv_reuse_conn(conn, mi-1);
761 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
762 vlr_subscr_name(conn->vsub));
763 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
764 return -EINVAL;
765 }
766
767 rc = msc_create_conn_fsm(conn, mi_string);
768 if (rc) {
769 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
770 /* logging already happened in msc_create_conn_fsm() */
771 return rc;
772 }
Harald Welte7659de12009-12-13 12:39:18 +0100773
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200774 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200775 vlr_proc_acc_req(conn->conn_fsm,
776 SUBSCR_CONN_E_ACCEPTED,
777 SUBSCR_CONN_E_CN_CLOSE,
778 (void*)&conn_from_cm_service_req,
779 net->vlr, conn,
780 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200781 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100782 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200783 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200784 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000785
Harald Welte2483f1b2016-06-19 18:06:02 +0200786 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000787}
788
Harald Welte49f8fcb2018-01-24 21:40:19 +0100789/* Receive a CM Re-establish Request */
790static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
791{
792 uint8_t mi_type;
793 char mi_string[GSM48_MI_SIZE];
794 struct gsm48_hdr *gh = msgb_l3(msg);
795
796 uint8_t classmark2_len = gh->data[1];
797 uint8_t *classmark2 = gh->data+2;
798 uint8_t mi_len = *(classmark2 + classmark2_len);
799 uint8_t *mi = (classmark2 + classmark2_len + 1);
800
801 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
802 mi_type = mi[0] & GSM_MI_TYPE_MASK;
803 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
804
805 /* we don't support CM call re-establishment */
806 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
807}
808
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200809static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000810{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200811 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000812 struct gsm48_hdr *gh = msgb_l3(msg);
813 struct gsm48_imsi_detach_ind *idi =
814 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200815 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200816 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200817 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000818
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200819 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200820 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
821 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000822
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200823 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100824
Harald Welte2a139372009-02-22 21:14:55 +0000825 switch (mi_type) {
826 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200827 vsub = vlr_subscr_find_by_tmsi(network->vlr,
828 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000829 break;
830 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200831 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000832 break;
833 case GSM_MI_TYPE_IMEI:
834 case GSM_MI_TYPE_IMEISV:
835 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200836 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
837 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000838 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200839 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200840 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
841 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000842 break;
843 }
844
Harald Welte2483f1b2016-06-19 18:06:02 +0200845 /* TODO? We used to remember the subscriber's classmark1 here and
846 * stored it in the old sqlite db, but now we store it in a conn that
847 * will be discarded anyway: */
848 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200849
Harald Welte2483f1b2016-06-19 18:06:02 +0200850 if (!vsub) {
851 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
852 gsm48_mi_type_name(mi_type), mi_string);
853 } else {
854 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100855
856 if (vsub->cs.is_paging)
857 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
858
Harald Welte2483f1b2016-06-19 18:06:02 +0200859 vlr_subscr_rx_imsi_detach(vsub);
860 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
861 vlr_subscr_put(vsub);
862 }
Harald Welte2a139372009-02-22 21:14:55 +0000863
Harald Welte2483f1b2016-06-19 18:06:02 +0200864 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000865 return 0;
866}
867
Harald Welted2a7f5a2009-06-05 20:08:20 +0000868static int gsm48_rx_mm_status(struct msgb *msg)
869{
870 struct gsm48_hdr *gh = msgb_l3(msg);
871
872 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
873
874 return 0;
875}
876
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100877static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
878 struct gsm_subscriber_connection *conn,
879 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100880{
881 struct gsm48_hdr *gh = msgb_l3(msg);
882 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100883
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100884 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
885 LOGP(DMM, LOGL_ERROR,
886 "%s: MM AUTHENTICATION RESPONSE:"
887 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200888 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100889 return -EINVAL;
890 }
891
892 *res_len = sizeof(ar->sres);
893 memcpy(res, ar->sres, sizeof(ar->sres));
894 return 0;
895}
896
897static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
898 struct gsm_subscriber_connection *conn,
899 struct msgb *msg)
900{
901 struct gsm48_hdr *gh;
902 uint8_t *data;
903 uint8_t iei;
904 uint8_t ie_len;
905 unsigned int data_len;
906
907 /* First parse the GSM part */
908 if (parse_gsm_auth_resp(res, res_len, conn, msg))
909 return -EINVAL;
910 OSMO_ASSERT(*res_len == 4);
911
912 /* Then add the extended res part */
913 gh = msgb_l3(msg);
914 data = gh->data + sizeof(struct gsm48_auth_resp);
915 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
916
917 if (data_len < 3) {
918 LOGP(DMM, LOGL_ERROR,
919 "%s: MM AUTHENTICATION RESPONSE:"
920 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200921 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100922 return -EINVAL;
923 }
924
925 iei = data[0];
926 ie_len = data[1];
927 if (iei != GSM48_IE_AUTH_RES_EXT) {
928 LOGP(DMM, LOGL_ERROR,
929 "%s: MM R99 AUTHENTICATION RESPONSE:"
930 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200931 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100932 GSM48_IE_AUTH_RES_EXT, iei);
933 return -EINVAL;
934 }
935
936 if (ie_len > 12) {
937 LOGP(DMM, LOGL_ERROR,
938 "%s: MM R99 AUTHENTICATION RESPONSE:"
939 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200940 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100941 return -EINVAL;
942 }
943
944 *res_len += ie_len;
945 memcpy(res + 4, &data[2], ie_len);
946 return 0;
947}
948
949/* Chapter 9.2.3: Authentication Response */
950static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
951{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100952 uint8_t res[16];
953 uint8_t res_len;
954 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100955 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100956
Harald Welte2483f1b2016-06-19 18:06:02 +0200957 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100958 LOGP(DMM, LOGL_ERROR,
959 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200960 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100961 return -EINVAL;
962 }
963
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100964 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
965
966 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100967 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100968 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100970
971 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100972 LOGP(DMM, LOGL_ERROR,
973 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
974 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100975 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100976 memset(res, 0, sizeof(res));
977 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100978 }
979
980 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200981 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100982 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983 osmo_hexdump_nospc(res, res_len));
984
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100985 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +0200986 conn->via_ran == RAN_UTRAN_IU,
987 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100988}
989
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100990static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
991{
992 struct gsm48_hdr *gh = msgb_l3(msg);
993 uint8_t cause;
994 uint8_t auts_tag;
995 uint8_t auts_len;
996 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100997
Harald Welte2483f1b2016-06-19 18:06:02 +0200998 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100999 LOGP(DMM, LOGL_ERROR,
1000 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001001 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001002 return -EINVAL;
1003 }
1004
1005 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1006 LOGP(DMM, LOGL_ERROR,
1007 "%s: MM R99 AUTHENTICATION FAILURE:"
1008 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001009 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1010 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001011 return -EINVAL;
1012 }
1013
1014 cause = gh->data[0];
1015
1016 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1017 LOGP(DMM, LOGL_INFO,
1018 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001019 vlr_subscr_name(conn->vsub), cause);
1020 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1021 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001022 }
1023
1024 /* This is a Synch Failure procedure, which should pass an AUTS to
1025 * resynchronize the sequence nr with the HLR. Expecting exactly one
1026 * TLV with 14 bytes of AUTS. */
1027
1028 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1029 LOGP(DMM, LOGL_INFO,
1030 "%s: MM R99 AUTHENTICATION FAILURE:"
1031 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001032 vlr_subscr_name(conn->vsub));
1033 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001034 return -EINVAL;
1035 }
1036
1037 auts_tag = gh->data[1];
1038 auts_len = gh->data[2];
1039 auts = &gh->data[3];
1040
1041 if (auts_tag != GSM48_IE_AUTS
1042 || auts_len != 14) {
1043 LOGP(DMM, LOGL_INFO,
1044 "%s: MM R99 AUTHENTICATION FAILURE:"
1045 " invalid Synch Failure:"
1046 " expected AUTS IE 0x%02x of 14 bytes,"
1047 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001048 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001049 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001050 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001051 return -EINVAL;
1052 }
1053
1054 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1055 LOGP(DMM, LOGL_INFO,
1056 "%s: MM R99 AUTHENTICATION FAILURE:"
1057 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001058 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1059 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001060 return -EINVAL;
1061 }
1062
1063 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1064 * large enough. */
1065
1066 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001067 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001068
Harald Welte2483f1b2016-06-19 18:06:02 +02001069 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1070}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001071
Harald Welte2483f1b2016-06-19 18:06:02 +02001072static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1073{
1074 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1075 vlr_subscr_name(conn->vsub));
1076 if (!conn->vsub) {
1077 LOGP(DMM, LOGL_ERROR,
1078 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1079 return -EINVAL;
1080 }
1081 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001082}
1083
Harald Weltebf5e8df2009-02-03 12:59:45 +00001084/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001085static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001086{
1087 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001088 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001089
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001090 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001091 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001092 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001093 break;
1094 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001095 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001096 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001097 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001098 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001099 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001100 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001101 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001102 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001103 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001104 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001105 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001106 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001107 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001108 break;
1109 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001110 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001111 break;
1112 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001113 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001114 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001115 case GSM48_MT_MM_AUTH_FAIL:
1116 rc = gsm48_rx_mm_auth_fail(conn, msg);
1117 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001118 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001119 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001120 gh->msg_type);
1121 break;
1122 }
1123
1124 return rc;
1125}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001126
Harald Welte2483f1b2016-06-19 18:06:02 +02001127static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1128{
1129 /* Check the size for the classmark */
1130 if (tot_len < 1 + *classmark2_lv)
1131 return NULL;
1132
1133 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1134 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1135 return NULL;
1136
1137 return mi_lv;
1138}
1139
Harald Welte2d35ae62009-02-06 12:02:13 +00001140/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001141static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001142{
Harald Welte2483f1b2016-06-19 18:06:02 +02001143 static const enum subscr_conn_from conn_from_paging_resp =
1144 SUBSCR_CONN_FROM_PAGING_RESP;
1145 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001147 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001148 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001149 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001150 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001151 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001152 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001153 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001154 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001155
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001156 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001157 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001158
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001159 resp = (struct gsm48_pag_resp *) &gh->data[0];
1160 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1161 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001162 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1163 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001164
Harald Welte2483f1b2016-06-19 18:06:02 +02001165 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1166 if (!mi_lv) {
1167 /* FIXME */
1168 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001169 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001170
Harald Welte2483f1b2016-06-19 18:06:02 +02001171 rc = msc_create_conn_fsm(conn, mi_string);
1172 if (rc)
1173 /* logging already happened in msc_create_conn_fsm() */
1174 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001175
Harald Welte2483f1b2016-06-19 18:06:02 +02001176 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1177 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001178
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001179 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001180 vlr_proc_acc_req(conn->conn_fsm,
1181 SUBSCR_CONN_E_ACCEPTED,
1182 SUBSCR_CONN_E_CN_CLOSE,
1183 (void*)&conn_from_paging_resp,
1184 net->vlr, conn,
1185 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001186 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001187 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001188 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001189 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001190
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001191 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001192}
1193
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001194static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001195{
1196 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001197 uint8_t apdu_id_flags;
1198 uint8_t apdu_len;
1199 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001200
1201 apdu_id_flags = gh->data[0];
1202 apdu_len = gh->data[1];
1203 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001204
Max8dc8f232017-02-09 19:13:02 +01001205 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001206 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001207
Harald Welte2483f1b2016-06-19 18:06:02 +02001208 /* we're not using the app info blob anywhere, so ignore. */
1209#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001210 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001211#else
1212 return 0;
1213#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001214}
1215
Harald Weltebf5e8df2009-02-03 12:59:45 +00001216/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001217static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001218{
1219 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001220 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001221
1222 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001223 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001224 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001225 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001227 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001228 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001229 default:
Philippd37981e2016-10-10 15:11:41 +02001230 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1231 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001232 break;
1233 }
1234
Harald Welte2d35ae62009-02-06 12:02:13 +00001235 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001236}
1237
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001238int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1239 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001240{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001241 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001242 struct gsm48_hdr *gh;
1243
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001244 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1245 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001246
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001247 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1248 gh->proto_discr = GSM48_PDISC_RR;
1249 gh->msg_type = GSM48_MT_RR_APP_INFO;
1250 gh->data[0] = apdu_id;
1251 gh->data[1] = apdu_len;
1252 memcpy(gh->data+2, apdu, apdu_len);
1253
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001254 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001255}
1256
Alexander Couzens92f552f2016-08-23 07:32:27 +02001257/* FIXME: this count_statistics is a state machine behaviour. we should convert
1258 * the complete call control into a state machine. Afterwards we can move this
1259 * code into state transitions.
1260 */
1261static void count_statistics(struct gsm_trans *trans, int new_state)
1262{
1263 int old_state = trans->cc.state;
1264 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1265
1266 if (old_state == new_state)
1267 return;
1268
1269 /* state incoming */
1270 switch (new_state) {
1271 case GSM_CSTATE_ACTIVE:
1272 osmo_counter_inc(trans->net->active_calls);
1273 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1274 break;
1275 }
1276
1277 /* state outgoing */
1278 switch (old_state) {
1279 case GSM_CSTATE_ACTIVE:
1280 osmo_counter_dec(trans->net->active_calls);
1281 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1282 new_state == GSM_CSTATE_DISCONNECT_IND)
1283 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1284 else
1285 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1286 break;
1287 }
1288}
1289
Harald Welte4bc90a12008-12-27 16:32:52 +00001290/* Call Control */
1291
Harald Welte7584aea2009-02-11 11:44:12 +00001292/* The entire call control code is written in accordance with Figure 7.10c
1293 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1294 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1295 * it for voice */
1296
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297static void new_cc_state(struct gsm_trans *trans, int state)
1298{
1299 if (state > 31 || state < 0)
1300 return;
1301
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001302 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1303 trans->transaction_id,
1304 vlr_subscr_name(trans->vsub),
1305 gsm48_cc_state_name(trans->cc.state),
1306 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307
Alexander Couzens92f552f2016-08-23 07:32:27 +02001308 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001309 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310}
1311
1312static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001313{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001314 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001315 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001316 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001317
Harald Welte4bc90a12008-12-27 16:32:52 +00001318 gh->msg_type = GSM48_MT_CC_STATUS;
1319
1320 cause = msgb_put(msg, 3);
1321 cause[0] = 2;
1322 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1323 cause[2] = 0x80 | 30; /* response to status inquiry */
1324
1325 call_state = msgb_put(msg, 1);
1326 call_state[0] = 0xc0 | 0x00;
1327
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001328 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001329}
1330
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001331static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001332 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001333{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001334 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001335 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1336
Harald Welte6f4b7532008-12-29 00:39:37 +00001337 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001338 gh->msg_type = msg_type;
1339
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001340 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001341}
1342
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1344{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001345 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001346 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001347 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001348 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349 }
1350}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001351
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1353 int msg_type, struct gsm_mncc *mncc)
1354{
1355 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001356 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357
Philipp Maierfbf66102017-04-09 12:32:51 +02001358 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1359
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001360#if BEFORE_MSCSPLIT
1361 /* Re-enable this log output once we can obtain this information via
1362 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001364 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001365 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001367 trans->conn->lchan->ts->trx->bts->nr,
1368 trans->conn->lchan->ts->trx->nr,
1369 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001370 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001371 get_mncc_name(msg_type));
1372 else
1373 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1374 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001375 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376 get_mncc_name(msg_type));
1377 else
1378 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1379 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001380#else
1381 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1382#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383
1384 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001385
Harald Welte966636f2009-06-26 19:39:35 +02001386 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 if (!msg)
1388 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001389
1390 data = msgb_put(msg, sizeof(struct gsm_mncc));
1391 memcpy(data, mncc, sizeof(struct gsm_mncc));
1392
Harald Welte54209c22010-12-22 23:43:29 +01001393 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394
1395 return 0;
1396}
1397
1398int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001399 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400{
1401 struct gsm_mncc rel;
1402
Harald Welte92f70c52009-06-12 01:54:08 +08001403 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001405 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001406 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1407 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1409}
1410
Harald Weltedcaf5652009-07-23 18:56:43 +02001411/* Call Control Specific transaction release.
1412 * gets called by trans_free, DO NOT CALL YOURSELF! */
1413void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415 gsm48_stop_cc_timer(trans);
1416
Philipp Maier621ba032017-11-07 17:19:25 +01001417 /* Initiate the teardown of the related connections on the MGW */
1418 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001419
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 /* send release to L4, if callref still exists */
1421 if (trans->callref) {
1422 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001423 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001424 GSM48_CAUSE_LOC_PRN_S_LU,
1425 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001427 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001428 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429}
1430
1431static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001432
Harald Welte09e38af2009-02-16 22:52:23 +00001433/* call-back from paging the B-end of the connection */
1434static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001435 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001436{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001437 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001438 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001439
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001440 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001441
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001442 switch (event) {
1443 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001444 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1445 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001446 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001447 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001448 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001449 /* send SETUP request to called party */
1450 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1451 break;
1452 case GSM_PAGING_EXPIRED:
1453 case GSM_PAGING_BUSY:
1454 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001455 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001456 /* Temporarily out of order */
1457 mncc_release_ind(transt->net, transt,
1458 transt->callref,
1459 GSM48_CAUSE_LOC_PRN_S_LU,
1460 GSM48_CC_CAUSE_DEST_OOO);
1461 transt->callref = 0;
1462 transt->paging_request = NULL;
1463 trans_free(transt);
1464 break;
1465 default:
1466 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1467 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001468 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001469
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001470 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001471 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001472}
Harald Welte7584aea2009-02-11 11:44:12 +00001473
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001475static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001476{
Harald Welte53d51f52015-12-03 14:59:04 +01001477 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1478 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001479 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001480
Harald Welte4bfdfe72009-06-10 23:11:52 +08001481 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001482 return -EIO;
1483
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001484 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001485 return -EIO;
1486
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001487 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001488 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001489
Philipp Maier621ba032017-11-07 17:19:25 +01001490 /* Bridge RTP streams */
1491 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1492 trans2->conn->rtp.local_addr_cn);
1493 if (rc)
1494 return -EINVAL;
1495
1496 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1497 trans1->conn->rtp.local_addr_cn);
1498 if (rc)
1499 return -EINVAL;
1500
1501 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001502}
1503
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1505{
1506 DEBUGP(DCC, "-> STATUS ENQ\n");
1507 return gsm48_cc_tx_status(trans, msg);
1508}
1509
1510static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1511static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1512
1513static void gsm48_cc_timeout(void *arg)
1514{
1515 struct gsm_trans *trans = arg;
1516 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001517 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1518 int mo_location = GSM48_CAUSE_LOC_USER;
1519 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1520 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 struct gsm_mncc mo_rel, l4_rel;
1522
1523 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1524 mo_rel.callref = trans->callref;
1525 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1526 l4_rel.callref = trans->callref;
1527
Harald Weltedcaf5652009-07-23 18:56:43 +02001528 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001529 case 0x303:
1530 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001531 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001532 break;
1533 case 0x310:
1534 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001535 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 break;
1537 case 0x313:
1538 disconnect = 1;
1539 /* unknown, did not find it in the specs */
1540 break;
1541 case 0x301:
1542 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001543 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001544 break;
1545 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001546 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001548 gsm48_cc_tx_release(trans, &trans->cc.msg);
1549 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001550 break; /* stay in release state */
1551 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001552 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 case 0x306:
1555 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001556 mo_cause = trans->cc.msg.cause.value;
1557 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001558 break;
1559 case 0x323:
1560 disconnect = 1;
1561 break;
1562 default:
1563 release = 1;
1564 }
1565
1566 if (release && trans->callref) {
1567 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001568 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 l4_location, l4_cause);
1570 trans->callref = 0;
1571 }
1572
1573 if (disconnect && trans->callref) {
1574 /* process disconnect towards layer 4 */
1575 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001576 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 }
1578
1579 /* process disconnect towards mobile station */
1580 if (disconnect || release) {
1581 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001582 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1583 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1584 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 mo_rel.cause.diag_len = 3;
1586
1587 if (disconnect)
1588 gsm48_cc_tx_disconnect(trans, &mo_rel);
1589 if (release)
1590 gsm48_cc_tx_release(trans, &mo_rel);
1591 }
1592
1593}
1594
Max3ffce192016-04-25 15:22:00 +02001595/* disconnect both calls from the bridge */
1596static inline void disconnect_bridge(struct gsm_network *net,
1597 struct gsm_mncc_bridge *bridge, int err)
1598{
1599 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1600 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1601 struct gsm_mncc mx_rel;
1602 if (!trans0 || !trans1)
1603 return;
1604
1605 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1606 trans0->callref, trans1->callref, strerror(err));
1607
1608 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1609 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1610 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1611
1612 mx_rel.callref = trans0->callref;
1613 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1614
1615 mx_rel.callref = trans1->callref;
1616 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1617}
1618
Harald Welte4bfdfe72009-06-10 23:11:52 +08001619static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1620 int sec, int micro)
1621{
1622 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001623 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001624 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001625 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626}
1627
1628static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1629{
1630 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001631 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001632 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1633 struct tlv_parsed tp;
1634 struct gsm_mncc setup;
1635
1636 memset(&setup, 0, sizeof(struct gsm_mncc));
1637 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001638
Harald Welte474d19f2010-03-02 23:18:30 +01001639 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001641 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1642 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001644 /* use destination number as configured by user (if any) */
1645 if (trans->net->emergency.route_to_msisdn) {
1646 setup.fields |= MNCC_F_CALLED;
1647 setup.called.type = 0; /* unknown */
1648 setup.called.plan = 0; /* unknown */
1649 OSMO_STRLCPY_ARRAY(setup.called.number,
1650 trans->net->emergency.route_to_msisdn);
1651 }
Harald Welte0dd01372018-01-24 11:06:19 +01001652 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653
1654 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001655 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001656 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1657 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001658
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 /* bearer capability */
1660 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1661 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001662 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001664
1665 /* Create a copy of the bearer capability
1666 * in the transaction struct, so we can use
1667 * this information later */
1668 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1669 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 }
1671 /* facility */
1672 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1673 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001674 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001675 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1676 }
1677 /* called party bcd number */
1678 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1679 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001680 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1682 }
1683 /* user-user */
1684 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1685 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001686 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001687 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1688 }
1689 /* ss-version */
1690 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1691 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001692 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1694 }
1695 /* CLIR suppression */
1696 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1697 setup.clir.sup = 1;
1698 /* CLIR invocation */
1699 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1700 setup.clir.inv = 1;
1701 /* cc cap */
1702 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1703 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001704 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1706 }
1707
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 new_cc_state(trans, GSM_CSTATE_INITIATED);
1709
Harald Welte79826e12018-01-24 10:50:24 +01001710 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1711 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001712 setup.called.number);
1713
Alexander Couzensb847a212016-08-02 11:34:11 +02001714 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001715
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001717 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718
Harald Welte13cac662009-07-29 12:10:35 +02001719 /* MNCC code will modify the channel asynchronously, we should
1720 * ipaccess-bind only after the modification has been made to the
1721 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 return 0;
1723}
1724
1725static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001726{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001727 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001728 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001730 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001731
Harald Welte7ccf7782009-02-17 01:43:01 +00001732 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 /* transaction id must not be assigned */
1735 if (trans->transaction_id != 0xff) { /* unasssigned */
1736 DEBUGP(DCC, "TX Setup with assigned transaction. "
1737 "This is not allowed!\n");
1738 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001739 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001740 GSM48_CAUSE_LOC_PRN_S_LU,
1741 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001743 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744 return rc;
1745 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001746
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001748 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001749 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001750 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001752 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001753 GSM48_CAUSE_LOC_PRN_S_LU,
1754 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001756 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 return rc;
1758 }
Harald Welte78283ef2009-07-23 21:36:44 +02001759 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001760
Harald Welte65e74cc2008-12-29 01:55:35 +00001761 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001762
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001764
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001766 if (setup->fields & MNCC_F_BEARER_CAP) {
1767 /* Create a copy of the bearer capability in the transaction struct, so we
1768 * can use this information later */
1769 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001770 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001771 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 /* facility */
1773 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001774 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 /* progress */
1776 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001777 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 /* calling party BCD number */
1779 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001780 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 /* called party BCD number */
1782 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001783 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 /* user-user */
1785 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001786 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 /* redirecting party BCD number */
1788 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001789 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 /* signal */
1791 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001792 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001793
Harald Welte4bfdfe72009-06-10 23:11:52 +08001794 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001795
Alexander Couzensb847a212016-08-02 11:34:11 +02001796 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001797
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001798 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001799}
1800
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1802{
1803 struct gsm48_hdr *gh = msgb_l3(msg);
1804 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1805 struct tlv_parsed tp;
1806 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001807 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808
1809 gsm48_stop_cc_timer(trans);
1810 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1811
1812 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1813 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001814
Harald Welte474d19f2010-03-02 23:18:30 +01001815 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816#if 0
1817 /* repeat */
1818 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1819 call_conf.repeat = 1;
1820 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1821 call_conf.repeat = 2;
1822#endif
1823 /* bearer capability */
1824 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1825 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001826 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001828
1829 /* Create a copy of the bearer capability
1830 * in the transaction struct, so we can use
1831 * this information later */
1832 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1833 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 }
1835 /* cause */
1836 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1837 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001838 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1840 }
1841 /* cc cap */
1842 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1843 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001844 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1846 }
1847
Andreas Eversberg035b8742013-09-19 09:27:06 +02001848 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001849 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001850
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1852
Philipp Maierfbf66102017-04-09 12:32:51 +02001853 /* Assign call (if not done yet) */
1854 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001855 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001856 trans->assignment_done = true;
1857 }
1858 else
1859 rc = 0;
1860
1861 /* don't continue, if there were problems with
1862 * the call assignment. */
1863 if (rc)
1864 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001865
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001866 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001867 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868}
1869
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001870static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871{
1872 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001873 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001875 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1878
1879 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1880
1881 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001882 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001884 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1885 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 /* facility */
1887 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001888 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 /* progress */
1890 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001891 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001893 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1894 if (rc)
1895 return rc;
1896
Philipp Maierfbf66102017-04-09 12:32:51 +02001897 /* Assign call (if not done yet) */
1898 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001899 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001900 trans->assignment_done = true;
1901 }
1902 else
1903 rc = 0;
1904
1905 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906}
1907
1908static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1909{
1910 struct gsm48_hdr *gh = msgb_l3(msg);
1911 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1912 struct tlv_parsed tp;
1913 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001914
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 gsm48_stop_cc_timer(trans);
1916 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1917
1918 memset(&alerting, 0, sizeof(struct gsm_mncc));
1919 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001920 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 /* facility */
1922 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1923 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001924 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1926 }
1927
1928 /* progress */
1929 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1930 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001931 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1933 }
1934 /* ss-version */
1935 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1936 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001937 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1939 }
1940
1941 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1942
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001943 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001944 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945}
1946
1947static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1948{
1949 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001950 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1952
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 gh->msg_type = GSM48_MT_CC_ALERTING;
1954
1955 /* facility */
1956 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001957 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 /* progress */
1959 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 /* user-user */
1962 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001963 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964
1965 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001966
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001967 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968}
1969
1970static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1971{
1972 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001973 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1975
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 gh->msg_type = GSM48_MT_CC_PROGRESS;
1977
1978 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 /* user-user */
1981 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001984 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985}
1986
1987static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1988{
1989 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001990 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1992
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 gh->msg_type = GSM48_MT_CC_CONNECT;
1994
1995 gsm48_stop_cc_timer(trans);
1996 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1997
1998 /* facility */
1999 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 /* progress */
2002 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 /* connected number */
2005 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 /* user-user */
2008 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010
2011 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2012
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002013 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014}
2015
2016static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2017{
2018 struct gsm48_hdr *gh = msgb_l3(msg);
2019 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2020 struct tlv_parsed tp;
2021 struct gsm_mncc connect;
2022
2023 gsm48_stop_cc_timer(trans);
2024
2025 memset(&connect, 0, sizeof(struct gsm_mncc));
2026 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002027 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002029 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002030 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2031 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002032
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033 /* facility */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2035 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2038 }
2039 /* user-user */
2040 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2041 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2044 }
2045 /* ss-version */
2046 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2047 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2050 }
2051
2052 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002053 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002055 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056}
2057
2058
2059static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2060{
2061 struct gsm_mncc connect_ack;
2062
2063 gsm48_stop_cc_timer(trans);
2064
2065 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002066 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002067
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2069 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002070
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002071 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 &connect_ack);
2073}
2074
2075static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2076{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002077 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2079
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2081
2082 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2083
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002084 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085}
2086
2087static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2088{
2089 struct gsm48_hdr *gh = msgb_l3(msg);
2090 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2091 struct tlv_parsed tp;
2092 struct gsm_mncc disc;
2093
2094 gsm48_stop_cc_timer(trans);
2095
2096 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2097
2098 memset(&disc, 0, sizeof(struct gsm_mncc));
2099 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002100 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 /* cause */
2102 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2103 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2106 }
2107 /* facility */
2108 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2109 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2112 }
2113 /* user-user */
2114 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2115 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2118 }
2119 /* ss-version */
2120 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2121 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2124 }
2125
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002126 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127
2128}
2129
Harald Weltec66b71c2009-06-11 14:23:20 +08002130static struct gsm_mncc_cause default_cause = {
2131 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2132 .coding = 0,
2133 .rec = 0,
2134 .rec_val = 0,
2135 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2136 .diag_len = 0,
2137 .diag = { 0 },
2138};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139
2140static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2141{
2142 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002143 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2145
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2147
2148 gsm48_stop_cc_timer(trans);
2149 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2150
2151 /* cause */
2152 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002153 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154 else
Harald Welte55c8f352010-03-07 23:40:35 +01002155 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156
2157 /* facility */
2158 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002159 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 /* progress */
2161 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 /* user-user */
2164 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002165 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166
2167 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002168 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169
2170 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2171
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002172 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173}
2174
2175static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2176{
2177 struct gsm48_hdr *gh = msgb_l3(msg);
2178 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2179 struct tlv_parsed tp;
2180 struct gsm_mncc rel;
2181 int rc;
2182
2183 gsm48_stop_cc_timer(trans);
2184
2185 memset(&rel, 0, sizeof(struct gsm_mncc));
2186 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002187 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 /* cause */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2190 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002191 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2193 }
2194 /* facility */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2196 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002197 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2199 }
2200 /* user-user */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2202 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2205 }
2206 /* ss-version */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2208 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2211 }
2212
Harald Weltedcaf5652009-07-23 18:56:43 +02002213 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002215 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002217 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002218 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002219 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002220 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 }
2222
2223 new_cc_state(trans, GSM_CSTATE_NULL);
2224
2225 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002226 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227
2228 return rc;
2229}
2230
2231static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2232{
2233 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002234 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2236
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 gh->msg_type = GSM48_MT_CC_RELEASE;
2238
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 gsm48_stop_cc_timer(trans);
2240 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2241
2242 /* cause */
2243 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002244 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 /* facility */
2246 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002247 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 /* user-user */
2249 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002250 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251
Harald Weltedcaf5652009-07-23 18:56:43 +02002252 trans->cc.T308_second = 0;
2253 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254
Harald Weltedcaf5652009-07-23 18:56:43 +02002255 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2257
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002258 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259}
2260
2261static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2262{
2263 struct gsm48_hdr *gh = msgb_l3(msg);
2264 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2265 struct tlv_parsed tp;
2266 struct gsm_mncc rel;
2267 int rc = 0;
2268
2269 gsm48_stop_cc_timer(trans);
2270
2271 memset(&rel, 0, sizeof(struct gsm_mncc));
2272 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002273 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 /* cause */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2276 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2279 }
2280 /* facility */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2282 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2285 }
2286 /* user-user */
2287 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2288 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002289 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2291 }
2292 /* ss-version */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2294 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002295 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2297 }
2298
2299 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002300 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002302 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 MNCC_REJ_IND, &rel);
2304 break;
2305 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002306 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 MNCC_REL_CNF, &rel);
2308 break;
2309 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002310 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 MNCC_REL_IND, &rel);
2312 }
2313 }
2314
2315 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002316 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317
2318 return rc;
2319}
2320
2321static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2322{
2323 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002324 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002326 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2329
2330 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002331
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 gsm48_stop_cc_timer(trans);
2333
2334 /* cause */
2335 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002336 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 /* facility */
2338 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 /* user-user */
2341 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343
Harald Weltec7782de2010-12-21 19:31:41 +01002344 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2345
Harald Weltedcaf5652009-07-23 18:56:43 +02002346 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347
Harald Weltec7782de2010-12-21 19:31:41 +01002348 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349}
2350
2351static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2352{
2353 struct gsm48_hdr *gh = msgb_l3(msg);
2354 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2355 struct tlv_parsed tp;
2356 struct gsm_mncc fac;
2357
2358 memset(&fac, 0, sizeof(struct gsm_mncc));
2359 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002360 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 /* facility */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2363 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002364 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2366 }
2367 /* ss-version */
2368 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2369 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002370 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2372 }
2373
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002374 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375}
2376
2377static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2378{
2379 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002380 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2382
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383 gh->msg_type = GSM48_MT_CC_FACILITY;
2384
2385 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002386 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002388 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389}
2390
2391static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2392{
2393 struct gsm_mncc hold;
2394
2395 memset(&hold, 0, sizeof(struct gsm_mncc));
2396 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002397 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398}
2399
2400static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2401{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002402 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2404
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2406
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002407 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408}
2409
2410static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2411{
2412 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002413 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2415
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2417
2418 /* cause */
2419 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 else
Harald Welte55c8f352010-03-07 23:40:35 +01002422 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002424 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2428{
2429 struct gsm_mncc retrieve;
2430
2431 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2432 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002433 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002434 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435}
2436
2437static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2438{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002439 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2441
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2443
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002444 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445}
2446
2447static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2448{
2449 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002450 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2452
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2454
2455 /* cause */
2456 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 else
Harald Welte55c8f352010-03-07 23:40:35 +01002459 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002461 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462}
2463
2464static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2465{
2466 struct gsm48_hdr *gh = msgb_l3(msg);
2467 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2468 struct tlv_parsed tp;
2469 struct gsm_mncc dtmf;
2470
2471 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2472 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002473 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 /* keypad facility */
2475 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2476 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002477 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2479 }
2480
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002481 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2485{
2486 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002487 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2491
2492 /* keypad */
2493 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002496 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497}
2498
2499static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2500{
2501 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002502 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2504
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2506
2507 /* cause */
2508 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510 else
Harald Welte55c8f352010-03-07 23:40:35 +01002511 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002513 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2517{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002518 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2520
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2522
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002523 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524}
2525
2526static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2527{
2528 struct gsm_mncc dtmf;
2529
2530 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2531 dtmf.callref = trans->callref;
2532
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002533 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534}
2535
2536static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2537{
2538 struct gsm48_hdr *gh = msgb_l3(msg);
2539 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2540 struct tlv_parsed tp;
2541 struct gsm_mncc modify;
2542
2543 memset(&modify, 0, sizeof(struct gsm_mncc));
2544 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002545 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546 /* bearer capability */
2547 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2548 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002549 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002551
2552 /* Create a copy of the bearer capability
2553 * in the transaction struct, so we can use
2554 * this information later */
2555 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2556 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 }
2558
2559 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2560
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002561 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562}
2563
2564static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2565{
2566 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002567 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002568 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2569
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 gh->msg_type = GSM48_MT_CC_MODIFY;
2571
2572 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2573
2574 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002576 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577
2578 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2579
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002580 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581}
2582
2583static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2584{
2585 struct gsm48_hdr *gh = msgb_l3(msg);
2586 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2587 struct tlv_parsed tp;
2588 struct gsm_mncc modify;
2589
2590 gsm48_stop_cc_timer(trans);
2591
2592 memset(&modify, 0, sizeof(struct gsm_mncc));
2593 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002594 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 /* bearer capability */
2596 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2597 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002598 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002600
2601 /* Create a copy of the bearer capability
2602 * in the transaction struct, so we can use
2603 * this information later */
2604 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2605 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606 }
2607
2608 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2609
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002610 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611}
2612
2613static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2614{
2615 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2618
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2620
2621 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002622 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002623 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624
2625 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2626
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002627 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628}
2629
2630static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2631{
2632 struct gsm48_hdr *gh = msgb_l3(msg);
2633 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2634 struct tlv_parsed tp;
2635 struct gsm_mncc modify;
2636
2637 gsm48_stop_cc_timer(trans);
2638
2639 memset(&modify, 0, sizeof(struct gsm_mncc));
2640 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002641 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 /* bearer capability */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2644 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002647
2648 /* Create a copy of the bearer capability
2649 * in the transaction struct, so we can use
2650 * this information later */
2651 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2652 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 }
2654 /* cause */
2655 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2656 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002657 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2659 }
2660
2661 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2662
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002663 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2667{
2668 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002669 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2671
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2673
2674 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002675 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002676 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002678 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679
2680 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2681
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002682 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683}
2684
2685static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2686{
2687 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002688 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2690
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 gh->msg_type = GSM48_MT_CC_NOTIFY;
2692
2693 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002694 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002696 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697}
2698
2699static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2700{
2701 struct gsm48_hdr *gh = msgb_l3(msg);
2702 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2703// struct tlv_parsed tp;
2704 struct gsm_mncc notify;
2705
2706 memset(&notify, 0, sizeof(struct gsm_mncc));
2707 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002708// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002710 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002712 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713}
2714
2715static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2716{
2717 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002718 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2720
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 gh->msg_type = GSM48_MT_CC_USER_INFO;
2722
2723 /* user-user */
2724 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002725 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 /* more data */
2727 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002728 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002730 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731}
2732
2733static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2734{
2735 struct gsm48_hdr *gh = msgb_l3(msg);
2736 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2737 struct tlv_parsed tp;
2738 struct gsm_mncc user;
2739
2740 memset(&user, 0, sizeof(struct gsm_mncc));
2741 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002742 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 /* user-user */
2744 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2745 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002746 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2748 }
2749 /* more data */
2750 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2751 user.more = 1;
2752
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002753 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754}
2755
Philipp Maierfbf66102017-04-09 12:32:51 +02002756static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2757 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2758 uint32_t payload_msg_type)
2759{
2760 uint8_t data[sizeof(struct gsm_mncc)];
2761 struct gsm_mncc_rtp *rtp;
2762
2763 memset(&data, 0, sizeof(data));
2764 rtp = (struct gsm_mncc_rtp *) &data[0];
2765
2766 rtp->callref = callref;
2767 rtp->msg_type = cmd;
2768 rtp->ip = addr;
2769 rtp->port = port;
2770 rtp->payload_type = payload_type;
2771 rtp->payload_msg_type = payload_msg_type;
2772 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2773}
2774
2775static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2776{
2777 int msg_type;
2778
2779 /* FIXME This has to be set to some meaningful value.
2780 * Possible options are:
2781 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2782 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2783 * (0 if unknown) */
2784 msg_type = GSM_TCHF_FRAME;
2785
Philipp Maier621ba032017-11-07 17:19:25 +01002786 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2787 uint16_t port = trans->conn->rtp.local_port_cn;
2788
2789 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2790
2791 if (addr == INADDR_NONE) {
2792 LOGP(DMNCC, LOGL_ERROR,
2793 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2794 vlr_subscr_name(trans->vsub));
2795 return;
2796 }
2797 if (port == 0) {
2798 LOGP(DMNCC, LOGL_ERROR,
2799 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2800 vlr_subscr_name(trans->vsub));
2801 return;
2802 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002803
2804 /* FIXME: This has to be set to some meaningful value,
2805 * before the MSC-Split, this value was pulled from
2806 * lchan->abis_ip.rtp_payload */
2807 uint32_t payload_type = 0;
2808
2809 return mncc_recv_rtp(net, trans->callref, cmd,
2810 addr,
2811 port,
2812 payload_type,
2813 msg_type);
2814}
2815
2816static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2817{
2818 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2819}
2820
2821static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2822{
2823 struct gsm_trans *trans;
2824 int rc;
2825
2826 /* Find callref */
2827 trans = trans_find_by_callref(net, callref);
2828 if (!trans) {
2829 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2830 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2831 return -EIO;
2832 }
2833 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2834 if (!trans->conn) {
2835 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2836 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2837 return 0;
2838 }
2839
2840 trans->conn->mncc_rtp_bridge = 1;
2841
Philipp Maier621ba032017-11-07 17:19:25 +01002842 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002843 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002844 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002845 * command to the MGCP-GW. The CRCX will return the port number,
2846 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2847 * have to return this port number back to the MNCC by sending
2848 * it back with the TCH_RTP_CREATE message. To make sure that
2849 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002850 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002851 * to take care of this by setting trans->tch_rtp_create to true.
2852 * This will make sure that gsm48_tch_rtp_create() (below) is
2853 * called as soon as the local port number has become known. */
2854 trans->tch_rtp_create = true;
2855
2856 /* Assign call (if not done yet) */
2857 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002858 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002859 trans->assignment_done = true;
2860 }
2861 else
2862 rc = 0;
2863
2864 return rc;
2865}
2866
2867/* Trigger TCH_RTP_CREATE acknowledgement */
2868int gsm48_tch_rtp_create(struct gsm_trans *trans)
2869{
2870 /* This function is called as soon as the port, on which the
2871 * mgcp-gw expects the incoming RTP stream from the remote
2872 * end (e.g. Asterisk) is known. */
2873
2874 struct gsm_subscriber_connection *conn = trans->conn;
2875 struct gsm_network *network = conn->network;
2876
2877 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2878 return 0;
2879}
2880
2881static int tch_rtp_connect(struct gsm_network *net, void *arg)
2882{
2883 struct gsm_trans *trans;
2884 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002885 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002886
2887 /* Find callref */
2888 trans = trans_find_by_callref(net, rtp->callref);
2889 if (!trans) {
2890 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2891 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2892 return -EIO;
2893 }
2894 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2895 if (!trans->conn) {
2896 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2897 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2898 return 0;
2899 }
2900
Philipp Maier621ba032017-11-07 17:19:25 +01002901 addr.s_addr = osmo_htonl(rtp->ip);
2902 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002903}
2904
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002906 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002907 int type;
2908 int (*rout) (struct gsm_trans *trans, void *arg);
2909} downstatelist[] = {
2910 /* mobile originating call establishment */
2911 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002912 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2914 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2915 {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 */
2916 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2917 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2918 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2919 /* mobile terminating call establishment */
2920 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2921 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2922 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2923 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2924 /* signalling during call */
2925 {SBIT(GSM_CSTATE_ACTIVE),
2926 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2927 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2928 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2929 {ALL_STATES,
2930 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2931 {ALL_STATES,
2932 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2933 {ALL_STATES,
2934 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2935 {SBIT(GSM_CSTATE_ACTIVE),
2936 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2937 {SBIT(GSM_CSTATE_ACTIVE),
2938 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2939 {SBIT(GSM_CSTATE_ACTIVE),
2940 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2941 {SBIT(GSM_CSTATE_ACTIVE),
2942 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2945 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2946 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2947 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2948 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2949 {SBIT(GSM_CSTATE_ACTIVE),
2950 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2951 /* clearing */
2952 {SBIT(GSM_CSTATE_INITIATED),
2953 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2954 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2955 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2956 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2957 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958};
2959
2960#define DOWNSLLEN \
2961 (sizeof(downstatelist) / sizeof(struct downstate))
2962
2963
Harald Welte76556372010-12-22 23:57:45 +01002964int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965{
Harald Welte1a6f7982009-08-09 18:52:33 +02002966 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002968 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969 struct gsm_mncc *data = arg, rel;
2970
Harald Welte04dc88f2010-12-22 21:45:05 +01002971 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2972
Harald Welte4bfdfe72009-06-10 23:11:52 +08002973 /* handle special messages */
2974 switch(msg_type) {
2975 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002976 rc = tch_bridge(net, arg);
2977 if (rc < 0)
2978 disconnect_bridge(net, arg, -rc);
2979 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002980 case MNCC_RTP_CREATE:
2981 return tch_rtp_create(net, data->callref);
2982 case MNCC_RTP_CONNECT:
2983 return tch_rtp_connect(net, arg);
2984 case MNCC_RTP_FREE:
2985 /* unused right now */
2986 return -EIO;
2987
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002990 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002991 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002992 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002993 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002994 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2995 get_mncc_name(msg_type));
2996 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 }
2998
2999 memset(&rel, 0, sizeof(struct gsm_mncc));
3000 rel.callref = data->callref;
3001
3002 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003003 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004
3005 /* Callref unknown */
3006 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003007 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003008
Harald Welte4a3464c2009-07-04 10:11:24 +02003009 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3011 "Received '%s' from MNCC with "
3012 "unknown callref %d\n", data->called.number,
3013 get_mncc_name(msg_type), data->callref);
3014 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003015 return mncc_release_ind(net, NULL, data->callref,
3016 GSM48_CAUSE_LOC_PRN_S_LU,
3017 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003019 if (!data->called.number[0] && !data->imsi[0]) {
3020 DEBUGP(DCC, "(bts - trx - ts - ti) "
3021 "Received '%s' from MNCC with "
3022 "no number or IMSI\n", get_mncc_name(msg_type));
3023 /* Invalid number */
3024 return mncc_release_ind(net, NULL, data->callref,
3025 GSM48_CAUSE_LOC_PRN_S_LU,
3026 GSM48_CC_CAUSE_INV_NR_FORMAT);
3027 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003029 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003030 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3031 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003032 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003033 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003034
3035 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003036 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003037
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003039 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3041 "Received '%s' from MNCC with "
3042 "unknown subscriber %s\n", data->called.number,
3043 get_mncc_name(msg_type), data->called.number);
3044 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003045 return mncc_release_ind(net, NULL, data->callref,
3046 GSM48_CAUSE_LOC_PRN_S_LU,
3047 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 }
3049 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003050 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3052 "Received '%s' from MNCC with "
3053 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003054 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003055 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003057 return mncc_release_ind(net, NULL, data->callref,
3058 GSM48_CAUSE_LOC_PRN_S_LU,
3059 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 }
3061 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003062 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003063 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003065 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003067 mncc_release_ind(net, NULL, data->callref,
3068 GSM48_CAUSE_LOC_PRN_S_LU,
3069 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 return -ENOMEM;
3071 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003072
3073 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003074 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003075
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003076 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003077 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 /* find transaction with this subscriber already paging */
3079 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003080 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003081 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003082 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003084 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085 "Received '%s' from MNCC with "
3086 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003087 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003089 get_mncc_name(msg_type), vsub->lac);
3090 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003091 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 return 0;
3093 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003094 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003095 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003096
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003097 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003098 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003099 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003100 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003101 trans,
3102 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003103 if (!trans->paging_request) {
3104 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003105 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003106 trans_free(trans);
3107 return 0;
3108 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003109 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 return 0;
3111 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003112
3113 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003114 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003115 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003116 } else {
3117 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003118 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003120
3121 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003122 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123
3124 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003125 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003126 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003127 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003128 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003130 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3131 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 if (msg_type == MNCC_REL_REQ)
3133 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3134 else
3135 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3136 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003137 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003139 } else {
3140 DEBUGP(DCC, "(ti %02x sub %s) "
3141 "Received '%s' from MNCC in state %d (%s)\n",
3142 trans->transaction_id,
3143 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3144 get_mncc_name(msg_type), trans->cc.state,
3145 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146 }
3147
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 /* Find function for current state and message */
3149 for (i = 0; i < DOWNSLLEN; i++)
3150 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003151 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 break;
3153 if (i == DOWNSLLEN) {
3154 DEBUGP(DCC, "Message unhandled at this state.\n");
3155 return 0;
3156 }
3157
3158 rc = downstatelist[i].rout(trans, arg);
3159
3160 return rc;
3161}
3162
3163
3164static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003165 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 int type;
3167 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3168} datastatelist[] = {
3169 /* mobile originating call establishment */
3170 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3171 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3172 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3173 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3174 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3175 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3176 /* mobile terminating call establishment */
3177 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3178 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3179 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3180 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003181 {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 +08003182 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3183 /* signalling during call */
3184 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3185 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3186 {SBIT(GSM_CSTATE_ACTIVE),
3187 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3188 {ALL_STATES,
3189 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3190 {ALL_STATES,
3191 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3192 {ALL_STATES,
3193 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3194 {SBIT(GSM_CSTATE_ACTIVE),
3195 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3200 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3201 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3202 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3203 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3204 {SBIT(GSM_CSTATE_ACTIVE),
3205 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3206 /* clearing */
3207 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3208 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3209 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3210 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3211 {ALL_STATES, /* 5.4.3.4 */
3212 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3213};
3214
3215#define DATASLLEN \
3216 (sizeof(datastatelist) / sizeof(struct datastate))
3217
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003218static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003219{
3220 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003221 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003222 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003223 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003224 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003225
Harald Welte4bfdfe72009-06-10 23:11:52 +08003226 if (msg_type & 0x80) {
3227 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3228 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003229 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003230
Harald Welte2483f1b2016-06-19 18:06:02 +02003231 if (!conn->vsub) {
3232 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003233 return -EINVAL;
3234 }
3235
Harald Welte4bfdfe72009-06-10 23:11:52 +08003236 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003237 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003238
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003239#if BEFORE_MSCSPLIT
3240 /* Re-enable this log output once we can obtain this information via
3241 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003242 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003244 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003245 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003246 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3247 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003248#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249
3250 /* Create transaction */
3251 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003252 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253 "creating new trans.\n", transaction_id);
3254 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003255 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003256 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003257 transaction_id, new_callref++);
3258 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003260 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003261 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 GSM48_MT_CC_RELEASE_COMPL);
3263 return -ENOMEM;
3264 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003266 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003267 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003268 }
3269
3270 /* find function for current state and message */
3271 for (i = 0; i < DATASLLEN; i++)
3272 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003273 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003274 break;
3275 if (i == DATASLLEN) {
3276 DEBUGP(DCC, "Message unhandled at this state.\n");
3277 return 0;
3278 }
3279
Harald Welte2483f1b2016-06-19 18:06:02 +02003280 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003281
Harald Welte4bfdfe72009-06-10 23:11:52 +08003282 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003283
Harald Welte2483f1b2016-06-19 18:06:02 +02003284 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003285 return rc;
3286}
3287
Harald Welte2483f1b2016-06-19 18:06:02 +02003288static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003289{
Harald Welte2483f1b2016-06-19 18:06:02 +02003290 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3291 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003292
Harald Welte2483f1b2016-06-19 18:06:02 +02003293 switch (pdisc) {
3294 case GSM48_PDISC_MM:
3295 switch (msg_type) {
3296 case GSM48_MT_MM_LOC_UPD_REQUEST:
3297 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003298 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003299 case GSM48_MT_MM_AUTH_RESP:
3300 case GSM48_MT_MM_AUTH_FAIL:
3301 case GSM48_MT_MM_ID_RESP:
3302 case GSM48_MT_MM_TMSI_REALL_COMPL:
3303 case GSM48_MT_MM_IMSI_DETACH_IND:
3304 return true;
3305 default:
3306 break;
3307 }
3308 break;
3309 case GSM48_PDISC_RR:
3310 switch (msg_type) {
3311 case GSM48_MT_RR_CIPH_M_COMPL:
3312 case GSM48_MT_RR_PAG_RESP:
3313 return true;
3314 default:
3315 break;
3316 }
3317 break;
3318 default:
3319 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003320 }
3321
Harald Welte2483f1b2016-06-19 18:06:02 +02003322 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003323}
3324
Harald Welte2483f1b2016-06-19 18:06:02 +02003325void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3326 struct msgb *msg)
3327{
3328
3329 /* If a CM Service Request was received before, this is the request the
3330 * conn was opened for. No need to wait for further messages. */
3331
3332 if (!conn->received_cm_service_request)
3333 return;
3334
3335 if (log_check_level(DMM, LOGL_DEBUG)) {
3336 struct gsm48_hdr *gh = msgb_l3(msg);
3337 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3338 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3339
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003340 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003341 " received_cm_service_request changes to false\n",
3342 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003343 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003344 }
3345 conn->received_cm_service_request = false;
3346}
3347
Harald Weltec2007852018-02-03 21:08:26 +01003348/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3349int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3350{
3351 switch (pdisc) {
3352 case GSM48_PDISC_MM:
3353 case GSM48_PDISC_CC:
3354 case GSM48_PDISC_NC_SS:
3355 return 0;
3356 case GSM48_PDISC_GROUP_CC:
3357 return 1;
3358 case GSM48_PDISC_BCAST_CC:
3359 return 2;
3360 case GSM48_PDISC_LOC:
3361 return 3;
3362 default:
3363 return -1;
3364 }
3365}
3366
3367/* extract the N(SD) and return the modulo value for a R98 message */
3368static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3369{
3370 switch (pdisc) {
3371 case GSM48_PDISC_MM:
3372 case GSM48_PDISC_CC:
3373 case GSM48_PDISC_NC_SS:
3374 *n_sd = (msg_type >> 6) & 0x3;
3375 return 4;
3376 case GSM48_PDISC_GROUP_CC:
3377 case GSM48_PDISC_BCAST_CC:
3378 case GSM48_PDISC_LOC:
3379 *n_sd = (msg_type >> 6) & 0x1;
3380 return 2;
3381 default:
3382 /* no sequence number, we cannot detect dups */
3383 return 0;
3384 }
3385}
3386
3387/* extract the N(SD) and return the modulo value for a R99 message */
3388static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3389{
3390 switch (pdisc) {
3391 case GSM48_PDISC_MM:
3392 case GSM48_PDISC_CC:
3393 case GSM48_PDISC_NC_SS:
3394 case GSM48_PDISC_GROUP_CC:
3395 case GSM48_PDISC_BCAST_CC:
3396 case GSM48_PDISC_LOC:
3397 *n_sd = (msg_type >> 6) & 0x1;
3398 return 2;
3399 default:
3400 /* no sequence number, we cannot detect dups */
3401 return 0;
3402 }
3403}
3404
3405/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3406static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3407{
3408 struct gsm48_hdr *gh;
3409 uint8_t pdisc;
3410 uint8_t n_sd, modulo, bin;
3411
3412 gh = msgb_l3(msg);
3413 pdisc = gsm48_hdr_pdisc(gh);
3414
3415 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3416 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3417 } else { /* R99 */
3418 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3419 }
3420 if (modulo == 0)
3421 return false;
3422 bin = gsm0407_pdisc_ctr_bin(pdisc);
3423 if (bin < 0)
3424 return false;
3425
3426 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3427 if (n_sd != conn->n_sd_next[bin]) {
3428 /* not what we expected: duplicate */
3429 return true;
3430 } else {
3431 /* as expected: no dup; update expected counter for next message */
3432 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3433 return false;
3434 }
3435}
Harald Welte2483f1b2016-06-19 18:06:02 +02003436
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003437/* 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 +08003438int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003439{
Philipp Maier91f10c72017-04-20 18:40:37 +02003440 struct gsm48_hdr *gh;
3441 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003442 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003443
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003444 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003445 OSMO_ASSERT(conn);
3446 OSMO_ASSERT(msg);
3447
Philipp Maier91f10c72017-04-20 18:40:37 +02003448 gh = msgb_l3(msg);
3449 pdisc = gsm48_hdr_pdisc(gh);
3450
Harald Weltec2007852018-02-03 21:08:26 +01003451 if (gsm0407_is_duplicate(conn, msg)) {
3452 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3453 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3454 return 0;
3455 }
3456
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003457 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3458 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3459 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003460
3461 if (!msc_subscr_conn_is_accepted(conn)
3462 && !msg_is_initially_permitted(gh)) {
3463 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003464 "subscr %s: Message not permitted for initial conn: %s\n",
3465 vlr_subscr_name(conn->vsub),
3466 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003467 return -EACCES;
3468 }
3469
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003470 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3471 LOGP(DMM, LOGL_ERROR,
3472 "%s: Illegal situation: RAN type mismatch:"
3473 " attached via %s, received message via %s\n",
3474 vlr_subscr_name(conn->vsub),
3475 ran_type_name(conn->vsub->cs.attached_via_ran),
3476 ran_type_name(conn->via_ran));
3477 return -EACCES;
3478 }
3479
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003480#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003481 if (silent_call_reroute(conn, msg))
3482 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003483#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003484
Harald Welte52b1f982008-12-23 20:25:15 +00003485 switch (pdisc) {
3486 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003487 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003488 break;
3489 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003490 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003491 break;
3492 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003493 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003494 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003495 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003496 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003497 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003498 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003499 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003500 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3501 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003502 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003503 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003504 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003505 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003506 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003507 case GSM48_PDISC_TEST:
3508 rc = gsm0414_rcv_test(conn, msg);
3509 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003510 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003511 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3512 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003513 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003514 break;
3515 }
3516
3517 return rc;
3518}
Harald Welte8470bf22008-12-25 23:28:35 +00003519
Harald Welte2483f1b2016-06-19 18:06:02 +02003520/***********************************************************************
3521 * VLR integration
3522 ***********************************************************************/
3523
3524/* VLR asks us to send an authentication request */
3525static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3526 bool send_autn)
3527{
3528 struct gsm_subscriber_connection *conn = msc_conn_ref;
3529 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3530 send_autn? at->vec.autn : NULL,
3531 at->key_seq);
3532}
3533
3534/* VLR asks us to send an authentication reject */
3535static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3536{
3537 struct gsm_subscriber_connection *conn = msc_conn_ref;
3538 return gsm48_tx_mm_auth_rej(conn);
3539}
3540
3541/* VLR asks us to transmit an Identity Request of given type */
3542static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3543{
3544 struct gsm_subscriber_connection *conn = msc_conn_ref;
3545 return mm_tx_identity_req(conn, mi_type);
3546}
3547
3548/* VLR asks us to transmit a Location Update Accept */
3549static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3550{
3551 struct gsm_subscriber_connection *conn = msc_conn_ref;
3552 return gsm0408_loc_upd_acc(conn, send_tmsi);
3553}
3554
3555/* VLR asks us to transmit a Location Update Reject */
3556static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3557{
3558 struct gsm_subscriber_connection *conn = msc_conn_ref;
3559 return gsm0408_loc_upd_rej(conn, cause);
3560}
3561
3562/* VLR asks us to transmit a CM Service Accept */
3563static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3564{
3565 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003566 return msc_gsm48_tx_mm_serv_ack(conn);
3567}
3568
3569static int msc_vlr_tx_common_id(void *msc_conn_ref)
3570{
3571 struct gsm_subscriber_connection *conn = msc_conn_ref;
3572 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003573}
3574
Stefan Sperling3a741282018-03-13 21:11:49 +01003575/* VLR asks us to transmit MM info. */
3576static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3577{
3578 struct gsm_subscriber_connection *conn = msc_conn_ref;
3579 if (!conn->network->send_mm_info)
3580 return 0;
3581 return gsm48_tx_mm_info(conn);
3582}
3583
Harald Welte2483f1b2016-06-19 18:06:02 +02003584/* VLR asks us to transmit a CM Service Reject */
3585static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3586{
3587 uint8_t cause;
3588 struct gsm_subscriber_connection *conn = msc_conn_ref;
3589 conn->received_cm_service_request = false;
3590
3591 switch (result) {
3592 default:
3593 case VLR_PR_ARQ_RES_NONE:
3594 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3595 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3596 cause = GSM48_REJECT_NETWORK_FAILURE;
3597 break;
3598 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3599 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3600 break;
3601 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3602 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3603 break;
3604 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3605 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3606 break;
3607 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3608 cause = GSM48_REJECT_ILLEGAL_MS;
3609 break;
3610 case VLR_PR_ARQ_RES_TIMEOUT:
3611 cause = GSM48_REJECT_CONGESTION;
3612 break;
3613 };
3614
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003615 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003616}
3617
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003618/* For msc_vlr_set_ciph_mode() */
3619osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3620 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3621
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003622/* VLR asks us to start using ciphering.
3623 * (Keep non-static to allow regression testing on this function.) */
3624int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3625 bool umts_aka,
3626 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003627{
3628 struct gsm_subscriber_connection *conn = msc_conn_ref;
3629 struct vlr_subscr *vsub;
3630 struct gsm_auth_tuple *tuple;
3631
3632 if (!conn || !conn->vsub) {
3633 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3634 " NULL conn/subscriber");
3635 return -EINVAL;
3636 }
3637
3638 vsub = conn->vsub;
3639 tuple = vsub->last_tuple;
3640
3641 if (!tuple) {
3642 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3643 " Command: no auth tuple available\n",
3644 vlr_subscr_name(vsub));
3645 return -EINVAL;
3646 }
3647
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003648 switch (conn->via_ran) {
3649 case RAN_GERAN_A:
3650 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3651 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003652 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003653 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003654 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003655 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003656
Harald Welte7b222aa2017-12-23 19:30:32 +01003657 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003658 if (net->a5_encryption_mask & (1 << i) &&
3659 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003660 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3661 }
3662 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003663
Harald Welte71330722017-12-23 19:59:02 +01003664 if (ei.perm_algo_len == 0) {
3665 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3666 "between MSC-configured and MS-supported A5 algorithms\n",
3667 vlr_subscr_name(conn->vsub));
3668 return -ENOTSUP;
3669 }
3670
Neels Hofmeyr52821712017-12-18 01:23:42 +01003671 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3672 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3673 * necessarily a match for the UMTS AKA tokens. */
3674 if (umts_aka)
3675 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3676 else
3677 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003678 ei.key_len = sizeof(tuple->vec.kc);
3679
3680 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3681 }
3682
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003683 case RAN_UTRAN_IU:
3684#ifdef BUILD_IU
3685 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3686 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003687 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003688#else
3689 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3690 " built without Iu support\n");
3691 return -ENOTSUP;
3692#endif
3693
3694 default:
3695 break;
3696 }
3697 LOGP(DMM, LOGL_ERROR,
3698 "%s: cannot start ciphering, unknown RAN type %d\n",
3699 vlr_subscr_name(conn->vsub), conn->via_ran);
3700 return -ENOTSUP;
3701}
3702
3703void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3704{
3705 struct vlr_ciph_result vlr_res = {};
3706
3707 if (!conn || !conn->vsub) {
3708 LOGP(DMM, LOGL_ERROR,
3709 "Rx Security Mode Complete for invalid conn\n");
3710 return;
3711 }
3712
3713 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3714 vlr_subscr_name(conn->vsub));
3715
3716 vlr_res.cause = VLR_CIPH_COMPL;
3717 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003718}
3719
3720/* VLR informs us that the subscriber data has somehow been modified */
3721static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3722{
Maxa263bb22017-12-27 13:23:44 +01003723 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3724 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003725}
3726
3727/* VLR informs us that the subscriber has been associated with a conn */
3728static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3729 struct vlr_subscr *vsub)
3730{
3731 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003732 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003733 OSMO_ASSERT(!conn->vsub);
3734 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003735 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003736 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003737}
3738
3739/* operations that we need to implement for libvlr */
3740static const struct vlr_ops msc_vlr_ops = {
3741 .tx_auth_req = msc_vlr_tx_auth_req,
3742 .tx_auth_rej = msc_vlr_tx_auth_rej,
3743 .tx_id_req = msc_vlr_tx_id_req,
3744 .tx_lu_acc = msc_vlr_tx_lu_acc,
3745 .tx_lu_rej = msc_vlr_tx_lu_rej,
3746 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3747 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3748 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003749 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003750 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003751 .subscr_update = msc_vlr_subscr_update,
3752 .subscr_assoc = msc_vlr_subscr_assoc,
3753};
3754
3755/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3756int msc_vlr_alloc(struct gsm_network *net)
3757{
3758 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3759 if (!net->vlr)
3760 return -ENOMEM;
3761 net->vlr->user_ctx = net;
3762 return 0;
3763}
3764
3765/* Launch the VLR, i.e. its GSUP connection */
3766int msc_vlr_start(struct gsm_network *net)
3767{
3768 OSMO_ASSERT(net->vlr);
3769 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3770 net->gsup_server_port);
3771}