blob: 76f8c799ac19e6a85229d6dff373251dc583b144 [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>
49#include <osmocom/msc/bsc_api.h>
50#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020051#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020052#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010053#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020054#include <osmocom/msc/vlr.h>
55#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080056
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010057#include <osmocom/gsm/gsm48.h>
58#include <osmocom/gsm/gsm0480.h>
59#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020060#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010061#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010063#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010064#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010065#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020067#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020068#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020069#endif
Harald Welte52b1f982008-12-23 20:25:15 +000070
Neels Hofmeyr90843962017-09-04 15:04:35 +020071#include <osmocom/msc/msc_ifaces.h>
72#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010073#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020074
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020075#include <assert.h>
76
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020077
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020078void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010079void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020080
Harald Welte2483f1b2016-06-19 18:06:02 +020081static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
82 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080083static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020084 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000085
Harald Welte52b1f982008-12-23 20:25:15 +000086struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020087 uint16_t mcc;
88 uint16_t mnc;
89 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000090};
91
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020092static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080093
Harald Welte71330722017-12-23 19:59:02 +010094/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
95static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
96{
97 switch (a5) {
98 case 0:
99 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
100 return true;
101 case 1:
102 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
103 * "The network shall not provide service to an MS which indicates that it does not
104 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
105 * on policy here */
106 /* See 3GPP TS 24.008 10.5.1.7 */
107 if (!cm->classmark1_set) {
108 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
109 return true;
110 } else {
111 if (cm->classmark1.a5_1)
112 return false; /* Inverted logic for this bit! */
113 else
114 return true;
115 }
116 break;
117 case 2:
118 case 3:
119 /* See 3GPP TS 24.008 10.5.1.6 */
120 if (cm->classmark2_len < 3) {
121 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
122 return false;
123 } else {
124 if (cm->classmark2[2] & (1 << (a5-2)))
125 return true;
126 else
127 return false;
128 }
129 break;
130 case 4:
131 case 5:
132 case 6:
133 case 7:
134 /* See 3GPP TS 24.008 10.5.1.7 */
135 if (cm->classmark3_len < 1) {
136 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
137 return false;
138 } else {
139 if (cm->classmark3[0] & (1 << (a5-4)))
140 return true;
141 else
142 return false;
143 }
144 break;
145 default:
146 return false;
147 }
148}
149
Harald Welte31c00f72011-03-03 23:29:05 +0100150void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
151{
152 net->mncc_recv(net, msg);
153}
154
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800155static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
156 struct gsm_trans *trans)
157{
158 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
159
160 /* if we get passed a transaction reference, do some common
161 * work that the caller no longer has to do */
162 if (trans) {
163 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800164 }
165
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200166 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800167}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100168
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800169int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
170{
171 struct gsm48_hdr *gh;
172 struct msgb *ss_notify;
173
174 ss_notify = gsm0480_create_notifySS(message);
175 if (!ss_notify)
176 return -1;
177
178 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
179 uint8_t *data = msgb_push(ss_notify, 1);
180 data[0] = ss_notify->len - 1;
181 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
182 gh->msg_type = GSM48_MT_CC_FACILITY;
183 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
184}
185
Harald Welte2483f1b2016-06-19 18:06:02 +0200186/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100187void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
188{
189 struct gsm_trans *trans, *temp;
190
191 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
192
193 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100194 if (trans->protocol == protocol) {
195 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100196 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100197 }
Harald Welte371efe52010-12-22 23:17:50 +0100198 }
199}
200
Holger Freyther429e7762008-12-30 13:28:30 +0000201/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200202static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000203{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800204 struct msgb *msg;
205
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800206 msg = gsm48_create_loc_upd_rej(cause);
207 if (!msg) {
208 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
209 return -1;
210 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200211
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200212 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
213 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100214
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800215 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000216}
217
218/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200219static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
220 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000221{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100222 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000223 struct gsm48_hdr *gh;
224 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200225 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200226
Harald Welte52b1f982008-12-23 20:25:15 +0000227 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
228 gh->proto_discr = GSM48_PDISC_MM;
229 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
230
231 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200232 gsm48_generate_lai(lai, conn->network->country_code,
233 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200234 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000235
Harald Welte2483f1b2016-06-19 18:06:02 +0200236 if (send_tmsi == GSM_RESERVED_TMSI) {
237 /* we did not allocate a TMSI to the MS, so we need to
238 * include the IMSI in order for the MS to delete any
239 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200240 uint8_t mi[10];
241 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200243 mid = msgb_put(msg, len);
244 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200245 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
246 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200247 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200248 /* Include the TMSI, which means that the MS will send a
249 * TMSI REALLOCATION COMPLETE, and we should wait for
250 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200251 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200252 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
254 vlr_subscr_name(conn->vsub),
255 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200256 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200257 /* TODO: Follow-on proceed */
258 /* TODO: CTS permission */
259 /* TODO: Equivalent PLMNs */
260 /* TODO: Emergency Number List */
261 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000262
Harald Welte52b1f982008-12-23 20:25:15 +0000263
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800264 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000265}
266
Harald Weltebf5e8df2009-02-03 12:59:45 +0000267/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200268static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000269{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100270 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000271 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000272
Harald Welte231ad4f2008-12-27 11:15:38 +0000273 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
274 gh->proto_discr = GSM48_PDISC_MM;
275 gh->msg_type = GSM48_MT_MM_ID_REQ;
276 gh->data[0] = id_type;
277
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800278 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000279}
280
Harald Weltebf5e8df2009-02-03 12:59:45 +0000281/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800282static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000283{
284 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200285 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200286 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000287
Harald Welte2483f1b2016-06-19 18:06:02 +0200288 if (!conn->vsub) {
289 LOGP(DMM, LOGL_ERROR,
290 "Rx MM Identity Response: invalid: no subscriber\n");
291 return -EINVAL;
292 }
293
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200294 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200295 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
296 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000297
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200298 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100299
Harald Welte2483f1b2016-06-19 18:06:02 +0200300 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000301}
302
Harald Welte2483f1b2016-06-19 18:06:02 +0200303/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200304static const struct value_string lupd_names[] = {
305 { GSM48_LUPD_NORMAL, "NORMAL" },
306 { GSM48_LUPD_PERIODIC, "PERIODIC" },
307 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
308 { 0, NULL }
309};
Harald Welte2a139372009-02-22 21:14:55 +0000310
Harald Welte2483f1b2016-06-19 18:06:02 +0200311/* Chapter 9.2.15: Receive Location Updating Request.
312 * Keep this function non-static for direct invocation by unit tests. */
313int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000314{
Harald Welte2483f1b2016-06-19 18:06:02 +0200315 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
316 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000317 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000318 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200319 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200320 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 uint32_t tmsi;
323 char *imsi;
324 struct osmo_location_area_id old_lai, new_lai;
325 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200326 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200327 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000328
Harald Welte8470bf22008-12-25 23:28:35 +0000329 lu = (struct gsm48_loc_upd_req *) gh->data;
330
331 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000332
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200333 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000334
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 rc = msc_create_conn_fsm(conn, mi_string);
336 if (rc)
337 /* logging already happened in msc_create_conn_fsm() */
338 return rc;
339
340 conn->classmark.classmark1 = lu->classmark1;
341 conn->classmark.classmark1_set = true;
342
343 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
344 gsm48_mi_type_name(mi_type), mi_string,
345 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000346
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200347 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100348
Harald Welte24ff6ee2009-12-22 00:41:05 +0100349 switch (lu->type) {
350 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200351 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200352 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100353 break;
354 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200355 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200356 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100357 break;
358 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200359 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200360 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100361 break;
362 }
363
Harald Welte2483f1b2016-06-19 18:06:02 +0200364 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
365 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
366 /* TODO: 10.5.7.8 Device properties */
367 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200368
Harald Welte52b1f982008-12-23 20:25:15 +0000369 switch (mi_type) {
370 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200371 tmsi = GSM_RESERVED_TMSI;
372 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000373 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000374 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200375 tmsi = tmsi_from_string(mi_string);
376 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000377 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200378 default:
Harald Weltea0368542009-06-27 02:58:43 +0200379 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200380 tmsi = GSM_RESERVED_TMSI;
381 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000382 break;
383 }
384
Harald Welte2483f1b2016-06-19 18:06:02 +0200385 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
386 &old_lai.plmn.mnc, &old_lai.lac);
387 new_lai.plmn.mcc = conn->network->country_code;
388 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200389 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200390 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200391
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200392 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200393 lu_fsm = vlr_loc_update(conn->conn_fsm,
394 SUBSCR_CONN_E_ACCEPTED,
395 SUBSCR_CONN_E_CN_CLOSE,
396 (void*)&conn_from_lu,
397 net->vlr, conn, vlr_lu_type, tmsi, imsi,
398 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200399 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100400 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200401 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200402 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200403 net->vlr->cfg.assign_tmsi);
404 if (!lu_fsm) {
405 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
406 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800407 }
408
Harald Welte2483f1b2016-06-19 18:06:02 +0200409 /* From vlr_loc_update() we expect an implicit dispatch of
410 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
411 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000412
Harald Welte2483f1b2016-06-19 18:06:02 +0200413 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
414 LOGP(DRR, LOGL_ERROR,
415 "%s: internal error during Location Updating attempt\n",
416 mi_string);
417 return -EIO;
418 }
419
420 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000421}
422
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100423/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200424/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100425static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800426{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100427 uint8_t ret;
428
429 ret = value / 10;
430 ret |= (value % 10) << 4;
431
432 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800433}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100434
Harald Welte4bfdfe72009-06-10 23:11:52 +0800435
Harald Weltedb253af2008-12-30 17:56:55 +0000436/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800437int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000438{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100439 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000440 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200441 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200443 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100444
Harald Welte4bfdfe72009-06-10 23:11:52 +0800445 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100446 struct tm* gmt_time;
447 struct tm* local_time;
448 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100449 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000450
Harald Weltedb253af2008-12-30 17:56:55 +0000451 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
452 gh->proto_discr = GSM48_PDISC_MM;
453 gh->msg_type = GSM48_MT_MM_INFO;
454
455 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200456#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000457 name_len = strlen(net->name_long);
458 /* 10.5.3.5a */
459 ptr8 = msgb_put(msg, 3);
460 ptr8[0] = GSM48_IE_NAME_LONG;
461 ptr8[1] = name_len*2 +1;
462 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
463
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200464 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000465 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000466 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000467
468 /* FIXME: Use Cell Broadcast, not UCS-2, since
469 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200470#endif
471 name_len = (strlen(net->name_long)*7)/8;
472 name_pad = (8 - strlen(net->name_long)*7)%8;
473 if (name_pad > 0)
474 name_len++;
475 /* 10.5.3.5a */
476 ptr8 = msgb_put(msg, 3);
477 ptr8[0] = GSM48_IE_NAME_LONG;
478 ptr8[1] = name_len +1;
479 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
480
481 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100482 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200483
Harald Weltedb253af2008-12-30 17:56:55 +0000484 }
485
486 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200487#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000488 name_len = strlen(net->name_short);
489 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200490 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200491 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000492 ptr8[1] = name_len*2 + 1;
493 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
494
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200495 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000496 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000497 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200498#endif
499 name_len = (strlen(net->name_short)*7)/8;
500 name_pad = (8 - strlen(net->name_short)*7)%8;
501 if (name_pad > 0)
502 name_len++;
503 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200504 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200505 ptr8[0] = GSM48_IE_NAME_SHORT;
506 ptr8[1] = name_len +1;
507 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
508
509 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100510 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200511
Harald Weltedb253af2008-12-30 17:56:55 +0000512 }
513
Harald Weltedb253af2008-12-30 17:56:55 +0000514 /* Section 10.5.3.9 */
515 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100516 gmt_time = gmtime(&cur_t);
517
Harald Weltedb253af2008-12-30 17:56:55 +0000518 ptr8 = msgb_put(msg, 8);
519 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100520 ptr8[1] = bcdify(gmt_time->tm_year % 100);
521 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
522 ptr8[3] = bcdify(gmt_time->tm_mday);
523 ptr8[4] = bcdify(gmt_time->tm_hour);
524 ptr8[5] = bcdify(gmt_time->tm_min);
525 ptr8[6] = bcdify(gmt_time->tm_sec);
526
Neels Hofmeyr73983952016-05-10 13:29:33 +0200527 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200528 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200529 if (net->tz.hr < 0) {
530 tzunits = ((net->tz.hr/-1)*4);
531 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100532 ptr8[7] = bcdify(tzunits);
533 /* Set negative time */
534 ptr8[7] |= 0x08;
535 }
536 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200537 tzunits = net->tz.hr*4;
538 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100539 ptr8[7] = bcdify(tzunits);
540 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100541 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200542 if (net->tz.dst >= 0 && net->tz.dst <= 2)
543 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100544 }
545 else {
546 /* Need to get GSM offset and convert into 15 min units */
547 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200548#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200549 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100550 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200551#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200552 /* find timezone offset */
553 time_t utc;
554 double offsetFromUTC;
555 utc = mktime(gmt_time);
556 local_time = localtime(&cur_t);
557 offsetFromUTC = difftime(cur_t, utc);
558 if (local_time->tm_isdst)
559 offsetFromUTC += 3600.0;
560 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200561#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100562 if (tzunits < 0) {
563 tzunits = tzunits/-1;
564 ptr8[7] = bcdify(tzunits);
565 /* Flip it to negative */
566 ptr8[7] |= 0x08;
567 }
568 else
569 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100570
571 /* Does not support DST +2 */
572 if (local_time->tm_isdst)
573 dst = 1;
574 }
575
576 if (dst) {
577 ptr8 = msgb_put(msg, 3);
578 ptr8[0] = GSM48_IE_NET_DST;
579 ptr8[1] = 1;
580 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100581 }
Harald Weltedb253af2008-12-30 17:56:55 +0000582
Daniel Willmanneea93372009-08-13 03:42:07 +0200583 DEBUGP(DMM, "-> MM INFO\n");
584
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800585 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000586}
587
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100588/*! Send an Authentication Request to MS on the given subscriber connection
589 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
590 * \param[in] conn Subscriber connection to send on.
591 * \param[in] rand Random challenge token to send, must be 16 bytes long.
592 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
593 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
594 * \param[in] key_seq auth tuple's sequence number.
595 */
596int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
597 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200598{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100599 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200600 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200601 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200602
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100603 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100604 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100605 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200606
Harald Welte7984d5c2009-08-12 22:56:50 +0200607 gh->proto_discr = GSM48_PDISC_MM;
608 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
609
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100610 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200611
Harald Welte7984d5c2009-08-12 22:56:50 +0200612 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100613 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200614 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200615 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200616
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100617
618 /* 16 bytes AUTN */
619 if (autn)
620 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
621
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800622 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200623}
624
625/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800626int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200627{
628 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800629 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200630}
631
Harald Welte2483f1b2016-06-19 18:06:02 +0200632static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
633static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
634
635static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100636{
Harald Welte2483f1b2016-06-19 18:06:02 +0200637 uint8_t mi_type;
638 char mi_string[GSM48_MI_SIZE];
639 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100640
Harald Welte2483f1b2016-06-19 18:06:02 +0200641 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
642 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100643
Harald Welte2483f1b2016-06-19 18:06:02 +0200644 switch (mi_type) {
645 case GSM_MI_TYPE_IMSI:
646 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
647 goto accept_reuse;
648 break;
649 case GSM_MI_TYPE_TMSI:
650 tmsi = osmo_load32be(mi_lv+2);
651 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
652 goto accept_reuse;
653 break;
654 case GSM_MI_TYPE_IMEI:
655 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
656 goto accept_reuse;
657 break;
658 default:
659 break;
660 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100661
Harald Welte2483f1b2016-06-19 18:06:02 +0200662 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
663 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
664 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
665 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100666
Harald Welte2483f1b2016-06-19 18:06:02 +0200667accept_reuse:
668 DEBUGP(DMM, "%s: re-using already accepted connection\n",
669 vlr_subscr_name(conn->vsub));
670 conn->received_cm_service_request = true;
671 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100672}
673
Harald Welte4ed0e922009-01-10 03:17:30 +0000674/*
675 * Handle CM Service Requests
676 * a) Verify that the packet is long enough to contain the information
677 * we require otherwsie reject with INCORRECT_MESSAGE
678 * b) Try to parse the TMSI. If we do not have one reject
679 * c) Check that we know the subscriber with the TMSI otherwise reject
680 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200681 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200682 *
683 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000684 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200685int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000686{
Harald Welte2483f1b2016-06-19 18:06:02 +0200687 static const enum subscr_conn_from conn_from_cm_service_req =
688 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
689 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200690 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200691 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000692
Harald Welteba4cf162009-01-10 01:49:35 +0000693 struct gsm48_hdr *gh = msgb_l3(msg);
694 struct gsm48_service_request *req =
695 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800696 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200697 uint8_t classmark2_len = gh->data[1];
698 uint8_t *classmark2 = gh->data+2;
699 uint8_t mi_len = *(classmark2 + classmark2_len);
700 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200701 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200702 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200703 int rc;
704
705 lai.plmn.mcc = conn->network->country_code;
706 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200707 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000708
Harald Weltec9e02182009-05-01 19:07:53 +0000709 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000710 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000711 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200712 return msc_gsm48_tx_mm_serv_rej(conn,
713 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000714 }
715
716 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000717 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200718 return msc_gsm48_tx_mm_serv_rej(conn,
719 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000720 }
721
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200722 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000723 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100724 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
725 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200726
Harald Welte3995d2e2018-01-24 11:23:54 +0100727 switch (mi_type) {
728 case GSM_MI_TYPE_IMSI:
729 case GSM_MI_TYPE_TMSI:
730 /* continue below */
731 break;
732 case GSM_MI_TYPE_IMEI:
733 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
734 /* We don't do emergency calls by IMEI */
735 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
736 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
737 }
738 /* fall-through for non-emergency setup */
739 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200740 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200741 return msc_gsm48_tx_mm_serv_rej(conn,
742 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000743 }
744
Harald Welte37382ec2018-01-24 12:49:34 +0100745 switch (req->cm_service_type) {
746 case GSM48_CMSERV_MO_CALL_PACKET:
747 case GSM48_CMSERV_EMERGENCY:
748 case GSM48_CMSERV_SMS:
749 case GSM48_CMSERV_SUP_SERV:
750 /* continue below */
751 break;
752 default:
753 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
754 }
755
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200756 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200757 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
758 conn->classmark.classmark2_len = classmark2_len;
759
760 if (conn->conn_fsm) {
761 if (msc_subscr_conn_is_accepted(conn))
762 return cm_serv_reuse_conn(conn, mi-1);
763 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
764 vlr_subscr_name(conn->vsub));
765 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
766 return -EINVAL;
767 }
768
769 rc = msc_create_conn_fsm(conn, mi_string);
770 if (rc) {
771 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
772 /* logging already happened in msc_create_conn_fsm() */
773 return rc;
774 }
Harald Welte7659de12009-12-13 12:39:18 +0100775
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200776 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200777 vlr_proc_acc_req(conn->conn_fsm,
778 SUBSCR_CONN_E_ACCEPTED,
779 SUBSCR_CONN_E_CN_CLOSE,
780 (void*)&conn_from_cm_service_req,
781 net->vlr, conn,
782 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200783 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100784 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200785 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200786 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000787
Harald Welte2483f1b2016-06-19 18:06:02 +0200788 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000789}
790
Harald Welte49f8fcb2018-01-24 21:40:19 +0100791/* Receive a CM Re-establish Request */
792static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
793{
794 uint8_t mi_type;
795 char mi_string[GSM48_MI_SIZE];
796 struct gsm48_hdr *gh = msgb_l3(msg);
797
798 uint8_t classmark2_len = gh->data[1];
799 uint8_t *classmark2 = gh->data+2;
800 uint8_t mi_len = *(classmark2 + classmark2_len);
801 uint8_t *mi = (classmark2 + classmark2_len + 1);
802
803 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
804 mi_type = mi[0] & GSM_MI_TYPE_MASK;
805 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
806
807 /* we don't support CM call re-establishment */
808 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
809}
810
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200811static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000812{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200813 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000814 struct gsm48_hdr *gh = msgb_l3(msg);
815 struct gsm48_imsi_detach_ind *idi =
816 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200817 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200818 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200819 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000820
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200821 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200822 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
823 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000824
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200825 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100826
Harald Welte2a139372009-02-22 21:14:55 +0000827 switch (mi_type) {
828 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200829 vsub = vlr_subscr_find_by_tmsi(network->vlr,
830 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000831 break;
832 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200833 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000834 break;
835 case GSM_MI_TYPE_IMEI:
836 case GSM_MI_TYPE_IMEISV:
837 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200838 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
839 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000840 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200841 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200842 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
843 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000844 break;
845 }
846
Harald Welte2483f1b2016-06-19 18:06:02 +0200847 /* TODO? We used to remember the subscriber's classmark1 here and
848 * stored it in the old sqlite db, but now we store it in a conn that
849 * will be discarded anyway: */
850 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200851
Harald Welte2483f1b2016-06-19 18:06:02 +0200852 if (!vsub) {
853 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
854 gsm48_mi_type_name(mi_type), mi_string);
855 } else {
856 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100857
858 if (vsub->cs.is_paging)
859 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
860
Harald Welte2483f1b2016-06-19 18:06:02 +0200861 vlr_subscr_rx_imsi_detach(vsub);
862 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
863 vlr_subscr_put(vsub);
864 }
Harald Welte2a139372009-02-22 21:14:55 +0000865
Harald Welte2483f1b2016-06-19 18:06:02 +0200866 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000867 return 0;
868}
869
Harald Welted2a7f5a2009-06-05 20:08:20 +0000870static int gsm48_rx_mm_status(struct msgb *msg)
871{
872 struct gsm48_hdr *gh = msgb_l3(msg);
873
874 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
875
876 return 0;
877}
878
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100879static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
880 struct gsm_subscriber_connection *conn,
881 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100885
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100886 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
887 LOGP(DMM, LOGL_ERROR,
888 "%s: MM AUTHENTICATION RESPONSE:"
889 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200890 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100891 return -EINVAL;
892 }
893
894 *res_len = sizeof(ar->sres);
895 memcpy(res, ar->sres, sizeof(ar->sres));
896 return 0;
897}
898
899static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
900 struct gsm_subscriber_connection *conn,
901 struct msgb *msg)
902{
903 struct gsm48_hdr *gh;
904 uint8_t *data;
905 uint8_t iei;
906 uint8_t ie_len;
907 unsigned int data_len;
908
909 /* First parse the GSM part */
910 if (parse_gsm_auth_resp(res, res_len, conn, msg))
911 return -EINVAL;
912 OSMO_ASSERT(*res_len == 4);
913
914 /* Then add the extended res part */
915 gh = msgb_l3(msg);
916 data = gh->data + sizeof(struct gsm48_auth_resp);
917 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
918
919 if (data_len < 3) {
920 LOGP(DMM, LOGL_ERROR,
921 "%s: MM AUTHENTICATION RESPONSE:"
922 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200923 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100924 return -EINVAL;
925 }
926
927 iei = data[0];
928 ie_len = data[1];
929 if (iei != GSM48_IE_AUTH_RES_EXT) {
930 LOGP(DMM, LOGL_ERROR,
931 "%s: MM R99 AUTHENTICATION RESPONSE:"
932 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200933 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100934 GSM48_IE_AUTH_RES_EXT, iei);
935 return -EINVAL;
936 }
937
938 if (ie_len > 12) {
939 LOGP(DMM, LOGL_ERROR,
940 "%s: MM R99 AUTHENTICATION RESPONSE:"
941 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200942 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100943 return -EINVAL;
944 }
945
946 *res_len += ie_len;
947 memcpy(res + 4, &data[2], ie_len);
948 return 0;
949}
950
951/* Chapter 9.2.3: Authentication Response */
952static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
953{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 uint8_t res[16];
955 uint8_t res_len;
956 int rc;
957 bool is_r99;
958
Harald Welte2483f1b2016-06-19 18:06:02 +0200959 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100960 LOGP(DMM, LOGL_ERROR,
961 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200962 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 return -EINVAL;
964 }
965
966 if (msgb_l3len(msg) >
967 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
968 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
969 is_r99 = true;
970 } else {
971 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
972 is_r99 = false;
973 }
974
975 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200976 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100977 return -EINVAL;
978 }
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 Hofmeyr22a735b2017-02-03 01:22:40 +0100982 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
983 osmo_hexdump_nospc(res, res_len));
984
Harald Welte2483f1b2016-06-19 18:06:02 +0200985 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
986 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
1156 lai.plmn.mcc = conn->network->country_code;
1157 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001158 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001159
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001160 resp = (struct gsm48_pag_resp *) &gh->data[0];
1161 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1162 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001163 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1164 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001165
Harald Welte2483f1b2016-06-19 18:06:02 +02001166 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1167 if (!mi_lv) {
1168 /* FIXME */
1169 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001170 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001171
Harald Welte2483f1b2016-06-19 18:06:02 +02001172 rc = msc_create_conn_fsm(conn, mi_string);
1173 if (rc)
1174 /* logging already happened in msc_create_conn_fsm() */
1175 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001176
Harald Welte2483f1b2016-06-19 18:06:02 +02001177 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1178 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001179
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001180 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001181 vlr_proc_acc_req(conn->conn_fsm,
1182 SUBSCR_CONN_E_ACCEPTED,
1183 SUBSCR_CONN_E_CN_CLOSE,
1184 (void*)&conn_from_paging_resp,
1185 net->vlr, conn,
1186 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001187 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001188 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001189 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001190 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001191
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001192 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001193}
1194
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001195static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001196{
1197 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001198 uint8_t apdu_id_flags;
1199 uint8_t apdu_len;
1200 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001201
1202 apdu_id_flags = gh->data[0];
1203 apdu_len = gh->data[1];
1204 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001205
Max8dc8f232017-02-09 19:13:02 +01001206 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001207 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001208
Harald Welte2483f1b2016-06-19 18:06:02 +02001209 /* we're not using the app info blob anywhere, so ignore. */
1210#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001211 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001212#else
1213 return 0;
1214#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001215}
1216
Harald Weltebf5e8df2009-02-03 12:59:45 +00001217/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001218static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001219{
1220 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001221 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001222
1223 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001224 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001225 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001226 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001228 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001229 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001230 default:
Philippd37981e2016-10-10 15:11:41 +02001231 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1232 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001233 break;
1234 }
1235
Harald Welte2d35ae62009-02-06 12:02:13 +00001236 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001237}
1238
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001239int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1240 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001241{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001242 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243 struct gsm48_hdr *gh;
1244
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1246 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001247
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1249 gh->proto_discr = GSM48_PDISC_RR;
1250 gh->msg_type = GSM48_MT_RR_APP_INFO;
1251 gh->data[0] = apdu_id;
1252 gh->data[1] = apdu_len;
1253 memcpy(gh->data+2, apdu, apdu_len);
1254
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001255 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001256}
1257
Alexander Couzens92f552f2016-08-23 07:32:27 +02001258/* FIXME: this count_statistics is a state machine behaviour. we should convert
1259 * the complete call control into a state machine. Afterwards we can move this
1260 * code into state transitions.
1261 */
1262static void count_statistics(struct gsm_trans *trans, int new_state)
1263{
1264 int old_state = trans->cc.state;
1265 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1266
1267 if (old_state == new_state)
1268 return;
1269
1270 /* state incoming */
1271 switch (new_state) {
1272 case GSM_CSTATE_ACTIVE:
1273 osmo_counter_inc(trans->net->active_calls);
1274 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1275 break;
1276 }
1277
1278 /* state outgoing */
1279 switch (old_state) {
1280 case GSM_CSTATE_ACTIVE:
1281 osmo_counter_dec(trans->net->active_calls);
1282 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1283 new_state == GSM_CSTATE_DISCONNECT_IND)
1284 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1285 else
1286 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1287 break;
1288 }
1289}
1290
Harald Welte4bc90a12008-12-27 16:32:52 +00001291/* Call Control */
1292
Harald Welte7584aea2009-02-11 11:44:12 +00001293/* The entire call control code is written in accordance with Figure 7.10c
1294 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1295 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1296 * it for voice */
1297
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298static void new_cc_state(struct gsm_trans *trans, int state)
1299{
1300 if (state > 31 || state < 0)
1301 return;
1302
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001303 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1304 trans->transaction_id,
1305 vlr_subscr_name(trans->vsub),
1306 gsm48_cc_state_name(trans->cc.state),
1307 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308
Alexander Couzens92f552f2016-08-23 07:32:27 +02001309 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001310 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311}
1312
1313static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001314{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001315 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001316 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001317 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001318
Harald Welte4bc90a12008-12-27 16:32:52 +00001319 gh->msg_type = GSM48_MT_CC_STATUS;
1320
1321 cause = msgb_put(msg, 3);
1322 cause[0] = 2;
1323 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1324 cause[2] = 0x80 | 30; /* response to status inquiry */
1325
1326 call_state = msgb_put(msg, 1);
1327 call_state[0] = 0xc0 | 0x00;
1328
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001329 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001330}
1331
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001332static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001333 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001334{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001335 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001336 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1337
Harald Welte6f4b7532008-12-29 00:39:37 +00001338 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001339 gh->msg_type = msg_type;
1340
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001341 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001342}
1343
Harald Welte4bfdfe72009-06-10 23:11:52 +08001344static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1345{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001346 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001347 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001348 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001349 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 }
1351}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001352
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1354 int msg_type, struct gsm_mncc *mncc)
1355{
1356 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001357 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358
Philipp Maierfbf66102017-04-09 12:32:51 +02001359 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1360
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001361#if BEFORE_MSCSPLIT
1362 /* Re-enable this log output once we can obtain this information via
1363 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001365 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001366 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001368 trans->conn->lchan->ts->trx->bts->nr,
1369 trans->conn->lchan->ts->trx->nr,
1370 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001371 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 get_mncc_name(msg_type));
1373 else
1374 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1375 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001376 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 get_mncc_name(msg_type));
1378 else
1379 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1380 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001381#else
1382 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1383#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001384
1385 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001386
Harald Welte966636f2009-06-26 19:39:35 +02001387 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 if (!msg)
1389 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001390
1391 data = msgb_put(msg, sizeof(struct gsm_mncc));
1392 memcpy(data, mncc, sizeof(struct gsm_mncc));
1393
Harald Welte54209c22010-12-22 23:43:29 +01001394 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395
1396 return 0;
1397}
1398
1399int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001400 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401{
1402 struct gsm_mncc rel;
1403
Harald Welte92f70c52009-06-12 01:54:08 +08001404 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001406 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001407 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1408 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001409 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1410}
1411
Harald Weltedcaf5652009-07-23 18:56:43 +02001412/* Call Control Specific transaction release.
1413 * gets called by trans_free, DO NOT CALL YOURSELF! */
1414void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 gsm48_stop_cc_timer(trans);
1417
Philipp Maier621ba032017-11-07 17:19:25 +01001418 /* Initiate the teardown of the related connections on the MGW */
1419 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001420
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 /* send release to L4, if callref still exists */
1422 if (trans->callref) {
1423 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001424 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001425 GSM48_CAUSE_LOC_PRN_S_LU,
1426 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001428 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001429 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001430}
1431
1432static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001433
Harald Welte09e38af2009-02-16 22:52:23 +00001434/* call-back from paging the B-end of the connection */
1435static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001436 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001437{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001438 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001439 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001440
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001441 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001442
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001443 switch (event) {
1444 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001445 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1446 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001447 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001448 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001449 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001450 /* send SETUP request to called party */
1451 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1452 break;
1453 case GSM_PAGING_EXPIRED:
1454 case GSM_PAGING_BUSY:
1455 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001456 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001457 /* Temporarily out of order */
1458 mncc_release_ind(transt->net, transt,
1459 transt->callref,
1460 GSM48_CAUSE_LOC_PRN_S_LU,
1461 GSM48_CC_CAUSE_DEST_OOO);
1462 transt->callref = 0;
1463 transt->paging_request = NULL;
1464 trans_free(transt);
1465 break;
1466 default:
1467 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1468 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001469 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001470
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001471 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001472 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001473}
Harald Welte7584aea2009-02-11 11:44:12 +00001474
Harald Welte4bfdfe72009-06-10 23:11:52 +08001475/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001476static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001477{
Harald Welte53d51f52015-12-03 14:59:04 +01001478 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1479 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001480 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001481
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001483 return -EIO;
1484
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001485 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 return -EIO;
1487
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001488 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001489 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001490
Philipp Maier621ba032017-11-07 17:19:25 +01001491 /* Bridge RTP streams */
1492 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1493 trans2->conn->rtp.local_addr_cn);
1494 if (rc)
1495 return -EINVAL;
1496
1497 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1498 trans1->conn->rtp.local_addr_cn);
1499 if (rc)
1500 return -EINVAL;
1501
1502 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503}
1504
Harald Welte4bfdfe72009-06-10 23:11:52 +08001505static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1506{
1507 DEBUGP(DCC, "-> STATUS ENQ\n");
1508 return gsm48_cc_tx_status(trans, msg);
1509}
1510
1511static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1512static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1513
1514static void gsm48_cc_timeout(void *arg)
1515{
1516 struct gsm_trans *trans = arg;
1517 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001518 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1519 int mo_location = GSM48_CAUSE_LOC_USER;
1520 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1521 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522 struct gsm_mncc mo_rel, l4_rel;
1523
1524 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1525 mo_rel.callref = trans->callref;
1526 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1527 l4_rel.callref = trans->callref;
1528
Harald Weltedcaf5652009-07-23 18:56:43 +02001529 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530 case 0x303:
1531 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001532 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001533 break;
1534 case 0x310:
1535 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001536 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001537 break;
1538 case 0x313:
1539 disconnect = 1;
1540 /* unknown, did not find it in the specs */
1541 break;
1542 case 0x301:
1543 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001544 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001545 break;
1546 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001547 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001549 gsm48_cc_tx_release(trans, &trans->cc.msg);
1550 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551 break; /* stay in release state */
1552 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001553 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 return;
1555// release = 1;
1556// l4_cause = 14;
1557// break;
1558 case 0x306:
1559 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001560 mo_cause = trans->cc.msg.cause.value;
1561 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001562 break;
1563 case 0x323:
1564 disconnect = 1;
1565 break;
1566 default:
1567 release = 1;
1568 }
1569
1570 if (release && trans->callref) {
1571 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001572 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 l4_location, l4_cause);
1574 trans->callref = 0;
1575 }
1576
1577 if (disconnect && trans->callref) {
1578 /* process disconnect towards layer 4 */
1579 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001580 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581 }
1582
1583 /* process disconnect towards mobile station */
1584 if (disconnect || release) {
1585 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001586 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1587 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1588 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 mo_rel.cause.diag_len = 3;
1590
1591 if (disconnect)
1592 gsm48_cc_tx_disconnect(trans, &mo_rel);
1593 if (release)
1594 gsm48_cc_tx_release(trans, &mo_rel);
1595 }
1596
1597}
1598
Max3ffce192016-04-25 15:22:00 +02001599/* disconnect both calls from the bridge */
1600static inline void disconnect_bridge(struct gsm_network *net,
1601 struct gsm_mncc_bridge *bridge, int err)
1602{
1603 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1604 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1605 struct gsm_mncc mx_rel;
1606 if (!trans0 || !trans1)
1607 return;
1608
1609 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1610 trans0->callref, trans1->callref, strerror(err));
1611
1612 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1613 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1614 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1615
1616 mx_rel.callref = trans0->callref;
1617 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1618
1619 mx_rel.callref = trans1->callref;
1620 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1621}
1622
Harald Welte4bfdfe72009-06-10 23:11:52 +08001623static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1624 int sec, int micro)
1625{
1626 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001627 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001628 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001629 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630}
1631
1632static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1633{
1634 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001635 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1637 struct tlv_parsed tp;
1638 struct gsm_mncc setup;
1639
1640 memset(&setup, 0, sizeof(struct gsm_mncc));
1641 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001642
Harald Welte474d19f2010-03-02 23:18:30 +01001643 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001645 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1646 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647 setup.emergency = 1;
Harald Welte0dd01372018-01-24 11:06:19 +01001648 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649
1650 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001651 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001652 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1653 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001654
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 /* bearer capability */
1656 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1657 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001658 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001660
1661 /* Create a copy of the bearer capability
1662 * in the transaction struct, so we can use
1663 * this information later */
1664 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1665 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 }
1667 /* facility */
1668 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1669 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001670 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1672 }
1673 /* called party bcd number */
1674 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1675 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001676 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1678 }
1679 /* user-user */
1680 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1681 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001682 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1684 }
1685 /* ss-version */
1686 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1687 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001688 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1690 }
1691 /* CLIR suppression */
1692 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1693 setup.clir.sup = 1;
1694 /* CLIR invocation */
1695 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1696 setup.clir.inv = 1;
1697 /* cc cap */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1699 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001700 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1702 }
1703
Harald Welte4bfdfe72009-06-10 23:11:52 +08001704 new_cc_state(trans, GSM_CSTATE_INITIATED);
1705
Harald Welte79826e12018-01-24 10:50:24 +01001706 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1707 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001708 setup.called.number);
1709
Alexander Couzensb847a212016-08-02 11:34:11 +02001710 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001711
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001713 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714
Harald Welte13cac662009-07-29 12:10:35 +02001715 /* MNCC code will modify the channel asynchronously, we should
1716 * ipaccess-bind only after the modification has been made to the
1717 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 return 0;
1719}
1720
1721static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001722{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001723 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001724 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001726 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001727
Harald Welte7ccf7782009-02-17 01:43:01 +00001728 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001729
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 /* transaction id must not be assigned */
1731 if (trans->transaction_id != 0xff) { /* unasssigned */
1732 DEBUGP(DCC, "TX Setup with assigned transaction. "
1733 "This is not allowed!\n");
1734 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001735 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001736 GSM48_CAUSE_LOC_PRN_S_LU,
1737 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001739 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 return rc;
1741 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001742
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001744 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001745 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001746 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001748 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001749 GSM48_CAUSE_LOC_PRN_S_LU,
1750 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001752 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 return rc;
1754 }
Harald Welte78283ef2009-07-23 21:36:44 +02001755 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001756
Harald Welte65e74cc2008-12-29 01:55:35 +00001757 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001758
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001760
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 /* bearer capability */
1762 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001763 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 /* facility */
1765 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001766 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 /* progress */
1768 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001769 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* calling party BCD number */
1771 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001772 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 /* called party BCD number */
1774 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001775 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 /* user-user */
1777 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001778 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* redirecting party BCD number */
1780 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* signal */
1783 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001784 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001785
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001787
Alexander Couzensb847a212016-08-02 11:34:11 +02001788 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001789
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001790 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001791}
1792
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1794{
1795 struct gsm48_hdr *gh = msgb_l3(msg);
1796 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1797 struct tlv_parsed tp;
1798 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001799 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800
1801 gsm48_stop_cc_timer(trans);
1802 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1803
1804 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1805 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001806
Harald Welte474d19f2010-03-02 23:18:30 +01001807 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808#if 0
1809 /* repeat */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1811 call_conf.repeat = 1;
1812 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1813 call_conf.repeat = 2;
1814#endif
1815 /* bearer capability */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1817 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001818 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001820
1821 /* Create a copy of the bearer capability
1822 * in the transaction struct, so we can use
1823 * this information later */
1824 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1825 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 }
1827 /* cause */
1828 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1829 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001830 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1832 }
1833 /* cc cap */
1834 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1835 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001836 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1838 }
1839
Andreas Eversberg035b8742013-09-19 09:27:06 +02001840 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001841 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001842
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1844
Philipp Maierfbf66102017-04-09 12:32:51 +02001845 /* Assign call (if not done yet) */
1846 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001847 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001848 trans->assignment_done = true;
1849 }
1850 else
1851 rc = 0;
1852
1853 /* don't continue, if there were problems with
1854 * the call assignment. */
1855 if (rc)
1856 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001857
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001858 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001859 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860}
1861
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001862static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863{
1864 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001865 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001867 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1870
1871 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1872
1873 /* bearer capability */
1874 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001875 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 /* facility */
1877 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001878 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879 /* progress */
1880 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001881 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001883 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1884 if (rc)
1885 return rc;
1886
Philipp Maierfbf66102017-04-09 12:32:51 +02001887 /* Assign call (if not done yet) */
1888 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001889 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001890 trans->assignment_done = true;
1891 }
1892 else
1893 rc = 0;
1894
1895 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896}
1897
1898static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1899{
1900 struct gsm48_hdr *gh = msgb_l3(msg);
1901 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1902 struct tlv_parsed tp;
1903 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001904
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 gsm48_stop_cc_timer(trans);
1906 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1907
1908 memset(&alerting, 0, sizeof(struct gsm_mncc));
1909 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001910 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 /* facility */
1912 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1913 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001914 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1916 }
1917
1918 /* progress */
1919 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1920 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001921 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1923 }
1924 /* ss-version */
1925 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1926 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1929 }
1930
1931 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1932
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001933 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001934 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935}
1936
1937static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1938{
1939 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001940 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1942
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 gh->msg_type = GSM48_MT_CC_ALERTING;
1944
1945 /* facility */
1946 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001947 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 /* progress */
1949 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001950 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* user-user */
1952 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001953 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954
1955 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001956
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001957 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958}
1959
1960static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1961{
1962 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001963 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1965
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 gh->msg_type = GSM48_MT_CC_PROGRESS;
1967
1968 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 /* user-user */
1971 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001974 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975}
1976
1977static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1978{
1979 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001980 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1982
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 gh->msg_type = GSM48_MT_CC_CONNECT;
1984
1985 gsm48_stop_cc_timer(trans);
1986 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1987
1988 /* facility */
1989 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 /* progress */
1992 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001993 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 /* connected number */
1995 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001996 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997 /* user-user */
1998 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000
2001 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2002
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002003 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004}
2005
2006static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2007{
2008 struct gsm48_hdr *gh = msgb_l3(msg);
2009 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2010 struct tlv_parsed tp;
2011 struct gsm_mncc connect;
2012
2013 gsm48_stop_cc_timer(trans);
2014
2015 memset(&connect, 0, sizeof(struct gsm_mncc));
2016 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002017 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002019 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002020 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2021 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002022
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 /* facility */
2024 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2025 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002026 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2028 }
2029 /* user-user */
2030 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2031 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002032 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2034 }
2035 /* ss-version */
2036 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2037 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002038 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2040 }
2041
2042 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002043 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002044
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002045 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046}
2047
2048
2049static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2050{
2051 struct gsm_mncc connect_ack;
2052
2053 gsm48_stop_cc_timer(trans);
2054
2055 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002056 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002057
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2059 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002060
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002061 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 &connect_ack);
2063}
2064
2065static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2066{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002067 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2069
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2071
2072 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2073
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002074 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075}
2076
2077static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2078{
2079 struct gsm48_hdr *gh = msgb_l3(msg);
2080 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2081 struct tlv_parsed tp;
2082 struct gsm_mncc disc;
2083
2084 gsm48_stop_cc_timer(trans);
2085
2086 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2087
2088 memset(&disc, 0, sizeof(struct gsm_mncc));
2089 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002090 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 /* cause */
2092 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2093 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2096 }
2097 /* facility */
2098 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2099 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002100 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2102 }
2103 /* user-user */
2104 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2105 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002106 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2108 }
2109 /* ss-version */
2110 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2111 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002112 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2114 }
2115
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002116 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117
2118}
2119
Harald Weltec66b71c2009-06-11 14:23:20 +08002120static struct gsm_mncc_cause default_cause = {
2121 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2122 .coding = 0,
2123 .rec = 0,
2124 .rec_val = 0,
2125 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2126 .diag_len = 0,
2127 .diag = { 0 },
2128};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129
2130static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2131{
2132 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002133 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2135
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2137
2138 gsm48_stop_cc_timer(trans);
2139 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2140
2141 /* cause */
2142 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 else
Harald Welte55c8f352010-03-07 23:40:35 +01002145 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146
2147 /* facility */
2148 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002149 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150 /* progress */
2151 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 /* user-user */
2154 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002155 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156
2157 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002158 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159
2160 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2161
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002162 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163}
2164
2165static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2166{
2167 struct gsm48_hdr *gh = msgb_l3(msg);
2168 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2169 struct tlv_parsed tp;
2170 struct gsm_mncc rel;
2171 int rc;
2172
2173 gsm48_stop_cc_timer(trans);
2174
2175 memset(&rel, 0, sizeof(struct gsm_mncc));
2176 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002177 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 /* cause */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2180 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2183 }
2184 /* facility */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2186 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2189 }
2190 /* user-user */
2191 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2192 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002193 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2195 }
2196 /* ss-version */
2197 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2198 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002199 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2201 }
2202
Harald Weltedcaf5652009-07-23 18:56:43 +02002203 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002205 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002207 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002208 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002209 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002210 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 }
2212
2213 new_cc_state(trans, GSM_CSTATE_NULL);
2214
2215 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002216 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217
2218 return rc;
2219}
2220
2221static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2222{
2223 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002224 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2226
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 gh->msg_type = GSM48_MT_CC_RELEASE;
2228
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 gsm48_stop_cc_timer(trans);
2230 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2231
2232 /* cause */
2233 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002234 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 /* facility */
2236 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002237 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 /* user-user */
2239 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002240 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241
Harald Weltedcaf5652009-07-23 18:56:43 +02002242 trans->cc.T308_second = 0;
2243 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244
Harald Weltedcaf5652009-07-23 18:56:43 +02002245 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2247
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002248 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249}
2250
2251static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2252{
2253 struct gsm48_hdr *gh = msgb_l3(msg);
2254 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2255 struct tlv_parsed tp;
2256 struct gsm_mncc rel;
2257 int rc = 0;
2258
2259 gsm48_stop_cc_timer(trans);
2260
2261 memset(&rel, 0, sizeof(struct gsm_mncc));
2262 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002263 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 /* cause */
2265 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2266 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002267 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2269 }
2270 /* facility */
2271 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2272 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002273 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2275 }
2276 /* user-user */
2277 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2278 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2281 }
2282 /* ss-version */
2283 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2284 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002285 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2287 }
2288
2289 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002290 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002292 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 MNCC_REJ_IND, &rel);
2294 break;
2295 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002296 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 MNCC_REL_CNF, &rel);
2298 break;
2299 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002300 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 MNCC_REL_IND, &rel);
2302 }
2303 }
2304
2305 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002306 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
2308 return rc;
2309}
2310
2311static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2312{
2313 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002314 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002316 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2319
2320 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002321
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 gsm48_stop_cc_timer(trans);
2323
2324 /* cause */
2325 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 /* facility */
2328 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002329 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 /* user-user */
2331 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002332 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333
Harald Weltec7782de2010-12-21 19:31:41 +01002334 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2335
Harald Weltedcaf5652009-07-23 18:56:43 +02002336 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337
Harald Weltec7782de2010-12-21 19:31:41 +01002338 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339}
2340
2341static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2342{
2343 struct gsm48_hdr *gh = msgb_l3(msg);
2344 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2345 struct tlv_parsed tp;
2346 struct gsm_mncc fac;
2347
2348 memset(&fac, 0, sizeof(struct gsm_mncc));
2349 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002350 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 /* facility */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2353 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2356 }
2357 /* ss-version */
2358 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2359 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2362 }
2363
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002364 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365}
2366
2367static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2368{
2369 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002370 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2372
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373 gh->msg_type = GSM48_MT_CC_FACILITY;
2374
2375 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002376 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002378 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379}
2380
2381static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2382{
2383 struct gsm_mncc hold;
2384
2385 memset(&hold, 0, sizeof(struct gsm_mncc));
2386 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002387 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2391{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002392 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2394
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2396
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002397 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398}
2399
2400static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2401{
2402 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002403 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2405
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2407
2408 /* cause */
2409 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002410 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 else
Harald Welte55c8f352010-03-07 23:40:35 +01002412 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002414 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2418{
2419 struct gsm_mncc retrieve;
2420
2421 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2422 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002423 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002424 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2428{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002429 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2431
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2433
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002434 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435}
2436
2437static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2438{
2439 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002440 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2442
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2444
2445 /* cause */
2446 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002447 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448 else
Harald Welte55c8f352010-03-07 23:40:35 +01002449 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002451 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452}
2453
2454static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2455{
2456 struct gsm48_hdr *gh = msgb_l3(msg);
2457 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2458 struct tlv_parsed tp;
2459 struct gsm_mncc dtmf;
2460
2461 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2462 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002463 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 /* keypad facility */
2465 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2466 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002467 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2469 }
2470
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002471 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472}
2473
2474static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2475{
2476 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002477 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2479
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2481
2482 /* keypad */
2483 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002484 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002486 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487}
2488
2489static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2490{
2491 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002492 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2494
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2496
2497 /* cause */
2498 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002499 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500 else
Harald Welte55c8f352010-03-07 23:40:35 +01002501 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002503 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2507{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002508 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2510
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2512
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_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2517{
2518 struct gsm_mncc dtmf;
2519
2520 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2521 dtmf.callref = trans->callref;
2522
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002523 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524}
2525
2526static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2527{
2528 struct gsm48_hdr *gh = msgb_l3(msg);
2529 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2530 struct tlv_parsed tp;
2531 struct gsm_mncc modify;
2532
2533 memset(&modify, 0, sizeof(struct gsm_mncc));
2534 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002535 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536 /* bearer capability */
2537 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2538 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002539 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002541
2542 /* Create a copy of the bearer capability
2543 * in the transaction struct, so we can use
2544 * this information later */
2545 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2546 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 }
2548
2549 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2550
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002551 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552}
2553
2554static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2555{
2556 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002557 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002558 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2559
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 gh->msg_type = GSM48_MT_CC_MODIFY;
2561
2562 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2563
2564 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002565 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566
2567 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2568
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002569 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570}
2571
2572static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2573{
2574 struct gsm48_hdr *gh = msgb_l3(msg);
2575 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2576 struct tlv_parsed tp;
2577 struct gsm_mncc modify;
2578
2579 gsm48_stop_cc_timer(trans);
2580
2581 memset(&modify, 0, sizeof(struct gsm_mncc));
2582 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002583 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584 /* bearer capability */
2585 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2586 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002587 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002589
2590 /* Create a copy of the bearer capability
2591 * in the transaction struct, so we can use
2592 * this information later */
2593 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2594 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 }
2596
2597 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2598
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002599 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600}
2601
2602static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2603{
2604 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002605 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2607
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2609
2610 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002611 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612
2613 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2614
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002615 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616}
2617
2618static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2619{
2620 struct gsm48_hdr *gh = msgb_l3(msg);
2621 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2622 struct tlv_parsed tp;
2623 struct gsm_mncc modify;
2624
2625 gsm48_stop_cc_timer(trans);
2626
2627 memset(&modify, 0, sizeof(struct gsm_mncc));
2628 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002629 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630 /* bearer capability */
2631 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2632 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002633 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002635
2636 /* Create a copy of the bearer capability
2637 * in the transaction struct, so we can use
2638 * this information later */
2639 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2640 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 }
2642 /* cause */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2644 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2647 }
2648
2649 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2650
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002651 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652}
2653
2654static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2655{
2656 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002657 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2659
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2661
2662 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002663 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002665 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666
2667 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2668
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002669 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2673{
2674 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002675 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2677
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 gh->msg_type = GSM48_MT_CC_NOTIFY;
2679
2680 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002681 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002683 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684}
2685
2686static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2687{
2688 struct gsm48_hdr *gh = msgb_l3(msg);
2689 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2690// struct tlv_parsed tp;
2691 struct gsm_mncc notify;
2692
2693 memset(&notify, 0, sizeof(struct gsm_mncc));
2694 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002695// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002697 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002699 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700}
2701
2702static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2703{
2704 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002705 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2707
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 gh->msg_type = GSM48_MT_CC_USER_INFO;
2709
2710 /* user-user */
2711 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002712 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 /* more data */
2714 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002715 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002716
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002717 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2721{
2722 struct gsm48_hdr *gh = msgb_l3(msg);
2723 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2724 struct tlv_parsed tp;
2725 struct gsm_mncc user;
2726
2727 memset(&user, 0, sizeof(struct gsm_mncc));
2728 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002729 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 /* user-user */
2731 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2732 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002733 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2735 }
2736 /* more data */
2737 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2738 user.more = 1;
2739
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002740 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741}
2742
Philipp Maierfbf66102017-04-09 12:32:51 +02002743static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2744 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2745 uint32_t payload_msg_type)
2746{
2747 uint8_t data[sizeof(struct gsm_mncc)];
2748 struct gsm_mncc_rtp *rtp;
2749
2750 memset(&data, 0, sizeof(data));
2751 rtp = (struct gsm_mncc_rtp *) &data[0];
2752
2753 rtp->callref = callref;
2754 rtp->msg_type = cmd;
2755 rtp->ip = addr;
2756 rtp->port = port;
2757 rtp->payload_type = payload_type;
2758 rtp->payload_msg_type = payload_msg_type;
2759 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2760}
2761
2762static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2763{
2764 int msg_type;
2765
2766 /* FIXME This has to be set to some meaningful value.
2767 * Possible options are:
2768 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2769 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2770 * (0 if unknown) */
2771 msg_type = GSM_TCHF_FRAME;
2772
Philipp Maier621ba032017-11-07 17:19:25 +01002773 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2774 uint16_t port = trans->conn->rtp.local_port_cn;
2775
2776 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2777
2778 if (addr == INADDR_NONE) {
2779 LOGP(DMNCC, LOGL_ERROR,
2780 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2781 vlr_subscr_name(trans->vsub));
2782 return;
2783 }
2784 if (port == 0) {
2785 LOGP(DMNCC, LOGL_ERROR,
2786 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2787 vlr_subscr_name(trans->vsub));
2788 return;
2789 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002790
2791 /* FIXME: This has to be set to some meaningful value,
2792 * before the MSC-Split, this value was pulled from
2793 * lchan->abis_ip.rtp_payload */
2794 uint32_t payload_type = 0;
2795
2796 return mncc_recv_rtp(net, trans->callref, cmd,
2797 addr,
2798 port,
2799 payload_type,
2800 msg_type);
2801}
2802
2803static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2804{
2805 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2806}
2807
2808static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2809{
2810 struct gsm_trans *trans;
2811 int rc;
2812
2813 /* Find callref */
2814 trans = trans_find_by_callref(net, callref);
2815 if (!trans) {
2816 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2817 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2818 return -EIO;
2819 }
2820 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2821 if (!trans->conn) {
2822 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2823 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2824 return 0;
2825 }
2826
2827 trans->conn->mncc_rtp_bridge = 1;
2828
Philipp Maier621ba032017-11-07 17:19:25 +01002829 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002830 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002831 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002832 * command to the MGCP-GW. The CRCX will return the port number,
2833 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2834 * have to return this port number back to the MNCC by sending
2835 * it back with the TCH_RTP_CREATE message. To make sure that
2836 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002837 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002838 * to take care of this by setting trans->tch_rtp_create to true.
2839 * This will make sure that gsm48_tch_rtp_create() (below) is
2840 * called as soon as the local port number has become known. */
2841 trans->tch_rtp_create = true;
2842
2843 /* Assign call (if not done yet) */
2844 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002845 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002846 trans->assignment_done = true;
2847 }
2848 else
2849 rc = 0;
2850
2851 return rc;
2852}
2853
2854/* Trigger TCH_RTP_CREATE acknowledgement */
2855int gsm48_tch_rtp_create(struct gsm_trans *trans)
2856{
2857 /* This function is called as soon as the port, on which the
2858 * mgcp-gw expects the incoming RTP stream from the remote
2859 * end (e.g. Asterisk) is known. */
2860
2861 struct gsm_subscriber_connection *conn = trans->conn;
2862 struct gsm_network *network = conn->network;
2863
2864 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2865 return 0;
2866}
2867
2868static int tch_rtp_connect(struct gsm_network *net, void *arg)
2869{
2870 struct gsm_trans *trans;
2871 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002872 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002873
2874 /* Find callref */
2875 trans = trans_find_by_callref(net, rtp->callref);
2876 if (!trans) {
2877 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2878 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2879 return -EIO;
2880 }
2881 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2882 if (!trans->conn) {
2883 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2884 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2885 return 0;
2886 }
2887
Philipp Maier621ba032017-11-07 17:19:25 +01002888 addr.s_addr = osmo_htonl(rtp->ip);
2889 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002890}
2891
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002893 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 int type;
2895 int (*rout) (struct gsm_trans *trans, void *arg);
2896} downstatelist[] = {
2897 /* mobile originating call establishment */
2898 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002899 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2901 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2902 {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 */
2903 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2904 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2905 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2906 /* mobile terminating call establishment */
2907 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2908 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2909 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2910 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2911 /* signalling during call */
2912 {SBIT(GSM_CSTATE_ACTIVE),
2913 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2914 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2915 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2916 {ALL_STATES,
2917 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2918 {ALL_STATES,
2919 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2920 {ALL_STATES,
2921 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2922 {SBIT(GSM_CSTATE_ACTIVE),
2923 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2924 {SBIT(GSM_CSTATE_ACTIVE),
2925 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2926 {SBIT(GSM_CSTATE_ACTIVE),
2927 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2930 {SBIT(GSM_CSTATE_ACTIVE),
2931 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2932 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2933 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2934 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2935 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2936 {SBIT(GSM_CSTATE_ACTIVE),
2937 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2938 /* clearing */
2939 {SBIT(GSM_CSTATE_INITIATED),
2940 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2941 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2942 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2943 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2944 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002945};
2946
2947#define DOWNSLLEN \
2948 (sizeof(downstatelist) / sizeof(struct downstate))
2949
2950
Harald Welte76556372010-12-22 23:57:45 +01002951int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952{
Harald Welte1a6f7982009-08-09 18:52:33 +02002953 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002955 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 struct gsm_mncc *data = arg, rel;
2957
Harald Welte04dc88f2010-12-22 21:45:05 +01002958 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2959
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960 /* handle special messages */
2961 switch(msg_type) {
2962 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002963 rc = tch_bridge(net, arg);
2964 if (rc < 0)
2965 disconnect_bridge(net, arg, -rc);
2966 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002967 case MNCC_RTP_CREATE:
2968 return tch_rtp_create(net, data->callref);
2969 case MNCC_RTP_CONNECT:
2970 return tch_rtp_connect(net, arg);
2971 case MNCC_RTP_FREE:
2972 /* unused right now */
2973 return -EIO;
2974
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002977 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002978 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002979 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002980 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002981 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2982 get_mncc_name(msg_type));
2983 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 }
2985
2986 memset(&rel, 0, sizeof(struct gsm_mncc));
2987 rel.callref = data->callref;
2988
2989 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002990 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991
2992 /* Callref unknown */
2993 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002994 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002995
Harald Welte4a3464c2009-07-04 10:11:24 +02002996 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2998 "Received '%s' from MNCC with "
2999 "unknown callref %d\n", data->called.number,
3000 get_mncc_name(msg_type), data->callref);
3001 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003002 return mncc_release_ind(net, NULL, data->callref,
3003 GSM48_CAUSE_LOC_PRN_S_LU,
3004 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003006 if (!data->called.number[0] && !data->imsi[0]) {
3007 DEBUGP(DCC, "(bts - trx - ts - ti) "
3008 "Received '%s' from MNCC with "
3009 "no number or IMSI\n", get_mncc_name(msg_type));
3010 /* Invalid number */
3011 return mncc_release_ind(net, NULL, data->callref,
3012 GSM48_CAUSE_LOC_PRN_S_LU,
3013 GSM48_CC_CAUSE_INV_NR_FORMAT);
3014 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003016 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003017 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3018 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003019 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003020 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003021
3022 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003023 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003024
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003026 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3028 "Received '%s' from MNCC with "
3029 "unknown subscriber %s\n", data->called.number,
3030 get_mncc_name(msg_type), data->called.number);
3031 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003032 return mncc_release_ind(net, NULL, data->callref,
3033 GSM48_CAUSE_LOC_PRN_S_LU,
3034 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 }
3036 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003037 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3039 "Received '%s' from MNCC with "
3040 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003041 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003042 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003043 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003044 return mncc_release_ind(net, NULL, data->callref,
3045 GSM48_CAUSE_LOC_PRN_S_LU,
3046 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 }
3048 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003049 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003050 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003052 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003054 mncc_release_ind(net, NULL, data->callref,
3055 GSM48_CAUSE_LOC_PRN_S_LU,
3056 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057 return -ENOMEM;
3058 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003059
3060 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003061 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003062
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003063 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003064 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065 /* find transaction with this subscriber already paging */
3066 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003067 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003069 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003071 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003072 "Received '%s' from MNCC with "
3073 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003074 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003076 get_mncc_name(msg_type), vsub->lac);
3077 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003078 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 return 0;
3080 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003081 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003082 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003083
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003084 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003085 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003086 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003087 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003088 trans,
3089 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003090 if (!trans->paging_request) {
3091 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003092 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003093 trans_free(trans);
3094 return 0;
3095 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003096 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003097 return 0;
3098 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003099
3100 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003101 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003102 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003103 } else {
3104 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003105 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003107
3108 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003109 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110
3111 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003112 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003113 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003114 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003115 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003116 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003117 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3118 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 if (msg_type == MNCC_REL_REQ)
3120 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3121 else
3122 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3123 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003124 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003126 } else {
3127 DEBUGP(DCC, "(ti %02x sub %s) "
3128 "Received '%s' from MNCC in state %d (%s)\n",
3129 trans->transaction_id,
3130 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3131 get_mncc_name(msg_type), trans->cc.state,
3132 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003133 }
3134
Harald Welte4bfdfe72009-06-10 23:11:52 +08003135 /* Find function for current state and message */
3136 for (i = 0; i < DOWNSLLEN; i++)
3137 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003138 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139 break;
3140 if (i == DOWNSLLEN) {
3141 DEBUGP(DCC, "Message unhandled at this state.\n");
3142 return 0;
3143 }
3144
3145 rc = downstatelist[i].rout(trans, arg);
3146
3147 return rc;
3148}
3149
3150
3151static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003152 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153 int type;
3154 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3155} datastatelist[] = {
3156 /* mobile originating call establishment */
3157 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3158 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3159 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3160 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3161 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3162 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3163 /* mobile terminating call establishment */
3164 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3165 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3166 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3167 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003168 {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 +08003169 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3170 /* signalling during call */
3171 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3172 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3173 {SBIT(GSM_CSTATE_ACTIVE),
3174 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3175 {ALL_STATES,
3176 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3177 {ALL_STATES,
3178 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3179 {ALL_STATES,
3180 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3181 {SBIT(GSM_CSTATE_ACTIVE),
3182 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3185 {SBIT(GSM_CSTATE_ACTIVE),
3186 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3187 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3188 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3189 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3190 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3191 {SBIT(GSM_CSTATE_ACTIVE),
3192 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3193 /* clearing */
3194 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3195 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3196 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3197 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3198 {ALL_STATES, /* 5.4.3.4 */
3199 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3200};
3201
3202#define DATASLLEN \
3203 (sizeof(datastatelist) / sizeof(struct datastate))
3204
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003205static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003206{
3207 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003208 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003209 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003210 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003211 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003212
Harald Welte4bfdfe72009-06-10 23:11:52 +08003213 if (msg_type & 0x80) {
3214 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3215 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003216 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003217
Harald Welte2483f1b2016-06-19 18:06:02 +02003218 if (!conn->vsub) {
3219 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003220 return -EINVAL;
3221 }
3222
Harald Welte4bfdfe72009-06-10 23:11:52 +08003223 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003224 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003225
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003226#if BEFORE_MSCSPLIT
3227 /* Re-enable this log output once we can obtain this information via
3228 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003229 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003230 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003231 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003232 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003233 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3234 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003235#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003236
3237 /* Create transaction */
3238 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003239 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003240 "creating new trans.\n", transaction_id);
3241 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003242 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003243 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003244 transaction_id, new_callref++);
3245 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003246 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003247 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003248 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249 GSM48_MT_CC_RELEASE_COMPL);
3250 return -ENOMEM;
3251 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003252 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003253 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003254 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 }
3256
3257 /* find function for current state and message */
3258 for (i = 0; i < DATASLLEN; i++)
3259 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003260 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003261 break;
3262 if (i == DATASLLEN) {
3263 DEBUGP(DCC, "Message unhandled at this state.\n");
3264 return 0;
3265 }
3266
Harald Welte2483f1b2016-06-19 18:06:02 +02003267 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003268
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003270
Harald Welte2483f1b2016-06-19 18:06:02 +02003271 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003272 return rc;
3273}
3274
Harald Welte2483f1b2016-06-19 18:06:02 +02003275static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003276{
Harald Welte2483f1b2016-06-19 18:06:02 +02003277 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3278 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003279
Harald Welte2483f1b2016-06-19 18:06:02 +02003280 switch (pdisc) {
3281 case GSM48_PDISC_MM:
3282 switch (msg_type) {
3283 case GSM48_MT_MM_LOC_UPD_REQUEST:
3284 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003285 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003286 case GSM48_MT_MM_AUTH_RESP:
3287 case GSM48_MT_MM_AUTH_FAIL:
3288 case GSM48_MT_MM_ID_RESP:
3289 case GSM48_MT_MM_TMSI_REALL_COMPL:
3290 case GSM48_MT_MM_IMSI_DETACH_IND:
3291 return true;
3292 default:
3293 break;
3294 }
3295 break;
3296 case GSM48_PDISC_RR:
3297 switch (msg_type) {
3298 case GSM48_MT_RR_CIPH_M_COMPL:
3299 case GSM48_MT_RR_PAG_RESP:
3300 return true;
3301 default:
3302 break;
3303 }
3304 break;
3305 default:
3306 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003307 }
3308
Harald Welte2483f1b2016-06-19 18:06:02 +02003309 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003310}
3311
Harald Welte2483f1b2016-06-19 18:06:02 +02003312void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3313 struct msgb *msg)
3314{
3315
3316 /* If a CM Service Request was received before, this is the request the
3317 * conn was opened for. No need to wait for further messages. */
3318
3319 if (!conn->received_cm_service_request)
3320 return;
3321
3322 if (log_check_level(DMM, LOGL_DEBUG)) {
3323 struct gsm48_hdr *gh = msgb_l3(msg);
3324 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3325 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3326
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003327 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003328 " received_cm_service_request changes to false\n",
3329 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003330 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003331 }
3332 conn->received_cm_service_request = false;
3333}
3334
Harald Weltec2007852018-02-03 21:08:26 +01003335/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3336int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3337{
3338 switch (pdisc) {
3339 case GSM48_PDISC_MM:
3340 case GSM48_PDISC_CC:
3341 case GSM48_PDISC_NC_SS:
3342 return 0;
3343 case GSM48_PDISC_GROUP_CC:
3344 return 1;
3345 case GSM48_PDISC_BCAST_CC:
3346 return 2;
3347 case GSM48_PDISC_LOC:
3348 return 3;
3349 default:
3350 return -1;
3351 }
3352}
3353
3354/* extract the N(SD) and return the modulo value for a R98 message */
3355static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3356{
3357 switch (pdisc) {
3358 case GSM48_PDISC_MM:
3359 case GSM48_PDISC_CC:
3360 case GSM48_PDISC_NC_SS:
3361 *n_sd = (msg_type >> 6) & 0x3;
3362 return 4;
3363 case GSM48_PDISC_GROUP_CC:
3364 case GSM48_PDISC_BCAST_CC:
3365 case GSM48_PDISC_LOC:
3366 *n_sd = (msg_type >> 6) & 0x1;
3367 return 2;
3368 default:
3369 /* no sequence number, we cannot detect dups */
3370 return 0;
3371 }
3372}
3373
3374/* extract the N(SD) and return the modulo value for a R99 message */
3375static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3376{
3377 switch (pdisc) {
3378 case GSM48_PDISC_MM:
3379 case GSM48_PDISC_CC:
3380 case GSM48_PDISC_NC_SS:
3381 case GSM48_PDISC_GROUP_CC:
3382 case GSM48_PDISC_BCAST_CC:
3383 case GSM48_PDISC_LOC:
3384 *n_sd = (msg_type >> 6) & 0x1;
3385 return 2;
3386 default:
3387 /* no sequence number, we cannot detect dups */
3388 return 0;
3389 }
3390}
3391
3392/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3393static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3394{
3395 struct gsm48_hdr *gh;
3396 uint8_t pdisc;
3397 uint8_t n_sd, modulo, bin;
3398
3399 gh = msgb_l3(msg);
3400 pdisc = gsm48_hdr_pdisc(gh);
3401
3402 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3403 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3404 } else { /* R99 */
3405 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3406 }
3407 if (modulo == 0)
3408 return false;
3409 bin = gsm0407_pdisc_ctr_bin(pdisc);
3410 if (bin < 0)
3411 return false;
3412
3413 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3414 if (n_sd != conn->n_sd_next[bin]) {
3415 /* not what we expected: duplicate */
3416 return true;
3417 } else {
3418 /* as expected: no dup; update expected counter for next message */
3419 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3420 return false;
3421 }
3422}
Harald Welte2483f1b2016-06-19 18:06:02 +02003423
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003424/* 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 +08003425int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003426{
Philipp Maier91f10c72017-04-20 18:40:37 +02003427 struct gsm48_hdr *gh;
3428 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003429 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003430
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003431 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003432 OSMO_ASSERT(conn);
3433 OSMO_ASSERT(msg);
3434
Philipp Maier91f10c72017-04-20 18:40:37 +02003435 gh = msgb_l3(msg);
3436 pdisc = gsm48_hdr_pdisc(gh);
3437
Harald Weltec2007852018-02-03 21:08:26 +01003438 if (gsm0407_is_duplicate(conn, msg)) {
3439 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3440 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3441 return 0;
3442 }
3443
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003444 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3445 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3446 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003447
3448 if (!msc_subscr_conn_is_accepted(conn)
3449 && !msg_is_initially_permitted(gh)) {
3450 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003451 "subscr %s: Message not permitted for initial conn: %s\n",
3452 vlr_subscr_name(conn->vsub),
3453 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003454 return -EACCES;
3455 }
3456
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003457 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3458 LOGP(DMM, LOGL_ERROR,
3459 "%s: Illegal situation: RAN type mismatch:"
3460 " attached via %s, received message via %s\n",
3461 vlr_subscr_name(conn->vsub),
3462 ran_type_name(conn->vsub->cs.attached_via_ran),
3463 ran_type_name(conn->via_ran));
3464 return -EACCES;
3465 }
3466
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003467#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003468 if (silent_call_reroute(conn, msg))
3469 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003470#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003471
Harald Welte52b1f982008-12-23 20:25:15 +00003472 switch (pdisc) {
3473 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003474 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003475 break;
3476 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003477 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003478 break;
3479 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003480 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003481 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003482 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003483 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003484 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003485 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003486 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003487 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3488 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003489 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003490 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003491 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003492 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003493 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003494 case GSM48_PDISC_TEST:
3495 rc = gsm0414_rcv_test(conn, msg);
3496 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003497 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003498 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3499 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003500 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003501 break;
3502 }
3503
3504 return rc;
3505}
Harald Welte8470bf22008-12-25 23:28:35 +00003506
Harald Welte2483f1b2016-06-19 18:06:02 +02003507/***********************************************************************
3508 * VLR integration
3509 ***********************************************************************/
3510
3511/* VLR asks us to send an authentication request */
3512static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3513 bool send_autn)
3514{
3515 struct gsm_subscriber_connection *conn = msc_conn_ref;
3516 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3517 send_autn? at->vec.autn : NULL,
3518 at->key_seq);
3519}
3520
3521/* VLR asks us to send an authentication reject */
3522static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3523{
3524 struct gsm_subscriber_connection *conn = msc_conn_ref;
3525 return gsm48_tx_mm_auth_rej(conn);
3526}
3527
3528/* VLR asks us to transmit an Identity Request of given type */
3529static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3530{
3531 struct gsm_subscriber_connection *conn = msc_conn_ref;
3532 return mm_tx_identity_req(conn, mi_type);
3533}
3534
3535/* VLR asks us to transmit a Location Update Accept */
3536static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3537{
3538 struct gsm_subscriber_connection *conn = msc_conn_ref;
3539 return gsm0408_loc_upd_acc(conn, send_tmsi);
3540}
3541
3542/* VLR asks us to transmit a Location Update Reject */
3543static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3544{
3545 struct gsm_subscriber_connection *conn = msc_conn_ref;
3546 return gsm0408_loc_upd_rej(conn, cause);
3547}
3548
3549/* VLR asks us to transmit a CM Service Accept */
3550static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3551{
3552 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003553 return msc_gsm48_tx_mm_serv_ack(conn);
3554}
3555
3556static int msc_vlr_tx_common_id(void *msc_conn_ref)
3557{
3558 struct gsm_subscriber_connection *conn = msc_conn_ref;
3559 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003560}
3561
3562/* VLR asks us to transmit a CM Service Reject */
3563static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3564{
3565 uint8_t cause;
3566 struct gsm_subscriber_connection *conn = msc_conn_ref;
3567 conn->received_cm_service_request = false;
3568
3569 switch (result) {
3570 default:
3571 case VLR_PR_ARQ_RES_NONE:
3572 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3573 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3574 cause = GSM48_REJECT_NETWORK_FAILURE;
3575 break;
3576 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3577 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3578 break;
3579 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3580 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3581 break;
3582 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3583 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3584 break;
3585 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3586 cause = GSM48_REJECT_ILLEGAL_MS;
3587 break;
3588 case VLR_PR_ARQ_RES_TIMEOUT:
3589 cause = GSM48_REJECT_CONGESTION;
3590 break;
3591 };
3592
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003593 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003594}
3595
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003596/* For msc_vlr_set_ciph_mode() */
3597osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3598 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3599
Harald Welte2483f1b2016-06-19 18:06:02 +02003600/* VLR asks us to start using ciphering */
3601static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01003602 bool umts_aka,
Harald Welte2483f1b2016-06-19 18:06:02 +02003603 bool retrieve_imeisv)
3604{
3605 struct gsm_subscriber_connection *conn = msc_conn_ref;
3606 struct vlr_subscr *vsub;
3607 struct gsm_auth_tuple *tuple;
3608
3609 if (!conn || !conn->vsub) {
3610 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3611 " NULL conn/subscriber");
3612 return -EINVAL;
3613 }
3614
3615 vsub = conn->vsub;
3616 tuple = vsub->last_tuple;
3617
3618 if (!tuple) {
3619 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3620 " Command: no auth tuple available\n",
3621 vlr_subscr_name(vsub));
3622 return -EINVAL;
3623 }
3624
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003625 switch (conn->via_ran) {
3626 case RAN_GERAN_A:
3627 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3628 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003629 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003630 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003631 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003632 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003633
Harald Welte7b222aa2017-12-23 19:30:32 +01003634 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003635 if (net->a5_encryption_mask & (1 << i) &&
3636 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003637 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3638 }
3639 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003640
Harald Welte71330722017-12-23 19:59:02 +01003641 if (ei.perm_algo_len == 0) {
3642 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3643 "between MSC-configured and MS-supported A5 algorithms\n",
3644 vlr_subscr_name(conn->vsub));
3645 return -ENOTSUP;
3646 }
3647
Neels Hofmeyr52821712017-12-18 01:23:42 +01003648 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3649 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3650 * necessarily a match for the UMTS AKA tokens. */
3651 if (umts_aka)
3652 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3653 else
3654 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003655 ei.key_len = sizeof(tuple->vec.kc);
3656
3657 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3658 }
3659
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003660 case RAN_UTRAN_IU:
3661#ifdef BUILD_IU
3662 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3663 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003664 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003665#else
3666 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3667 " built without Iu support\n");
3668 return -ENOTSUP;
3669#endif
3670
3671 default:
3672 break;
3673 }
3674 LOGP(DMM, LOGL_ERROR,
3675 "%s: cannot start ciphering, unknown RAN type %d\n",
3676 vlr_subscr_name(conn->vsub), conn->via_ran);
3677 return -ENOTSUP;
3678}
3679
3680void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3681{
3682 struct vlr_ciph_result vlr_res = {};
3683
3684 if (!conn || !conn->vsub) {
3685 LOGP(DMM, LOGL_ERROR,
3686 "Rx Security Mode Complete for invalid conn\n");
3687 return;
3688 }
3689
3690 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3691 vlr_subscr_name(conn->vsub));
3692
3693 vlr_res.cause = VLR_CIPH_COMPL;
3694 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003695}
3696
3697/* VLR informs us that the subscriber data has somehow been modified */
3698static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3699{
Maxa263bb22017-12-27 13:23:44 +01003700 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3701 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003702}
3703
3704/* VLR informs us that the subscriber has been associated with a conn */
3705static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3706 struct vlr_subscr *vsub)
3707{
3708 struct gsm_subscriber_connection *conn = msc_conn_ref;
3709 OSMO_ASSERT(!conn->vsub);
3710 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003711 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003712}
3713
3714/* operations that we need to implement for libvlr */
3715static const struct vlr_ops msc_vlr_ops = {
3716 .tx_auth_req = msc_vlr_tx_auth_req,
3717 .tx_auth_rej = msc_vlr_tx_auth_rej,
3718 .tx_id_req = msc_vlr_tx_id_req,
3719 .tx_lu_acc = msc_vlr_tx_lu_acc,
3720 .tx_lu_rej = msc_vlr_tx_lu_rej,
3721 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3722 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3723 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003724 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003725 .subscr_update = msc_vlr_subscr_update,
3726 .subscr_assoc = msc_vlr_subscr_assoc,
3727};
3728
3729/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3730int msc_vlr_alloc(struct gsm_network *net)
3731{
3732 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3733 if (!net->vlr)
3734 return -ENOMEM;
3735 net->vlr->user_ctx = net;
3736 return 0;
3737}
3738
3739/* Launch the VLR, i.e. its GSUP connection */
3740int msc_vlr_start(struct gsm_network *net)
3741{
3742 OSMO_ASSERT(net->vlr);
3743 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3744 net->gsup_server_port);
3745}