blob: 26ac923191a64671ae6ef5978ee783c3cda65849 [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>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020034#include <openssl/rand.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Harald Welte9c3dc902012-04-08 16:59:24 +020036#include "bscconfig.h"
37
Neels Hofmeyr90843962017-09-04 15:04:35 +020038#include <osmocom/msc/auth.h>
39#include <osmocom/msc/db.h>
40#include <osmocom/msc/debug.h>
41#include <osmocom/msc/gsm_data.h>
42#include <osmocom/msc/gsm_subscriber.h>
43#include <osmocom/msc/gsm_04_11.h>
44#include <osmocom/msc/gsm_04_08.h>
45#include <osmocom/msc/gsm_04_80.h>
46#include <osmocom/msc/gsm_04_14.h>
47#include <osmocom/msc/abis_rsl.h>
48#include <osmocom/msc/chan_alloc.h>
49#include <osmocom/msc/paging.h>
50#include <osmocom/msc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/trau_frame.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020052#include <osmocom/msc/trau_mux.h>
53#include <osmocom/msc/rtp_proxy.h>
54#include <osmocom/msc/transaction.h>
55#include <osmocom/msc/ussd.h>
56#include <osmocom/msc/silent_call.h>
57#include <osmocom/msc/bsc_api.h>
58#include <osmocom/msc/osmo_msc.h>
59#include <osmocom/msc/handover.h>
60#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020061#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020063#include <osmocom/msc/vlr.h>
64#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080065
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/gsm/gsm48.h>
67#include <osmocom/gsm/gsm0480.h>
68#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020069#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010070#include <osmocom/core/msgb.h>
71#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010072#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010073#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020074#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020075#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020076#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020077#endif
Harald Welte52b1f982008-12-23 20:25:15 +000078
Neels Hofmeyr90843962017-09-04 15:04:35 +020079#include <osmocom/msc/msc_ifaces.h>
80#include <osmocom/msc/a_iface.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020081
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020082#include <assert.h>
83
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020084
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020085void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010086void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020087
Harald Welte2483f1b2016-06-19 18:06:02 +020088static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
89 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080090static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020091 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000092
Harald Welte52b1f982008-12-23 20:25:15 +000093struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020094 uint16_t mcc;
95 uint16_t mnc;
96 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000097};
98
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020099static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800100
Harald Welte31c00f72011-03-03 23:29:05 +0100101void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
102{
103 net->mncc_recv(net, msg);
104}
105
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800106static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
107 struct gsm_trans *trans)
108{
109 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
110
111 /* if we get passed a transaction reference, do some common
112 * work that the caller no longer has to do */
113 if (trans) {
114 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800115 }
116
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200117 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800118}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100119
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800120int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
121{
122 struct gsm48_hdr *gh;
123 struct msgb *ss_notify;
124
125 ss_notify = gsm0480_create_notifySS(message);
126 if (!ss_notify)
127 return -1;
128
129 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
130 uint8_t *data = msgb_push(ss_notify, 1);
131 data[0] = ss_notify->len - 1;
132 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
133 gh->msg_type = GSM48_MT_CC_FACILITY;
134 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
135}
136
Harald Welte2483f1b2016-06-19 18:06:02 +0200137/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100138void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
139{
140 struct gsm_trans *trans, *temp;
141
142 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
143
144 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100145 if (trans->protocol == protocol) {
146 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100147 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100148 }
Harald Welte371efe52010-12-22 23:17:50 +0100149 }
150}
151
Holger Freyther429e7762008-12-30 13:28:30 +0000152/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200153static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000154{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800155 struct msgb *msg;
156
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800157 msg = gsm48_create_loc_upd_rej(cause);
158 if (!msg) {
159 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
160 return -1;
161 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200162
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200163 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
164 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100165
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800166 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000167}
168
169/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200170static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
171 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000172{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100173 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000174 struct gsm48_hdr *gh;
175 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200176 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200177
Harald Welte52b1f982008-12-23 20:25:15 +0000178 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
179 gh->proto_discr = GSM48_PDISC_MM;
180 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
181
182 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200183 gsm48_generate_lai(lai, conn->network->country_code,
184 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200185 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000186
Harald Welte2483f1b2016-06-19 18:06:02 +0200187 if (send_tmsi == GSM_RESERVED_TMSI) {
188 /* we did not allocate a TMSI to the MS, so we need to
189 * include the IMSI in order for the MS to delete any
190 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200191 uint8_t mi[10];
192 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200193 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200194 mid = msgb_put(msg, len);
195 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200196 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
197 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200198 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200199 /* Include the TMSI, which means that the MS will send a
200 * TMSI REALLOCATION COMPLETE, and we should wait for
201 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200202 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200203 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200204 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
205 vlr_subscr_name(conn->vsub),
206 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200207 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200208 /* TODO: Follow-on proceed */
209 /* TODO: CTS permission */
210 /* TODO: Equivalent PLMNs */
211 /* TODO: Emergency Number List */
212 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000213
Harald Welte52b1f982008-12-23 20:25:15 +0000214
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800215 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000216}
217
Harald Weltebf5e8df2009-02-03 12:59:45 +0000218/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200219static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000220{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100221 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000222 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000223
Harald Welte231ad4f2008-12-27 11:15:38 +0000224 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
225 gh->proto_discr = GSM48_PDISC_MM;
226 gh->msg_type = GSM48_MT_MM_ID_REQ;
227 gh->data[0] = id_type;
228
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800229 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000230}
231
Harald Weltebf5e8df2009-02-03 12:59:45 +0000232/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800233static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000234{
235 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200236 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200237 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000238
Harald Welte2483f1b2016-06-19 18:06:02 +0200239 if (!conn->vsub) {
240 LOGP(DMM, LOGL_ERROR,
241 "Rx MM Identity Response: invalid: no subscriber\n");
242 return -EINVAL;
243 }
244
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200245 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200246 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
247 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000248
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200249 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100250
Harald Welte2483f1b2016-06-19 18:06:02 +0200251 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000252}
253
Harald Welte2483f1b2016-06-19 18:06:02 +0200254/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200255static const struct value_string lupd_names[] = {
256 { GSM48_LUPD_NORMAL, "NORMAL" },
257 { GSM48_LUPD_PERIODIC, "PERIODIC" },
258 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
259 { 0, NULL }
260};
Harald Welte2a139372009-02-22 21:14:55 +0000261
Harald Welte2483f1b2016-06-19 18:06:02 +0200262/* Chapter 9.2.15: Receive Location Updating Request.
263 * Keep this function non-static for direct invocation by unit tests. */
264int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000265{
Harald Welte2483f1b2016-06-19 18:06:02 +0200266 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
267 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000268 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000269 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200270 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200271 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200272 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200273 uint32_t tmsi;
274 char *imsi;
275 struct osmo_location_area_id old_lai, new_lai;
276 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200277 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000279
Harald Welte8470bf22008-12-25 23:28:35 +0000280 lu = (struct gsm48_loc_upd_req *) gh->data;
281
282 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000283
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200284 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000285
Harald Welte2483f1b2016-06-19 18:06:02 +0200286 rc = msc_create_conn_fsm(conn, mi_string);
287 if (rc)
288 /* logging already happened in msc_create_conn_fsm() */
289 return rc;
290
291 conn->classmark.classmark1 = lu->classmark1;
292 conn->classmark.classmark1_set = true;
293
294 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
295 gsm48_mi_type_name(mi_type), mi_string,
296 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000297
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200298 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100299
Harald Welte24ff6ee2009-12-22 00:41:05 +0100300 switch (lu->type) {
301 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200302 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200303 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100304 break;
305 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200306 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200307 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100308 break;
309 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200310 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200311 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100312 break;
313 }
314
Harald Welte2483f1b2016-06-19 18:06:02 +0200315 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
316 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
317 /* TODO: 10.5.7.8 Device properties */
318 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200319
Harald Welte52b1f982008-12-23 20:25:15 +0000320 switch (mi_type) {
321 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 tmsi = GSM_RESERVED_TMSI;
323 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000324 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000325 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 tmsi = tmsi_from_string(mi_string);
327 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000328 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200329 default:
Harald Weltea0368542009-06-27 02:58:43 +0200330 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200331 tmsi = GSM_RESERVED_TMSI;
332 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000333 break;
334 }
335
Harald Welte2483f1b2016-06-19 18:06:02 +0200336 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
337 &old_lai.plmn.mnc, &old_lai.lac);
338 new_lai.plmn.mcc = conn->network->country_code;
339 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200340 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200341 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200342
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200343 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200344 lu_fsm = vlr_loc_update(conn->conn_fsm,
345 SUBSCR_CONN_E_ACCEPTED,
346 SUBSCR_CONN_E_CN_CLOSE,
347 (void*)&conn_from_lu,
348 net->vlr, conn, vlr_lu_type, tmsi, imsi,
349 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200350 is_utran || conn->network->authentication_required,
351 is_utran? VLR_CIPH_A5_3
352 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200353 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200354 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200355 net->vlr->cfg.assign_tmsi);
356 if (!lu_fsm) {
357 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
358 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800359 }
360
Harald Welte2483f1b2016-06-19 18:06:02 +0200361 /* From vlr_loc_update() we expect an implicit dispatch of
362 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
363 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000364
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
366 LOGP(DRR, LOGL_ERROR,
367 "%s: internal error during Location Updating attempt\n",
368 mi_string);
369 return -EIO;
370 }
371
372 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000373}
374
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100375/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200376/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100377static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800378{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100379 uint8_t ret;
380
381 ret = value / 10;
382 ret |= (value % 10) << 4;
383
384 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800385}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100386
Harald Welte4bfdfe72009-06-10 23:11:52 +0800387
Harald Weltedb253af2008-12-30 17:56:55 +0000388/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800389int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000390{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100391 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000392 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200393 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200394 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200395 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100396
Harald Welte4bfdfe72009-06-10 23:11:52 +0800397 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100398 struct tm* gmt_time;
399 struct tm* local_time;
400 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100401 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000402
Harald Weltedb253af2008-12-30 17:56:55 +0000403 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
404 gh->proto_discr = GSM48_PDISC_MM;
405 gh->msg_type = GSM48_MT_MM_INFO;
406
407 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200408#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000409 name_len = strlen(net->name_long);
410 /* 10.5.3.5a */
411 ptr8 = msgb_put(msg, 3);
412 ptr8[0] = GSM48_IE_NAME_LONG;
413 ptr8[1] = name_len*2 +1;
414 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
415
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200416 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000417 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000418 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000419
420 /* FIXME: Use Cell Broadcast, not UCS-2, since
421 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200422#endif
423 name_len = (strlen(net->name_long)*7)/8;
424 name_pad = (8 - strlen(net->name_long)*7)%8;
425 if (name_pad > 0)
426 name_len++;
427 /* 10.5.3.5a */
428 ptr8 = msgb_put(msg, 3);
429 ptr8[0] = GSM48_IE_NAME_LONG;
430 ptr8[1] = name_len +1;
431 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
432
433 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100434 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200435
Harald Weltedb253af2008-12-30 17:56:55 +0000436 }
437
438 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200439#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000440 name_len = strlen(net->name_short);
441 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200443 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000444 ptr8[1] = name_len*2 + 1;
445 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
446
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200447 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000448 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000449 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200450#endif
451 name_len = (strlen(net->name_short)*7)/8;
452 name_pad = (8 - strlen(net->name_short)*7)%8;
453 if (name_pad > 0)
454 name_len++;
455 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200456 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200457 ptr8[0] = GSM48_IE_NAME_SHORT;
458 ptr8[1] = name_len +1;
459 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
460
461 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100462 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200463
Harald Weltedb253af2008-12-30 17:56:55 +0000464 }
465
Harald Weltedb253af2008-12-30 17:56:55 +0000466 /* Section 10.5.3.9 */
467 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100468 gmt_time = gmtime(&cur_t);
469
Harald Weltedb253af2008-12-30 17:56:55 +0000470 ptr8 = msgb_put(msg, 8);
471 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100472 ptr8[1] = bcdify(gmt_time->tm_year % 100);
473 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
474 ptr8[3] = bcdify(gmt_time->tm_mday);
475 ptr8[4] = bcdify(gmt_time->tm_hour);
476 ptr8[5] = bcdify(gmt_time->tm_min);
477 ptr8[6] = bcdify(gmt_time->tm_sec);
478
Neels Hofmeyr73983952016-05-10 13:29:33 +0200479 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200480 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200481 if (net->tz.hr < 0) {
482 tzunits = ((net->tz.hr/-1)*4);
483 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100484 ptr8[7] = bcdify(tzunits);
485 /* Set negative time */
486 ptr8[7] |= 0x08;
487 }
488 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200489 tzunits = net->tz.hr*4;
490 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100491 ptr8[7] = bcdify(tzunits);
492 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100493 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200494 if (net->tz.dst >= 0 && net->tz.dst <= 2)
495 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100496 }
497 else {
498 /* Need to get GSM offset and convert into 15 min units */
499 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200500#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200501 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100502 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200503#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200504 /* find timezone offset */
505 time_t utc;
506 double offsetFromUTC;
507 utc = mktime(gmt_time);
508 local_time = localtime(&cur_t);
509 offsetFromUTC = difftime(cur_t, utc);
510 if (local_time->tm_isdst)
511 offsetFromUTC += 3600.0;
512 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200513#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100514 if (tzunits < 0) {
515 tzunits = tzunits/-1;
516 ptr8[7] = bcdify(tzunits);
517 /* Flip it to negative */
518 ptr8[7] |= 0x08;
519 }
520 else
521 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100522
523 /* Does not support DST +2 */
524 if (local_time->tm_isdst)
525 dst = 1;
526 }
527
528 if (dst) {
529 ptr8 = msgb_put(msg, 3);
530 ptr8[0] = GSM48_IE_NET_DST;
531 ptr8[1] = 1;
532 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100533 }
Harald Weltedb253af2008-12-30 17:56:55 +0000534
Daniel Willmanneea93372009-08-13 03:42:07 +0200535 DEBUGP(DMM, "-> MM INFO\n");
536
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800537 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000538}
539
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100540/*! Send an Authentication Request to MS on the given subscriber connection
541 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
542 * \param[in] conn Subscriber connection to send on.
543 * \param[in] rand Random challenge token to send, must be 16 bytes long.
544 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
545 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
546 * \param[in] key_seq auth tuple's sequence number.
547 */
548int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
549 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200550{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200553 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200554
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100555 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100556 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100557 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200558
Harald Welte7984d5c2009-08-12 22:56:50 +0200559 gh->proto_discr = GSM48_PDISC_MM;
560 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
561
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100562 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200563
Harald Welte7984d5c2009-08-12 22:56:50 +0200564 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100565 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200566 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200567 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200568
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100569
570 /* 16 bytes AUTN */
571 if (autn)
572 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
573
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800574 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200575}
576
577/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800578int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200579{
580 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800581 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200582}
583
Harald Welte2483f1b2016-06-19 18:06:02 +0200584static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
585static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
586
587static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100588{
Harald Welte2483f1b2016-06-19 18:06:02 +0200589 uint8_t mi_type;
590 char mi_string[GSM48_MI_SIZE];
591 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100592
Harald Welte2483f1b2016-06-19 18:06:02 +0200593 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
594 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100595
Harald Welte2483f1b2016-06-19 18:06:02 +0200596 switch (mi_type) {
597 case GSM_MI_TYPE_IMSI:
598 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
599 goto accept_reuse;
600 break;
601 case GSM_MI_TYPE_TMSI:
602 tmsi = osmo_load32be(mi_lv+2);
603 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
604 goto accept_reuse;
605 break;
606 case GSM_MI_TYPE_IMEI:
607 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
608 goto accept_reuse;
609 break;
610 default:
611 break;
612 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100613
Harald Welte2483f1b2016-06-19 18:06:02 +0200614 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
615 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
616 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
617 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100618
Harald Welte2483f1b2016-06-19 18:06:02 +0200619accept_reuse:
620 DEBUGP(DMM, "%s: re-using already accepted connection\n",
621 vlr_subscr_name(conn->vsub));
622 conn->received_cm_service_request = true;
623 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100624}
625
Harald Welte4ed0e922009-01-10 03:17:30 +0000626/*
627 * Handle CM Service Requests
628 * a) Verify that the packet is long enough to contain the information
629 * we require otherwsie reject with INCORRECT_MESSAGE
630 * b) Try to parse the TMSI. If we do not have one reject
631 * c) Check that we know the subscriber with the TMSI otherwise reject
632 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200633 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200634 *
635 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000636 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200637int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000638{
Harald Welte2483f1b2016-06-19 18:06:02 +0200639 static const enum subscr_conn_from conn_from_cm_service_req =
640 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
641 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200642 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200643 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000644
Harald Welteba4cf162009-01-10 01:49:35 +0000645 struct gsm48_hdr *gh = msgb_l3(msg);
646 struct gsm48_service_request *req =
647 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800648 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200649 uint8_t classmark2_len = gh->data[1];
650 uint8_t *classmark2 = gh->data+2;
651 uint8_t mi_len = *(classmark2 + classmark2_len);
652 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200653 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200654 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 int rc;
656
657 lai.plmn.mcc = conn->network->country_code;
658 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200659 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000660
Harald Weltec9e02182009-05-01 19:07:53 +0000661 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000662 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000663 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200664 return msc_gsm48_tx_mm_serv_rej(conn,
665 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000666 }
667
668 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000669 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200670 return msc_gsm48_tx_mm_serv_rej(conn,
671 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000672 }
673
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200674 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000675 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200676
677 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200678 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
679 req->cm_service_type, gsm48_mi_type_name(mi_type),
680 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200681 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200682 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
683 req->cm_service_type, gsm48_mi_type_name(mi_type),
684 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200685 } else {
686 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200687 return msc_gsm48_tx_mm_serv_rej(conn,
688 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000689 }
690
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200691 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200692 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
693 conn->classmark.classmark2_len = classmark2_len;
694
695 if (conn->conn_fsm) {
696 if (msc_subscr_conn_is_accepted(conn))
697 return cm_serv_reuse_conn(conn, mi-1);
698 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
699 vlr_subscr_name(conn->vsub));
700 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
701 return -EINVAL;
702 }
703
704 rc = msc_create_conn_fsm(conn, mi_string);
705 if (rc) {
706 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
707 /* logging already happened in msc_create_conn_fsm() */
708 return rc;
709 }
Harald Welte7659de12009-12-13 12:39:18 +0100710
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200711 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200712 vlr_proc_acc_req(conn->conn_fsm,
713 SUBSCR_CONN_E_ACCEPTED,
714 SUBSCR_CONN_E_CN_CLOSE,
715 (void*)&conn_from_cm_service_req,
716 net->vlr, conn,
717 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200718 is_utran || conn->network->authentication_required,
719 is_utran? VLR_CIPH_A5_3
720 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200721 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200722 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000723
Harald Welte2483f1b2016-06-19 18:06:02 +0200724 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000725}
726
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200727static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000728{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200729 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000730 struct gsm48_hdr *gh = msgb_l3(msg);
731 struct gsm48_imsi_detach_ind *idi =
732 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200733 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200734 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200735 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000736
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200737 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200738 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
739 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000740
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200741 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100742
Harald Welte2a139372009-02-22 21:14:55 +0000743 switch (mi_type) {
744 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200745 vsub = vlr_subscr_find_by_tmsi(network->vlr,
746 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000747 break;
748 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200749 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000750 break;
751 case GSM_MI_TYPE_IMEI:
752 case GSM_MI_TYPE_IMEISV:
753 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200754 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
755 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000756 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200757 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200758 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
759 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000760 break;
761 }
762
Harald Welte2483f1b2016-06-19 18:06:02 +0200763 /* TODO? We used to remember the subscriber's classmark1 here and
764 * stored it in the old sqlite db, but now we store it in a conn that
765 * will be discarded anyway: */
766 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200767
Harald Welte2483f1b2016-06-19 18:06:02 +0200768 if (!vsub) {
769 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
770 gsm48_mi_type_name(mi_type), mi_string);
771 } else {
772 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100773
774 if (vsub->cs.is_paging)
775 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
776
Harald Welte2483f1b2016-06-19 18:06:02 +0200777 vlr_subscr_rx_imsi_detach(vsub);
778 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
779 vlr_subscr_put(vsub);
780 }
Harald Welte2a139372009-02-22 21:14:55 +0000781
Harald Welte2483f1b2016-06-19 18:06:02 +0200782 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000783 return 0;
784}
785
Harald Welted2a7f5a2009-06-05 20:08:20 +0000786static int gsm48_rx_mm_status(struct msgb *msg)
787{
788 struct gsm48_hdr *gh = msgb_l3(msg);
789
790 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
791
792 return 0;
793}
794
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100795static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
796 struct gsm_subscriber_connection *conn,
797 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100798{
799 struct gsm48_hdr *gh = msgb_l3(msg);
800 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100801
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100802 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
803 LOGP(DMM, LOGL_ERROR,
804 "%s: MM AUTHENTICATION RESPONSE:"
805 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200806 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100807 return -EINVAL;
808 }
809
810 *res_len = sizeof(ar->sres);
811 memcpy(res, ar->sres, sizeof(ar->sres));
812 return 0;
813}
814
815static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
816 struct gsm_subscriber_connection *conn,
817 struct msgb *msg)
818{
819 struct gsm48_hdr *gh;
820 uint8_t *data;
821 uint8_t iei;
822 uint8_t ie_len;
823 unsigned int data_len;
824
825 /* First parse the GSM part */
826 if (parse_gsm_auth_resp(res, res_len, conn, msg))
827 return -EINVAL;
828 OSMO_ASSERT(*res_len == 4);
829
830 /* Then add the extended res part */
831 gh = msgb_l3(msg);
832 data = gh->data + sizeof(struct gsm48_auth_resp);
833 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
834
835 if (data_len < 3) {
836 LOGP(DMM, LOGL_ERROR,
837 "%s: MM AUTHENTICATION RESPONSE:"
838 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200839 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100840 return -EINVAL;
841 }
842
843 iei = data[0];
844 ie_len = data[1];
845 if (iei != GSM48_IE_AUTH_RES_EXT) {
846 LOGP(DMM, LOGL_ERROR,
847 "%s: MM R99 AUTHENTICATION RESPONSE:"
848 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200849 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100850 GSM48_IE_AUTH_RES_EXT, iei);
851 return -EINVAL;
852 }
853
854 if (ie_len > 12) {
855 LOGP(DMM, LOGL_ERROR,
856 "%s: MM R99 AUTHENTICATION RESPONSE:"
857 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200858 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100859 return -EINVAL;
860 }
861
862 *res_len += ie_len;
863 memcpy(res + 4, &data[2], ie_len);
864 return 0;
865}
866
867/* Chapter 9.2.3: Authentication Response */
868static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
869{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100870 uint8_t res[16];
871 uint8_t res_len;
872 int rc;
873 bool is_r99;
874
Harald Welte2483f1b2016-06-19 18:06:02 +0200875 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100876 LOGP(DMM, LOGL_ERROR,
877 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200878 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100879 return -EINVAL;
880 }
881
882 if (msgb_l3len(msg) >
883 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
884 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
885 is_r99 = true;
886 } else {
887 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
888 is_r99 = false;
889 }
890
891 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200892 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100893 return -EINVAL;
894 }
895
896 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200897 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100898 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
899 osmo_hexdump_nospc(res, res_len));
900
Harald Welte2483f1b2016-06-19 18:06:02 +0200901 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
902 conn->via_ran == RAN_UTRAN_IU,
903 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100904}
905
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100906static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
907{
908 struct gsm48_hdr *gh = msgb_l3(msg);
909 uint8_t cause;
910 uint8_t auts_tag;
911 uint8_t auts_len;
912 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100913
Harald Welte2483f1b2016-06-19 18:06:02 +0200914 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100915 LOGP(DMM, LOGL_ERROR,
916 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200917 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100918 return -EINVAL;
919 }
920
921 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
922 LOGP(DMM, LOGL_ERROR,
923 "%s: MM R99 AUTHENTICATION FAILURE:"
924 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200925 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
926 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100927 return -EINVAL;
928 }
929
930 cause = gh->data[0];
931
932 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
933 LOGP(DMM, LOGL_INFO,
934 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200935 vlr_subscr_name(conn->vsub), cause);
936 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
937 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100938 }
939
940 /* This is a Synch Failure procedure, which should pass an AUTS to
941 * resynchronize the sequence nr with the HLR. Expecting exactly one
942 * TLV with 14 bytes of AUTS. */
943
944 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
945 LOGP(DMM, LOGL_INFO,
946 "%s: MM R99 AUTHENTICATION FAILURE:"
947 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200948 vlr_subscr_name(conn->vsub));
949 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100950 return -EINVAL;
951 }
952
953 auts_tag = gh->data[1];
954 auts_len = gh->data[2];
955 auts = &gh->data[3];
956
957 if (auts_tag != GSM48_IE_AUTS
958 || auts_len != 14) {
959 LOGP(DMM, LOGL_INFO,
960 "%s: MM R99 AUTHENTICATION FAILURE:"
961 " invalid Synch Failure:"
962 " expected AUTS IE 0x%02x of 14 bytes,"
963 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200964 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200966 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100967 return -EINVAL;
968 }
969
970 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
971 LOGP(DMM, LOGL_INFO,
972 "%s: MM R99 AUTHENTICATION FAILURE:"
973 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200974 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
975 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100976 return -EINVAL;
977 }
978
979 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
980 * large enough. */
981
982 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200983 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100984
Harald Welte2483f1b2016-06-19 18:06:02 +0200985 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
986}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100987
Harald Welte2483f1b2016-06-19 18:06:02 +0200988static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
989{
990 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
991 vlr_subscr_name(conn->vsub));
992 if (!conn->vsub) {
993 LOGP(DMM, LOGL_ERROR,
994 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
995 return -EINVAL;
996 }
997 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100998}
999
Harald Weltebf5e8df2009-02-03 12:59:45 +00001000/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001001static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001002{
1003 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001004 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001005
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001006 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001007 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001008 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001009 break;
1010 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001011 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001012 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001013 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001014 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001015 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001016 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001017 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001018 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001019 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001020 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001021 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001022 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001023 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001024 break;
1025 case GSM48_MT_MM_CM_REEST_REQ:
1026 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1027 break;
1028 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001029 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001030 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001031 case GSM48_MT_MM_AUTH_FAIL:
1032 rc = gsm48_rx_mm_auth_fail(conn, msg);
1033 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001034 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001035 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001036 gh->msg_type);
1037 break;
1038 }
1039
1040 return rc;
1041}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001042
Harald Welte2483f1b2016-06-19 18:06:02 +02001043static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1044{
1045 /* Check the size for the classmark */
1046 if (tot_len < 1 + *classmark2_lv)
1047 return NULL;
1048
1049 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1050 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1051 return NULL;
1052
1053 return mi_lv;
1054}
1055
Harald Welte2d35ae62009-02-06 12:02:13 +00001056/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001057static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001058{
Harald Welte2483f1b2016-06-19 18:06:02 +02001059 static const enum subscr_conn_from conn_from_paging_resp =
1060 SUBSCR_CONN_FROM_PAGING_RESP;
1061 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001062 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001063 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001064 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001065 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001066 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001067 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001068 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001069 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001070 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001071
1072 lai.plmn.mcc = conn->network->country_code;
1073 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001074 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001075
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001076 resp = (struct gsm48_pag_resp *) &gh->data[0];
1077 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1078 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001079 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1080 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001081
Harald Welte2483f1b2016-06-19 18:06:02 +02001082 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1083 if (!mi_lv) {
1084 /* FIXME */
1085 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001086 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001087
Harald Welte2483f1b2016-06-19 18:06:02 +02001088 rc = msc_create_conn_fsm(conn, mi_string);
1089 if (rc)
1090 /* logging already happened in msc_create_conn_fsm() */
1091 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001092
Harald Welte2483f1b2016-06-19 18:06:02 +02001093 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1094 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001095
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001096 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001097 vlr_proc_acc_req(conn->conn_fsm,
1098 SUBSCR_CONN_E_ACCEPTED,
1099 SUBSCR_CONN_E_CN_CLOSE,
1100 (void*)&conn_from_paging_resp,
1101 net->vlr, conn,
1102 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001103 is_utran || conn->network->authentication_required,
1104 is_utran? VLR_CIPH_A5_3
1105 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +02001106 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001107 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001108
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001109 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001110}
1111
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001112static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001113{
1114 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001115 uint8_t apdu_id_flags;
1116 uint8_t apdu_len;
1117 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001118
1119 apdu_id_flags = gh->data[0];
1120 apdu_len = gh->data[1];
1121 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001122
Max8dc8f232017-02-09 19:13:02 +01001123 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001124 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001125
Harald Welte2483f1b2016-06-19 18:06:02 +02001126 /* we're not using the app info blob anywhere, so ignore. */
1127#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001128 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001129#else
1130 return 0;
1131#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001132}
1133
Harald Weltebf5e8df2009-02-03 12:59:45 +00001134/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001135static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001136{
1137 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001138 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001139
1140 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001141 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001142 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001143 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001144 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001146 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001147 default:
Philippd37981e2016-10-10 15:11:41 +02001148 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1149 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001150 break;
1151 }
1152
Harald Welte2d35ae62009-02-06 12:02:13 +00001153 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001154}
1155
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001156int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1157 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001158{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001159 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001160 struct gsm48_hdr *gh;
1161
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001162 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1163 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001164
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001165 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1166 gh->proto_discr = GSM48_PDISC_RR;
1167 gh->msg_type = GSM48_MT_RR_APP_INFO;
1168 gh->data[0] = apdu_id;
1169 gh->data[1] = apdu_len;
1170 memcpy(gh->data+2, apdu, apdu_len);
1171
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001172 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001173}
1174
Alexander Couzens92f552f2016-08-23 07:32:27 +02001175/* FIXME: this count_statistics is a state machine behaviour. we should convert
1176 * the complete call control into a state machine. Afterwards we can move this
1177 * code into state transitions.
1178 */
1179static void count_statistics(struct gsm_trans *trans, int new_state)
1180{
1181 int old_state = trans->cc.state;
1182 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1183
1184 if (old_state == new_state)
1185 return;
1186
1187 /* state incoming */
1188 switch (new_state) {
1189 case GSM_CSTATE_ACTIVE:
1190 osmo_counter_inc(trans->net->active_calls);
1191 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1192 break;
1193 }
1194
1195 /* state outgoing */
1196 switch (old_state) {
1197 case GSM_CSTATE_ACTIVE:
1198 osmo_counter_dec(trans->net->active_calls);
1199 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1200 new_state == GSM_CSTATE_DISCONNECT_IND)
1201 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1202 else
1203 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1204 break;
1205 }
1206}
1207
Harald Welte4bc90a12008-12-27 16:32:52 +00001208/* Call Control */
1209
Harald Welte7584aea2009-02-11 11:44:12 +00001210/* The entire call control code is written in accordance with Figure 7.10c
1211 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1212 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1213 * it for voice */
1214
Harald Welte4bfdfe72009-06-10 23:11:52 +08001215static void new_cc_state(struct gsm_trans *trans, int state)
1216{
1217 if (state > 31 || state < 0)
1218 return;
1219
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001220 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1221 trans->transaction_id,
1222 vlr_subscr_name(trans->vsub),
1223 gsm48_cc_state_name(trans->cc.state),
1224 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001225
Alexander Couzens92f552f2016-08-23 07:32:27 +02001226 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001227 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001228}
1229
1230static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001231{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001232 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001233 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001234 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001235
Harald Welte4bc90a12008-12-27 16:32:52 +00001236 gh->msg_type = GSM48_MT_CC_STATUS;
1237
1238 cause = msgb_put(msg, 3);
1239 cause[0] = 2;
1240 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1241 cause[2] = 0x80 | 30; /* response to status inquiry */
1242
1243 call_state = msgb_put(msg, 1);
1244 call_state[0] = 0xc0 | 0x00;
1245
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001246 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001247}
1248
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001249static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001250 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001251{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001252 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001253 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1254
Harald Welte6f4b7532008-12-29 00:39:37 +00001255 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001256 gh->msg_type = msg_type;
1257
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001258 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001259}
1260
Harald Welte4bfdfe72009-06-10 23:11:52 +08001261static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1262{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001263 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001264 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001265 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001266 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267 }
1268}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001269
Harald Welte4bfdfe72009-06-10 23:11:52 +08001270static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1271 int msg_type, struct gsm_mncc *mncc)
1272{
1273 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001274 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275
Philipp Maierfbf66102017-04-09 12:32:51 +02001276 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1277
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001278#if BEFORE_MSCSPLIT
1279 /* Re-enable this log output once we can obtain this information via
1280 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001281 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001282 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001283 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001284 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001285 trans->conn->lchan->ts->trx->bts->nr,
1286 trans->conn->lchan->ts->trx->nr,
1287 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001288 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289 get_mncc_name(msg_type));
1290 else
1291 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1292 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001293 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294 get_mncc_name(msg_type));
1295 else
1296 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1297 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001298#else
1299 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1300#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301
1302 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001303
Harald Welte966636f2009-06-26 19:39:35 +02001304 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305 if (!msg)
1306 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001307
1308 data = msgb_put(msg, sizeof(struct gsm_mncc));
1309 memcpy(data, mncc, sizeof(struct gsm_mncc));
1310
Harald Welte54209c22010-12-22 23:43:29 +01001311 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001312
1313 return 0;
1314}
1315
1316int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001317 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318{
1319 struct gsm_mncc rel;
1320
Harald Welte92f70c52009-06-12 01:54:08 +08001321 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001323 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001324 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1325 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1327}
1328
Harald Weltedcaf5652009-07-23 18:56:43 +02001329/* Call Control Specific transaction release.
1330 * gets called by trans_free, DO NOT CALL YOURSELF! */
1331void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333 gsm48_stop_cc_timer(trans);
1334
Philipp Maierfbf66102017-04-09 12:32:51 +02001335 /* Make sure call also gets released on the mgcp side */
1336 msc_call_release(trans);
1337
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 /* send release to L4, if callref still exists */
1339 if (trans->callref) {
1340 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001341 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001342 GSM48_CAUSE_LOC_PRN_S_LU,
1343 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001344 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001345 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347}
1348
1349static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001350
Harald Welte09e38af2009-02-16 22:52:23 +00001351/* call-back from paging the B-end of the connection */
1352static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001353 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001354{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001355 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001356 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001357
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001358 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001359
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001360 switch (event) {
1361 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001362 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1363 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001364 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001365 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001366 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001367 /* send SETUP request to called party */
1368 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1369 break;
1370 case GSM_PAGING_EXPIRED:
1371 case GSM_PAGING_BUSY:
1372 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001373 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001374 /* Temporarily out of order */
1375 mncc_release_ind(transt->net, transt,
1376 transt->callref,
1377 GSM48_CAUSE_LOC_PRN_S_LU,
1378 GSM48_CC_CAUSE_DEST_OOO);
1379 transt->callref = 0;
1380 transt->paging_request = NULL;
1381 trans_free(transt);
1382 break;
1383 default:
1384 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1385 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001387
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001388 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001389 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001390}
Harald Welte7584aea2009-02-11 11:44:12 +00001391
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001393static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001394{
Harald Welte53d51f52015-12-03 14:59:04 +01001395 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1396 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001397
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001399 return -EIO;
1400
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001401 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 return -EIO;
1403
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001404 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001405 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001406
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001407 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001408}
1409
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1411{
1412 DEBUGP(DCC, "-> STATUS ENQ\n");
1413 return gsm48_cc_tx_status(trans, msg);
1414}
1415
1416static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1417static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1418
1419static void gsm48_cc_timeout(void *arg)
1420{
1421 struct gsm_trans *trans = arg;
1422 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001423 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1424 int mo_location = GSM48_CAUSE_LOC_USER;
1425 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1426 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 struct gsm_mncc mo_rel, l4_rel;
1428
1429 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1430 mo_rel.callref = trans->callref;
1431 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1432 l4_rel.callref = trans->callref;
1433
Harald Weltedcaf5652009-07-23 18:56:43 +02001434 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435 case 0x303:
1436 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001437 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438 break;
1439 case 0x310:
1440 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001441 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001442 break;
1443 case 0x313:
1444 disconnect = 1;
1445 /* unknown, did not find it in the specs */
1446 break;
1447 case 0x301:
1448 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001449 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001450 break;
1451 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001452 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001454 gsm48_cc_tx_release(trans, &trans->cc.msg);
1455 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001456 break; /* stay in release state */
1457 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001458 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001459 return;
1460// release = 1;
1461// l4_cause = 14;
1462// break;
1463 case 0x306:
1464 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001465 mo_cause = trans->cc.msg.cause.value;
1466 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467 break;
1468 case 0x323:
1469 disconnect = 1;
1470 break;
1471 default:
1472 release = 1;
1473 }
1474
1475 if (release && trans->callref) {
1476 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001477 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 l4_location, l4_cause);
1479 trans->callref = 0;
1480 }
1481
1482 if (disconnect && trans->callref) {
1483 /* process disconnect towards layer 4 */
1484 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001485 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 }
1487
1488 /* process disconnect towards mobile station */
1489 if (disconnect || release) {
1490 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001491 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1492 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1493 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001494 mo_rel.cause.diag_len = 3;
1495
1496 if (disconnect)
1497 gsm48_cc_tx_disconnect(trans, &mo_rel);
1498 if (release)
1499 gsm48_cc_tx_release(trans, &mo_rel);
1500 }
1501
1502}
1503
Max3ffce192016-04-25 15:22:00 +02001504/* disconnect both calls from the bridge */
1505static inline void disconnect_bridge(struct gsm_network *net,
1506 struct gsm_mncc_bridge *bridge, int err)
1507{
1508 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1509 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1510 struct gsm_mncc mx_rel;
1511 if (!trans0 || !trans1)
1512 return;
1513
1514 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1515 trans0->callref, trans1->callref, strerror(err));
1516
1517 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1518 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1519 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1520
1521 mx_rel.callref = trans0->callref;
1522 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1523
1524 mx_rel.callref = trans1->callref;
1525 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1526}
1527
Harald Welte4bfdfe72009-06-10 23:11:52 +08001528static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1529 int sec, int micro)
1530{
1531 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001532 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001533 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001534 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001535}
1536
1537static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1538{
1539 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001540 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1542 struct tlv_parsed tp;
1543 struct gsm_mncc setup;
1544
1545 memset(&setup, 0, sizeof(struct gsm_mncc));
1546 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001547
Harald Welte474d19f2010-03-02 23:18:30 +01001548 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 /* emergency setup is identified by msg_type */
1550 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1551 setup.emergency = 1;
1552
1553 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001554 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001555 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1556 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001557
Harald Welte4bfdfe72009-06-10 23:11:52 +08001558 /* bearer capability */
1559 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1560 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001561 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001562 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001563
1564 /* Create a copy of the bearer capability
1565 * in the transaction struct, so we can use
1566 * this information later */
1567 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1568 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 }
1570 /* facility */
1571 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1572 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001573 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1575 }
1576 /* called party bcd number */
1577 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1578 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001579 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1581 }
1582 /* user-user */
1583 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1584 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001585 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1587 }
1588 /* ss-version */
1589 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1590 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001591 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1593 }
1594 /* CLIR suppression */
1595 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1596 setup.clir.sup = 1;
1597 /* CLIR invocation */
1598 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1599 setup.clir.inv = 1;
1600 /* cc cap */
1601 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1602 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001603 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1605 }
1606
Harald Welte4bfdfe72009-06-10 23:11:52 +08001607 new_cc_state(trans, GSM_CSTATE_INITIATED);
1608
Harald Welte (local)47df3992009-12-26 19:45:03 +01001609 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001610 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001611 setup.called.number);
1612
Alexander Couzensb847a212016-08-02 11:34:11 +02001613 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001614
Harald Welte4bfdfe72009-06-10 23:11:52 +08001615 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001616 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617
Harald Welte13cac662009-07-29 12:10:35 +02001618 /* MNCC code will modify the channel asynchronously, we should
1619 * ipaccess-bind only after the modification has been made to the
1620 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621 return 0;
1622}
1623
1624static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001625{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001626 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001627 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001628 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001629 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001630
Harald Welte7ccf7782009-02-17 01:43:01 +00001631 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001632
Harald Welte4bfdfe72009-06-10 23:11:52 +08001633 /* transaction id must not be assigned */
1634 if (trans->transaction_id != 0xff) { /* unasssigned */
1635 DEBUGP(DCC, "TX Setup with assigned transaction. "
1636 "This is not allowed!\n");
1637 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001638 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001639 GSM48_CAUSE_LOC_PRN_S_LU,
1640 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001641 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001642 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 return rc;
1644 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001645
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001647 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001648 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001649 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001651 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001652 GSM48_CAUSE_LOC_PRN_S_LU,
1653 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001655 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656 return rc;
1657 }
Harald Welte78283ef2009-07-23 21:36:44 +02001658 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001659
Harald Welte65e74cc2008-12-29 01:55:35 +00001660 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001661
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001663
Harald Welte4bfdfe72009-06-10 23:11:52 +08001664 /* bearer capability */
1665 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001666 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667 /* facility */
1668 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001669 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 /* progress */
1671 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001672 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 /* calling party BCD number */
1674 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001675 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 /* called party BCD number */
1677 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001678 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 /* user-user */
1680 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001681 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 /* redirecting party BCD number */
1683 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001684 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 /* signal */
1686 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001687 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001688
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001690
Alexander Couzensb847a212016-08-02 11:34:11 +02001691 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001692
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001693 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001694}
1695
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1697{
1698 struct gsm48_hdr *gh = msgb_l3(msg);
1699 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1700 struct tlv_parsed tp;
1701 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001702 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703
1704 gsm48_stop_cc_timer(trans);
1705 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1706
1707 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1708 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001709
Harald Welte474d19f2010-03-02 23:18:30 +01001710 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711#if 0
1712 /* repeat */
1713 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1714 call_conf.repeat = 1;
1715 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1716 call_conf.repeat = 2;
1717#endif
1718 /* bearer capability */
1719 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1720 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001721 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001723
1724 /* Create a copy of the bearer capability
1725 * in the transaction struct, so we can use
1726 * this information later */
1727 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1728 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 }
1730 /* cause */
1731 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1732 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001733 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1735 }
1736 /* cc cap */
1737 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1738 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001739 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1741 }
1742
Andreas Eversberg035b8742013-09-19 09:27:06 +02001743 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001744 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001745
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1747
Philipp Maierfbf66102017-04-09 12:32:51 +02001748 /* Assign call (if not done yet) */
1749 if (trans->assignment_done == false) {
1750 rc = msc_call_assignment(trans);
1751 trans->assignment_done = true;
1752 }
1753 else
1754 rc = 0;
1755
1756 /* don't continue, if there were problems with
1757 * the call assignment. */
1758 if (rc)
1759 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001760
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001761 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001762 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763}
1764
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001765static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766{
1767 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001768 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001769 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001770 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1773
1774 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1775
1776 /* bearer capability */
1777 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001778 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* facility */
1780 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001781 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* progress */
1783 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001784 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001786 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1787 if (rc)
1788 return rc;
1789
Philipp Maierfbf66102017-04-09 12:32:51 +02001790 /* Assign call (if not done yet) */
1791 if (trans->assignment_done == false) {
1792 rc = msc_call_assignment(trans);
1793 trans->assignment_done = true;
1794 }
1795 else
1796 rc = 0;
1797
1798 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799}
1800
1801static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1802{
1803 struct gsm48_hdr *gh = msgb_l3(msg);
1804 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1805 struct tlv_parsed tp;
1806 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001807
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 gsm48_stop_cc_timer(trans);
1809 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1810
1811 memset(&alerting, 0, sizeof(struct gsm_mncc));
1812 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001813 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 /* facility */
1815 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1816 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001817 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1819 }
1820
1821 /* progress */
1822 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1823 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001824 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1826 }
1827 /* ss-version */
1828 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1829 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001830 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1832 }
1833
1834 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1835
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001836 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001837 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838}
1839
1840static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1841{
1842 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001843 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1845
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 gh->msg_type = GSM48_MT_CC_ALERTING;
1847
1848 /* facility */
1849 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001850 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 /* progress */
1852 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 /* user-user */
1855 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001856 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857
1858 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001859
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001860 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861}
1862
1863static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1864{
1865 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001866 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1868
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 gh->msg_type = GSM48_MT_CC_PROGRESS;
1870
1871 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001872 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 /* user-user */
1874 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001875 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001877 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878}
1879
1880static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1881{
1882 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001883 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1885
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 gh->msg_type = GSM48_MT_CC_CONNECT;
1887
1888 gsm48_stop_cc_timer(trans);
1889 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1890
1891 /* facility */
1892 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001893 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 /* progress */
1895 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001896 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 /* connected number */
1898 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001899 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 /* user-user */
1901 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
1904 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1905
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001906 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907}
1908
1909static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1910{
1911 struct gsm48_hdr *gh = msgb_l3(msg);
1912 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1913 struct tlv_parsed tp;
1914 struct gsm_mncc connect;
1915
1916 gsm48_stop_cc_timer(trans);
1917
1918 memset(&connect, 0, sizeof(struct gsm_mncc));
1919 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001920 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001922 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001923 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1924 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001925
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926 /* facility */
1927 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1928 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1931 }
1932 /* user-user */
1933 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1934 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001935 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1937 }
1938 /* ss-version */
1939 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1940 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001941 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1943 }
1944
1945 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001946 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001948 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949}
1950
1951
1952static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1953{
1954 struct gsm_mncc connect_ack;
1955
1956 gsm48_stop_cc_timer(trans);
1957
1958 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001959 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001960
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1962 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001963
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001964 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 &connect_ack);
1966}
1967
1968static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1969{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001970 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1972
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1974
1975 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1976
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001977 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978}
1979
1980static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1981{
1982 struct gsm48_hdr *gh = msgb_l3(msg);
1983 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1984 struct tlv_parsed tp;
1985 struct gsm_mncc disc;
1986
1987 gsm48_stop_cc_timer(trans);
1988
1989 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1990
1991 memset(&disc, 0, sizeof(struct gsm_mncc));
1992 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001993 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 /* cause */
1995 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1996 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001997 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1999 }
2000 /* facility */
2001 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2002 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2005 }
2006 /* user-user */
2007 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2008 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2011 }
2012 /* ss-version */
2013 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2014 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002015 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2017 }
2018
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002019 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020
2021}
2022
Harald Weltec66b71c2009-06-11 14:23:20 +08002023static struct gsm_mncc_cause default_cause = {
2024 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2025 .coding = 0,
2026 .rec = 0,
2027 .rec_val = 0,
2028 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2029 .diag_len = 0,
2030 .diag = { 0 },
2031};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032
2033static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2034{
2035 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002036 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2038
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2040
2041 gsm48_stop_cc_timer(trans);
2042 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2043
2044 /* cause */
2045 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 else
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049
2050 /* facility */
2051 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002052 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 /* progress */
2054 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 /* user-user */
2057 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002058 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059
2060 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002061 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062
2063 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2064
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002065 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066}
2067
2068static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2069{
2070 struct gsm48_hdr *gh = msgb_l3(msg);
2071 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2072 struct tlv_parsed tp;
2073 struct gsm_mncc rel;
2074 int rc;
2075
2076 gsm48_stop_cc_timer(trans);
2077
2078 memset(&rel, 0, sizeof(struct gsm_mncc));
2079 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002080 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* cause */
2082 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2083 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2086 }
2087 /* facility */
2088 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2089 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2092 }
2093 /* user-user */
2094 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2095 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2098 }
2099 /* ss-version */
2100 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2101 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002102 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2104 }
2105
Harald Weltedcaf5652009-07-23 18:56:43 +02002106 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002108 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002110 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002111 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002112 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002113 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 }
2115
2116 new_cc_state(trans, GSM_CSTATE_NULL);
2117
2118 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002119 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120
2121 return rc;
2122}
2123
2124static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2125{
2126 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002127 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2129
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 gh->msg_type = GSM48_MT_CC_RELEASE;
2131
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132 gsm48_stop_cc_timer(trans);
2133 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2134
2135 /* cause */
2136 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* facility */
2139 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 /* user-user */
2142 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144
Harald Weltedcaf5652009-07-23 18:56:43 +02002145 trans->cc.T308_second = 0;
2146 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
Harald Weltedcaf5652009-07-23 18:56:43 +02002148 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2150
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002151 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152}
2153
2154static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2155{
2156 struct gsm48_hdr *gh = msgb_l3(msg);
2157 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2158 struct tlv_parsed tp;
2159 struct gsm_mncc rel;
2160 int rc = 0;
2161
2162 gsm48_stop_cc_timer(trans);
2163
2164 memset(&rel, 0, sizeof(struct gsm_mncc));
2165 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002166 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 /* cause */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2169 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2172 }
2173 /* facility */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2175 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002176 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2178 }
2179 /* user-user */
2180 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2181 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002182 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2184 }
2185 /* ss-version */
2186 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2187 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002188 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002189 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2190 }
2191
2192 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002193 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002195 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 MNCC_REJ_IND, &rel);
2197 break;
2198 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002199 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 MNCC_REL_CNF, &rel);
2201 break;
2202 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002203 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 MNCC_REL_IND, &rel);
2205 }
2206 }
2207
2208 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002209 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210
2211 return rc;
2212}
2213
2214static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2215{
2216 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002217 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002219 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2222
2223 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002224
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 gsm48_stop_cc_timer(trans);
2226
2227 /* cause */
2228 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002229 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 /* facility */
2231 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002232 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 /* user-user */
2234 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002235 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236
Harald Weltec7782de2010-12-21 19:31:41 +01002237 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2238
Harald Weltedcaf5652009-07-23 18:56:43 +02002239 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240
Harald Weltec7782de2010-12-21 19:31:41 +01002241 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242}
2243
2244static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2245{
2246 struct gsm48_hdr *gh = msgb_l3(msg);
2247 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2248 struct tlv_parsed tp;
2249 struct gsm_mncc fac;
2250
2251 memset(&fac, 0, sizeof(struct gsm_mncc));
2252 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002253 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 /* facility */
2255 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2256 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2259 }
2260 /* ss-version */
2261 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2262 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002263 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2265 }
2266
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002267 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268}
2269
2270static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2271{
2272 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002273 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2275
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 gh->msg_type = GSM48_MT_CC_FACILITY;
2277
2278 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002279 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002281 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002282}
2283
2284static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2285{
2286 struct gsm_mncc hold;
2287
2288 memset(&hold, 0, sizeof(struct gsm_mncc));
2289 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002290 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291}
2292
2293static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2294{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002295 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2297
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2299
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002300 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301}
2302
2303static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2304{
2305 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002306 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2308
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2310
2311 /* cause */
2312 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002313 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 else
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002317 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318}
2319
2320static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2321{
2322 struct gsm_mncc retrieve;
2323
2324 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2325 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002326 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002327 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2331{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002332 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2334
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2336
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002337 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338}
2339
2340static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2341{
2342 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002343 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2345
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2347
2348 /* cause */
2349 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002350 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 else
Harald Welte55c8f352010-03-07 23:40:35 +01002352 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002354 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355}
2356
2357static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2358{
2359 struct gsm48_hdr *gh = msgb_l3(msg);
2360 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2361 struct tlv_parsed tp;
2362 struct gsm_mncc dtmf;
2363
2364 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2365 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002366 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 /* keypad facility */
2368 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2369 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002370 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2372 }
2373
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002374 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375}
2376
2377static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2378{
2379 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002380 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2382
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2384
2385 /* keypad */
2386 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002389 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390}
2391
2392static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2393{
2394 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002395 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2397
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2399
2400 /* cause */
2401 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002402 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 else
Harald Welte55c8f352010-03-07 23:40:35 +01002404 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002406 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2410{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002411 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2413
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2415
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002416 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417}
2418
2419static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2420{
2421 struct gsm_mncc dtmf;
2422
2423 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2424 dtmf.callref = trans->callref;
2425
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002426 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2430{
2431 struct gsm48_hdr *gh = msgb_l3(msg);
2432 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2433 struct tlv_parsed tp;
2434 struct gsm_mncc modify;
2435
2436 memset(&modify, 0, sizeof(struct gsm_mncc));
2437 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002438 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 /* bearer capability */
2440 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2441 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002444
2445 /* Create a copy of the bearer capability
2446 * in the transaction struct, so we can use
2447 * this information later */
2448 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2449 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 }
2451
2452 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2453
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002454 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455}
2456
2457static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2458{
2459 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002460 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2462
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 gh->msg_type = GSM48_MT_CC_MODIFY;
2464
2465 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2466
2467 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002468 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469
2470 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2471
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002472 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473}
2474
2475static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2476{
2477 struct gsm48_hdr *gh = msgb_l3(msg);
2478 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2479 struct tlv_parsed tp;
2480 struct gsm_mncc modify;
2481
2482 gsm48_stop_cc_timer(trans);
2483
2484 memset(&modify, 0, sizeof(struct gsm_mncc));
2485 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002486 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 /* bearer capability */
2488 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2489 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002490 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002492
2493 /* Create a copy of the bearer capability
2494 * in the transaction struct, so we can use
2495 * this information later */
2496 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2497 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498 }
2499
2500 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2501
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002502 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503}
2504
2505static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2506{
2507 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002508 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
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_MODIFY_COMPL;
2512
2513 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002514 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
2516 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2517
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002518 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2522{
2523 struct gsm48_hdr *gh = msgb_l3(msg);
2524 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2525 struct tlv_parsed tp;
2526 struct gsm_mncc modify;
2527
2528 gsm48_stop_cc_timer(trans);
2529
2530 memset(&modify, 0, sizeof(struct gsm_mncc));
2531 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002532 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 /* bearer capability */
2534 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2535 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002536 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002538
2539 /* Create a copy of the bearer capability
2540 * in the transaction struct, so we can use
2541 * this information later */
2542 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2543 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544 }
2545 /* cause */
2546 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2547 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002548 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2550 }
2551
2552 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2553
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002554 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555}
2556
2557static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2558{
2559 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002560 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2562
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2564
2565 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002568 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569
2570 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2571
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002572 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573}
2574
2575static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2576{
2577 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002578 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2580
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581 gh->msg_type = GSM48_MT_CC_NOTIFY;
2582
2583 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002584 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002586 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587}
2588
2589static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2590{
2591 struct gsm48_hdr *gh = msgb_l3(msg);
2592 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2593// struct tlv_parsed tp;
2594 struct gsm_mncc notify;
2595
2596 memset(&notify, 0, sizeof(struct gsm_mncc));
2597 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002598// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002602 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2606{
2607 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002608 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2610
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 gh->msg_type = GSM48_MT_CC_USER_INFO;
2612
2613 /* user-user */
2614 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002615 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616 /* more data */
2617 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002620 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621}
2622
2623static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2624{
2625 struct gsm48_hdr *gh = msgb_l3(msg);
2626 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2627 struct tlv_parsed tp;
2628 struct gsm_mncc user;
2629
2630 memset(&user, 0, sizeof(struct gsm_mncc));
2631 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002632 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 /* user-user */
2634 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2635 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002636 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2638 }
2639 /* more data */
2640 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2641 user.more = 1;
2642
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002643 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644}
2645
Philipp Maierfbf66102017-04-09 12:32:51 +02002646static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2647 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2648 uint32_t payload_msg_type)
2649{
2650 uint8_t data[sizeof(struct gsm_mncc)];
2651 struct gsm_mncc_rtp *rtp;
2652
2653 memset(&data, 0, sizeof(data));
2654 rtp = (struct gsm_mncc_rtp *) &data[0];
2655
2656 rtp->callref = callref;
2657 rtp->msg_type = cmd;
2658 rtp->ip = addr;
2659 rtp->port = port;
2660 rtp->payload_type = payload_type;
2661 rtp->payload_msg_type = payload_msg_type;
2662 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2663}
2664
2665static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2666{
2667 int msg_type;
2668
2669 /* FIXME This has to be set to some meaningful value.
2670 * Possible options are:
2671 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2672 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2673 * (0 if unknown) */
2674 msg_type = GSM_TCHF_FRAME;
2675
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +02002676 uint32_t addr = mgcp_client_remote_addr_n(net->mgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +02002677 uint16_t port = trans->conn->rtp.port_cn;
2678
2679 /* FIXME: This has to be set to some meaningful value,
2680 * before the MSC-Split, this value was pulled from
2681 * lchan->abis_ip.rtp_payload */
2682 uint32_t payload_type = 0;
2683
2684 return mncc_recv_rtp(net, trans->callref, cmd,
2685 addr,
2686 port,
2687 payload_type,
2688 msg_type);
2689}
2690
2691static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2692{
2693 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2694}
2695
2696static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2697{
2698 struct gsm_trans *trans;
2699 int rc;
2700
2701 /* Find callref */
2702 trans = trans_find_by_callref(net, callref);
2703 if (!trans) {
2704 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2705 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2706 return -EIO;
2707 }
2708 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2709 if (!trans->conn) {
2710 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2711 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2712 return 0;
2713 }
2714
2715 trans->conn->mncc_rtp_bridge = 1;
2716
2717 /* When we call msc_call_assignment() we will trigger, depending
2718 * on the RAN type the call assignment on the A or Iu interface.
2719 * msc_call_assignment() also takes care about sending the CRCX
2720 * command to the MGCP-GW. The CRCX will return the port number,
2721 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2722 * have to return this port number back to the MNCC by sending
2723 * it back with the TCH_RTP_CREATE message. To make sure that
2724 * this message is sent AFTER the response to CRCX from the
2725 * MGCP-GW has arrived, we need will instruct msc_call_assignment()
2726 * to take care of this by setting trans->tch_rtp_create to true.
2727 * This will make sure that gsm48_tch_rtp_create() (below) is
2728 * called as soon as the local port number has become known. */
2729 trans->tch_rtp_create = true;
2730
2731 /* Assign call (if not done yet) */
2732 if (trans->assignment_done == false) {
2733 rc = msc_call_assignment(trans);
2734 trans->assignment_done = true;
2735 }
2736 else
2737 rc = 0;
2738
2739 return rc;
2740}
2741
2742/* Trigger TCH_RTP_CREATE acknowledgement */
2743int gsm48_tch_rtp_create(struct gsm_trans *trans)
2744{
2745 /* This function is called as soon as the port, on which the
2746 * mgcp-gw expects the incoming RTP stream from the remote
2747 * end (e.g. Asterisk) is known. */
2748
2749 struct gsm_subscriber_connection *conn = trans->conn;
2750 struct gsm_network *network = conn->network;
2751
2752 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2753 return 0;
2754}
2755
2756static int tch_rtp_connect(struct gsm_network *net, void *arg)
2757{
2758 struct gsm_trans *trans;
2759 struct gsm_mncc_rtp *rtp = arg;
2760
2761 /* Find callref */
2762 trans = trans_find_by_callref(net, rtp->callref);
2763 if (!trans) {
2764 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2765 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2766 return -EIO;
2767 }
2768 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2769 if (!trans->conn) {
2770 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2771 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2772 return 0;
2773 }
2774
Philipp Maier2f108b02017-11-23 13:38:02 +01002775 msc_call_connect(trans, rtp->port, rtp->ip);
Philipp Maierfbf66102017-04-09 12:32:51 +02002776 return 0;
2777}
2778
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002780 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002781 int type;
2782 int (*rout) (struct gsm_trans *trans, void *arg);
2783} downstatelist[] = {
2784 /* mobile originating call establishment */
2785 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002786 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002787 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2788 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2789 {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 */
2790 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2791 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2792 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2793 /* mobile terminating call establishment */
2794 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2795 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2796 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2797 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2798 /* signalling during call */
2799 {SBIT(GSM_CSTATE_ACTIVE),
2800 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2801 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2802 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2803 {ALL_STATES,
2804 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2805 {ALL_STATES,
2806 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2807 {ALL_STATES,
2808 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2809 {SBIT(GSM_CSTATE_ACTIVE),
2810 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2811 {SBIT(GSM_CSTATE_ACTIVE),
2812 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2813 {SBIT(GSM_CSTATE_ACTIVE),
2814 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2815 {SBIT(GSM_CSTATE_ACTIVE),
2816 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2817 {SBIT(GSM_CSTATE_ACTIVE),
2818 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2819 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2820 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2821 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2822 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2823 {SBIT(GSM_CSTATE_ACTIVE),
2824 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2825 /* clearing */
2826 {SBIT(GSM_CSTATE_INITIATED),
2827 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2828 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2829 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2830 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2831 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832};
2833
2834#define DOWNSLLEN \
2835 (sizeof(downstatelist) / sizeof(struct downstate))
2836
2837
Harald Welte76556372010-12-22 23:57:45 +01002838int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839{
Harald Welte1a6f7982009-08-09 18:52:33 +02002840 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002842 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843 struct gsm_mncc *data = arg, rel;
2844
Harald Welte04dc88f2010-12-22 21:45:05 +01002845 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2846
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* handle special messages */
2848 switch(msg_type) {
2849 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002850 rc = tch_bridge(net, arg);
2851 if (rc < 0)
2852 disconnect_bridge(net, arg, -rc);
2853 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002854 case MNCC_RTP_CREATE:
2855 return tch_rtp_create(net, data->callref);
2856 case MNCC_RTP_CONNECT:
2857 return tch_rtp_connect(net, arg);
2858 case MNCC_RTP_FREE:
2859 /* unused right now */
2860 return -EIO;
2861
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002864 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002865 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002866 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002867 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002868 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2869 get_mncc_name(msg_type));
2870 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 }
2872
2873 memset(&rel, 0, sizeof(struct gsm_mncc));
2874 rel.callref = data->callref;
2875
2876 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002877 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878
2879 /* Callref unknown */
2880 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002881 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002882
Harald Welte4a3464c2009-07-04 10:11:24 +02002883 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2885 "Received '%s' from MNCC with "
2886 "unknown callref %d\n", data->called.number,
2887 get_mncc_name(msg_type), data->callref);
2888 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002889 return mncc_release_ind(net, NULL, data->callref,
2890 GSM48_CAUSE_LOC_PRN_S_LU,
2891 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002893 if (!data->called.number[0] && !data->imsi[0]) {
2894 DEBUGP(DCC, "(bts - trx - ts - ti) "
2895 "Received '%s' from MNCC with "
2896 "no number or IMSI\n", get_mncc_name(msg_type));
2897 /* Invalid number */
2898 return mncc_release_ind(net, NULL, data->callref,
2899 GSM48_CAUSE_LOC_PRN_S_LU,
2900 GSM48_CC_CAUSE_INV_NR_FORMAT);
2901 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002903 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002904 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2905 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002906 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002907 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002908
2909 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002910 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002911
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002913 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002914 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2915 "Received '%s' from MNCC with "
2916 "unknown subscriber %s\n", data->called.number,
2917 get_mncc_name(msg_type), data->called.number);
2918 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002919 return mncc_release_ind(net, NULL, data->callref,
2920 GSM48_CAUSE_LOC_PRN_S_LU,
2921 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922 }
2923 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002924 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2926 "Received '%s' from MNCC with "
2927 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01002928 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02002929 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002931 return mncc_release_ind(net, NULL, data->callref,
2932 GSM48_CAUSE_LOC_PRN_S_LU,
2933 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 }
2935 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002936 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002937 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002938 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002939 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002941 mncc_release_ind(net, NULL, data->callref,
2942 GSM48_CAUSE_LOC_PRN_S_LU,
2943 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944 return -ENOMEM;
2945 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002946
2947 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002948 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002949
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002950 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002951 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 /* find transaction with this subscriber already paging */
2953 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002954 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002956 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002958 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 "Received '%s' from MNCC with "
2960 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002961 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02002963 get_mncc_name(msg_type), vsub->lac);
2964 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002965 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966 return 0;
2967 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002968 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002969 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002970
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002971 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002972 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02002973 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02002974 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002975 trans,
2976 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002977 if (!trans->paging_request) {
2978 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002979 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002980 trans_free(trans);
2981 return 0;
2982 }
Harald Welte2483f1b2016-06-19 18:06:02 +02002983 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 return 0;
2985 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002986
2987 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01002988 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02002989 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002990 } else {
2991 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002992 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002994
2995 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002996 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997
2998 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002999 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003000 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003001 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003002 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003004 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3005 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 if (msg_type == MNCC_REL_REQ)
3007 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3008 else
3009 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3010 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003011 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003013 } else {
3014 DEBUGP(DCC, "(ti %02x sub %s) "
3015 "Received '%s' from MNCC in state %d (%s)\n",
3016 trans->transaction_id,
3017 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3018 get_mncc_name(msg_type), trans->cc.state,
3019 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003020 }
3021
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022 /* Find function for current state and message */
3023 for (i = 0; i < DOWNSLLEN; i++)
3024 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003025 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026 break;
3027 if (i == DOWNSLLEN) {
3028 DEBUGP(DCC, "Message unhandled at this state.\n");
3029 return 0;
3030 }
3031
3032 rc = downstatelist[i].rout(trans, arg);
3033
3034 return rc;
3035}
3036
3037
3038static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003039 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 int type;
3041 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3042} datastatelist[] = {
3043 /* mobile originating call establishment */
3044 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3045 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3046 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3047 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3048 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3049 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3050 /* mobile terminating call establishment */
3051 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3052 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3053 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3054 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003055 {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 +08003056 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3057 /* signalling during call */
3058 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3059 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3060 {SBIT(GSM_CSTATE_ACTIVE),
3061 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3062 {ALL_STATES,
3063 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3064 {ALL_STATES,
3065 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3066 {ALL_STATES,
3067 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3068 {SBIT(GSM_CSTATE_ACTIVE),
3069 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3070 {SBIT(GSM_CSTATE_ACTIVE),
3071 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3072 {SBIT(GSM_CSTATE_ACTIVE),
3073 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3074 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3075 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3076 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3077 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3078 {SBIT(GSM_CSTATE_ACTIVE),
3079 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3080 /* clearing */
3081 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3082 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3083 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3084 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3085 {ALL_STATES, /* 5.4.3.4 */
3086 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3087};
3088
3089#define DATASLLEN \
3090 (sizeof(datastatelist) / sizeof(struct datastate))
3091
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003092static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003093{
3094 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003095 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003096 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003097 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003098 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003099
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 if (msg_type & 0x80) {
3101 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3102 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003103 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003104
Harald Welte2483f1b2016-06-19 18:06:02 +02003105 if (!conn->vsub) {
3106 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003107 return -EINVAL;
3108 }
3109
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003111 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003112
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003113#if BEFORE_MSCSPLIT
3114 /* Re-enable this log output once we can obtain this information via
3115 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003116 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003117 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003118 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003119 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003120 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3121 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003122#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123
3124 /* Create transaction */
3125 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003126 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127 "creating new trans.\n", transaction_id);
3128 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003129 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003130 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003131 transaction_id, new_callref++);
3132 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003133 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003134 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003135 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136 GSM48_MT_CC_RELEASE_COMPL);
3137 return -ENOMEM;
3138 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003140 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003141 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142 }
3143
3144 /* find function for current state and message */
3145 for (i = 0; i < DATASLLEN; i++)
3146 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003147 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 break;
3149 if (i == DATASLLEN) {
3150 DEBUGP(DCC, "Message unhandled at this state.\n");
3151 return 0;
3152 }
3153
Harald Welte2483f1b2016-06-19 18:06:02 +02003154 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003155
Harald Welte4bfdfe72009-06-10 23:11:52 +08003156 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003157
Harald Welte2483f1b2016-06-19 18:06:02 +02003158 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003159 return rc;
3160}
3161
Harald Welte2483f1b2016-06-19 18:06:02 +02003162static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003163{
Harald Welte2483f1b2016-06-19 18:06:02 +02003164 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3165 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003166
Harald Welte2483f1b2016-06-19 18:06:02 +02003167 switch (pdisc) {
3168 case GSM48_PDISC_MM:
3169 switch (msg_type) {
3170 case GSM48_MT_MM_LOC_UPD_REQUEST:
3171 case GSM48_MT_MM_CM_SERV_REQ:
3172 case GSM48_MT_MM_AUTH_RESP:
3173 case GSM48_MT_MM_AUTH_FAIL:
3174 case GSM48_MT_MM_ID_RESP:
3175 case GSM48_MT_MM_TMSI_REALL_COMPL:
3176 case GSM48_MT_MM_IMSI_DETACH_IND:
3177 return true;
3178 default:
3179 break;
3180 }
3181 break;
3182 case GSM48_PDISC_RR:
3183 switch (msg_type) {
3184 case GSM48_MT_RR_CIPH_M_COMPL:
3185 case GSM48_MT_RR_PAG_RESP:
3186 return true;
3187 default:
3188 break;
3189 }
3190 break;
3191 default:
3192 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003193 }
3194
Harald Welte2483f1b2016-06-19 18:06:02 +02003195 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003196}
3197
Harald Welte2483f1b2016-06-19 18:06:02 +02003198void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3199 struct msgb *msg)
3200{
3201
3202 /* If a CM Service Request was received before, this is the request the
3203 * conn was opened for. No need to wait for further messages. */
3204
3205 if (!conn->received_cm_service_request)
3206 return;
3207
3208 if (log_check_level(DMM, LOGL_DEBUG)) {
3209 struct gsm48_hdr *gh = msgb_l3(msg);
3210 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3211 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3212
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003213 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003214 " received_cm_service_request changes to false\n",
3215 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003216 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003217 }
3218 conn->received_cm_service_request = false;
3219}
3220
3221
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003222/* 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 +08003223int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003224{
Philipp Maier91f10c72017-04-20 18:40:37 +02003225 struct gsm48_hdr *gh;
3226 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003227 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003228
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003229 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003230 OSMO_ASSERT(conn);
3231 OSMO_ASSERT(msg);
3232
Philipp Maier91f10c72017-04-20 18:40:37 +02003233 gh = msgb_l3(msg);
3234 pdisc = gsm48_hdr_pdisc(gh);
3235
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003236 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3237 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3238 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003239
3240 if (!msc_subscr_conn_is_accepted(conn)
3241 && !msg_is_initially_permitted(gh)) {
3242 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003243 "subscr %s: Message not permitted for initial conn: %s\n",
3244 vlr_subscr_name(conn->vsub),
3245 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003246 return -EACCES;
3247 }
3248
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003249 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3250 LOGP(DMM, LOGL_ERROR,
3251 "%s: Illegal situation: RAN type mismatch:"
3252 " attached via %s, received message via %s\n",
3253 vlr_subscr_name(conn->vsub),
3254 ran_type_name(conn->vsub->cs.attached_via_ran),
3255 ran_type_name(conn->via_ran));
3256 return -EACCES;
3257 }
3258
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003259#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003260 if (silent_call_reroute(conn, msg))
3261 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003262#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003263
Harald Welte52b1f982008-12-23 20:25:15 +00003264 switch (pdisc) {
3265 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003266 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003267 break;
3268 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003269 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003270 break;
3271 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003272 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003273 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003274 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003275 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003276 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003277 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003278 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003279 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3280 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003281 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003282 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003283 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003284 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003285 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003286 case GSM48_PDISC_TEST:
3287 rc = gsm0414_rcv_test(conn, msg);
3288 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003289 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003290 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3291 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003292 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003293 break;
3294 }
3295
3296 return rc;
3297}
Harald Welte8470bf22008-12-25 23:28:35 +00003298
Harald Welte2483f1b2016-06-19 18:06:02 +02003299/***********************************************************************
3300 * VLR integration
3301 ***********************************************************************/
3302
3303/* VLR asks us to send an authentication request */
3304static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3305 bool send_autn)
3306{
3307 struct gsm_subscriber_connection *conn = msc_conn_ref;
3308 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3309 send_autn? at->vec.autn : NULL,
3310 at->key_seq);
3311}
3312
3313/* VLR asks us to send an authentication reject */
3314static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3315{
3316 struct gsm_subscriber_connection *conn = msc_conn_ref;
3317 return gsm48_tx_mm_auth_rej(conn);
3318}
3319
3320/* VLR asks us to transmit an Identity Request of given type */
3321static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3322{
3323 struct gsm_subscriber_connection *conn = msc_conn_ref;
3324 return mm_tx_identity_req(conn, mi_type);
3325}
3326
3327/* VLR asks us to transmit a Location Update Accept */
3328static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3329{
3330 struct gsm_subscriber_connection *conn = msc_conn_ref;
3331 return gsm0408_loc_upd_acc(conn, send_tmsi);
3332}
3333
3334/* VLR asks us to transmit a Location Update Reject */
3335static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3336{
3337 struct gsm_subscriber_connection *conn = msc_conn_ref;
3338 return gsm0408_loc_upd_rej(conn, cause);
3339}
3340
3341/* VLR asks us to transmit a CM Service Accept */
3342static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3343{
3344 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003345 return msc_gsm48_tx_mm_serv_ack(conn);
3346}
3347
3348static int msc_vlr_tx_common_id(void *msc_conn_ref)
3349{
3350 struct gsm_subscriber_connection *conn = msc_conn_ref;
3351 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003352}
3353
3354/* VLR asks us to transmit a CM Service Reject */
3355static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3356{
3357 uint8_t cause;
3358 struct gsm_subscriber_connection *conn = msc_conn_ref;
3359 conn->received_cm_service_request = false;
3360
3361 switch (result) {
3362 default:
3363 case VLR_PR_ARQ_RES_NONE:
3364 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3365 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3366 cause = GSM48_REJECT_NETWORK_FAILURE;
3367 break;
3368 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3369 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3370 break;
3371 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3372 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3373 break;
3374 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3375 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3376 break;
3377 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3378 cause = GSM48_REJECT_ILLEGAL_MS;
3379 break;
3380 case VLR_PR_ARQ_RES_TIMEOUT:
3381 cause = GSM48_REJECT_CONGESTION;
3382 break;
3383 };
3384
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003385 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003386}
3387
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003388/* For msc_vlr_set_ciph_mode() */
3389osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3390 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3391
Harald Welte2483f1b2016-06-19 18:06:02 +02003392/* VLR asks us to start using ciphering */
3393static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3394 enum vlr_ciph ciph,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01003395 bool umts_aka,
Harald Welte2483f1b2016-06-19 18:06:02 +02003396 bool retrieve_imeisv)
3397{
3398 struct gsm_subscriber_connection *conn = msc_conn_ref;
3399 struct vlr_subscr *vsub;
3400 struct gsm_auth_tuple *tuple;
3401
3402 if (!conn || !conn->vsub) {
3403 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3404 " NULL conn/subscriber");
3405 return -EINVAL;
3406 }
3407
3408 vsub = conn->vsub;
3409 tuple = vsub->last_tuple;
3410
3411 if (!tuple) {
3412 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3413 " Command: no auth tuple available\n",
3414 vlr_subscr_name(vsub));
3415 return -EINVAL;
3416 }
3417
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003418 switch (conn->via_ran) {
3419 case RAN_GERAN_A:
3420 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3421 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003422 {
3423 struct gsm0808_encrypt_info ei;
3424
3425 ei.perm_algo[0] = vlr_ciph_to_gsm0808_alg_id(ciph);
3426 ei.perm_algo_len = 1;
3427
Neels Hofmeyr52821712017-12-18 01:23:42 +01003428 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3429 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3430 * necessarily a match for the UMTS AKA tokens. */
3431 if (umts_aka)
3432 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3433 else
3434 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003435 ei.key_len = sizeof(tuple->vec.kc);
3436
3437 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3438 }
3439
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003440 case RAN_UTRAN_IU:
3441#ifdef BUILD_IU
3442 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3443 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003444 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003445#else
3446 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3447 " built without Iu support\n");
3448 return -ENOTSUP;
3449#endif
3450
3451 default:
3452 break;
3453 }
3454 LOGP(DMM, LOGL_ERROR,
3455 "%s: cannot start ciphering, unknown RAN type %d\n",
3456 vlr_subscr_name(conn->vsub), conn->via_ran);
3457 return -ENOTSUP;
3458}
3459
3460void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3461{
3462 struct vlr_ciph_result vlr_res = {};
3463
3464 if (!conn || !conn->vsub) {
3465 LOGP(DMM, LOGL_ERROR,
3466 "Rx Security Mode Complete for invalid conn\n");
3467 return;
3468 }
3469
3470 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3471 vlr_subscr_name(conn->vsub));
3472
3473 vlr_res.cause = VLR_CIPH_COMPL;
3474 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003475}
3476
3477/* VLR informs us that the subscriber data has somehow been modified */
3478static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3479{
3480 /* FIXME */
3481}
3482
3483/* VLR informs us that the subscriber has been associated with a conn */
3484static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3485 struct vlr_subscr *vsub)
3486{
3487 struct gsm_subscriber_connection *conn = msc_conn_ref;
3488 OSMO_ASSERT(!conn->vsub);
3489 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003490 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003491}
3492
3493/* operations that we need to implement for libvlr */
3494static const struct vlr_ops msc_vlr_ops = {
3495 .tx_auth_req = msc_vlr_tx_auth_req,
3496 .tx_auth_rej = msc_vlr_tx_auth_rej,
3497 .tx_id_req = msc_vlr_tx_id_req,
3498 .tx_lu_acc = msc_vlr_tx_lu_acc,
3499 .tx_lu_rej = msc_vlr_tx_lu_rej,
3500 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3501 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3502 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003503 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003504 .subscr_update = msc_vlr_subscr_update,
3505 .subscr_assoc = msc_vlr_subscr_assoc,
3506};
3507
3508/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3509int msc_vlr_alloc(struct gsm_network *net)
3510{
3511 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3512 if (!net->vlr)
3513 return -ENOMEM;
3514 net->vlr->user_ctx = net;
3515 return 0;
3516}
3517
3518/* Launch the VLR, i.e. its GSUP connection */
3519int msc_vlr_start(struct gsm_network *net)
3520{
3521 OSMO_ASSERT(net->vlr);
3522 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3523 net->gsup_server_port);
3524}