blob: 3574583a6c108f26db919ebf12ef8260c03974e8 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/transaction.h>
47#include <osmocom/msc/ussd.h>
48#include <osmocom/msc/silent_call.h>
49#include <osmocom/msc/bsc_api.h>
50#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020051#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020052#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010053#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020054#include <osmocom/msc/vlr.h>
55#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080056
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010057#include <osmocom/gsm/gsm48.h>
58#include <osmocom/gsm/gsm0480.h>
59#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020060#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010061#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010063#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020072
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020073#include <assert.h>
74
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020075
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020076void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010077void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020078
Harald Welte2483f1b2016-06-19 18:06:02 +020079static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
80 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080081static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020090static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080091
Harald Welte31c00f72011-03-03 23:29:05 +010092void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
93{
94 net->mncc_recv(net, msg);
95}
96
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080097static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
98 struct gsm_trans *trans)
99{
100 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
101
102 /* if we get passed a transaction reference, do some common
103 * work that the caller no longer has to do */
104 if (trans) {
105 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800106 }
107
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200108 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800109}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100110
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800111int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
112{
113 struct gsm48_hdr *gh;
114 struct msgb *ss_notify;
115
116 ss_notify = gsm0480_create_notifySS(message);
117 if (!ss_notify)
118 return -1;
119
120 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
121 uint8_t *data = msgb_push(ss_notify, 1);
122 data[0] = ss_notify->len - 1;
123 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
124 gh->msg_type = GSM48_MT_CC_FACILITY;
125 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
126}
127
Harald Welte2483f1b2016-06-19 18:06:02 +0200128/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100129void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
130{
131 struct gsm_trans *trans, *temp;
132
133 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
134
135 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100136 if (trans->protocol == protocol) {
137 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100138 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100139 }
Harald Welte371efe52010-12-22 23:17:50 +0100140 }
141}
142
Holger Freyther429e7762008-12-30 13:28:30 +0000143/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200144static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000145{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800146 struct msgb *msg;
147
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800148 msg = gsm48_create_loc_upd_rej(cause);
149 if (!msg) {
150 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
151 return -1;
152 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200153
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200154 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
155 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100156
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800157 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000158}
159
160/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200161static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
162 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000163{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100164 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000165 struct gsm48_hdr *gh;
166 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200167 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200168
Harald Welte52b1f982008-12-23 20:25:15 +0000169 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
170 gh->proto_discr = GSM48_PDISC_MM;
171 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
172
173 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200174 gsm48_generate_lai(lai, conn->network->country_code,
175 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200176 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000177
Harald Welte2483f1b2016-06-19 18:06:02 +0200178 if (send_tmsi == GSM_RESERVED_TMSI) {
179 /* we did not allocate a TMSI to the MS, so we need to
180 * include the IMSI in order for the MS to delete any
181 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200182 uint8_t mi[10];
183 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200184 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200185 mid = msgb_put(msg, len);
186 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200187 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
188 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200189 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200190 /* Include the TMSI, which means that the MS will send a
191 * TMSI REALLOCATION COMPLETE, and we should wait for
192 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200193 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200194 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200195 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
196 vlr_subscr_name(conn->vsub),
197 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200198 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200199 /* TODO: Follow-on proceed */
200 /* TODO: CTS permission */
201 /* TODO: Equivalent PLMNs */
202 /* TODO: Emergency Number List */
203 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000204
Harald Welte52b1f982008-12-23 20:25:15 +0000205
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800206 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000207}
208
Harald Weltebf5e8df2009-02-03 12:59:45 +0000209/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200210static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000211{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100212 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000213 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000214
Harald Welte231ad4f2008-12-27 11:15:38 +0000215 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
216 gh->proto_discr = GSM48_PDISC_MM;
217 gh->msg_type = GSM48_MT_MM_ID_REQ;
218 gh->data[0] = id_type;
219
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800220 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000221}
222
Harald Weltebf5e8df2009-02-03 12:59:45 +0000223/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800224static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000225{
226 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200227 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200228 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000229
Harald Welte2483f1b2016-06-19 18:06:02 +0200230 if (!conn->vsub) {
231 LOGP(DMM, LOGL_ERROR,
232 "Rx MM Identity Response: invalid: no subscriber\n");
233 return -EINVAL;
234 }
235
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200236 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200237 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
238 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000239
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200240 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100241
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000243}
244
Harald Welte2483f1b2016-06-19 18:06:02 +0200245/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200246static const struct value_string lupd_names[] = {
247 { GSM48_LUPD_NORMAL, "NORMAL" },
248 { GSM48_LUPD_PERIODIC, "PERIODIC" },
249 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
250 { 0, NULL }
251};
Harald Welte2a139372009-02-22 21:14:55 +0000252
Harald Welte2483f1b2016-06-19 18:06:02 +0200253/* Chapter 9.2.15: Receive Location Updating Request.
254 * Keep this function non-static for direct invocation by unit tests. */
255int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000256{
Harald Welte2483f1b2016-06-19 18:06:02 +0200257 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
258 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000259 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000260 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200261 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200262 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200263 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200264 uint32_t tmsi;
265 char *imsi;
266 struct osmo_location_area_id old_lai, new_lai;
267 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200268 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200269 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000270
Harald Welte8470bf22008-12-25 23:28:35 +0000271 lu = (struct gsm48_loc_upd_req *) gh->data;
272
273 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000274
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200275 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000276
Harald Welte2483f1b2016-06-19 18:06:02 +0200277 rc = msc_create_conn_fsm(conn, mi_string);
278 if (rc)
279 /* logging already happened in msc_create_conn_fsm() */
280 return rc;
281
282 conn->classmark.classmark1 = lu->classmark1;
283 conn->classmark.classmark1_set = true;
284
285 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
286 gsm48_mi_type_name(mi_type), mi_string,
287 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000288
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200289 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100290
Harald Welte24ff6ee2009-12-22 00:41:05 +0100291 switch (lu->type) {
292 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200293 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200294 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100295 break;
296 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200297 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200298 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100299 break;
300 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200301 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200302 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100303 break;
304 }
305
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
307 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
308 /* TODO: 10.5.7.8 Device properties */
309 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200310
Harald Welte52b1f982008-12-23 20:25:15 +0000311 switch (mi_type) {
312 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200313 tmsi = GSM_RESERVED_TMSI;
314 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000315 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000316 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 tmsi = tmsi_from_string(mi_string);
318 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000319 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200320 default:
Harald Weltea0368542009-06-27 02:58:43 +0200321 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200322 tmsi = GSM_RESERVED_TMSI;
323 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000324 break;
325 }
326
Harald Welte2483f1b2016-06-19 18:06:02 +0200327 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
328 &old_lai.plmn.mnc, &old_lai.lac);
329 new_lai.plmn.mcc = conn->network->country_code;
330 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200331 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200332 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200333
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200334 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200335 lu_fsm = vlr_loc_update(conn->conn_fsm,
336 SUBSCR_CONN_E_ACCEPTED,
337 SUBSCR_CONN_E_CN_CLOSE,
338 (void*)&conn_from_lu,
339 net->vlr, conn, vlr_lu_type, tmsi, imsi,
340 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200341 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100342 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200343 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200344 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200345 net->vlr->cfg.assign_tmsi);
346 if (!lu_fsm) {
347 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
348 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800349 }
350
Harald Welte2483f1b2016-06-19 18:06:02 +0200351 /* From vlr_loc_update() we expect an implicit dispatch of
352 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
353 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000354
Harald Welte2483f1b2016-06-19 18:06:02 +0200355 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
356 LOGP(DRR, LOGL_ERROR,
357 "%s: internal error during Location Updating attempt\n",
358 mi_string);
359 return -EIO;
360 }
361
362 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000363}
364
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100365/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200366/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100367static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800368{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100369 uint8_t ret;
370
371 ret = value / 10;
372 ret |= (value % 10) << 4;
373
374 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800375}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100376
Harald Welte4bfdfe72009-06-10 23:11:52 +0800377
Harald Weltedb253af2008-12-30 17:56:55 +0000378/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800379int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000380{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100381 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000382 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200383 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200384 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200385 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100386
Harald Welte4bfdfe72009-06-10 23:11:52 +0800387 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100388 struct tm* gmt_time;
389 struct tm* local_time;
390 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100391 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000392
Harald Weltedb253af2008-12-30 17:56:55 +0000393 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
394 gh->proto_discr = GSM48_PDISC_MM;
395 gh->msg_type = GSM48_MT_MM_INFO;
396
397 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200398#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000399 name_len = strlen(net->name_long);
400 /* 10.5.3.5a */
401 ptr8 = msgb_put(msg, 3);
402 ptr8[0] = GSM48_IE_NAME_LONG;
403 ptr8[1] = name_len*2 +1;
404 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
405
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200406 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000407 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000408 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000409
410 /* FIXME: Use Cell Broadcast, not UCS-2, since
411 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200412#endif
413 name_len = (strlen(net->name_long)*7)/8;
414 name_pad = (8 - strlen(net->name_long)*7)%8;
415 if (name_pad > 0)
416 name_len++;
417 /* 10.5.3.5a */
418 ptr8 = msgb_put(msg, 3);
419 ptr8[0] = GSM48_IE_NAME_LONG;
420 ptr8[1] = name_len +1;
421 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
422
423 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100424 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200425
Harald Weltedb253af2008-12-30 17:56:55 +0000426 }
427
428 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200429#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000430 name_len = strlen(net->name_short);
431 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200432 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200433 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000434 ptr8[1] = name_len*2 + 1;
435 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
436
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200437 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000438 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000439 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200440#endif
441 name_len = (strlen(net->name_short)*7)/8;
442 name_pad = (8 - strlen(net->name_short)*7)%8;
443 if (name_pad > 0)
444 name_len++;
445 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200446 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200447 ptr8[0] = GSM48_IE_NAME_SHORT;
448 ptr8[1] = name_len +1;
449 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
450
451 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100452 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200453
Harald Weltedb253af2008-12-30 17:56:55 +0000454 }
455
Harald Weltedb253af2008-12-30 17:56:55 +0000456 /* Section 10.5.3.9 */
457 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100458 gmt_time = gmtime(&cur_t);
459
Harald Weltedb253af2008-12-30 17:56:55 +0000460 ptr8 = msgb_put(msg, 8);
461 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100462 ptr8[1] = bcdify(gmt_time->tm_year % 100);
463 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
464 ptr8[3] = bcdify(gmt_time->tm_mday);
465 ptr8[4] = bcdify(gmt_time->tm_hour);
466 ptr8[5] = bcdify(gmt_time->tm_min);
467 ptr8[6] = bcdify(gmt_time->tm_sec);
468
Neels Hofmeyr73983952016-05-10 13:29:33 +0200469 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200470 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200471 if (net->tz.hr < 0) {
472 tzunits = ((net->tz.hr/-1)*4);
473 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100474 ptr8[7] = bcdify(tzunits);
475 /* Set negative time */
476 ptr8[7] |= 0x08;
477 }
478 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200479 tzunits = net->tz.hr*4;
480 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100481 ptr8[7] = bcdify(tzunits);
482 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100483 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200484 if (net->tz.dst >= 0 && net->tz.dst <= 2)
485 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100486 }
487 else {
488 /* Need to get GSM offset and convert into 15 min units */
489 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200490#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200491 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100492 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200493#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200494 /* find timezone offset */
495 time_t utc;
496 double offsetFromUTC;
497 utc = mktime(gmt_time);
498 local_time = localtime(&cur_t);
499 offsetFromUTC = difftime(cur_t, utc);
500 if (local_time->tm_isdst)
501 offsetFromUTC += 3600.0;
502 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200503#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100504 if (tzunits < 0) {
505 tzunits = tzunits/-1;
506 ptr8[7] = bcdify(tzunits);
507 /* Flip it to negative */
508 ptr8[7] |= 0x08;
509 }
510 else
511 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100512
513 /* Does not support DST +2 */
514 if (local_time->tm_isdst)
515 dst = 1;
516 }
517
518 if (dst) {
519 ptr8 = msgb_put(msg, 3);
520 ptr8[0] = GSM48_IE_NET_DST;
521 ptr8[1] = 1;
522 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100523 }
Harald Weltedb253af2008-12-30 17:56:55 +0000524
Daniel Willmanneea93372009-08-13 03:42:07 +0200525 DEBUGP(DMM, "-> MM INFO\n");
526
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800527 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000528}
529
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100530/*! Send an Authentication Request to MS on the given subscriber connection
531 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
532 * \param[in] conn Subscriber connection to send on.
533 * \param[in] rand Random challenge token to send, must be 16 bytes long.
534 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
535 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
536 * \param[in] key_seq auth tuple's sequence number.
537 */
538int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
539 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200540{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100541 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200543 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200544
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100545 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100546 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100547 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200548
Harald Welte7984d5c2009-08-12 22:56:50 +0200549 gh->proto_discr = GSM48_PDISC_MM;
550 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
551
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100552 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200553
Harald Welte7984d5c2009-08-12 22:56:50 +0200554 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100555 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200556 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200557 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200558
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100559
560 /* 16 bytes AUTN */
561 if (autn)
562 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
563
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800564 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200565}
566
567/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800568int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200569{
570 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800571 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200572}
573
Harald Welte2483f1b2016-06-19 18:06:02 +0200574static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
575static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
576
577static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100578{
Harald Welte2483f1b2016-06-19 18:06:02 +0200579 uint8_t mi_type;
580 char mi_string[GSM48_MI_SIZE];
581 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100582
Harald Welte2483f1b2016-06-19 18:06:02 +0200583 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
584 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100585
Harald Welte2483f1b2016-06-19 18:06:02 +0200586 switch (mi_type) {
587 case GSM_MI_TYPE_IMSI:
588 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
589 goto accept_reuse;
590 break;
591 case GSM_MI_TYPE_TMSI:
592 tmsi = osmo_load32be(mi_lv+2);
593 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
594 goto accept_reuse;
595 break;
596 case GSM_MI_TYPE_IMEI:
597 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
598 goto accept_reuse;
599 break;
600 default:
601 break;
602 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100603
Harald Welte2483f1b2016-06-19 18:06:02 +0200604 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
605 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
606 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
607 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100608
Harald Welte2483f1b2016-06-19 18:06:02 +0200609accept_reuse:
610 DEBUGP(DMM, "%s: re-using already accepted connection\n",
611 vlr_subscr_name(conn->vsub));
612 conn->received_cm_service_request = true;
613 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100614}
615
Harald Welte4ed0e922009-01-10 03:17:30 +0000616/*
617 * Handle CM Service Requests
618 * a) Verify that the packet is long enough to contain the information
619 * we require otherwsie reject with INCORRECT_MESSAGE
620 * b) Try to parse the TMSI. If we do not have one reject
621 * c) Check that we know the subscriber with the TMSI otherwise reject
622 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200623 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200624 *
625 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000626 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200627int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000628{
Harald Welte2483f1b2016-06-19 18:06:02 +0200629 static const enum subscr_conn_from conn_from_cm_service_req =
630 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
631 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200632 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200633 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000634
Harald Welteba4cf162009-01-10 01:49:35 +0000635 struct gsm48_hdr *gh = msgb_l3(msg);
636 struct gsm48_service_request *req =
637 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800638 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200639 uint8_t classmark2_len = gh->data[1];
640 uint8_t *classmark2 = gh->data+2;
641 uint8_t mi_len = *(classmark2 + classmark2_len);
642 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200643 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200644 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200645 int rc;
646
647 lai.plmn.mcc = conn->network->country_code;
648 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200649 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000650
Harald Weltec9e02182009-05-01 19:07:53 +0000651 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000652 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000653 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200654 return msc_gsm48_tx_mm_serv_rej(conn,
655 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000656 }
657
658 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000659 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200660 return msc_gsm48_tx_mm_serv_rej(conn,
661 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000662 }
663
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200664 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000665 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100666 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
667 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200668
Harald Welte3995d2e2018-01-24 11:23:54 +0100669 switch (mi_type) {
670 case GSM_MI_TYPE_IMSI:
671 case GSM_MI_TYPE_TMSI:
672 /* continue below */
673 break;
674 case GSM_MI_TYPE_IMEI:
675 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
676 /* We don't do emergency calls by IMEI */
677 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
678 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
679 }
680 /* fall-through for non-emergency setup */
681 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200682 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200683 return msc_gsm48_tx_mm_serv_rej(conn,
684 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000685 }
686
Harald Welte37382ec2018-01-24 12:49:34 +0100687 switch (req->cm_service_type) {
688 case GSM48_CMSERV_MO_CALL_PACKET:
689 case GSM48_CMSERV_EMERGENCY:
690 case GSM48_CMSERV_SMS:
691 case GSM48_CMSERV_SUP_SERV:
692 /* continue below */
693 break;
694 default:
695 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
696 }
697
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200698 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200699 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
700 conn->classmark.classmark2_len = classmark2_len;
701
702 if (conn->conn_fsm) {
703 if (msc_subscr_conn_is_accepted(conn))
704 return cm_serv_reuse_conn(conn, mi-1);
705 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
706 vlr_subscr_name(conn->vsub));
707 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
708 return -EINVAL;
709 }
710
711 rc = msc_create_conn_fsm(conn, mi_string);
712 if (rc) {
713 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
714 /* logging already happened in msc_create_conn_fsm() */
715 return rc;
716 }
Harald Welte7659de12009-12-13 12:39:18 +0100717
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200718 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200719 vlr_proc_acc_req(conn->conn_fsm,
720 SUBSCR_CONN_E_ACCEPTED,
721 SUBSCR_CONN_E_CN_CLOSE,
722 (void*)&conn_from_cm_service_req,
723 net->vlr, conn,
724 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200725 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100726 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200727 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200728 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000729
Harald Welte2483f1b2016-06-19 18:06:02 +0200730 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000731}
732
Harald Welte49f8fcb2018-01-24 21:40:19 +0100733/* Receive a CM Re-establish Request */
734static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
735{
736 uint8_t mi_type;
737 char mi_string[GSM48_MI_SIZE];
738 struct gsm48_hdr *gh = msgb_l3(msg);
739
740 uint8_t classmark2_len = gh->data[1];
741 uint8_t *classmark2 = gh->data+2;
742 uint8_t mi_len = *(classmark2 + classmark2_len);
743 uint8_t *mi = (classmark2 + classmark2_len + 1);
744
745 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
746 mi_type = mi[0] & GSM_MI_TYPE_MASK;
747 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
748
749 /* we don't support CM call re-establishment */
750 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
751}
752
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200753static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000754{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200755 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000756 struct gsm48_hdr *gh = msgb_l3(msg);
757 struct gsm48_imsi_detach_ind *idi =
758 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200759 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200760 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200761 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000762
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200763 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200764 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
765 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000766
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200767 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100768
Harald Welte2a139372009-02-22 21:14:55 +0000769 switch (mi_type) {
770 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200771 vsub = vlr_subscr_find_by_tmsi(network->vlr,
772 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000773 break;
774 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200775 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000776 break;
777 case GSM_MI_TYPE_IMEI:
778 case GSM_MI_TYPE_IMEISV:
779 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
781 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000782 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200783 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200784 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
785 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000786 break;
787 }
788
Harald Welte2483f1b2016-06-19 18:06:02 +0200789 /* TODO? We used to remember the subscriber's classmark1 here and
790 * stored it in the old sqlite db, but now we store it in a conn that
791 * will be discarded anyway: */
792 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200793
Harald Welte2483f1b2016-06-19 18:06:02 +0200794 if (!vsub) {
795 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
796 gsm48_mi_type_name(mi_type), mi_string);
797 } else {
798 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100799
800 if (vsub->cs.is_paging)
801 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
802
Harald Welte2483f1b2016-06-19 18:06:02 +0200803 vlr_subscr_rx_imsi_detach(vsub);
804 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
805 vlr_subscr_put(vsub);
806 }
Harald Welte2a139372009-02-22 21:14:55 +0000807
Harald Welte2483f1b2016-06-19 18:06:02 +0200808 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000809 return 0;
810}
811
Harald Welted2a7f5a2009-06-05 20:08:20 +0000812static int gsm48_rx_mm_status(struct msgb *msg)
813{
814 struct gsm48_hdr *gh = msgb_l3(msg);
815
816 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
817
818 return 0;
819}
820
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100821static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
822 struct gsm_subscriber_connection *conn,
823 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100824{
825 struct gsm48_hdr *gh = msgb_l3(msg);
826 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100827
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100828 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
829 LOGP(DMM, LOGL_ERROR,
830 "%s: MM AUTHENTICATION RESPONSE:"
831 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200832 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100833 return -EINVAL;
834 }
835
836 *res_len = sizeof(ar->sres);
837 memcpy(res, ar->sres, sizeof(ar->sres));
838 return 0;
839}
840
841static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
842 struct gsm_subscriber_connection *conn,
843 struct msgb *msg)
844{
845 struct gsm48_hdr *gh;
846 uint8_t *data;
847 uint8_t iei;
848 uint8_t ie_len;
849 unsigned int data_len;
850
851 /* First parse the GSM part */
852 if (parse_gsm_auth_resp(res, res_len, conn, msg))
853 return -EINVAL;
854 OSMO_ASSERT(*res_len == 4);
855
856 /* Then add the extended res part */
857 gh = msgb_l3(msg);
858 data = gh->data + sizeof(struct gsm48_auth_resp);
859 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
860
861 if (data_len < 3) {
862 LOGP(DMM, LOGL_ERROR,
863 "%s: MM AUTHENTICATION RESPONSE:"
864 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200865 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100866 return -EINVAL;
867 }
868
869 iei = data[0];
870 ie_len = data[1];
871 if (iei != GSM48_IE_AUTH_RES_EXT) {
872 LOGP(DMM, LOGL_ERROR,
873 "%s: MM R99 AUTHENTICATION RESPONSE:"
874 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200875 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100876 GSM48_IE_AUTH_RES_EXT, iei);
877 return -EINVAL;
878 }
879
880 if (ie_len > 12) {
881 LOGP(DMM, LOGL_ERROR,
882 "%s: MM R99 AUTHENTICATION RESPONSE:"
883 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200884 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100885 return -EINVAL;
886 }
887
888 *res_len += ie_len;
889 memcpy(res + 4, &data[2], ie_len);
890 return 0;
891}
892
893/* Chapter 9.2.3: Authentication Response */
894static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
895{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100896 uint8_t res[16];
897 uint8_t res_len;
898 int rc;
899 bool is_r99;
900
Harald Welte2483f1b2016-06-19 18:06:02 +0200901 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100902 LOGP(DMM, LOGL_ERROR,
903 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200904 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100905 return -EINVAL;
906 }
907
908 if (msgb_l3len(msg) >
909 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
910 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
911 is_r99 = true;
912 } else {
913 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
914 is_r99 = false;
915 }
916
917 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200918 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100919 return -EINVAL;
920 }
921
922 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200923 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100924 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
925 osmo_hexdump_nospc(res, res_len));
926
Harald Welte2483f1b2016-06-19 18:06:02 +0200927 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
928 conn->via_ran == RAN_UTRAN_IU,
929 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100930}
931
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100932static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
933{
934 struct gsm48_hdr *gh = msgb_l3(msg);
935 uint8_t cause;
936 uint8_t auts_tag;
937 uint8_t auts_len;
938 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100939
Harald Welte2483f1b2016-06-19 18:06:02 +0200940 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100941 LOGP(DMM, LOGL_ERROR,
942 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200943 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100944 return -EINVAL;
945 }
946
947 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
948 LOGP(DMM, LOGL_ERROR,
949 "%s: MM R99 AUTHENTICATION FAILURE:"
950 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200951 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
952 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100953 return -EINVAL;
954 }
955
956 cause = gh->data[0];
957
958 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
959 LOGP(DMM, LOGL_INFO,
960 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200961 vlr_subscr_name(conn->vsub), cause);
962 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
963 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100964 }
965
966 /* This is a Synch Failure procedure, which should pass an AUTS to
967 * resynchronize the sequence nr with the HLR. Expecting exactly one
968 * TLV with 14 bytes of AUTS. */
969
970 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
971 LOGP(DMM, LOGL_INFO,
972 "%s: MM R99 AUTHENTICATION FAILURE:"
973 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200974 vlr_subscr_name(conn->vsub));
975 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100976 return -EINVAL;
977 }
978
979 auts_tag = gh->data[1];
980 auts_len = gh->data[2];
981 auts = &gh->data[3];
982
983 if (auts_tag != GSM48_IE_AUTS
984 || auts_len != 14) {
985 LOGP(DMM, LOGL_INFO,
986 "%s: MM R99 AUTHENTICATION FAILURE:"
987 " invalid Synch Failure:"
988 " expected AUTS IE 0x%02x of 14 bytes,"
989 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200990 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200992 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100993 return -EINVAL;
994 }
995
996 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
997 LOGP(DMM, LOGL_INFO,
998 "%s: MM R99 AUTHENTICATION FAILURE:"
999 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001000 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1001 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001002 return -EINVAL;
1003 }
1004
1005 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1006 * large enough. */
1007
1008 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001009 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001010
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1012}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001013
Harald Welte2483f1b2016-06-19 18:06:02 +02001014static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1015{
1016 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1017 vlr_subscr_name(conn->vsub));
1018 if (!conn->vsub) {
1019 LOGP(DMM, LOGL_ERROR,
1020 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1021 return -EINVAL;
1022 }
1023 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024}
1025
Harald Weltebf5e8df2009-02-03 12:59:45 +00001026/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001027static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001028{
1029 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001030 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001031
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001032 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001033 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001034 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001035 break;
1036 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001037 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001038 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001039 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001041 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001042 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001043 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001044 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001045 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001046 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001047 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001048 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001049 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001050 break;
1051 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001052 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001053 break;
1054 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001055 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001056 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001057 case GSM48_MT_MM_AUTH_FAIL:
1058 rc = gsm48_rx_mm_auth_fail(conn, msg);
1059 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001060 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001061 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001062 gh->msg_type);
1063 break;
1064 }
1065
1066 return rc;
1067}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001068
Harald Welte2483f1b2016-06-19 18:06:02 +02001069static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1070{
1071 /* Check the size for the classmark */
1072 if (tot_len < 1 + *classmark2_lv)
1073 return NULL;
1074
1075 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1076 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1077 return NULL;
1078
1079 return mi_lv;
1080}
1081
Harald Welte2d35ae62009-02-06 12:02:13 +00001082/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001083static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001084{
Harald Welte2483f1b2016-06-19 18:06:02 +02001085 static const enum subscr_conn_from conn_from_paging_resp =
1086 SUBSCR_CONN_FROM_PAGING_RESP;
1087 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001088 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001089 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001090 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001091 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001092 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001093 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001094 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001095 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001096 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001097
1098 lai.plmn.mcc = conn->network->country_code;
1099 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001100 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001101
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001102 resp = (struct gsm48_pag_resp *) &gh->data[0];
1103 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1104 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001105 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1106 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001107
Harald Welte2483f1b2016-06-19 18:06:02 +02001108 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1109 if (!mi_lv) {
1110 /* FIXME */
1111 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001112 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001113
Harald Welte2483f1b2016-06-19 18:06:02 +02001114 rc = msc_create_conn_fsm(conn, mi_string);
1115 if (rc)
1116 /* logging already happened in msc_create_conn_fsm() */
1117 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001118
Harald Welte2483f1b2016-06-19 18:06:02 +02001119 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1120 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001121
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001122 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001123 vlr_proc_acc_req(conn->conn_fsm,
1124 SUBSCR_CONN_E_ACCEPTED,
1125 SUBSCR_CONN_E_CN_CLOSE,
1126 (void*)&conn_from_paging_resp,
1127 net->vlr, conn,
1128 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001129 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001130 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001131 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001132 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001133
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001134 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001135}
1136
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001137static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001138{
1139 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001140 uint8_t apdu_id_flags;
1141 uint8_t apdu_len;
1142 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001143
1144 apdu_id_flags = gh->data[0];
1145 apdu_len = gh->data[1];
1146 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001147
Max8dc8f232017-02-09 19:13:02 +01001148 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001149 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001150
Harald Welte2483f1b2016-06-19 18:06:02 +02001151 /* we're not using the app info blob anywhere, so ignore. */
1152#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001153 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001154#else
1155 return 0;
1156#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001157}
1158
Harald Weltebf5e8df2009-02-03 12:59:45 +00001159/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001160static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001161{
1162 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001163 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001164
1165 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001166 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001167 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001168 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001169 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001170 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001171 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001172 default:
Philippd37981e2016-10-10 15:11:41 +02001173 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1174 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001175 break;
1176 }
1177
Harald Welte2d35ae62009-02-06 12:02:13 +00001178 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001179}
1180
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001181int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1182 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001183{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001184 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001185 struct gsm48_hdr *gh;
1186
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001187 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1188 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001189
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001190 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1191 gh->proto_discr = GSM48_PDISC_RR;
1192 gh->msg_type = GSM48_MT_RR_APP_INFO;
1193 gh->data[0] = apdu_id;
1194 gh->data[1] = apdu_len;
1195 memcpy(gh->data+2, apdu, apdu_len);
1196
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001197 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001198}
1199
Alexander Couzens92f552f2016-08-23 07:32:27 +02001200/* FIXME: this count_statistics is a state machine behaviour. we should convert
1201 * the complete call control into a state machine. Afterwards we can move this
1202 * code into state transitions.
1203 */
1204static void count_statistics(struct gsm_trans *trans, int new_state)
1205{
1206 int old_state = trans->cc.state;
1207 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1208
1209 if (old_state == new_state)
1210 return;
1211
1212 /* state incoming */
1213 switch (new_state) {
1214 case GSM_CSTATE_ACTIVE:
1215 osmo_counter_inc(trans->net->active_calls);
1216 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1217 break;
1218 }
1219
1220 /* state outgoing */
1221 switch (old_state) {
1222 case GSM_CSTATE_ACTIVE:
1223 osmo_counter_dec(trans->net->active_calls);
1224 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1225 new_state == GSM_CSTATE_DISCONNECT_IND)
1226 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1227 else
1228 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1229 break;
1230 }
1231}
1232
Harald Welte4bc90a12008-12-27 16:32:52 +00001233/* Call Control */
1234
Harald Welte7584aea2009-02-11 11:44:12 +00001235/* The entire call control code is written in accordance with Figure 7.10c
1236 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1237 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1238 * it for voice */
1239
Harald Welte4bfdfe72009-06-10 23:11:52 +08001240static void new_cc_state(struct gsm_trans *trans, int state)
1241{
1242 if (state > 31 || state < 0)
1243 return;
1244
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001245 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1246 trans->transaction_id,
1247 vlr_subscr_name(trans->vsub),
1248 gsm48_cc_state_name(trans->cc.state),
1249 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001250
Alexander Couzens92f552f2016-08-23 07:32:27 +02001251 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001252 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001253}
1254
1255static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001256{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001257 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001258 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001259 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001260
Harald Welte4bc90a12008-12-27 16:32:52 +00001261 gh->msg_type = GSM48_MT_CC_STATUS;
1262
1263 cause = msgb_put(msg, 3);
1264 cause[0] = 2;
1265 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1266 cause[2] = 0x80 | 30; /* response to status inquiry */
1267
1268 call_state = msgb_put(msg, 1);
1269 call_state[0] = 0xc0 | 0x00;
1270
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001271 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001272}
1273
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001274static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001275 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001276{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001277 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001278 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1279
Harald Welte6f4b7532008-12-29 00:39:37 +00001280 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001281 gh->msg_type = msg_type;
1282
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001283 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001284}
1285
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1287{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001288 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001289 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001290 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001291 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292 }
1293}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001294
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1296 int msg_type, struct gsm_mncc *mncc)
1297{
1298 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001299 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300
Philipp Maierfbf66102017-04-09 12:32:51 +02001301 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1302
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001303#if BEFORE_MSCSPLIT
1304 /* Re-enable this log output once we can obtain this information via
1305 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001307 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001308 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001310 trans->conn->lchan->ts->trx->bts->nr,
1311 trans->conn->lchan->ts->trx->nr,
1312 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001313 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 get_mncc_name(msg_type));
1315 else
1316 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1317 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001318 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001319 get_mncc_name(msg_type));
1320 else
1321 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1322 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001323#else
1324 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1325#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326
1327 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001328
Harald Welte966636f2009-06-26 19:39:35 +02001329 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 if (!msg)
1331 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001332
1333 data = msgb_put(msg, sizeof(struct gsm_mncc));
1334 memcpy(data, mncc, sizeof(struct gsm_mncc));
1335
Harald Welte54209c22010-12-22 23:43:29 +01001336 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337
1338 return 0;
1339}
1340
1341int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001342 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343{
1344 struct gsm_mncc rel;
1345
Harald Welte92f70c52009-06-12 01:54:08 +08001346 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001348 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001349 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1350 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1352}
1353
Harald Weltedcaf5652009-07-23 18:56:43 +02001354/* Call Control Specific transaction release.
1355 * gets called by trans_free, DO NOT CALL YOURSELF! */
1356void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 gsm48_stop_cc_timer(trans);
1359
Philipp Maierfbf66102017-04-09 12:32:51 +02001360 /* Make sure call also gets released on the mgcp side */
1361 msc_call_release(trans);
1362
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 /* send release to L4, if callref still exists */
1364 if (trans->callref) {
1365 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001366 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001367 GSM48_CAUSE_LOC_PRN_S_LU,
1368 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001370 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001371 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372}
1373
1374static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001375
Harald Welte09e38af2009-02-16 22:52:23 +00001376/* call-back from paging the B-end of the connection */
1377static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001378 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001379{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001380 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001381 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001382
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001383 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001384
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001385 switch (event) {
1386 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001387 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1388 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001389 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001390 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001391 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001392 /* send SETUP request to called party */
1393 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1394 break;
1395 case GSM_PAGING_EXPIRED:
1396 case GSM_PAGING_BUSY:
1397 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001398 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001399 /* Temporarily out of order */
1400 mncc_release_ind(transt->net, transt,
1401 transt->callref,
1402 GSM48_CAUSE_LOC_PRN_S_LU,
1403 GSM48_CC_CAUSE_DEST_OOO);
1404 transt->callref = 0;
1405 transt->paging_request = NULL;
1406 trans_free(transt);
1407 break;
1408 default:
1409 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1410 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001412
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001413 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001414 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001415}
Harald Welte7584aea2009-02-11 11:44:12 +00001416
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001418static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001419{
Harald Welte53d51f52015-12-03 14:59:04 +01001420 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1421 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001422
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001424 return -EIO;
1425
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001426 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 return -EIO;
1428
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001429 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001430 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001431
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001432 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433}
1434
Harald Welte4bfdfe72009-06-10 23:11:52 +08001435static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1436{
1437 DEBUGP(DCC, "-> STATUS ENQ\n");
1438 return gsm48_cc_tx_status(trans, msg);
1439}
1440
1441static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1442static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1443
1444static void gsm48_cc_timeout(void *arg)
1445{
1446 struct gsm_trans *trans = arg;
1447 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001448 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1449 int mo_location = GSM48_CAUSE_LOC_USER;
1450 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1451 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 struct gsm_mncc mo_rel, l4_rel;
1453
1454 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1455 mo_rel.callref = trans->callref;
1456 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1457 l4_rel.callref = trans->callref;
1458
Harald Weltedcaf5652009-07-23 18:56:43 +02001459 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001460 case 0x303:
1461 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001462 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001463 break;
1464 case 0x310:
1465 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001466 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467 break;
1468 case 0x313:
1469 disconnect = 1;
1470 /* unknown, did not find it in the specs */
1471 break;
1472 case 0x301:
1473 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001474 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001475 break;
1476 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001477 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001479 gsm48_cc_tx_release(trans, &trans->cc.msg);
1480 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001481 break; /* stay in release state */
1482 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001483 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001484 return;
1485// release = 1;
1486// l4_cause = 14;
1487// break;
1488 case 0x306:
1489 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001490 mo_cause = trans->cc.msg.cause.value;
1491 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 break;
1493 case 0x323:
1494 disconnect = 1;
1495 break;
1496 default:
1497 release = 1;
1498 }
1499
1500 if (release && trans->callref) {
1501 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001502 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503 l4_location, l4_cause);
1504 trans->callref = 0;
1505 }
1506
1507 if (disconnect && trans->callref) {
1508 /* process disconnect towards layer 4 */
1509 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001510 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001511 }
1512
1513 /* process disconnect towards mobile station */
1514 if (disconnect || release) {
1515 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001516 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1517 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1518 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001519 mo_rel.cause.diag_len = 3;
1520
1521 if (disconnect)
1522 gsm48_cc_tx_disconnect(trans, &mo_rel);
1523 if (release)
1524 gsm48_cc_tx_release(trans, &mo_rel);
1525 }
1526
1527}
1528
Max3ffce192016-04-25 15:22:00 +02001529/* disconnect both calls from the bridge */
1530static inline void disconnect_bridge(struct gsm_network *net,
1531 struct gsm_mncc_bridge *bridge, int err)
1532{
1533 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1534 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1535 struct gsm_mncc mx_rel;
1536 if (!trans0 || !trans1)
1537 return;
1538
1539 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1540 trans0->callref, trans1->callref, strerror(err));
1541
1542 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1543 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1544 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1545
1546 mx_rel.callref = trans0->callref;
1547 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1548
1549 mx_rel.callref = trans1->callref;
1550 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1551}
1552
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1554 int sec, int micro)
1555{
1556 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001557 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001558 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001559 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001560}
1561
1562static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1563{
1564 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001565 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001566 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1567 struct tlv_parsed tp;
1568 struct gsm_mncc setup;
1569
1570 memset(&setup, 0, sizeof(struct gsm_mncc));
1571 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001572
Harald Welte474d19f2010-03-02 23:18:30 +01001573 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001575 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1576 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 setup.emergency = 1;
Harald Welte0dd01372018-01-24 11:06:19 +01001578 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001579
1580 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001581 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001582 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1583 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001584
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 /* bearer capability */
1586 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1587 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001588 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001589 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001590
1591 /* Create a copy of the bearer capability
1592 * in the transaction struct, so we can use
1593 * this information later */
1594 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1595 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 }
1597 /* facility */
1598 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1599 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001600 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001601 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1602 }
1603 /* called party bcd number */
1604 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1605 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001606 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001607 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1608 }
1609 /* user-user */
1610 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1611 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001612 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001613 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1614 }
1615 /* ss-version */
1616 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1617 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001618 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001619 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1620 }
1621 /* CLIR suppression */
1622 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1623 setup.clir.sup = 1;
1624 /* CLIR invocation */
1625 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1626 setup.clir.inv = 1;
1627 /* cc cap */
1628 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1629 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001630 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001631 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1632 }
1633
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 new_cc_state(trans, GSM_CSTATE_INITIATED);
1635
Harald Welte79826e12018-01-24 10:50:24 +01001636 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1637 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001638 setup.called.number);
1639
Alexander Couzensb847a212016-08-02 11:34:11 +02001640 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001643 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644
Harald Welte13cac662009-07-29 12:10:35 +02001645 /* MNCC code will modify the channel asynchronously, we should
1646 * ipaccess-bind only after the modification has been made to the
1647 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001648 return 0;
1649}
1650
1651static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001652{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001653 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001654 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001656 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001657
Harald Welte7ccf7782009-02-17 01:43:01 +00001658 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001659
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660 /* transaction id must not be assigned */
1661 if (trans->transaction_id != 0xff) { /* unasssigned */
1662 DEBUGP(DCC, "TX Setup with assigned transaction. "
1663 "This is not allowed!\n");
1664 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001665 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001666 GSM48_CAUSE_LOC_PRN_S_LU,
1667 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001669 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 return rc;
1671 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001672
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001674 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001675 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001676 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001678 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001679 GSM48_CAUSE_LOC_PRN_S_LU,
1680 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001682 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 return rc;
1684 }
Harald Welte78283ef2009-07-23 21:36:44 +02001685 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001686
Harald Welte65e74cc2008-12-29 01:55:35 +00001687 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001688
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001690
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691 /* bearer capability */
1692 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001693 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 /* facility */
1695 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001696 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 /* progress */
1698 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001699 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 /* calling party BCD number */
1701 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001702 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 /* called party BCD number */
1704 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001705 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 /* user-user */
1707 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001708 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 /* redirecting party BCD number */
1710 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 /* signal */
1713 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001714 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001715
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001717
Alexander Couzensb847a212016-08-02 11:34:11 +02001718 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001719
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001720 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001721}
1722
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1724{
1725 struct gsm48_hdr *gh = msgb_l3(msg);
1726 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1727 struct tlv_parsed tp;
1728 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001729 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730
1731 gsm48_stop_cc_timer(trans);
1732 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1733
1734 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1735 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001736
Harald Welte474d19f2010-03-02 23:18:30 +01001737 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738#if 0
1739 /* repeat */
1740 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1741 call_conf.repeat = 1;
1742 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1743 call_conf.repeat = 2;
1744#endif
1745 /* bearer capability */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1747 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001748 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001750
1751 /* Create a copy of the bearer capability
1752 * in the transaction struct, so we can use
1753 * this information later */
1754 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1755 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 }
1757 /* cause */
1758 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1759 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001760 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1762 }
1763 /* cc cap */
1764 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1765 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001766 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1768 }
1769
Andreas Eversberg035b8742013-09-19 09:27:06 +02001770 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001771 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001772
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1774
Philipp Maierfbf66102017-04-09 12:32:51 +02001775 /* Assign call (if not done yet) */
1776 if (trans->assignment_done == false) {
1777 rc = msc_call_assignment(trans);
1778 trans->assignment_done = true;
1779 }
1780 else
1781 rc = 0;
1782
1783 /* don't continue, if there were problems with
1784 * the call assignment. */
1785 if (rc)
1786 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001787
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001788 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001789 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790}
1791
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001792static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793{
1794 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001795 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001797 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1800
1801 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1802
1803 /* bearer capability */
1804 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001805 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001806 /* facility */
1807 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001808 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 /* progress */
1810 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001811 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001813 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1814 if (rc)
1815 return rc;
1816
Philipp Maierfbf66102017-04-09 12:32:51 +02001817 /* Assign call (if not done yet) */
1818 if (trans->assignment_done == false) {
1819 rc = msc_call_assignment(trans);
1820 trans->assignment_done = true;
1821 }
1822 else
1823 rc = 0;
1824
1825 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826}
1827
1828static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1829{
1830 struct gsm48_hdr *gh = msgb_l3(msg);
1831 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1832 struct tlv_parsed tp;
1833 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001834
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835 gsm48_stop_cc_timer(trans);
1836 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1837
1838 memset(&alerting, 0, sizeof(struct gsm_mncc));
1839 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001840 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 /* facility */
1842 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1843 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001844 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1846 }
1847
1848 /* progress */
1849 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1850 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001851 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1853 }
1854 /* ss-version */
1855 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1856 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001857 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1859 }
1860
1861 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1862
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001863 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001864 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865}
1866
1867static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1868{
1869 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001870 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1872
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 gh->msg_type = GSM48_MT_CC_ALERTING;
1874
1875 /* facility */
1876 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* progress */
1879 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* user-user */
1882 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884
1885 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001886
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001887 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888}
1889
1890static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1891{
1892 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001893 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_PROGRESS;
1897
1898 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001899 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 /* user-user */
1901 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001904 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1908{
1909 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001910 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 gh->msg_type = GSM48_MT_CC_CONNECT;
1914
1915 gsm48_stop_cc_timer(trans);
1916 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1917
1918 /* facility */
1919 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 /* progress */
1922 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 /* connected number */
1925 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 /* user-user */
1928 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930
1931 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1932
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001933 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934}
1935
1936static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1937{
1938 struct gsm48_hdr *gh = msgb_l3(msg);
1939 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1940 struct tlv_parsed tp;
1941 struct gsm_mncc connect;
1942
1943 gsm48_stop_cc_timer(trans);
1944
1945 memset(&connect, 0, sizeof(struct gsm_mncc));
1946 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001947 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001949 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001950 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1951 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001952
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 /* facility */
1954 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1955 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1958 }
1959 /* user-user */
1960 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1961 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1964 }
1965 /* ss-version */
1966 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1967 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001968 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1970 }
1971
1972 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001973 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001975 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976}
1977
1978
1979static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1980{
1981 struct gsm_mncc connect_ack;
1982
1983 gsm48_stop_cc_timer(trans);
1984
1985 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001986 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001987
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1989 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001990
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001991 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 &connect_ack);
1993}
1994
1995static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1996{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001997 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1999
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2001
2002 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2003
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002004 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005}
2006
2007static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2008{
2009 struct gsm48_hdr *gh = msgb_l3(msg);
2010 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2011 struct tlv_parsed tp;
2012 struct gsm_mncc disc;
2013
2014 gsm48_stop_cc_timer(trans);
2015
2016 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2017
2018 memset(&disc, 0, sizeof(struct gsm_mncc));
2019 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002020 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 /* cause */
2022 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2023 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2026 }
2027 /* facility */
2028 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2029 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2032 }
2033 /* user-user */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2035 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002036 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2038 }
2039 /* ss-version */
2040 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2041 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2044 }
2045
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002046 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047
2048}
2049
Harald Weltec66b71c2009-06-11 14:23:20 +08002050static struct gsm_mncc_cause default_cause = {
2051 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2052 .coding = 0,
2053 .rec = 0,
2054 .rec_val = 0,
2055 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2056 .diag_len = 0,
2057 .diag = { 0 },
2058};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059
2060static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2061{
2062 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002063 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2065
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2067
2068 gsm48_stop_cc_timer(trans);
2069 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2070
2071 /* cause */
2072 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002073 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074 else
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076
2077 /* facility */
2078 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002079 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 /* progress */
2081 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002082 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* user-user */
2084 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002085 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086
2087 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002088 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089
2090 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2091
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002092 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093}
2094
2095static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2096{
2097 struct gsm48_hdr *gh = msgb_l3(msg);
2098 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2099 struct tlv_parsed tp;
2100 struct gsm_mncc rel;
2101 int rc;
2102
2103 gsm48_stop_cc_timer(trans);
2104
2105 memset(&rel, 0, sizeof(struct gsm_mncc));
2106 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002107 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 /* cause */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2110 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2113 }
2114 /* facility */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2116 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2119 }
2120 /* user-user */
2121 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2122 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2125 }
2126 /* ss-version */
2127 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2128 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2131 }
2132
Harald Weltedcaf5652009-07-23 18:56:43 +02002133 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002135 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002137 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002138 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002139 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002140 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 }
2142
2143 new_cc_state(trans, GSM_CSTATE_NULL);
2144
2145 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002146 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002147
2148 return rc;
2149}
2150
2151static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2152{
2153 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002154 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2156
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 gh->msg_type = GSM48_MT_CC_RELEASE;
2158
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 gsm48_stop_cc_timer(trans);
2160 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2161
2162 /* cause */
2163 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 /* facility */
2166 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002167 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 /* user-user */
2169 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171
Harald Weltedcaf5652009-07-23 18:56:43 +02002172 trans->cc.T308_second = 0;
2173 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174
Harald Weltedcaf5652009-07-23 18:56:43 +02002175 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2177
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002178 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179}
2180
2181static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2182{
2183 struct gsm48_hdr *gh = msgb_l3(msg);
2184 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2185 struct tlv_parsed tp;
2186 struct gsm_mncc rel;
2187 int rc = 0;
2188
2189 gsm48_stop_cc_timer(trans);
2190
2191 memset(&rel, 0, sizeof(struct gsm_mncc));
2192 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002193 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 /* cause */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2196 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002197 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2199 }
2200 /* facility */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2202 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2205 }
2206 /* user-user */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2208 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2211 }
2212 /* ss-version */
2213 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2214 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002215 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2217 }
2218
2219 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002220 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002222 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 MNCC_REJ_IND, &rel);
2224 break;
2225 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002226 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 MNCC_REL_CNF, &rel);
2228 break;
2229 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002230 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 MNCC_REL_IND, &rel);
2232 }
2233 }
2234
2235 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002236 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237
2238 return rc;
2239}
2240
2241static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2242{
2243 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002244 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002246 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2249
2250 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002251
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 gsm48_stop_cc_timer(trans);
2253
2254 /* cause */
2255 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 /* facility */
2258 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 /* user-user */
2261 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263
Harald Weltec7782de2010-12-21 19:31:41 +01002264 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2265
Harald Weltedcaf5652009-07-23 18:56:43 +02002266 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267
Harald Weltec7782de2010-12-21 19:31:41 +01002268 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269}
2270
2271static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2272{
2273 struct gsm48_hdr *gh = msgb_l3(msg);
2274 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2275 struct tlv_parsed tp;
2276 struct gsm_mncc fac;
2277
2278 memset(&fac, 0, sizeof(struct gsm_mncc));
2279 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002280 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 /* facility */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2283 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2286 }
2287 /* ss-version */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2289 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2292 }
2293
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002294 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295}
2296
2297static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2298{
2299 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002300 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2302
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 gh->msg_type = GSM48_MT_CC_FACILITY;
2304
2305 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002306 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002308 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309}
2310
2311static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2312{
2313 struct gsm_mncc hold;
2314
2315 memset(&hold, 0, sizeof(struct gsm_mncc));
2316 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002317 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318}
2319
2320static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2321{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002322 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2324
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2326
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002327 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2331{
2332 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002333 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
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_REJ;
2337
2338 /* cause */
2339 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002340 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341 else
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002344 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345}
2346
2347static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2348{
2349 struct gsm_mncc retrieve;
2350
2351 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2352 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002353 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002354 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355}
2356
2357static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2358{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002359 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2361
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2363
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002364 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365}
2366
2367static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2368{
2369 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002370 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
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_REJ;
2374
2375 /* cause */
2376 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002377 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 else
Harald Welte55c8f352010-03-07 23:40:35 +01002379 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002381 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382}
2383
2384static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2385{
2386 struct gsm48_hdr *gh = msgb_l3(msg);
2387 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2388 struct tlv_parsed tp;
2389 struct gsm_mncc dtmf;
2390
2391 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2392 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 /* keypad facility */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2396 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002397 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2399 }
2400
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002401 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402}
2403
2404static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2405{
2406 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002407 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2409
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2411
2412 /* keypad */
2413 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002414 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002416 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417}
2418
2419static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2420{
2421 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002422 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2424
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2426
2427 /* cause */
2428 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002429 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 else
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002433 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434}
2435
2436static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2437{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002438 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2440
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2442
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002443 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2447{
2448 struct gsm_mncc dtmf;
2449
2450 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2451 dtmf.callref = trans->callref;
2452
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002453 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm48_hdr *gh = msgb_l3(msg);
2459 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2460 struct tlv_parsed tp;
2461 struct gsm_mncc modify;
2462
2463 memset(&modify, 0, sizeof(struct gsm_mncc));
2464 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002465 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 /* bearer capability */
2467 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2468 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002469 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002471
2472 /* Create a copy of the bearer capability
2473 * in the transaction struct, so we can use
2474 * this information later */
2475 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2476 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 }
2478
2479 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2480
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002481 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2485{
2486 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002487 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_MODIFY;
2491
2492 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2493
2494 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496
2497 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2498
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002499 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500}
2501
2502static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2503{
2504 struct gsm48_hdr *gh = msgb_l3(msg);
2505 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2506 struct tlv_parsed tp;
2507 struct gsm_mncc modify;
2508
2509 gsm48_stop_cc_timer(trans);
2510
2511 memset(&modify, 0, sizeof(struct gsm_mncc));
2512 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002513 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514 /* bearer capability */
2515 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2516 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002517 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002519
2520 /* Create a copy of the bearer capability
2521 * in the transaction struct, so we can use
2522 * this information later */
2523 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2524 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 }
2526
2527 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2528
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002529 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530}
2531
2532static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2533{
2534 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002535 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2537
Harald Welte4bfdfe72009-06-10 23:11:52 +08002538 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2539
2540 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002541 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542
2543 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2544
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002545 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002546}
2547
2548static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2549{
2550 struct gsm48_hdr *gh = msgb_l3(msg);
2551 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2552 struct tlv_parsed tp;
2553 struct gsm_mncc modify;
2554
2555 gsm48_stop_cc_timer(trans);
2556
2557 memset(&modify, 0, sizeof(struct gsm_mncc));
2558 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002559 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 /* bearer capability */
2561 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2562 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002563 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002565
2566 /* Create a copy of the bearer capability
2567 * in the transaction struct, so we can use
2568 * this information later */
2569 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2570 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 }
2572 /* cause */
2573 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2574 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002575 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2577 }
2578
2579 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2580
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002581 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582}
2583
2584static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2585{
2586 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002587 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2589
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2591
2592 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002595 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596
2597 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2598
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002599 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600}
2601
2602static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2603{
2604 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002605 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2607
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608 gh->msg_type = GSM48_MT_CC_NOTIFY;
2609
2610 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002611 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002613 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614}
2615
2616static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2617{
2618 struct gsm48_hdr *gh = msgb_l3(msg);
2619 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2620// struct tlv_parsed tp;
2621 struct gsm_mncc notify;
2622
2623 memset(&notify, 0, sizeof(struct gsm_mncc));
2624 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002625// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002627 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002629 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2633{
2634 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002635 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2637
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 gh->msg_type = GSM48_MT_CC_USER_INFO;
2639
2640 /* user-user */
2641 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002642 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643 /* more data */
2644 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002647 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2651{
2652 struct gsm48_hdr *gh = msgb_l3(msg);
2653 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2654 struct tlv_parsed tp;
2655 struct gsm_mncc user;
2656
2657 memset(&user, 0, sizeof(struct gsm_mncc));
2658 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002659 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 /* user-user */
2661 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2662 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002663 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2665 }
2666 /* more data */
2667 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2668 user.more = 1;
2669
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002670 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671}
2672
Philipp Maierfbf66102017-04-09 12:32:51 +02002673static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2674 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2675 uint32_t payload_msg_type)
2676{
2677 uint8_t data[sizeof(struct gsm_mncc)];
2678 struct gsm_mncc_rtp *rtp;
2679
2680 memset(&data, 0, sizeof(data));
2681 rtp = (struct gsm_mncc_rtp *) &data[0];
2682
2683 rtp->callref = callref;
2684 rtp->msg_type = cmd;
2685 rtp->ip = addr;
2686 rtp->port = port;
2687 rtp->payload_type = payload_type;
2688 rtp->payload_msg_type = payload_msg_type;
2689 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2690}
2691
2692static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2693{
2694 int msg_type;
2695
2696 /* FIXME This has to be set to some meaningful value.
2697 * Possible options are:
2698 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2699 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2700 * (0 if unknown) */
2701 msg_type = GSM_TCHF_FRAME;
2702
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +02002703 uint32_t addr = mgcp_client_remote_addr_n(net->mgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +02002704 uint16_t port = trans->conn->rtp.port_cn;
2705
2706 /* FIXME: This has to be set to some meaningful value,
2707 * before the MSC-Split, this value was pulled from
2708 * lchan->abis_ip.rtp_payload */
2709 uint32_t payload_type = 0;
2710
2711 return mncc_recv_rtp(net, trans->callref, cmd,
2712 addr,
2713 port,
2714 payload_type,
2715 msg_type);
2716}
2717
2718static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2719{
2720 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2721}
2722
2723static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2724{
2725 struct gsm_trans *trans;
2726 int rc;
2727
2728 /* Find callref */
2729 trans = trans_find_by_callref(net, callref);
2730 if (!trans) {
2731 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2732 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2733 return -EIO;
2734 }
2735 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2736 if (!trans->conn) {
2737 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2738 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2739 return 0;
2740 }
2741
2742 trans->conn->mncc_rtp_bridge = 1;
2743
2744 /* When we call msc_call_assignment() we will trigger, depending
2745 * on the RAN type the call assignment on the A or Iu interface.
2746 * msc_call_assignment() also takes care about sending the CRCX
2747 * command to the MGCP-GW. The CRCX will return the port number,
2748 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2749 * have to return this port number back to the MNCC by sending
2750 * it back with the TCH_RTP_CREATE message. To make sure that
2751 * this message is sent AFTER the response to CRCX from the
2752 * MGCP-GW has arrived, we need will instruct msc_call_assignment()
2753 * to take care of this by setting trans->tch_rtp_create to true.
2754 * This will make sure that gsm48_tch_rtp_create() (below) is
2755 * called as soon as the local port number has become known. */
2756 trans->tch_rtp_create = true;
2757
2758 /* Assign call (if not done yet) */
2759 if (trans->assignment_done == false) {
2760 rc = msc_call_assignment(trans);
2761 trans->assignment_done = true;
2762 }
2763 else
2764 rc = 0;
2765
2766 return rc;
2767}
2768
2769/* Trigger TCH_RTP_CREATE acknowledgement */
2770int gsm48_tch_rtp_create(struct gsm_trans *trans)
2771{
2772 /* This function is called as soon as the port, on which the
2773 * mgcp-gw expects the incoming RTP stream from the remote
2774 * end (e.g. Asterisk) is known. */
2775
2776 struct gsm_subscriber_connection *conn = trans->conn;
2777 struct gsm_network *network = conn->network;
2778
2779 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2780 return 0;
2781}
2782
2783static int tch_rtp_connect(struct gsm_network *net, void *arg)
2784{
2785 struct gsm_trans *trans;
2786 struct gsm_mncc_rtp *rtp = arg;
2787
2788 /* Find callref */
2789 trans = trans_find_by_callref(net, rtp->callref);
2790 if (!trans) {
2791 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2792 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2793 return -EIO;
2794 }
2795 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2796 if (!trans->conn) {
2797 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2798 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2799 return 0;
2800 }
2801
Philipp Maier2f108b02017-11-23 13:38:02 +01002802 msc_call_connect(trans, rtp->port, rtp->ip);
Philipp Maierfbf66102017-04-09 12:32:51 +02002803 return 0;
2804}
2805
Harald Welte4bfdfe72009-06-10 23:11:52 +08002806static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002807 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 int type;
2809 int (*rout) (struct gsm_trans *trans, void *arg);
2810} downstatelist[] = {
2811 /* mobile originating call establishment */
2812 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002813 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2815 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2816 {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 */
2817 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2818 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2819 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2820 /* mobile terminating call establishment */
2821 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2822 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2823 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2824 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2825 /* signalling during call */
2826 {SBIT(GSM_CSTATE_ACTIVE),
2827 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2828 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2829 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2830 {ALL_STATES,
2831 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2832 {ALL_STATES,
2833 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2834 {ALL_STATES,
2835 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2836 {SBIT(GSM_CSTATE_ACTIVE),
2837 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2838 {SBIT(GSM_CSTATE_ACTIVE),
2839 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2840 {SBIT(GSM_CSTATE_ACTIVE),
2841 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2842 {SBIT(GSM_CSTATE_ACTIVE),
2843 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2844 {SBIT(GSM_CSTATE_ACTIVE),
2845 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2846 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2847 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2848 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2849 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2850 {SBIT(GSM_CSTATE_ACTIVE),
2851 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2852 /* clearing */
2853 {SBIT(GSM_CSTATE_INITIATED),
2854 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2855 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2856 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2857 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2858 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859};
2860
2861#define DOWNSLLEN \
2862 (sizeof(downstatelist) / sizeof(struct downstate))
2863
2864
Harald Welte76556372010-12-22 23:57:45 +01002865int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002866{
Harald Welte1a6f7982009-08-09 18:52:33 +02002867 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002868 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002869 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870 struct gsm_mncc *data = arg, rel;
2871
Harald Welte04dc88f2010-12-22 21:45:05 +01002872 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2873
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874 /* handle special messages */
2875 switch(msg_type) {
2876 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002877 rc = tch_bridge(net, arg);
2878 if (rc < 0)
2879 disconnect_bridge(net, arg, -rc);
2880 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002881 case MNCC_RTP_CREATE:
2882 return tch_rtp_create(net, data->callref);
2883 case MNCC_RTP_CONNECT:
2884 return tch_rtp_connect(net, arg);
2885 case MNCC_RTP_FREE:
2886 /* unused right now */
2887 return -EIO;
2888
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002890 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002891 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002892 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002893 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002894 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002895 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2896 get_mncc_name(msg_type));
2897 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898 }
2899
2900 memset(&rel, 0, sizeof(struct gsm_mncc));
2901 rel.callref = data->callref;
2902
2903 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002904 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905
2906 /* Callref unknown */
2907 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002908 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002909
Harald Welte4a3464c2009-07-04 10:11:24 +02002910 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2912 "Received '%s' from MNCC with "
2913 "unknown callref %d\n", data->called.number,
2914 get_mncc_name(msg_type), data->callref);
2915 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002916 return mncc_release_ind(net, NULL, data->callref,
2917 GSM48_CAUSE_LOC_PRN_S_LU,
2918 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002920 if (!data->called.number[0] && !data->imsi[0]) {
2921 DEBUGP(DCC, "(bts - trx - ts - ti) "
2922 "Received '%s' from MNCC with "
2923 "no number or IMSI\n", get_mncc_name(msg_type));
2924 /* Invalid number */
2925 return mncc_release_ind(net, NULL, data->callref,
2926 GSM48_CAUSE_LOC_PRN_S_LU,
2927 GSM48_CC_CAUSE_INV_NR_FORMAT);
2928 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002930 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002931 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2932 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002933 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002934 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002935
2936 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002937 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002938
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002940 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2942 "Received '%s' from MNCC with "
2943 "unknown subscriber %s\n", data->called.number,
2944 get_mncc_name(msg_type), data->called.number);
2945 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002946 return mncc_release_ind(net, NULL, data->callref,
2947 GSM48_CAUSE_LOC_PRN_S_LU,
2948 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 }
2950 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002951 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2953 "Received '%s' from MNCC with "
2954 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01002955 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02002956 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002958 return mncc_release_ind(net, NULL, data->callref,
2959 GSM48_CAUSE_LOC_PRN_S_LU,
2960 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 }
2962 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002963 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002964 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002966 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002968 mncc_release_ind(net, NULL, data->callref,
2969 GSM48_CAUSE_LOC_PRN_S_LU,
2970 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971 return -ENOMEM;
2972 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002973
2974 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002975 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002976
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002977 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002978 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979 /* find transaction with this subscriber already paging */
2980 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002981 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002983 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002984 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002985 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 "Received '%s' from MNCC with "
2987 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002988 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02002990 get_mncc_name(msg_type), vsub->lac);
2991 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002992 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 return 0;
2994 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002995 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002996 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002997
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002998 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002999 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003000 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003001 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003002 trans,
3003 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003004 if (!trans->paging_request) {
3005 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003006 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003007 trans_free(trans);
3008 return 0;
3009 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003010 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 return 0;
3012 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003013
3014 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003015 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003016 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003017 } else {
3018 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003019 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003020 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003021
3022 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003023 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024
3025 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003026 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003027 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003028 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003029 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003030 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003031 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3032 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 if (msg_type == MNCC_REL_REQ)
3034 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3035 else
3036 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3037 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003038 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003040 } else {
3041 DEBUGP(DCC, "(ti %02x sub %s) "
3042 "Received '%s' from MNCC in state %d (%s)\n",
3043 trans->transaction_id,
3044 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3045 get_mncc_name(msg_type), trans->cc.state,
3046 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 }
3048
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 /* Find function for current state and message */
3050 for (i = 0; i < DOWNSLLEN; i++)
3051 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003052 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 break;
3054 if (i == DOWNSLLEN) {
3055 DEBUGP(DCC, "Message unhandled at this state.\n");
3056 return 0;
3057 }
3058
3059 rc = downstatelist[i].rout(trans, arg);
3060
3061 return rc;
3062}
3063
3064
3065static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003066 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 int type;
3068 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3069} datastatelist[] = {
3070 /* mobile originating call establishment */
3071 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3072 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3073 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3074 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3075 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3076 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3077 /* mobile terminating call establishment */
3078 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3079 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3080 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3081 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003082 {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 +08003083 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3084 /* signalling during call */
3085 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3086 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3087 {SBIT(GSM_CSTATE_ACTIVE),
3088 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3089 {ALL_STATES,
3090 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3091 {ALL_STATES,
3092 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3093 {ALL_STATES,
3094 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3095 {SBIT(GSM_CSTATE_ACTIVE),
3096 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3097 {SBIT(GSM_CSTATE_ACTIVE),
3098 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3099 {SBIT(GSM_CSTATE_ACTIVE),
3100 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3101 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3102 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3103 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3104 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3105 {SBIT(GSM_CSTATE_ACTIVE),
3106 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3107 /* clearing */
3108 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3109 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3110 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3111 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3112 {ALL_STATES, /* 5.4.3.4 */
3113 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3114};
3115
3116#define DATASLLEN \
3117 (sizeof(datastatelist) / sizeof(struct datastate))
3118
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003119static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003120{
3121 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003122 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003123 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003124 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003126
Harald Welte4bfdfe72009-06-10 23:11:52 +08003127 if (msg_type & 0x80) {
3128 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3129 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003130 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003131
Harald Welte2483f1b2016-06-19 18:06:02 +02003132 if (!conn->vsub) {
3133 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003134 return -EINVAL;
3135 }
3136
Harald Welte4bfdfe72009-06-10 23:11:52 +08003137 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003138 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003139
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003140#if BEFORE_MSCSPLIT
3141 /* Re-enable this log output once we can obtain this information via
3142 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003143 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003145 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003146 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003147 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3148 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003149#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150
3151 /* Create transaction */
3152 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003153 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154 "creating new trans.\n", transaction_id);
3155 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003156 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003157 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003158 transaction_id, new_callref++);
3159 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003160 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003161 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003162 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003163 GSM48_MT_CC_RELEASE_COMPL);
3164 return -ENOMEM;
3165 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003167 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003168 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 }
3170
3171 /* find function for current state and message */
3172 for (i = 0; i < DATASLLEN; i++)
3173 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003174 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003175 break;
3176 if (i == DATASLLEN) {
3177 DEBUGP(DCC, "Message unhandled at this state.\n");
3178 return 0;
3179 }
3180
Harald Welte2483f1b2016-06-19 18:06:02 +02003181 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003182
Harald Welte4bfdfe72009-06-10 23:11:52 +08003183 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003184
Harald Welte2483f1b2016-06-19 18:06:02 +02003185 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003186 return rc;
3187}
3188
Harald Welte2483f1b2016-06-19 18:06:02 +02003189static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003190{
Harald Welte2483f1b2016-06-19 18:06:02 +02003191 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3192 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003193
Harald Welte2483f1b2016-06-19 18:06:02 +02003194 switch (pdisc) {
3195 case GSM48_PDISC_MM:
3196 switch (msg_type) {
3197 case GSM48_MT_MM_LOC_UPD_REQUEST:
3198 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003199 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003200 case GSM48_MT_MM_AUTH_RESP:
3201 case GSM48_MT_MM_AUTH_FAIL:
3202 case GSM48_MT_MM_ID_RESP:
3203 case GSM48_MT_MM_TMSI_REALL_COMPL:
3204 case GSM48_MT_MM_IMSI_DETACH_IND:
3205 return true;
3206 default:
3207 break;
3208 }
3209 break;
3210 case GSM48_PDISC_RR:
3211 switch (msg_type) {
3212 case GSM48_MT_RR_CIPH_M_COMPL:
3213 case GSM48_MT_RR_PAG_RESP:
3214 return true;
3215 default:
3216 break;
3217 }
3218 break;
3219 default:
3220 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003221 }
3222
Harald Welte2483f1b2016-06-19 18:06:02 +02003223 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003224}
3225
Harald Welte2483f1b2016-06-19 18:06:02 +02003226void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3227 struct msgb *msg)
3228{
3229
3230 /* If a CM Service Request was received before, this is the request the
3231 * conn was opened for. No need to wait for further messages. */
3232
3233 if (!conn->received_cm_service_request)
3234 return;
3235
3236 if (log_check_level(DMM, LOGL_DEBUG)) {
3237 struct gsm48_hdr *gh = msgb_l3(msg);
3238 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3239 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3240
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003241 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003242 " received_cm_service_request changes to false\n",
3243 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003244 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003245 }
3246 conn->received_cm_service_request = false;
3247}
3248
3249
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003250/* 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 +08003251int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003252{
Philipp Maier91f10c72017-04-20 18:40:37 +02003253 struct gsm48_hdr *gh;
3254 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003255 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003256
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003257 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003258 OSMO_ASSERT(conn);
3259 OSMO_ASSERT(msg);
3260
Philipp Maier91f10c72017-04-20 18:40:37 +02003261 gh = msgb_l3(msg);
3262 pdisc = gsm48_hdr_pdisc(gh);
3263
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003264 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3265 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3266 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003267
3268 if (!msc_subscr_conn_is_accepted(conn)
3269 && !msg_is_initially_permitted(gh)) {
3270 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003271 "subscr %s: Message not permitted for initial conn: %s\n",
3272 vlr_subscr_name(conn->vsub),
3273 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003274 return -EACCES;
3275 }
3276
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003277 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3278 LOGP(DMM, LOGL_ERROR,
3279 "%s: Illegal situation: RAN type mismatch:"
3280 " attached via %s, received message via %s\n",
3281 vlr_subscr_name(conn->vsub),
3282 ran_type_name(conn->vsub->cs.attached_via_ran),
3283 ran_type_name(conn->via_ran));
3284 return -EACCES;
3285 }
3286
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003287#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003288 if (silent_call_reroute(conn, msg))
3289 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003290#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003291
Harald Welte52b1f982008-12-23 20:25:15 +00003292 switch (pdisc) {
3293 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003294 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003295 break;
3296 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003297 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003298 break;
3299 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003300 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003301 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003302 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003303 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003304 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003305 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003306 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003307 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3308 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003309 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003310 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003311 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003312 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003313 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003314 case GSM48_PDISC_TEST:
3315 rc = gsm0414_rcv_test(conn, msg);
3316 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003317 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003318 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3319 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003320 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003321 break;
3322 }
3323
3324 return rc;
3325}
Harald Welte8470bf22008-12-25 23:28:35 +00003326
Harald Welte2483f1b2016-06-19 18:06:02 +02003327/***********************************************************************
3328 * VLR integration
3329 ***********************************************************************/
3330
3331/* VLR asks us to send an authentication request */
3332static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3333 bool send_autn)
3334{
3335 struct gsm_subscriber_connection *conn = msc_conn_ref;
3336 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3337 send_autn? at->vec.autn : NULL,
3338 at->key_seq);
3339}
3340
3341/* VLR asks us to send an authentication reject */
3342static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3343{
3344 struct gsm_subscriber_connection *conn = msc_conn_ref;
3345 return gsm48_tx_mm_auth_rej(conn);
3346}
3347
3348/* VLR asks us to transmit an Identity Request of given type */
3349static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3350{
3351 struct gsm_subscriber_connection *conn = msc_conn_ref;
3352 return mm_tx_identity_req(conn, mi_type);
3353}
3354
3355/* VLR asks us to transmit a Location Update Accept */
3356static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3357{
3358 struct gsm_subscriber_connection *conn = msc_conn_ref;
3359 return gsm0408_loc_upd_acc(conn, send_tmsi);
3360}
3361
3362/* VLR asks us to transmit a Location Update Reject */
3363static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3364{
3365 struct gsm_subscriber_connection *conn = msc_conn_ref;
3366 return gsm0408_loc_upd_rej(conn, cause);
3367}
3368
3369/* VLR asks us to transmit a CM Service Accept */
3370static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3371{
3372 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003373 return msc_gsm48_tx_mm_serv_ack(conn);
3374}
3375
3376static int msc_vlr_tx_common_id(void *msc_conn_ref)
3377{
3378 struct gsm_subscriber_connection *conn = msc_conn_ref;
3379 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003380}
3381
3382/* VLR asks us to transmit a CM Service Reject */
3383static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3384{
3385 uint8_t cause;
3386 struct gsm_subscriber_connection *conn = msc_conn_ref;
3387 conn->received_cm_service_request = false;
3388
3389 switch (result) {
3390 default:
3391 case VLR_PR_ARQ_RES_NONE:
3392 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3393 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3394 cause = GSM48_REJECT_NETWORK_FAILURE;
3395 break;
3396 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3397 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3398 break;
3399 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3400 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3401 break;
3402 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3403 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3404 break;
3405 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3406 cause = GSM48_REJECT_ILLEGAL_MS;
3407 break;
3408 case VLR_PR_ARQ_RES_TIMEOUT:
3409 cause = GSM48_REJECT_CONGESTION;
3410 break;
3411 };
3412
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003413 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003414}
3415
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003416/* For msc_vlr_set_ciph_mode() */
3417osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3418 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3419
Harald Welte2483f1b2016-06-19 18:06:02 +02003420/* VLR asks us to start using ciphering */
3421static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01003422 bool umts_aka,
Harald Welte2483f1b2016-06-19 18:06:02 +02003423 bool retrieve_imeisv)
3424{
3425 struct gsm_subscriber_connection *conn = msc_conn_ref;
3426 struct vlr_subscr *vsub;
3427 struct gsm_auth_tuple *tuple;
3428
3429 if (!conn || !conn->vsub) {
3430 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3431 " NULL conn/subscriber");
3432 return -EINVAL;
3433 }
3434
3435 vsub = conn->vsub;
3436 tuple = vsub->last_tuple;
3437
3438 if (!tuple) {
3439 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3440 " Command: no auth tuple available\n",
3441 vlr_subscr_name(vsub));
3442 return -EINVAL;
3443 }
3444
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003445 switch (conn->via_ran) {
3446 case RAN_GERAN_A:
3447 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3448 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003449 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003450 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003451 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003452 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003453
Harald Welte7b222aa2017-12-23 19:30:32 +01003454 for (i = 0; i < 8; i++) {
3455 if (net->a5_encryption_mask & (1 << i))
3456 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3457 }
3458 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003459
Neels Hofmeyr52821712017-12-18 01:23:42 +01003460 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3461 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3462 * necessarily a match for the UMTS AKA tokens. */
3463 if (umts_aka)
3464 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3465 else
3466 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003467 ei.key_len = sizeof(tuple->vec.kc);
3468
3469 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3470 }
3471
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003472 case RAN_UTRAN_IU:
3473#ifdef BUILD_IU
3474 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3475 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003476 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003477#else
3478 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3479 " built without Iu support\n");
3480 return -ENOTSUP;
3481#endif
3482
3483 default:
3484 break;
3485 }
3486 LOGP(DMM, LOGL_ERROR,
3487 "%s: cannot start ciphering, unknown RAN type %d\n",
3488 vlr_subscr_name(conn->vsub), conn->via_ran);
3489 return -ENOTSUP;
3490}
3491
3492void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3493{
3494 struct vlr_ciph_result vlr_res = {};
3495
3496 if (!conn || !conn->vsub) {
3497 LOGP(DMM, LOGL_ERROR,
3498 "Rx Security Mode Complete for invalid conn\n");
3499 return;
3500 }
3501
3502 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3503 vlr_subscr_name(conn->vsub));
3504
3505 vlr_res.cause = VLR_CIPH_COMPL;
3506 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003507}
3508
3509/* VLR informs us that the subscriber data has somehow been modified */
3510static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3511{
Maxa263bb22017-12-27 13:23:44 +01003512 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3513 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003514}
3515
3516/* VLR informs us that the subscriber has been associated with a conn */
3517static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3518 struct vlr_subscr *vsub)
3519{
3520 struct gsm_subscriber_connection *conn = msc_conn_ref;
3521 OSMO_ASSERT(!conn->vsub);
3522 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003523 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003524}
3525
3526/* operations that we need to implement for libvlr */
3527static const struct vlr_ops msc_vlr_ops = {
3528 .tx_auth_req = msc_vlr_tx_auth_req,
3529 .tx_auth_rej = msc_vlr_tx_auth_rej,
3530 .tx_id_req = msc_vlr_tx_id_req,
3531 .tx_lu_acc = msc_vlr_tx_lu_acc,
3532 .tx_lu_rej = msc_vlr_tx_lu_rej,
3533 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3534 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3535 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003536 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003537 .subscr_update = msc_vlr_subscr_update,
3538 .subscr_assoc = msc_vlr_subscr_assoc,
3539};
3540
3541/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3542int msc_vlr_alloc(struct gsm_network *net)
3543{
3544 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3545 if (!net->vlr)
3546 return -ENOMEM;
3547 net->vlr->user_ctx = net;
3548 return 0;
3549}
3550
3551/* Launch the VLR, i.e. its GSUP connection */
3552int msc_vlr_start(struct gsm_network *net)
3553{
3554 OSMO_ASSERT(net->vlr);
3555 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3556 net->gsup_server_port);
3557}