blob: 51eb1c0677616cb51c77eb2751d6982615d18803 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/auth.h>
38#include <osmocom/msc/db.h>
39#include <osmocom/msc/debug.h>
40#include <osmocom/msc/gsm_data.h>
41#include <osmocom/msc/gsm_subscriber.h>
42#include <osmocom/msc/gsm_04_11.h>
43#include <osmocom/msc/gsm_04_08.h>
44#include <osmocom/msc/gsm_04_80.h>
45#include <osmocom/msc/gsm_04_14.h>
46#include <osmocom/msc/abis_rsl.h>
47#include <osmocom/msc/chan_alloc.h>
48#include <osmocom/msc/paging.h>
49#include <osmocom/msc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020050#include <osmocom/abis/trau_frame.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020051#include <osmocom/msc/trau_mux.h>
52#include <osmocom/msc/rtp_proxy.h>
53#include <osmocom/msc/transaction.h>
54#include <osmocom/msc/ussd.h>
55#include <osmocom/msc/silent_call.h>
56#include <osmocom/msc/bsc_api.h>
57#include <osmocom/msc/osmo_msc.h>
58#include <osmocom/msc/handover.h>
59#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020060#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010061#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020062#include <osmocom/msc/vlr.h>
63#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080064
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010065#include <osmocom/gsm/gsm48.h>
66#include <osmocom/gsm/gsm0480.h>
67#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020068#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010069#include <osmocom/core/msgb.h>
70#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010071#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010072#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020073#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020074#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020075#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020076#endif
Harald Welte52b1f982008-12-23 20:25:15 +000077
Neels Hofmeyr90843962017-09-04 15:04:35 +020078#include <osmocom/msc/msc_ifaces.h>
79#include <osmocom/msc/a_iface.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020080
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020081#include <assert.h>
82
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020083
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020084void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010085void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020086
Harald Welte2483f1b2016-06-19 18:06:02 +020087static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
88 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080089static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020090 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000091
Harald Welte52b1f982008-12-23 20:25:15 +000092struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020093 uint16_t mcc;
94 uint16_t mnc;
95 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000096};
97
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020098static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080099
Harald Welte31c00f72011-03-03 23:29:05 +0100100void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
101{
102 net->mncc_recv(net, msg);
103}
104
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800105static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
106 struct gsm_trans *trans)
107{
108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
109
110 /* if we get passed a transaction reference, do some common
111 * work that the caller no longer has to do */
112 if (trans) {
113 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800114 }
115
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200116 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800117}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100118
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800119int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
120{
121 struct gsm48_hdr *gh;
122 struct msgb *ss_notify;
123
124 ss_notify = gsm0480_create_notifySS(message);
125 if (!ss_notify)
126 return -1;
127
128 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
129 uint8_t *data = msgb_push(ss_notify, 1);
130 data[0] = ss_notify->len - 1;
131 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
132 gh->msg_type = GSM48_MT_CC_FACILITY;
133 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
134}
135
Harald Welte2483f1b2016-06-19 18:06:02 +0200136/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100137void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
138{
139 struct gsm_trans *trans, *temp;
140
141 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
142
143 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100144 if (trans->protocol == protocol) {
145 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100146 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100147 }
Harald Welte371efe52010-12-22 23:17:50 +0100148 }
149}
150
Holger Freyther429e7762008-12-30 13:28:30 +0000151/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200152static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000153{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800154 struct msgb *msg;
155
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800156 msg = gsm48_create_loc_upd_rej(cause);
157 if (!msg) {
158 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
159 return -1;
160 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200161
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200162 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
163 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100164
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800165 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000166}
167
168/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200169static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
170 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000171{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100172 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000173 struct gsm48_hdr *gh;
174 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200175 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200176
Harald Welte52b1f982008-12-23 20:25:15 +0000177 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
178 gh->proto_discr = GSM48_PDISC_MM;
179 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
180
181 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200182 gsm48_generate_lai(lai, conn->network->country_code,
183 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200184 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000185
Harald Welte2483f1b2016-06-19 18:06:02 +0200186 if (send_tmsi == GSM_RESERVED_TMSI) {
187 /* we did not allocate a TMSI to the MS, so we need to
188 * include the IMSI in order for the MS to delete any
189 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200190 uint8_t mi[10];
191 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200192 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200193 mid = msgb_put(msg, len);
194 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200195 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
196 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200197 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200198 /* Include the TMSI, which means that the MS will send a
199 * TMSI REALLOCATION COMPLETE, and we should wait for
200 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200201 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200202 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200203 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
204 vlr_subscr_name(conn->vsub),
205 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200206 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200207 /* TODO: Follow-on proceed */
208 /* TODO: CTS permission */
209 /* TODO: Equivalent PLMNs */
210 /* TODO: Emergency Number List */
211 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000212
Harald Welte52b1f982008-12-23 20:25:15 +0000213
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800214 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000215}
216
Harald Weltebf5e8df2009-02-03 12:59:45 +0000217/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200218static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000219{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100220 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000221 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000222
Harald Welte231ad4f2008-12-27 11:15:38 +0000223 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
224 gh->proto_discr = GSM48_PDISC_MM;
225 gh->msg_type = GSM48_MT_MM_ID_REQ;
226 gh->data[0] = id_type;
227
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800228 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000229}
230
Harald Weltebf5e8df2009-02-03 12:59:45 +0000231/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800232static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000233{
234 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200235 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200236 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000237
Harald Welte2483f1b2016-06-19 18:06:02 +0200238 if (!conn->vsub) {
239 LOGP(DMM, LOGL_ERROR,
240 "Rx MM Identity Response: invalid: no subscriber\n");
241 return -EINVAL;
242 }
243
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200244 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200245 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
246 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000247
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200248 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100249
Harald Welte2483f1b2016-06-19 18:06:02 +0200250 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000251}
252
Harald Welte2483f1b2016-06-19 18:06:02 +0200253/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200254static const struct value_string lupd_names[] = {
255 { GSM48_LUPD_NORMAL, "NORMAL" },
256 { GSM48_LUPD_PERIODIC, "PERIODIC" },
257 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
258 { 0, NULL }
259};
Harald Welte2a139372009-02-22 21:14:55 +0000260
Harald Welte2483f1b2016-06-19 18:06:02 +0200261/* Chapter 9.2.15: Receive Location Updating Request.
262 * Keep this function non-static for direct invocation by unit tests. */
263int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000264{
Harald Welte2483f1b2016-06-19 18:06:02 +0200265 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
266 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000267 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000268 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200269 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200270 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200271 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200272 uint32_t tmsi;
273 char *imsi;
274 struct osmo_location_area_id old_lai, new_lai;
275 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200276 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200277 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000278
Harald Welte8470bf22008-12-25 23:28:35 +0000279 lu = (struct gsm48_loc_upd_req *) gh->data;
280
281 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000282
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200283 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000284
Harald Welte2483f1b2016-06-19 18:06:02 +0200285 rc = msc_create_conn_fsm(conn, mi_string);
286 if (rc)
287 /* logging already happened in msc_create_conn_fsm() */
288 return rc;
289
290 conn->classmark.classmark1 = lu->classmark1;
291 conn->classmark.classmark1_set = true;
292
293 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
294 gsm48_mi_type_name(mi_type), mi_string,
295 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000296
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200297 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100298
Harald Welte24ff6ee2009-12-22 00:41:05 +0100299 switch (lu->type) {
300 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200301 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200302 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100303 break;
304 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200305 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100307 break;
308 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200309 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200310 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100311 break;
312 }
313
Harald Welte2483f1b2016-06-19 18:06:02 +0200314 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
315 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
316 /* TODO: 10.5.7.8 Device properties */
317 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200318
Harald Welte52b1f982008-12-23 20:25:15 +0000319 switch (mi_type) {
320 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 tmsi = GSM_RESERVED_TMSI;
322 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000323 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000324 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200325 tmsi = tmsi_from_string(mi_string);
326 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000327 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200328 default:
Harald Weltea0368542009-06-27 02:58:43 +0200329 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200330 tmsi = GSM_RESERVED_TMSI;
331 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000332 break;
333 }
334
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
336 &old_lai.plmn.mnc, &old_lai.lac);
337 new_lai.plmn.mcc = conn->network->country_code;
338 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200339 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200340 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200341
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200342 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200343 lu_fsm = vlr_loc_update(conn->conn_fsm,
344 SUBSCR_CONN_E_ACCEPTED,
345 SUBSCR_CONN_E_CN_CLOSE,
346 (void*)&conn_from_lu,
347 net->vlr, conn, vlr_lu_type, tmsi, imsi,
348 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200349 is_utran || conn->network->authentication_required,
350 is_utran? VLR_CIPH_A5_3
351 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200352 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200353 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200354 net->vlr->cfg.assign_tmsi);
355 if (!lu_fsm) {
356 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
357 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800358 }
359
Harald Welte2483f1b2016-06-19 18:06:02 +0200360 /* From vlr_loc_update() we expect an implicit dispatch of
361 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
362 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000363
Harald Welte2483f1b2016-06-19 18:06:02 +0200364 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
365 LOGP(DRR, LOGL_ERROR,
366 "%s: internal error during Location Updating attempt\n",
367 mi_string);
368 return -EIO;
369 }
370
371 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000372}
373
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100374/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200375/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100376static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800377{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100378 uint8_t ret;
379
380 ret = value / 10;
381 ret |= (value % 10) << 4;
382
383 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800384}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100385
Harald Welte4bfdfe72009-06-10 23:11:52 +0800386
Harald Weltedb253af2008-12-30 17:56:55 +0000387/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800388int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000389{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100390 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000391 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200392 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200393 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200394 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100395
Harald Welte4bfdfe72009-06-10 23:11:52 +0800396 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100397 struct tm* gmt_time;
398 struct tm* local_time;
399 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100400 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000401
Harald Weltedb253af2008-12-30 17:56:55 +0000402 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
403 gh->proto_discr = GSM48_PDISC_MM;
404 gh->msg_type = GSM48_MT_MM_INFO;
405
406 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200407#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000408 name_len = strlen(net->name_long);
409 /* 10.5.3.5a */
410 ptr8 = msgb_put(msg, 3);
411 ptr8[0] = GSM48_IE_NAME_LONG;
412 ptr8[1] = name_len*2 +1;
413 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
414
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200415 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000416 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000417 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000418
419 /* FIXME: Use Cell Broadcast, not UCS-2, since
420 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200421#endif
422 name_len = (strlen(net->name_long)*7)/8;
423 name_pad = (8 - strlen(net->name_long)*7)%8;
424 if (name_pad > 0)
425 name_len++;
426 /* 10.5.3.5a */
427 ptr8 = msgb_put(msg, 3);
428 ptr8[0] = GSM48_IE_NAME_LONG;
429 ptr8[1] = name_len +1;
430 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
431
432 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100433 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200434
Harald Weltedb253af2008-12-30 17:56:55 +0000435 }
436
437 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200438#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000439 name_len = strlen(net->name_short);
440 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200441 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200442 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000443 ptr8[1] = name_len*2 + 1;
444 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
445
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200446 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000447 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000448 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200449#endif
450 name_len = (strlen(net->name_short)*7)/8;
451 name_pad = (8 - strlen(net->name_short)*7)%8;
452 if (name_pad > 0)
453 name_len++;
454 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200456 ptr8[0] = GSM48_IE_NAME_SHORT;
457 ptr8[1] = name_len +1;
458 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
459
460 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100461 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200462
Harald Weltedb253af2008-12-30 17:56:55 +0000463 }
464
Harald Weltedb253af2008-12-30 17:56:55 +0000465 /* Section 10.5.3.9 */
466 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100467 gmt_time = gmtime(&cur_t);
468
Harald Weltedb253af2008-12-30 17:56:55 +0000469 ptr8 = msgb_put(msg, 8);
470 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100471 ptr8[1] = bcdify(gmt_time->tm_year % 100);
472 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
473 ptr8[3] = bcdify(gmt_time->tm_mday);
474 ptr8[4] = bcdify(gmt_time->tm_hour);
475 ptr8[5] = bcdify(gmt_time->tm_min);
476 ptr8[6] = bcdify(gmt_time->tm_sec);
477
Neels Hofmeyr73983952016-05-10 13:29:33 +0200478 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200479 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200480 if (net->tz.hr < 0) {
481 tzunits = ((net->tz.hr/-1)*4);
482 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100483 ptr8[7] = bcdify(tzunits);
484 /* Set negative time */
485 ptr8[7] |= 0x08;
486 }
487 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200488 tzunits = net->tz.hr*4;
489 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100490 ptr8[7] = bcdify(tzunits);
491 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100492 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200493 if (net->tz.dst >= 0 && net->tz.dst <= 2)
494 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100495 }
496 else {
497 /* Need to get GSM offset and convert into 15 min units */
498 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200499#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200500 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100501 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200502#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200503 /* find timezone offset */
504 time_t utc;
505 double offsetFromUTC;
506 utc = mktime(gmt_time);
507 local_time = localtime(&cur_t);
508 offsetFromUTC = difftime(cur_t, utc);
509 if (local_time->tm_isdst)
510 offsetFromUTC += 3600.0;
511 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200512#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100513 if (tzunits < 0) {
514 tzunits = tzunits/-1;
515 ptr8[7] = bcdify(tzunits);
516 /* Flip it to negative */
517 ptr8[7] |= 0x08;
518 }
519 else
520 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100521
522 /* Does not support DST +2 */
523 if (local_time->tm_isdst)
524 dst = 1;
525 }
526
527 if (dst) {
528 ptr8 = msgb_put(msg, 3);
529 ptr8[0] = GSM48_IE_NET_DST;
530 ptr8[1] = 1;
531 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100532 }
Harald Weltedb253af2008-12-30 17:56:55 +0000533
Daniel Willmanneea93372009-08-13 03:42:07 +0200534 DEBUGP(DMM, "-> MM INFO\n");
535
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800536 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000537}
538
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100539/*! Send an Authentication Request to MS on the given subscriber connection
540 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
541 * \param[in] conn Subscriber connection to send on.
542 * \param[in] rand Random challenge token to send, must be 16 bytes long.
543 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
544 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
545 * \param[in] key_seq auth tuple's sequence number.
546 */
547int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
548 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200549{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100550 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200551 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200552 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200553
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100554 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100555 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100556 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200557
Harald Welte7984d5c2009-08-12 22:56:50 +0200558 gh->proto_discr = GSM48_PDISC_MM;
559 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
560
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100561 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200562
Harald Welte7984d5c2009-08-12 22:56:50 +0200563 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100564 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200565 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200566 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200567
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100568
569 /* 16 bytes AUTN */
570 if (autn)
571 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
572
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800573 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200574}
575
576/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800577int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200578{
579 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800580 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200581}
582
Harald Welte2483f1b2016-06-19 18:06:02 +0200583static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
584static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
585
586static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100587{
Harald Welte2483f1b2016-06-19 18:06:02 +0200588 uint8_t mi_type;
589 char mi_string[GSM48_MI_SIZE];
590 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100591
Harald Welte2483f1b2016-06-19 18:06:02 +0200592 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
593 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100594
Harald Welte2483f1b2016-06-19 18:06:02 +0200595 switch (mi_type) {
596 case GSM_MI_TYPE_IMSI:
597 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
598 goto accept_reuse;
599 break;
600 case GSM_MI_TYPE_TMSI:
601 tmsi = osmo_load32be(mi_lv+2);
602 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
603 goto accept_reuse;
604 break;
605 case GSM_MI_TYPE_IMEI:
606 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
607 goto accept_reuse;
608 break;
609 default:
610 break;
611 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100612
Harald Welte2483f1b2016-06-19 18:06:02 +0200613 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
614 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
615 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
616 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100617
Harald Welte2483f1b2016-06-19 18:06:02 +0200618accept_reuse:
619 DEBUGP(DMM, "%s: re-using already accepted connection\n",
620 vlr_subscr_name(conn->vsub));
621 conn->received_cm_service_request = true;
622 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100623}
624
Harald Welte4ed0e922009-01-10 03:17:30 +0000625/*
626 * Handle CM Service Requests
627 * a) Verify that the packet is long enough to contain the information
628 * we require otherwsie reject with INCORRECT_MESSAGE
629 * b) Try to parse the TMSI. If we do not have one reject
630 * c) Check that we know the subscriber with the TMSI otherwise reject
631 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200632 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200633 *
634 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000635 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200636int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000637{
Harald Welte2483f1b2016-06-19 18:06:02 +0200638 static const enum subscr_conn_from conn_from_cm_service_req =
639 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
640 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200641 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200642 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000643
Harald Welteba4cf162009-01-10 01:49:35 +0000644 struct gsm48_hdr *gh = msgb_l3(msg);
645 struct gsm48_service_request *req =
646 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800647 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200648 uint8_t classmark2_len = gh->data[1];
649 uint8_t *classmark2 = gh->data+2;
650 uint8_t mi_len = *(classmark2 + classmark2_len);
651 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200652 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200653 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200654 int rc;
655
656 lai.plmn.mcc = conn->network->country_code;
657 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200658 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000659
Harald Weltec9e02182009-05-01 19:07:53 +0000660 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000661 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000662 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200663 return msc_gsm48_tx_mm_serv_rej(conn,
664 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000665 }
666
667 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000668 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200669 return msc_gsm48_tx_mm_serv_rej(conn,
670 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000671 }
672
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200673 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000674 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100675 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
676 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200677
Harald Welte3995d2e2018-01-24 11:23:54 +0100678 switch (mi_type) {
679 case GSM_MI_TYPE_IMSI:
680 case GSM_MI_TYPE_TMSI:
681 /* continue below */
682 break;
683 case GSM_MI_TYPE_IMEI:
684 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
685 /* We don't do emergency calls by IMEI */
686 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
687 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
688 }
689 /* fall-through for non-emergency setup */
690 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200691 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200692 return msc_gsm48_tx_mm_serv_rej(conn,
693 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000694 }
695
Harald Welte37382ec2018-01-24 12:49:34 +0100696 switch (req->cm_service_type) {
697 case GSM48_CMSERV_MO_CALL_PACKET:
698 case GSM48_CMSERV_EMERGENCY:
699 case GSM48_CMSERV_SMS:
700 case GSM48_CMSERV_SUP_SERV:
701 /* continue below */
702 break;
703 default:
704 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
705 }
706
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200707 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200708 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
709 conn->classmark.classmark2_len = classmark2_len;
710
711 if (conn->conn_fsm) {
712 if (msc_subscr_conn_is_accepted(conn))
713 return cm_serv_reuse_conn(conn, mi-1);
714 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
715 vlr_subscr_name(conn->vsub));
716 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
717 return -EINVAL;
718 }
719
720 rc = msc_create_conn_fsm(conn, mi_string);
721 if (rc) {
722 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
723 /* logging already happened in msc_create_conn_fsm() */
724 return rc;
725 }
Harald Welte7659de12009-12-13 12:39:18 +0100726
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200727 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200728 vlr_proc_acc_req(conn->conn_fsm,
729 SUBSCR_CONN_E_ACCEPTED,
730 SUBSCR_CONN_E_CN_CLOSE,
731 (void*)&conn_from_cm_service_req,
732 net->vlr, conn,
733 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200734 is_utran || conn->network->authentication_required,
735 is_utran? VLR_CIPH_A5_3
736 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200737 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200738 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000739
Harald Welte2483f1b2016-06-19 18:06:02 +0200740 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000741}
742
Harald Welte49f8fcb2018-01-24 21:40:19 +0100743/* Receive a CM Re-establish Request */
744static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
745{
746 uint8_t mi_type;
747 char mi_string[GSM48_MI_SIZE];
748 struct gsm48_hdr *gh = msgb_l3(msg);
749
750 uint8_t classmark2_len = gh->data[1];
751 uint8_t *classmark2 = gh->data+2;
752 uint8_t mi_len = *(classmark2 + classmark2_len);
753 uint8_t *mi = (classmark2 + classmark2_len + 1);
754
755 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
756 mi_type = mi[0] & GSM_MI_TYPE_MASK;
757 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
758
759 /* we don't support CM call re-establishment */
760 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
761}
762
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200763static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000764{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200765 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000766 struct gsm48_hdr *gh = msgb_l3(msg);
767 struct gsm48_imsi_detach_ind *idi =
768 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200770 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200771 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000772
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200773 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200774 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
775 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000776
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200777 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100778
Harald Welte2a139372009-02-22 21:14:55 +0000779 switch (mi_type) {
780 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200781 vsub = vlr_subscr_find_by_tmsi(network->vlr,
782 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000783 break;
784 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200785 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000786 break;
787 case GSM_MI_TYPE_IMEI:
788 case GSM_MI_TYPE_IMEISV:
789 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200790 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
791 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000792 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200793 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200794 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
795 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000796 break;
797 }
798
Harald Welte2483f1b2016-06-19 18:06:02 +0200799 /* TODO? We used to remember the subscriber's classmark1 here and
800 * stored it in the old sqlite db, but now we store it in a conn that
801 * will be discarded anyway: */
802 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200803
Harald Welte2483f1b2016-06-19 18:06:02 +0200804 if (!vsub) {
805 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
806 gsm48_mi_type_name(mi_type), mi_string);
807 } else {
808 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100809
810 if (vsub->cs.is_paging)
811 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
812
Harald Welte2483f1b2016-06-19 18:06:02 +0200813 vlr_subscr_rx_imsi_detach(vsub);
814 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
815 vlr_subscr_put(vsub);
816 }
Harald Welte2a139372009-02-22 21:14:55 +0000817
Harald Welte2483f1b2016-06-19 18:06:02 +0200818 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000819 return 0;
820}
821
Harald Welted2a7f5a2009-06-05 20:08:20 +0000822static int gsm48_rx_mm_status(struct msgb *msg)
823{
824 struct gsm48_hdr *gh = msgb_l3(msg);
825
826 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
827
828 return 0;
829}
830
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100831static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
832 struct gsm_subscriber_connection *conn,
833 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100834{
835 struct gsm48_hdr *gh = msgb_l3(msg);
836 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100837
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100838 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
839 LOGP(DMM, LOGL_ERROR,
840 "%s: MM AUTHENTICATION RESPONSE:"
841 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200842 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100843 return -EINVAL;
844 }
845
846 *res_len = sizeof(ar->sres);
847 memcpy(res, ar->sres, sizeof(ar->sres));
848 return 0;
849}
850
851static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
852 struct gsm_subscriber_connection *conn,
853 struct msgb *msg)
854{
855 struct gsm48_hdr *gh;
856 uint8_t *data;
857 uint8_t iei;
858 uint8_t ie_len;
859 unsigned int data_len;
860
861 /* First parse the GSM part */
862 if (parse_gsm_auth_resp(res, res_len, conn, msg))
863 return -EINVAL;
864 OSMO_ASSERT(*res_len == 4);
865
866 /* Then add the extended res part */
867 gh = msgb_l3(msg);
868 data = gh->data + sizeof(struct gsm48_auth_resp);
869 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
870
871 if (data_len < 3) {
872 LOGP(DMM, LOGL_ERROR,
873 "%s: MM AUTHENTICATION RESPONSE:"
874 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200875 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100876 return -EINVAL;
877 }
878
879 iei = data[0];
880 ie_len = data[1];
881 if (iei != GSM48_IE_AUTH_RES_EXT) {
882 LOGP(DMM, LOGL_ERROR,
883 "%s: MM R99 AUTHENTICATION RESPONSE:"
884 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200885 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100886 GSM48_IE_AUTH_RES_EXT, iei);
887 return -EINVAL;
888 }
889
890 if (ie_len > 12) {
891 LOGP(DMM, LOGL_ERROR,
892 "%s: MM R99 AUTHENTICATION RESPONSE:"
893 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200894 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100895 return -EINVAL;
896 }
897
898 *res_len += ie_len;
899 memcpy(res + 4, &data[2], ie_len);
900 return 0;
901}
902
903/* Chapter 9.2.3: Authentication Response */
904static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
905{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100906 uint8_t res[16];
907 uint8_t res_len;
908 int rc;
909 bool is_r99;
910
Harald Welte2483f1b2016-06-19 18:06:02 +0200911 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100912 LOGP(DMM, LOGL_ERROR,
913 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200914 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100915 return -EINVAL;
916 }
917
918 if (msgb_l3len(msg) >
919 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
920 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
921 is_r99 = true;
922 } else {
923 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
924 is_r99 = false;
925 }
926
927 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200928 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100929 return -EINVAL;
930 }
931
932 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200933 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100934 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
935 osmo_hexdump_nospc(res, res_len));
936
Harald Welte2483f1b2016-06-19 18:06:02 +0200937 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
938 conn->via_ran == RAN_UTRAN_IU,
939 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100940}
941
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100942static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
943{
944 struct gsm48_hdr *gh = msgb_l3(msg);
945 uint8_t cause;
946 uint8_t auts_tag;
947 uint8_t auts_len;
948 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100949
Harald Welte2483f1b2016-06-19 18:06:02 +0200950 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100951 LOGP(DMM, LOGL_ERROR,
952 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 return -EINVAL;
955 }
956
957 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
958 LOGP(DMM, LOGL_ERROR,
959 "%s: MM R99 AUTHENTICATION FAILURE:"
960 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200961 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
962 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 return -EINVAL;
964 }
965
966 cause = gh->data[0];
967
968 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
969 LOGP(DMM, LOGL_INFO,
970 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200971 vlr_subscr_name(conn->vsub), cause);
972 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
973 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 }
975
976 /* This is a Synch Failure procedure, which should pass an AUTS to
977 * resynchronize the sequence nr with the HLR. Expecting exactly one
978 * TLV with 14 bytes of AUTS. */
979
980 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
981 LOGP(DMM, LOGL_INFO,
982 "%s: MM R99 AUTHENTICATION FAILURE:"
983 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200984 vlr_subscr_name(conn->vsub));
985 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100986 return -EINVAL;
987 }
988
989 auts_tag = gh->data[1];
990 auts_len = gh->data[2];
991 auts = &gh->data[3];
992
993 if (auts_tag != GSM48_IE_AUTS
994 || auts_len != 14) {
995 LOGP(DMM, LOGL_INFO,
996 "%s: MM R99 AUTHENTICATION FAILURE:"
997 " invalid Synch Failure:"
998 " expected AUTS IE 0x%02x of 14 bytes,"
999 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001000 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001001 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001002 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001003 return -EINVAL;
1004 }
1005
1006 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1007 LOGP(DMM, LOGL_INFO,
1008 "%s: MM R99 AUTHENTICATION FAILURE:"
1009 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001010 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1011 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 return -EINVAL;
1013 }
1014
1015 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1016 * large enough. */
1017
1018 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001019 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001020
Harald Welte2483f1b2016-06-19 18:06:02 +02001021 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1022}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001023
Harald Welte2483f1b2016-06-19 18:06:02 +02001024static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1025{
1026 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1027 vlr_subscr_name(conn->vsub));
1028 if (!conn->vsub) {
1029 LOGP(DMM, LOGL_ERROR,
1030 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1031 return -EINVAL;
1032 }
1033 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001034}
1035
Harald Weltebf5e8df2009-02-03 12:59:45 +00001036/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001037static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001038{
1039 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001040 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001041
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001042 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001043 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001044 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001045 break;
1046 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001047 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001048 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001049 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001050 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001051 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001052 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001053 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001054 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001055 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001056 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001057 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001059 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001060 break;
1061 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001062 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001063 break;
1064 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001065 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001066 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001067 case GSM48_MT_MM_AUTH_FAIL:
1068 rc = gsm48_rx_mm_auth_fail(conn, msg);
1069 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001070 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001071 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001072 gh->msg_type);
1073 break;
1074 }
1075
1076 return rc;
1077}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001078
Harald Welte2483f1b2016-06-19 18:06:02 +02001079static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1080{
1081 /* Check the size for the classmark */
1082 if (tot_len < 1 + *classmark2_lv)
1083 return NULL;
1084
1085 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1086 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1087 return NULL;
1088
1089 return mi_lv;
1090}
1091
Harald Welte2d35ae62009-02-06 12:02:13 +00001092/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001093static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001094{
Harald Welte2483f1b2016-06-19 18:06:02 +02001095 static const enum subscr_conn_from conn_from_paging_resp =
1096 SUBSCR_CONN_FROM_PAGING_RESP;
1097 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001098 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001099 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001100 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001101 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001102 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001103 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001104 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001105 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001106 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001107
1108 lai.plmn.mcc = conn->network->country_code;
1109 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001110 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001111
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001112 resp = (struct gsm48_pag_resp *) &gh->data[0];
1113 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1114 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001115 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1116 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001117
Harald Welte2483f1b2016-06-19 18:06:02 +02001118 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1119 if (!mi_lv) {
1120 /* FIXME */
1121 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001122 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001123
Harald Welte2483f1b2016-06-19 18:06:02 +02001124 rc = msc_create_conn_fsm(conn, mi_string);
1125 if (rc)
1126 /* logging already happened in msc_create_conn_fsm() */
1127 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001128
Harald Welte2483f1b2016-06-19 18:06:02 +02001129 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1130 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001131
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001132 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001133 vlr_proc_acc_req(conn->conn_fsm,
1134 SUBSCR_CONN_E_ACCEPTED,
1135 SUBSCR_CONN_E_CN_CLOSE,
1136 (void*)&conn_from_paging_resp,
1137 net->vlr, conn,
1138 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001139 is_utran || conn->network->authentication_required,
1140 is_utran? VLR_CIPH_A5_3
1141 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +02001142 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001143 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001144
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001145 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001146}
1147
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001148static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001149{
1150 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001151 uint8_t apdu_id_flags;
1152 uint8_t apdu_len;
1153 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001154
1155 apdu_id_flags = gh->data[0];
1156 apdu_len = gh->data[1];
1157 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001158
Max8dc8f232017-02-09 19:13:02 +01001159 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001160 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001161
Harald Welte2483f1b2016-06-19 18:06:02 +02001162 /* we're not using the app info blob anywhere, so ignore. */
1163#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001164 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001165#else
1166 return 0;
1167#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001168}
1169
Harald Weltebf5e8df2009-02-03 12:59:45 +00001170/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001171static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001172{
1173 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001174 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001175
1176 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001177 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001178 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001179 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001180 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001181 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001182 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001183 default:
Philippd37981e2016-10-10 15:11:41 +02001184 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1185 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001186 break;
1187 }
1188
Harald Welte2d35ae62009-02-06 12:02:13 +00001189 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001190}
1191
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001192int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1193 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001194{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001195 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001196 struct gsm48_hdr *gh;
1197
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001198 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1199 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001200
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001201 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1202 gh->proto_discr = GSM48_PDISC_RR;
1203 gh->msg_type = GSM48_MT_RR_APP_INFO;
1204 gh->data[0] = apdu_id;
1205 gh->data[1] = apdu_len;
1206 memcpy(gh->data+2, apdu, apdu_len);
1207
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001208 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001209}
1210
Alexander Couzens92f552f2016-08-23 07:32:27 +02001211/* FIXME: this count_statistics is a state machine behaviour. we should convert
1212 * the complete call control into a state machine. Afterwards we can move this
1213 * code into state transitions.
1214 */
1215static void count_statistics(struct gsm_trans *trans, int new_state)
1216{
1217 int old_state = trans->cc.state;
1218 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1219
1220 if (old_state == new_state)
1221 return;
1222
1223 /* state incoming */
1224 switch (new_state) {
1225 case GSM_CSTATE_ACTIVE:
1226 osmo_counter_inc(trans->net->active_calls);
1227 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1228 break;
1229 }
1230
1231 /* state outgoing */
1232 switch (old_state) {
1233 case GSM_CSTATE_ACTIVE:
1234 osmo_counter_dec(trans->net->active_calls);
1235 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1236 new_state == GSM_CSTATE_DISCONNECT_IND)
1237 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1238 else
1239 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1240 break;
1241 }
1242}
1243
Harald Welte4bc90a12008-12-27 16:32:52 +00001244/* Call Control */
1245
Harald Welte7584aea2009-02-11 11:44:12 +00001246/* The entire call control code is written in accordance with Figure 7.10c
1247 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1248 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1249 * it for voice */
1250
Harald Welte4bfdfe72009-06-10 23:11:52 +08001251static void new_cc_state(struct gsm_trans *trans, int state)
1252{
1253 if (state > 31 || state < 0)
1254 return;
1255
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001256 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1257 trans->transaction_id,
1258 vlr_subscr_name(trans->vsub),
1259 gsm48_cc_state_name(trans->cc.state),
1260 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001261
Alexander Couzens92f552f2016-08-23 07:32:27 +02001262 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001263 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264}
1265
1266static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001267{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001268 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001269 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001270 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001271
Harald Welte4bc90a12008-12-27 16:32:52 +00001272 gh->msg_type = GSM48_MT_CC_STATUS;
1273
1274 cause = msgb_put(msg, 3);
1275 cause[0] = 2;
1276 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1277 cause[2] = 0x80 | 30; /* response to status inquiry */
1278
1279 call_state = msgb_put(msg, 1);
1280 call_state[0] = 0xc0 | 0x00;
1281
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001282 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001283}
1284
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001285static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001286 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001287{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001288 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001289 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1290
Harald Welte6f4b7532008-12-29 00:39:37 +00001291 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001292 gh->msg_type = msg_type;
1293
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001294 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001295}
1296
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1298{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001299 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001300 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001301 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001302 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303 }
1304}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001305
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1307 int msg_type, struct gsm_mncc *mncc)
1308{
1309 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001310 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001311
Philipp Maierfbf66102017-04-09 12:32:51 +02001312 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1313
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001314#if BEFORE_MSCSPLIT
1315 /* Re-enable this log output once we can obtain this information via
1316 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001317 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001318 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001319 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001320 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001321 trans->conn->lchan->ts->trx->bts->nr,
1322 trans->conn->lchan->ts->trx->nr,
1323 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001324 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001325 get_mncc_name(msg_type));
1326 else
1327 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1328 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001329 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 get_mncc_name(msg_type));
1331 else
1332 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1333 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001334#else
1335 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1336#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337
1338 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001339
Harald Welte966636f2009-06-26 19:39:35 +02001340 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341 if (!msg)
1342 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001343
1344 data = msgb_put(msg, sizeof(struct gsm_mncc));
1345 memcpy(data, mncc, sizeof(struct gsm_mncc));
1346
Harald Welte54209c22010-12-22 23:43:29 +01001347 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348
1349 return 0;
1350}
1351
1352int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001353 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354{
1355 struct gsm_mncc rel;
1356
Harald Welte92f70c52009-06-12 01:54:08 +08001357 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001359 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001360 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1361 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1363}
1364
Harald Weltedcaf5652009-07-23 18:56:43 +02001365/* Call Control Specific transaction release.
1366 * gets called by trans_free, DO NOT CALL YOURSELF! */
1367void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 gsm48_stop_cc_timer(trans);
1370
Philipp Maierfbf66102017-04-09 12:32:51 +02001371 /* Make sure call also gets released on the mgcp side */
1372 msc_call_release(trans);
1373
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 /* send release to L4, if callref still exists */
1375 if (trans->callref) {
1376 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001377 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001378 GSM48_CAUSE_LOC_PRN_S_LU,
1379 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001381 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383}
1384
1385static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001386
Harald Welte09e38af2009-02-16 22:52:23 +00001387/* call-back from paging the B-end of the connection */
1388static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001389 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001390{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001391 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001392 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001393
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001394 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001395
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001396 switch (event) {
1397 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001398 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1399 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001400 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001401 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001402 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001403 /* send SETUP request to called party */
1404 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1405 break;
1406 case GSM_PAGING_EXPIRED:
1407 case GSM_PAGING_BUSY:
1408 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001409 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001410 /* Temporarily out of order */
1411 mncc_release_ind(transt->net, transt,
1412 transt->callref,
1413 GSM48_CAUSE_LOC_PRN_S_LU,
1414 GSM48_CC_CAUSE_DEST_OOO);
1415 transt->callref = 0;
1416 transt->paging_request = NULL;
1417 trans_free(transt);
1418 break;
1419 default:
1420 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1421 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001422 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001423
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001424 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001425 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001426}
Harald Welte7584aea2009-02-11 11:44:12 +00001427
Harald Welte4bfdfe72009-06-10 23:11:52 +08001428/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001429static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001430{
Harald Welte53d51f52015-12-03 14:59:04 +01001431 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1432 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001433
Harald Welte4bfdfe72009-06-10 23:11:52 +08001434 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001435 return -EIO;
1436
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001437 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438 return -EIO;
1439
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001440 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001441 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001442
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001443 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001444}
1445
Harald Welte4bfdfe72009-06-10 23:11:52 +08001446static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1447{
1448 DEBUGP(DCC, "-> STATUS ENQ\n");
1449 return gsm48_cc_tx_status(trans, msg);
1450}
1451
1452static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1453static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1454
1455static void gsm48_cc_timeout(void *arg)
1456{
1457 struct gsm_trans *trans = arg;
1458 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001459 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1460 int mo_location = GSM48_CAUSE_LOC_USER;
1461 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1462 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001463 struct gsm_mncc mo_rel, l4_rel;
1464
1465 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1466 mo_rel.callref = trans->callref;
1467 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1468 l4_rel.callref = trans->callref;
1469
Harald Weltedcaf5652009-07-23 18:56:43 +02001470 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001471 case 0x303:
1472 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001473 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474 break;
1475 case 0x310:
1476 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001477 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 break;
1479 case 0x313:
1480 disconnect = 1;
1481 /* unknown, did not find it in the specs */
1482 break;
1483 case 0x301:
1484 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001485 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 break;
1487 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001488 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001489 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001490 gsm48_cc_tx_release(trans, &trans->cc.msg);
1491 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 break; /* stay in release state */
1493 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001494 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001495 return;
1496// release = 1;
1497// l4_cause = 14;
1498// break;
1499 case 0x306:
1500 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001501 mo_cause = trans->cc.msg.cause.value;
1502 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503 break;
1504 case 0x323:
1505 disconnect = 1;
1506 break;
1507 default:
1508 release = 1;
1509 }
1510
1511 if (release && trans->callref) {
1512 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001513 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001514 l4_location, l4_cause);
1515 trans->callref = 0;
1516 }
1517
1518 if (disconnect && trans->callref) {
1519 /* process disconnect towards layer 4 */
1520 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001521 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522 }
1523
1524 /* process disconnect towards mobile station */
1525 if (disconnect || release) {
1526 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001527 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1528 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1529 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530 mo_rel.cause.diag_len = 3;
1531
1532 if (disconnect)
1533 gsm48_cc_tx_disconnect(trans, &mo_rel);
1534 if (release)
1535 gsm48_cc_tx_release(trans, &mo_rel);
1536 }
1537
1538}
1539
Max3ffce192016-04-25 15:22:00 +02001540/* disconnect both calls from the bridge */
1541static inline void disconnect_bridge(struct gsm_network *net,
1542 struct gsm_mncc_bridge *bridge, int err)
1543{
1544 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1545 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1546 struct gsm_mncc mx_rel;
1547 if (!trans0 || !trans1)
1548 return;
1549
1550 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1551 trans0->callref, trans1->callref, strerror(err));
1552
1553 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1554 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1555 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1556
1557 mx_rel.callref = trans0->callref;
1558 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1559
1560 mx_rel.callref = trans1->callref;
1561 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1562}
1563
Harald Welte4bfdfe72009-06-10 23:11:52 +08001564static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1565 int sec, int micro)
1566{
1567 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001568 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001569 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001570 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571}
1572
1573static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1574{
1575 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001576 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1578 struct tlv_parsed tp;
1579 struct gsm_mncc setup;
1580
1581 memset(&setup, 0, sizeof(struct gsm_mncc));
1582 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001583
Harald Welte474d19f2010-03-02 23:18:30 +01001584 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001586 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1587 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 setup.emergency = 1;
Harald Welte0dd01372018-01-24 11:06:19 +01001589 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590
1591 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001592 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001593 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1594 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001595
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 /* bearer capability */
1597 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1598 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001599 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001601
1602 /* Create a copy of the bearer capability
1603 * in the transaction struct, so we can use
1604 * this information later */
1605 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1606 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001607 }
1608 /* facility */
1609 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1610 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001611 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1613 }
1614 /* called party bcd number */
1615 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1616 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001617 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001618 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1619 }
1620 /* user-user */
1621 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1622 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001623 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1625 }
1626 /* ss-version */
1627 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1628 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001629 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1631 }
1632 /* CLIR suppression */
1633 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1634 setup.clir.sup = 1;
1635 /* CLIR invocation */
1636 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1637 setup.clir.inv = 1;
1638 /* cc cap */
1639 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1640 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001641 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1643 }
1644
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 new_cc_state(trans, GSM_CSTATE_INITIATED);
1646
Harald Welte79826e12018-01-24 10:50:24 +01001647 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1648 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001649 setup.called.number);
1650
Alexander Couzensb847a212016-08-02 11:34:11 +02001651 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001652
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001654 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655
Harald Welte13cac662009-07-29 12:10:35 +02001656 /* MNCC code will modify the channel asynchronously, we should
1657 * ipaccess-bind only after the modification has been made to the
1658 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 return 0;
1660}
1661
1662static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001663{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001664 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001665 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001667 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001668
Harald Welte7ccf7782009-02-17 01:43:01 +00001669 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001670
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 /* transaction id must not be assigned */
1672 if (trans->transaction_id != 0xff) { /* unasssigned */
1673 DEBUGP(DCC, "TX Setup with assigned transaction. "
1674 "This is not allowed!\n");
1675 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001676 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001677 GSM48_CAUSE_LOC_PRN_S_LU,
1678 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001680 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 return rc;
1682 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001683
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001685 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001686 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001687 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001689 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001690 GSM48_CAUSE_LOC_PRN_S_LU,
1691 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 return rc;
1695 }
Harald Welte78283ef2009-07-23 21:36:44 +02001696 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001697
Harald Welte65e74cc2008-12-29 01:55:35 +00001698 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001699
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001701
Harald Welte4bfdfe72009-06-10 23:11:52 +08001702 /* bearer capability */
1703 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001704 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 /* facility */
1706 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001707 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 /* progress */
1709 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001710 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 /* calling party BCD number */
1712 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001713 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 /* called party BCD number */
1715 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001716 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717 /* user-user */
1718 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001719 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 /* redirecting party BCD number */
1721 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001722 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 /* signal */
1724 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001725 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001726
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001728
Alexander Couzensb847a212016-08-02 11:34:11 +02001729 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001730
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001731 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001732}
1733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1735{
1736 struct gsm48_hdr *gh = msgb_l3(msg);
1737 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1738 struct tlv_parsed tp;
1739 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001740 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741
1742 gsm48_stop_cc_timer(trans);
1743 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1744
1745 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1746 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001747
Harald Welte474d19f2010-03-02 23:18:30 +01001748 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749#if 0
1750 /* repeat */
1751 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1752 call_conf.repeat = 1;
1753 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1754 call_conf.repeat = 2;
1755#endif
1756 /* bearer capability */
1757 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1758 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001759 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001761
1762 /* Create a copy of the bearer capability
1763 * in the transaction struct, so we can use
1764 * this information later */
1765 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1766 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 }
1768 /* cause */
1769 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1770 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001771 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1773 }
1774 /* cc cap */
1775 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1776 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001777 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1779 }
1780
Andreas Eversberg035b8742013-09-19 09:27:06 +02001781 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001782 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001783
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1785
Philipp Maierfbf66102017-04-09 12:32:51 +02001786 /* Assign call (if not done yet) */
1787 if (trans->assignment_done == false) {
1788 rc = msc_call_assignment(trans);
1789 trans->assignment_done = true;
1790 }
1791 else
1792 rc = 0;
1793
1794 /* don't continue, if there were problems with
1795 * the call assignment. */
1796 if (rc)
1797 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001798
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001799 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001800 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801}
1802
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001803static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804{
1805 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001806 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001808 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1811
1812 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1813
1814 /* bearer capability */
1815 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001816 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 /* facility */
1818 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001819 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 /* progress */
1821 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001822 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001824 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1825 if (rc)
1826 return rc;
1827
Philipp Maierfbf66102017-04-09 12:32:51 +02001828 /* Assign call (if not done yet) */
1829 if (trans->assignment_done == false) {
1830 rc = msc_call_assignment(trans);
1831 trans->assignment_done = true;
1832 }
1833 else
1834 rc = 0;
1835
1836 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837}
1838
1839static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1840{
1841 struct gsm48_hdr *gh = msgb_l3(msg);
1842 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1843 struct tlv_parsed tp;
1844 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001845
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 gsm48_stop_cc_timer(trans);
1847 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1848
1849 memset(&alerting, 0, sizeof(struct gsm_mncc));
1850 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001851 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 /* facility */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1854 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1857 }
1858
1859 /* progress */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1861 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001862 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1864 }
1865 /* ss-version */
1866 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1867 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1870 }
1871
1872 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1873
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001874 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001875 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876}
1877
1878static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1879{
1880 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001881 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1883
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 gh->msg_type = GSM48_MT_CC_ALERTING;
1885
1886 /* facility */
1887 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001888 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 /* progress */
1890 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001891 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892 /* user-user */
1893 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001894 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895
1896 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001897
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001898 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899}
1900
1901static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1902{
1903 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001904 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1906
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 gh->msg_type = GSM48_MT_CC_PROGRESS;
1908
1909 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001910 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 /* user-user */
1912 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001913 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001915 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916}
1917
1918static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1919{
1920 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001921 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1923
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 gh->msg_type = GSM48_MT_CC_CONNECT;
1925
1926 gsm48_stop_cc_timer(trans);
1927 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1928
1929 /* facility */
1930 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001931 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 /* progress */
1933 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001934 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 /* connected number */
1936 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001937 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 /* user-user */
1939 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001940 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941
1942 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1943
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001944 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945}
1946
1947static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1948{
1949 struct gsm48_hdr *gh = msgb_l3(msg);
1950 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1951 struct tlv_parsed tp;
1952 struct gsm_mncc connect;
1953
1954 gsm48_stop_cc_timer(trans);
1955
1956 memset(&connect, 0, sizeof(struct gsm_mncc));
1957 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001958 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001960 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001961 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1962 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001963
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 /* facility */
1965 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1966 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1969 }
1970 /* user-user */
1971 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1972 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1975 }
1976 /* ss-version */
1977 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1978 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1981 }
1982
1983 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001984 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001986 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987}
1988
1989
1990static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1991{
1992 struct gsm_mncc connect_ack;
1993
1994 gsm48_stop_cc_timer(trans);
1995
1996 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001997 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001998
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2000 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002001
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002002 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 &connect_ack);
2004}
2005
2006static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2007{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002008 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2010
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2012
2013 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2014
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002015 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016}
2017
2018static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2019{
2020 struct gsm48_hdr *gh = msgb_l3(msg);
2021 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2022 struct tlv_parsed tp;
2023 struct gsm_mncc disc;
2024
2025 gsm48_stop_cc_timer(trans);
2026
2027 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2028
2029 memset(&disc, 0, sizeof(struct gsm_mncc));
2030 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002031 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 /* cause */
2033 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2034 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002035 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2037 }
2038 /* facility */
2039 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2040 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002041 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2043 }
2044 /* user-user */
2045 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2046 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002047 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2049 }
2050 /* ss-version */
2051 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2052 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002053 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2055 }
2056
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002057 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058
2059}
2060
Harald Weltec66b71c2009-06-11 14:23:20 +08002061static struct gsm_mncc_cause default_cause = {
2062 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2063 .coding = 0,
2064 .rec = 0,
2065 .rec_val = 0,
2066 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2067 .diag_len = 0,
2068 .diag = { 0 },
2069};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070
2071static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2072{
2073 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002074 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2076
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2078
2079 gsm48_stop_cc_timer(trans);
2080 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2081
2082 /* cause */
2083 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 else
Harald Welte55c8f352010-03-07 23:40:35 +01002086 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087
2088 /* facility */
2089 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 /* progress */
2092 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002093 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 /* user-user */
2095 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097
2098 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002099 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
2101 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2102
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002103 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104}
2105
2106static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2107{
2108 struct gsm48_hdr *gh = msgb_l3(msg);
2109 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2110 struct tlv_parsed tp;
2111 struct gsm_mncc rel;
2112 int rc;
2113
2114 gsm48_stop_cc_timer(trans);
2115
2116 memset(&rel, 0, sizeof(struct gsm_mncc));
2117 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002118 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 /* cause */
2120 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2121 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2124 }
2125 /* facility */
2126 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2127 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002128 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2130 }
2131 /* user-user */
2132 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2133 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002134 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2136 }
2137 /* ss-version */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2139 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2142 }
2143
Harald Weltedcaf5652009-07-23 18:56:43 +02002144 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002146 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002148 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002149 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002150 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002151 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 }
2153
2154 new_cc_state(trans, GSM_CSTATE_NULL);
2155
2156 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002157 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158
2159 return rc;
2160}
2161
2162static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2163{
2164 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002165 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2167
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 gh->msg_type = GSM48_MT_CC_RELEASE;
2169
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 gsm48_stop_cc_timer(trans);
2171 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2172
2173 /* cause */
2174 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 /* facility */
2177 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 /* user-user */
2180 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182
Harald Weltedcaf5652009-07-23 18:56:43 +02002183 trans->cc.T308_second = 0;
2184 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185
Harald Weltedcaf5652009-07-23 18:56:43 +02002186 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2188
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002189 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190}
2191
2192static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2193{
2194 struct gsm48_hdr *gh = msgb_l3(msg);
2195 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2196 struct tlv_parsed tp;
2197 struct gsm_mncc rel;
2198 int rc = 0;
2199
2200 gsm48_stop_cc_timer(trans);
2201
2202 memset(&rel, 0, sizeof(struct gsm_mncc));
2203 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002204 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 /* cause */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2207 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002208 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2210 }
2211 /* facility */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2213 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002214 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2216 }
2217 /* user-user */
2218 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2219 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002220 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2222 }
2223 /* ss-version */
2224 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2225 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002226 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2228 }
2229
2230 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002231 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002233 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 MNCC_REJ_IND, &rel);
2235 break;
2236 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002237 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 MNCC_REL_CNF, &rel);
2239 break;
2240 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002241 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 MNCC_REL_IND, &rel);
2243 }
2244 }
2245
2246 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002247 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248
2249 return rc;
2250}
2251
2252static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2253{
2254 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002255 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002257 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2260
2261 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002262
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 gsm48_stop_cc_timer(trans);
2264
2265 /* cause */
2266 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002267 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002268 /* facility */
2269 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002270 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 /* user-user */
2272 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002273 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274
Harald Weltec7782de2010-12-21 19:31:41 +01002275 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2276
Harald Weltedcaf5652009-07-23 18:56:43 +02002277 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278
Harald Weltec7782de2010-12-21 19:31:41 +01002279 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280}
2281
2282static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2283{
2284 struct gsm48_hdr *gh = msgb_l3(msg);
2285 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2286 struct tlv_parsed tp;
2287 struct gsm_mncc fac;
2288
2289 memset(&fac, 0, sizeof(struct gsm_mncc));
2290 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002291 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 /* facility */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2294 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002295 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2297 }
2298 /* ss-version */
2299 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2300 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002301 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2303 }
2304
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002305 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306}
2307
2308static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2309{
2310 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002311 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2313
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 gh->msg_type = GSM48_MT_CC_FACILITY;
2315
2316 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002317 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002319 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320}
2321
2322static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2323{
2324 struct gsm_mncc hold;
2325
2326 memset(&hold, 0, sizeof(struct gsm_mncc));
2327 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002328 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329}
2330
2331static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2332{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002333 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2335
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2337
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002338 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339}
2340
2341static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2342{
2343 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002344 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2346
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2348
2349 /* cause */
2350 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002351 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 else
Harald Welte55c8f352010-03-07 23:40:35 +01002353 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002355 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356}
2357
2358static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2359{
2360 struct gsm_mncc retrieve;
2361
2362 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2363 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002364 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002365 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366}
2367
2368static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2369{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002370 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2372
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2374
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002375 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376}
2377
2378static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2379{
2380 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002381 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2383
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2385
2386 /* cause */
2387 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002388 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389 else
Harald Welte55c8f352010-03-07 23:40:35 +01002390 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002392 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393}
2394
2395static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2396{
2397 struct gsm48_hdr *gh = msgb_l3(msg);
2398 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2399 struct tlv_parsed tp;
2400 struct gsm_mncc dtmf;
2401
2402 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2403 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002404 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 /* keypad facility */
2406 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2407 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002408 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2410 }
2411
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002412 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413}
2414
2415static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2416{
2417 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002418 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2420
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2422
2423 /* keypad */
2424 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002425 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002427 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428}
2429
2430static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2431{
2432 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002433 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2435
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2437
2438 /* cause */
2439 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002440 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 else
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002444 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445}
2446
2447static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2448{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002449 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2451
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2453
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002454 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455}
2456
2457static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2458{
2459 struct gsm_mncc dtmf;
2460
2461 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2462 dtmf.callref = trans->callref;
2463
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002464 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465}
2466
2467static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2468{
2469 struct gsm48_hdr *gh = msgb_l3(msg);
2470 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2471 struct tlv_parsed tp;
2472 struct gsm_mncc modify;
2473
2474 memset(&modify, 0, sizeof(struct gsm_mncc));
2475 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002476 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 /* bearer capability */
2478 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2479 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002480 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002482
2483 /* Create a copy of the bearer capability
2484 * in the transaction struct, so we can use
2485 * this information later */
2486 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2487 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 }
2489
2490 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2491
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002492 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2496{
2497 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002498 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2500
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 gh->msg_type = GSM48_MT_CC_MODIFY;
2502
2503 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2504
2505 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507
2508 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2509
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002510 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2514{
2515 struct gsm48_hdr *gh = msgb_l3(msg);
2516 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2517 struct tlv_parsed tp;
2518 struct gsm_mncc modify;
2519
2520 gsm48_stop_cc_timer(trans);
2521
2522 memset(&modify, 0, sizeof(struct gsm_mncc));
2523 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002524 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 /* bearer capability */
2526 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2527 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002530
2531 /* Create a copy of the bearer capability
2532 * in the transaction struct, so we can use
2533 * this information later */
2534 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2535 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536 }
2537
2538 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2539
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002540 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541}
2542
2543static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2544{
2545 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002546 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2548
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2550
2551 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002552 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553
2554 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2555
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002556 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2560{
2561 struct gsm48_hdr *gh = msgb_l3(msg);
2562 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2563 struct tlv_parsed tp;
2564 struct gsm_mncc modify;
2565
2566 gsm48_stop_cc_timer(trans);
2567
2568 memset(&modify, 0, sizeof(struct gsm_mncc));
2569 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002570 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 /* bearer capability */
2572 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2573 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002576
2577 /* Create a copy of the bearer capability
2578 * in the transaction struct, so we can use
2579 * this information later */
2580 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2581 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 }
2583 /* cause */
2584 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2585 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002586 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2588 }
2589
2590 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2591
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002592 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2596{
2597 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002598 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2600
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2602
2603 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002604 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002606 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607
2608 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2609
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002610 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611}
2612
2613static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2614{
2615 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2618
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 gh->msg_type = GSM48_MT_CC_NOTIFY;
2620
2621 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002622 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002624 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625}
2626
2627static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2628{
2629 struct gsm48_hdr *gh = msgb_l3(msg);
2630 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2631// struct tlv_parsed tp;
2632 struct gsm_mncc notify;
2633
2634 memset(&notify, 0, sizeof(struct gsm_mncc));
2635 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002636// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002638 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002640 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641}
2642
2643static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2644{
2645 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002646 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2648
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 gh->msg_type = GSM48_MT_CC_USER_INFO;
2650
2651 /* user-user */
2652 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002653 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 /* more data */
2655 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002656 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002658 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659}
2660
2661static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2662{
2663 struct gsm48_hdr *gh = msgb_l3(msg);
2664 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2665 struct tlv_parsed tp;
2666 struct gsm_mncc user;
2667
2668 memset(&user, 0, sizeof(struct gsm_mncc));
2669 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002670 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 /* user-user */
2672 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2673 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2676 }
2677 /* more data */
2678 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2679 user.more = 1;
2680
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002681 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682}
2683
Philipp Maierfbf66102017-04-09 12:32:51 +02002684static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2685 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2686 uint32_t payload_msg_type)
2687{
2688 uint8_t data[sizeof(struct gsm_mncc)];
2689 struct gsm_mncc_rtp *rtp;
2690
2691 memset(&data, 0, sizeof(data));
2692 rtp = (struct gsm_mncc_rtp *) &data[0];
2693
2694 rtp->callref = callref;
2695 rtp->msg_type = cmd;
2696 rtp->ip = addr;
2697 rtp->port = port;
2698 rtp->payload_type = payload_type;
2699 rtp->payload_msg_type = payload_msg_type;
2700 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2701}
2702
2703static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2704{
2705 int msg_type;
2706
2707 /* FIXME This has to be set to some meaningful value.
2708 * Possible options are:
2709 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2710 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2711 * (0 if unknown) */
2712 msg_type = GSM_TCHF_FRAME;
2713
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +02002714 uint32_t addr = mgcp_client_remote_addr_n(net->mgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +02002715 uint16_t port = trans->conn->rtp.port_cn;
2716
2717 /* FIXME: This has to be set to some meaningful value,
2718 * before the MSC-Split, this value was pulled from
2719 * lchan->abis_ip.rtp_payload */
2720 uint32_t payload_type = 0;
2721
2722 return mncc_recv_rtp(net, trans->callref, cmd,
2723 addr,
2724 port,
2725 payload_type,
2726 msg_type);
2727}
2728
2729static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2730{
2731 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2732}
2733
2734static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2735{
2736 struct gsm_trans *trans;
2737 int rc;
2738
2739 /* Find callref */
2740 trans = trans_find_by_callref(net, callref);
2741 if (!trans) {
2742 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2743 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2744 return -EIO;
2745 }
2746 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2747 if (!trans->conn) {
2748 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2749 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2750 return 0;
2751 }
2752
2753 trans->conn->mncc_rtp_bridge = 1;
2754
2755 /* When we call msc_call_assignment() we will trigger, depending
2756 * on the RAN type the call assignment on the A or Iu interface.
2757 * msc_call_assignment() also takes care about sending the CRCX
2758 * command to the MGCP-GW. The CRCX will return the port number,
2759 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2760 * have to return this port number back to the MNCC by sending
2761 * it back with the TCH_RTP_CREATE message. To make sure that
2762 * this message is sent AFTER the response to CRCX from the
2763 * MGCP-GW has arrived, we need will instruct msc_call_assignment()
2764 * to take care of this by setting trans->tch_rtp_create to true.
2765 * This will make sure that gsm48_tch_rtp_create() (below) is
2766 * called as soon as the local port number has become known. */
2767 trans->tch_rtp_create = true;
2768
2769 /* Assign call (if not done yet) */
2770 if (trans->assignment_done == false) {
2771 rc = msc_call_assignment(trans);
2772 trans->assignment_done = true;
2773 }
2774 else
2775 rc = 0;
2776
2777 return rc;
2778}
2779
2780/* Trigger TCH_RTP_CREATE acknowledgement */
2781int gsm48_tch_rtp_create(struct gsm_trans *trans)
2782{
2783 /* This function is called as soon as the port, on which the
2784 * mgcp-gw expects the incoming RTP stream from the remote
2785 * end (e.g. Asterisk) is known. */
2786
2787 struct gsm_subscriber_connection *conn = trans->conn;
2788 struct gsm_network *network = conn->network;
2789
2790 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2791 return 0;
2792}
2793
2794static int tch_rtp_connect(struct gsm_network *net, void *arg)
2795{
2796 struct gsm_trans *trans;
2797 struct gsm_mncc_rtp *rtp = arg;
2798
2799 /* Find callref */
2800 trans = trans_find_by_callref(net, rtp->callref);
2801 if (!trans) {
2802 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2803 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2804 return -EIO;
2805 }
2806 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2807 if (!trans->conn) {
2808 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2809 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2810 return 0;
2811 }
2812
Philipp Maier2f108b02017-11-23 13:38:02 +01002813 msc_call_connect(trans, rtp->port, rtp->ip);
Philipp Maierfbf66102017-04-09 12:32:51 +02002814 return 0;
2815}
2816
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002818 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819 int type;
2820 int (*rout) (struct gsm_trans *trans, void *arg);
2821} downstatelist[] = {
2822 /* mobile originating call establishment */
2823 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002824 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2826 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2827 {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 */
2828 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2829 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2830 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2831 /* mobile terminating call establishment */
2832 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2833 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2834 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2835 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2836 /* signalling during call */
2837 {SBIT(GSM_CSTATE_ACTIVE),
2838 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2839 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2840 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2841 {ALL_STATES,
2842 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2843 {ALL_STATES,
2844 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2845 {ALL_STATES,
2846 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2847 {SBIT(GSM_CSTATE_ACTIVE),
2848 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2849 {SBIT(GSM_CSTATE_ACTIVE),
2850 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2851 {SBIT(GSM_CSTATE_ACTIVE),
2852 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2853 {SBIT(GSM_CSTATE_ACTIVE),
2854 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2855 {SBIT(GSM_CSTATE_ACTIVE),
2856 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2857 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2858 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2859 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2860 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2861 {SBIT(GSM_CSTATE_ACTIVE),
2862 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2863 /* clearing */
2864 {SBIT(GSM_CSTATE_INITIATED),
2865 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2866 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2867 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2868 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2869 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870};
2871
2872#define DOWNSLLEN \
2873 (sizeof(downstatelist) / sizeof(struct downstate))
2874
2875
Harald Welte76556372010-12-22 23:57:45 +01002876int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877{
Harald Welte1a6f7982009-08-09 18:52:33 +02002878 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002880 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 struct gsm_mncc *data = arg, rel;
2882
Harald Welte04dc88f2010-12-22 21:45:05 +01002883 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2884
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 /* handle special messages */
2886 switch(msg_type) {
2887 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002888 rc = tch_bridge(net, arg);
2889 if (rc < 0)
2890 disconnect_bridge(net, arg, -rc);
2891 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002892 case MNCC_RTP_CREATE:
2893 return tch_rtp_create(net, data->callref);
2894 case MNCC_RTP_CONNECT:
2895 return tch_rtp_connect(net, arg);
2896 case MNCC_RTP_FREE:
2897 /* unused right now */
2898 return -EIO;
2899
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002902 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002903 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002904 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002905 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002906 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2907 get_mncc_name(msg_type));
2908 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 }
2910
2911 memset(&rel, 0, sizeof(struct gsm_mncc));
2912 rel.callref = data->callref;
2913
2914 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002915 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916
2917 /* Callref unknown */
2918 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002919 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002920
Harald Welte4a3464c2009-07-04 10:11:24 +02002921 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2923 "Received '%s' from MNCC with "
2924 "unknown callref %d\n", data->called.number,
2925 get_mncc_name(msg_type), data->callref);
2926 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002927 return mncc_release_ind(net, NULL, data->callref,
2928 GSM48_CAUSE_LOC_PRN_S_LU,
2929 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002931 if (!data->called.number[0] && !data->imsi[0]) {
2932 DEBUGP(DCC, "(bts - trx - ts - ti) "
2933 "Received '%s' from MNCC with "
2934 "no number or IMSI\n", get_mncc_name(msg_type));
2935 /* Invalid number */
2936 return mncc_release_ind(net, NULL, data->callref,
2937 GSM48_CAUSE_LOC_PRN_S_LU,
2938 GSM48_CC_CAUSE_INV_NR_FORMAT);
2939 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002941 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002942 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2943 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002944 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002945 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002946
2947 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002948 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002949
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002951 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2953 "Received '%s' from MNCC with "
2954 "unknown subscriber %s\n", data->called.number,
2955 get_mncc_name(msg_type), data->called.number);
2956 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002957 return mncc_release_ind(net, NULL, data->callref,
2958 GSM48_CAUSE_LOC_PRN_S_LU,
2959 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960 }
2961 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002962 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2964 "Received '%s' from MNCC with "
2965 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01002966 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02002967 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002969 return mncc_release_ind(net, NULL, data->callref,
2970 GSM48_CAUSE_LOC_PRN_S_LU,
2971 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972 }
2973 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002974 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002975 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002977 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002979 mncc_release_ind(net, NULL, data->callref,
2980 GSM48_CAUSE_LOC_PRN_S_LU,
2981 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 return -ENOMEM;
2983 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002984
2985 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002986 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002987
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002988 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002989 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 /* find transaction with this subscriber already paging */
2991 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002992 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002994 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002995 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002996 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 "Received '%s' from MNCC with "
2998 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002999 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003001 get_mncc_name(msg_type), vsub->lac);
3002 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003003 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003004 return 0;
3005 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003006 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003007 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003008
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003009 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003010 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003011 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003012 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003013 trans,
3014 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003015 if (!trans->paging_request) {
3016 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003017 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003018 trans_free(trans);
3019 return 0;
3020 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003021 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022 return 0;
3023 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003024
3025 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003026 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003027 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003028 } else {
3029 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003030 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003031 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003032
3033 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003034 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035
3036 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003037 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003038 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003039 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003040 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003041 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003042 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3043 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 if (msg_type == MNCC_REL_REQ)
3045 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3046 else
3047 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3048 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003049 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003051 } else {
3052 DEBUGP(DCC, "(ti %02x sub %s) "
3053 "Received '%s' from MNCC in state %d (%s)\n",
3054 trans->transaction_id,
3055 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3056 get_mncc_name(msg_type), trans->cc.state,
3057 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 }
3059
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 /* Find function for current state and message */
3061 for (i = 0; i < DOWNSLLEN; i++)
3062 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003063 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 break;
3065 if (i == DOWNSLLEN) {
3066 DEBUGP(DCC, "Message unhandled at this state.\n");
3067 return 0;
3068 }
3069
3070 rc = downstatelist[i].rout(trans, arg);
3071
3072 return rc;
3073}
3074
3075
3076static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003077 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 int type;
3079 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3080} datastatelist[] = {
3081 /* mobile originating call establishment */
3082 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3083 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3084 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3085 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3086 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3087 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3088 /* mobile terminating call establishment */
3089 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3090 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3091 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3092 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003093 {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 +08003094 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3095 /* signalling during call */
3096 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3097 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3098 {SBIT(GSM_CSTATE_ACTIVE),
3099 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3100 {ALL_STATES,
3101 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3102 {ALL_STATES,
3103 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3104 {ALL_STATES,
3105 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3106 {SBIT(GSM_CSTATE_ACTIVE),
3107 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3108 {SBIT(GSM_CSTATE_ACTIVE),
3109 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3110 {SBIT(GSM_CSTATE_ACTIVE),
3111 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3112 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3113 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3114 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3115 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3116 {SBIT(GSM_CSTATE_ACTIVE),
3117 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3118 /* clearing */
3119 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3120 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3121 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3122 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3123 {ALL_STATES, /* 5.4.3.4 */
3124 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3125};
3126
3127#define DATASLLEN \
3128 (sizeof(datastatelist) / sizeof(struct datastate))
3129
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003130static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003131{
3132 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003133 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003134 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003135 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003136 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003137
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 if (msg_type & 0x80) {
3139 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3140 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003141 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003142
Harald Welte2483f1b2016-06-19 18:06:02 +02003143 if (!conn->vsub) {
3144 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003145 return -EINVAL;
3146 }
3147
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003149 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003150
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003151#if BEFORE_MSCSPLIT
3152 /* Re-enable this log output once we can obtain this information via
3153 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003154 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003155 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003156 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003157 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003158 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3159 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003160#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161
3162 /* Create transaction */
3163 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003164 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003165 "creating new trans.\n", transaction_id);
3166 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003167 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003168 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003169 transaction_id, new_callref++);
3170 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003172 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003173 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003174 GSM48_MT_CC_RELEASE_COMPL);
3175 return -ENOMEM;
3176 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003178 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003179 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180 }
3181
3182 /* find function for current state and message */
3183 for (i = 0; i < DATASLLEN; i++)
3184 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003185 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 break;
3187 if (i == DATASLLEN) {
3188 DEBUGP(DCC, "Message unhandled at this state.\n");
3189 return 0;
3190 }
3191
Harald Welte2483f1b2016-06-19 18:06:02 +02003192 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003193
Harald Welte4bfdfe72009-06-10 23:11:52 +08003194 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003195
Harald Welte2483f1b2016-06-19 18:06:02 +02003196 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003197 return rc;
3198}
3199
Harald Welte2483f1b2016-06-19 18:06:02 +02003200static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003201{
Harald Welte2483f1b2016-06-19 18:06:02 +02003202 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3203 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003204
Harald Welte2483f1b2016-06-19 18:06:02 +02003205 switch (pdisc) {
3206 case GSM48_PDISC_MM:
3207 switch (msg_type) {
3208 case GSM48_MT_MM_LOC_UPD_REQUEST:
3209 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003210 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003211 case GSM48_MT_MM_AUTH_RESP:
3212 case GSM48_MT_MM_AUTH_FAIL:
3213 case GSM48_MT_MM_ID_RESP:
3214 case GSM48_MT_MM_TMSI_REALL_COMPL:
3215 case GSM48_MT_MM_IMSI_DETACH_IND:
3216 return true;
3217 default:
3218 break;
3219 }
3220 break;
3221 case GSM48_PDISC_RR:
3222 switch (msg_type) {
3223 case GSM48_MT_RR_CIPH_M_COMPL:
3224 case GSM48_MT_RR_PAG_RESP:
3225 return true;
3226 default:
3227 break;
3228 }
3229 break;
3230 default:
3231 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003232 }
3233
Harald Welte2483f1b2016-06-19 18:06:02 +02003234 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003235}
3236
Harald Welte2483f1b2016-06-19 18:06:02 +02003237void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3238 struct msgb *msg)
3239{
3240
3241 /* If a CM Service Request was received before, this is the request the
3242 * conn was opened for. No need to wait for further messages. */
3243
3244 if (!conn->received_cm_service_request)
3245 return;
3246
3247 if (log_check_level(DMM, LOGL_DEBUG)) {
3248 struct gsm48_hdr *gh = msgb_l3(msg);
3249 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3250 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3251
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003252 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003253 " received_cm_service_request changes to false\n",
3254 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003255 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003256 }
3257 conn->received_cm_service_request = false;
3258}
3259
3260
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003261/* 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 +08003262int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003263{
Philipp Maier91f10c72017-04-20 18:40:37 +02003264 struct gsm48_hdr *gh;
3265 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003266 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003267
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003268 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003269 OSMO_ASSERT(conn);
3270 OSMO_ASSERT(msg);
3271
Philipp Maier91f10c72017-04-20 18:40:37 +02003272 gh = msgb_l3(msg);
3273 pdisc = gsm48_hdr_pdisc(gh);
3274
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003275 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3276 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3277 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003278
3279 if (!msc_subscr_conn_is_accepted(conn)
3280 && !msg_is_initially_permitted(gh)) {
3281 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003282 "subscr %s: Message not permitted for initial conn: %s\n",
3283 vlr_subscr_name(conn->vsub),
3284 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003285 return -EACCES;
3286 }
3287
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003288 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3289 LOGP(DMM, LOGL_ERROR,
3290 "%s: Illegal situation: RAN type mismatch:"
3291 " attached via %s, received message via %s\n",
3292 vlr_subscr_name(conn->vsub),
3293 ran_type_name(conn->vsub->cs.attached_via_ran),
3294 ran_type_name(conn->via_ran));
3295 return -EACCES;
3296 }
3297
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003298#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003299 if (silent_call_reroute(conn, msg))
3300 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003301#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003302
Harald Welte52b1f982008-12-23 20:25:15 +00003303 switch (pdisc) {
3304 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003305 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003306 break;
3307 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003308 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003309 break;
3310 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003311 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003312 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003313 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003314 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003315 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003316 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003317 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003318 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3319 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003320 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003321 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003322 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003323 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003324 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003325 case GSM48_PDISC_TEST:
3326 rc = gsm0414_rcv_test(conn, msg);
3327 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003328 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003329 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3330 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003331 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003332 break;
3333 }
3334
3335 return rc;
3336}
Harald Welte8470bf22008-12-25 23:28:35 +00003337
Harald Welte2483f1b2016-06-19 18:06:02 +02003338/***********************************************************************
3339 * VLR integration
3340 ***********************************************************************/
3341
3342/* VLR asks us to send an authentication request */
3343static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3344 bool send_autn)
3345{
3346 struct gsm_subscriber_connection *conn = msc_conn_ref;
3347 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3348 send_autn? at->vec.autn : NULL,
3349 at->key_seq);
3350}
3351
3352/* VLR asks us to send an authentication reject */
3353static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3354{
3355 struct gsm_subscriber_connection *conn = msc_conn_ref;
3356 return gsm48_tx_mm_auth_rej(conn);
3357}
3358
3359/* VLR asks us to transmit an Identity Request of given type */
3360static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3361{
3362 struct gsm_subscriber_connection *conn = msc_conn_ref;
3363 return mm_tx_identity_req(conn, mi_type);
3364}
3365
3366/* VLR asks us to transmit a Location Update Accept */
3367static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3368{
3369 struct gsm_subscriber_connection *conn = msc_conn_ref;
3370 return gsm0408_loc_upd_acc(conn, send_tmsi);
3371}
3372
3373/* VLR asks us to transmit a Location Update Reject */
3374static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3375{
3376 struct gsm_subscriber_connection *conn = msc_conn_ref;
3377 return gsm0408_loc_upd_rej(conn, cause);
3378}
3379
3380/* VLR asks us to transmit a CM Service Accept */
3381static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3382{
3383 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003384 return msc_gsm48_tx_mm_serv_ack(conn);
3385}
3386
3387static int msc_vlr_tx_common_id(void *msc_conn_ref)
3388{
3389 struct gsm_subscriber_connection *conn = msc_conn_ref;
3390 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003391}
3392
3393/* VLR asks us to transmit a CM Service Reject */
3394static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3395{
3396 uint8_t cause;
3397 struct gsm_subscriber_connection *conn = msc_conn_ref;
3398 conn->received_cm_service_request = false;
3399
3400 switch (result) {
3401 default:
3402 case VLR_PR_ARQ_RES_NONE:
3403 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3404 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3405 cause = GSM48_REJECT_NETWORK_FAILURE;
3406 break;
3407 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3408 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3409 break;
3410 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3411 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3412 break;
3413 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3414 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3415 break;
3416 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3417 cause = GSM48_REJECT_ILLEGAL_MS;
3418 break;
3419 case VLR_PR_ARQ_RES_TIMEOUT:
3420 cause = GSM48_REJECT_CONGESTION;
3421 break;
3422 };
3423
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003424 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003425}
3426
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003427/* For msc_vlr_set_ciph_mode() */
3428osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3429 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3430
Harald Welte2483f1b2016-06-19 18:06:02 +02003431/* VLR asks us to start using ciphering */
3432static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3433 enum vlr_ciph ciph,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01003434 bool umts_aka,
Harald Welte2483f1b2016-06-19 18:06:02 +02003435 bool retrieve_imeisv)
3436{
3437 struct gsm_subscriber_connection *conn = msc_conn_ref;
3438 struct vlr_subscr *vsub;
3439 struct gsm_auth_tuple *tuple;
3440
3441 if (!conn || !conn->vsub) {
3442 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3443 " NULL conn/subscriber");
3444 return -EINVAL;
3445 }
3446
3447 vsub = conn->vsub;
3448 tuple = vsub->last_tuple;
3449
3450 if (!tuple) {
3451 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3452 " Command: no auth tuple available\n",
3453 vlr_subscr_name(vsub));
3454 return -EINVAL;
3455 }
3456
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003457 switch (conn->via_ran) {
3458 case RAN_GERAN_A:
3459 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3460 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003461 {
3462 struct gsm0808_encrypt_info ei;
3463
3464 ei.perm_algo[0] = vlr_ciph_to_gsm0808_alg_id(ciph);
3465 ei.perm_algo_len = 1;
3466
Neels Hofmeyr52821712017-12-18 01:23:42 +01003467 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3468 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3469 * necessarily a match for the UMTS AKA tokens. */
3470 if (umts_aka)
3471 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3472 else
3473 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003474 ei.key_len = sizeof(tuple->vec.kc);
3475
3476 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3477 }
3478
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003479 case RAN_UTRAN_IU:
3480#ifdef BUILD_IU
3481 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3482 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003483 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003484#else
3485 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3486 " built without Iu support\n");
3487 return -ENOTSUP;
3488#endif
3489
3490 default:
3491 break;
3492 }
3493 LOGP(DMM, LOGL_ERROR,
3494 "%s: cannot start ciphering, unknown RAN type %d\n",
3495 vlr_subscr_name(conn->vsub), conn->via_ran);
3496 return -ENOTSUP;
3497}
3498
3499void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3500{
3501 struct vlr_ciph_result vlr_res = {};
3502
3503 if (!conn || !conn->vsub) {
3504 LOGP(DMM, LOGL_ERROR,
3505 "Rx Security Mode Complete for invalid conn\n");
3506 return;
3507 }
3508
3509 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3510 vlr_subscr_name(conn->vsub));
3511
3512 vlr_res.cause = VLR_CIPH_COMPL;
3513 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003514}
3515
3516/* VLR informs us that the subscriber data has somehow been modified */
3517static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3518{
Maxa263bb22017-12-27 13:23:44 +01003519 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3520 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003521}
3522
3523/* VLR informs us that the subscriber has been associated with a conn */
3524static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3525 struct vlr_subscr *vsub)
3526{
3527 struct gsm_subscriber_connection *conn = msc_conn_ref;
3528 OSMO_ASSERT(!conn->vsub);
3529 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003530 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003531}
3532
3533/* operations that we need to implement for libvlr */
3534static const struct vlr_ops msc_vlr_ops = {
3535 .tx_auth_req = msc_vlr_tx_auth_req,
3536 .tx_auth_rej = msc_vlr_tx_auth_rej,
3537 .tx_id_req = msc_vlr_tx_id_req,
3538 .tx_lu_acc = msc_vlr_tx_lu_acc,
3539 .tx_lu_rej = msc_vlr_tx_lu_rej,
3540 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3541 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3542 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003543 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003544 .subscr_update = msc_vlr_subscr_update,
3545 .subscr_assoc = msc_vlr_subscr_assoc,
3546};
3547
3548/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3549int msc_vlr_alloc(struct gsm_network *net)
3550{
3551 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3552 if (!net->vlr)
3553 return -ENOMEM;
3554 net->vlr->user_ctx = net;
3555 return 0;
3556}
3557
3558/* Launch the VLR, i.e. its GSUP connection */
3559int msc_vlr_start(struct gsm_network *net)
3560{
3561 OSMO_ASSERT(net->vlr);
3562 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3563 net->gsup_server_port);
3564}