blob: 126a216e17e3c4ded718c532d1571703adea8497 [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,
342 is_utran? VLR_CIPH_A5_3
343 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200344 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200345 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 net->vlr->cfg.assign_tmsi);
347 if (!lu_fsm) {
348 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
349 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800350 }
351
Harald Welte2483f1b2016-06-19 18:06:02 +0200352 /* From vlr_loc_update() we expect an implicit dispatch of
353 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
354 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000355
Harald Welte2483f1b2016-06-19 18:06:02 +0200356 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
357 LOGP(DRR, LOGL_ERROR,
358 "%s: internal error during Location Updating attempt\n",
359 mi_string);
360 return -EIO;
361 }
362
363 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000364}
365
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100366/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200367/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100368static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800369{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100370 uint8_t ret;
371
372 ret = value / 10;
373 ret |= (value % 10) << 4;
374
375 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800376}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100377
Harald Welte4bfdfe72009-06-10 23:11:52 +0800378
Harald Weltedb253af2008-12-30 17:56:55 +0000379/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800380int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000381{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100382 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000383 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200384 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200385 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200386 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100387
Harald Welte4bfdfe72009-06-10 23:11:52 +0800388 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100389 struct tm* gmt_time;
390 struct tm* local_time;
391 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100392 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000393
Harald Weltedb253af2008-12-30 17:56:55 +0000394 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
395 gh->proto_discr = GSM48_PDISC_MM;
396 gh->msg_type = GSM48_MT_MM_INFO;
397
398 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200399#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000400 name_len = strlen(net->name_long);
401 /* 10.5.3.5a */
402 ptr8 = msgb_put(msg, 3);
403 ptr8[0] = GSM48_IE_NAME_LONG;
404 ptr8[1] = name_len*2 +1;
405 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
406
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200407 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000408 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000409 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000410
411 /* FIXME: Use Cell Broadcast, not UCS-2, since
412 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200413#endif
414 name_len = (strlen(net->name_long)*7)/8;
415 name_pad = (8 - strlen(net->name_long)*7)%8;
416 if (name_pad > 0)
417 name_len++;
418 /* 10.5.3.5a */
419 ptr8 = msgb_put(msg, 3);
420 ptr8[0] = GSM48_IE_NAME_LONG;
421 ptr8[1] = name_len +1;
422 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
423
424 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100425 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200426
Harald Weltedb253af2008-12-30 17:56:55 +0000427 }
428
429 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200430#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000431 name_len = strlen(net->name_short);
432 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200433 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200434 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000435 ptr8[1] = name_len*2 + 1;
436 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
437
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200438 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000439 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000440 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200441#endif
442 name_len = (strlen(net->name_short)*7)/8;
443 name_pad = (8 - strlen(net->name_short)*7)%8;
444 if (name_pad > 0)
445 name_len++;
446 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200447 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200448 ptr8[0] = GSM48_IE_NAME_SHORT;
449 ptr8[1] = name_len +1;
450 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
451
452 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100453 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200454
Harald Weltedb253af2008-12-30 17:56:55 +0000455 }
456
Harald Weltedb253af2008-12-30 17:56:55 +0000457 /* Section 10.5.3.9 */
458 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100459 gmt_time = gmtime(&cur_t);
460
Harald Weltedb253af2008-12-30 17:56:55 +0000461 ptr8 = msgb_put(msg, 8);
462 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100463 ptr8[1] = bcdify(gmt_time->tm_year % 100);
464 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
465 ptr8[3] = bcdify(gmt_time->tm_mday);
466 ptr8[4] = bcdify(gmt_time->tm_hour);
467 ptr8[5] = bcdify(gmt_time->tm_min);
468 ptr8[6] = bcdify(gmt_time->tm_sec);
469
Neels Hofmeyr73983952016-05-10 13:29:33 +0200470 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200471 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200472 if (net->tz.hr < 0) {
473 tzunits = ((net->tz.hr/-1)*4);
474 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100475 ptr8[7] = bcdify(tzunits);
476 /* Set negative time */
477 ptr8[7] |= 0x08;
478 }
479 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200480 tzunits = net->tz.hr*4;
481 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100482 ptr8[7] = bcdify(tzunits);
483 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100484 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200485 if (net->tz.dst >= 0 && net->tz.dst <= 2)
486 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100487 }
488 else {
489 /* Need to get GSM offset and convert into 15 min units */
490 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200491#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200492 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100493 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200494#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200495 /* find timezone offset */
496 time_t utc;
497 double offsetFromUTC;
498 utc = mktime(gmt_time);
499 local_time = localtime(&cur_t);
500 offsetFromUTC = difftime(cur_t, utc);
501 if (local_time->tm_isdst)
502 offsetFromUTC += 3600.0;
503 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200504#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100505 if (tzunits < 0) {
506 tzunits = tzunits/-1;
507 ptr8[7] = bcdify(tzunits);
508 /* Flip it to negative */
509 ptr8[7] |= 0x08;
510 }
511 else
512 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100513
514 /* Does not support DST +2 */
515 if (local_time->tm_isdst)
516 dst = 1;
517 }
518
519 if (dst) {
520 ptr8 = msgb_put(msg, 3);
521 ptr8[0] = GSM48_IE_NET_DST;
522 ptr8[1] = 1;
523 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100524 }
Harald Weltedb253af2008-12-30 17:56:55 +0000525
Daniel Willmanneea93372009-08-13 03:42:07 +0200526 DEBUGP(DMM, "-> MM INFO\n");
527
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800528 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000529}
530
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100531/*! Send an Authentication Request to MS on the given subscriber connection
532 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
533 * \param[in] conn Subscriber connection to send on.
534 * \param[in] rand Random challenge token to send, must be 16 bytes long.
535 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
536 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
537 * \param[in] key_seq auth tuple's sequence number.
538 */
539int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
540 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200541{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100542 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200543 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200544 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200545
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100546 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100547 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100548 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200549
Harald Welte7984d5c2009-08-12 22:56:50 +0200550 gh->proto_discr = GSM48_PDISC_MM;
551 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
552
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100553 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200554
Harald Welte7984d5c2009-08-12 22:56:50 +0200555 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100556 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200557 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200558 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200559
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100560
561 /* 16 bytes AUTN */
562 if (autn)
563 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
564
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800565 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200566}
567
568/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800569int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200570{
571 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800572 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200573}
574
Harald Welte2483f1b2016-06-19 18:06:02 +0200575static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
576static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
577
578static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100579{
Harald Welte2483f1b2016-06-19 18:06:02 +0200580 uint8_t mi_type;
581 char mi_string[GSM48_MI_SIZE];
582 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100583
Harald Welte2483f1b2016-06-19 18:06:02 +0200584 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
585 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100586
Harald Welte2483f1b2016-06-19 18:06:02 +0200587 switch (mi_type) {
588 case GSM_MI_TYPE_IMSI:
589 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
590 goto accept_reuse;
591 break;
592 case GSM_MI_TYPE_TMSI:
593 tmsi = osmo_load32be(mi_lv+2);
594 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
595 goto accept_reuse;
596 break;
597 case GSM_MI_TYPE_IMEI:
598 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
599 goto accept_reuse;
600 break;
601 default:
602 break;
603 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100604
Harald Welte2483f1b2016-06-19 18:06:02 +0200605 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
606 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
607 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
608 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100609
Harald Welte2483f1b2016-06-19 18:06:02 +0200610accept_reuse:
611 DEBUGP(DMM, "%s: re-using already accepted connection\n",
612 vlr_subscr_name(conn->vsub));
613 conn->received_cm_service_request = true;
614 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100615}
616
Harald Welte4ed0e922009-01-10 03:17:30 +0000617/*
618 * Handle CM Service Requests
619 * a) Verify that the packet is long enough to contain the information
620 * we require otherwsie reject with INCORRECT_MESSAGE
621 * b) Try to parse the TMSI. If we do not have one reject
622 * c) Check that we know the subscriber with the TMSI otherwise reject
623 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200624 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200625 *
626 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000627 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200628int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000629{
Harald Welte2483f1b2016-06-19 18:06:02 +0200630 static const enum subscr_conn_from conn_from_cm_service_req =
631 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
632 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200633 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200634 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000635
Harald Welteba4cf162009-01-10 01:49:35 +0000636 struct gsm48_hdr *gh = msgb_l3(msg);
637 struct gsm48_service_request *req =
638 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800639 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200640 uint8_t classmark2_len = gh->data[1];
641 uint8_t *classmark2 = gh->data+2;
642 uint8_t mi_len = *(classmark2 + classmark2_len);
643 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200644 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200645 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200646 int rc;
647
648 lai.plmn.mcc = conn->network->country_code;
649 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200650 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000651
Harald Weltec9e02182009-05-01 19:07:53 +0000652 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000653 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000654 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200655 return msc_gsm48_tx_mm_serv_rej(conn,
656 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000657 }
658
659 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000660 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200661 return msc_gsm48_tx_mm_serv_rej(conn,
662 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000663 }
664
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200665 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000666 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100667 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
668 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200669
Harald Welte3995d2e2018-01-24 11:23:54 +0100670 switch (mi_type) {
671 case GSM_MI_TYPE_IMSI:
672 case GSM_MI_TYPE_TMSI:
673 /* continue below */
674 break;
675 case GSM_MI_TYPE_IMEI:
676 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
677 /* We don't do emergency calls by IMEI */
678 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
679 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
680 }
681 /* fall-through for non-emergency setup */
682 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200683 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200684 return msc_gsm48_tx_mm_serv_rej(conn,
685 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000686 }
687
Harald Welte37382ec2018-01-24 12:49:34 +0100688 switch (req->cm_service_type) {
689 case GSM48_CMSERV_MO_CALL_PACKET:
690 case GSM48_CMSERV_EMERGENCY:
691 case GSM48_CMSERV_SMS:
692 case GSM48_CMSERV_SUP_SERV:
693 /* continue below */
694 break;
695 default:
696 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
697 }
698
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200699 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200700 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
701 conn->classmark.classmark2_len = classmark2_len;
702
703 if (conn->conn_fsm) {
704 if (msc_subscr_conn_is_accepted(conn))
705 return cm_serv_reuse_conn(conn, mi-1);
706 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
707 vlr_subscr_name(conn->vsub));
708 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
709 return -EINVAL;
710 }
711
712 rc = msc_create_conn_fsm(conn, mi_string);
713 if (rc) {
714 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
715 /* logging already happened in msc_create_conn_fsm() */
716 return rc;
717 }
Harald Welte7659de12009-12-13 12:39:18 +0100718
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200719 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200720 vlr_proc_acc_req(conn->conn_fsm,
721 SUBSCR_CONN_E_ACCEPTED,
722 SUBSCR_CONN_E_CN_CLOSE,
723 (void*)&conn_from_cm_service_req,
724 net->vlr, conn,
725 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200726 is_utran || conn->network->authentication_required,
727 is_utran? VLR_CIPH_A5_3
728 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200729 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200730 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000731
Harald Welte2483f1b2016-06-19 18:06:02 +0200732 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000733}
734
Harald Welte49f8fcb2018-01-24 21:40:19 +0100735/* Receive a CM Re-establish Request */
736static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
737{
738 uint8_t mi_type;
739 char mi_string[GSM48_MI_SIZE];
740 struct gsm48_hdr *gh = msgb_l3(msg);
741
742 uint8_t classmark2_len = gh->data[1];
743 uint8_t *classmark2 = gh->data+2;
744 uint8_t mi_len = *(classmark2 + classmark2_len);
745 uint8_t *mi = (classmark2 + classmark2_len + 1);
746
747 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
748 mi_type = mi[0] & GSM_MI_TYPE_MASK;
749 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
750
751 /* we don't support CM call re-establishment */
752 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
753}
754
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200755static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000756{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200757 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000758 struct gsm48_hdr *gh = msgb_l3(msg);
759 struct gsm48_imsi_detach_ind *idi =
760 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200761 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200762 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200763 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000764
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200765 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200766 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
767 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000768
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200769 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100770
Harald Welte2a139372009-02-22 21:14:55 +0000771 switch (mi_type) {
772 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200773 vsub = vlr_subscr_find_by_tmsi(network->vlr,
774 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000775 break;
776 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200777 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000778 break;
779 case GSM_MI_TYPE_IMEI:
780 case GSM_MI_TYPE_IMEISV:
781 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200782 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
783 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000784 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200785 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200786 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
787 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000788 break;
789 }
790
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 /* TODO? We used to remember the subscriber's classmark1 here and
792 * stored it in the old sqlite db, but now we store it in a conn that
793 * will be discarded anyway: */
794 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200795
Harald Welte2483f1b2016-06-19 18:06:02 +0200796 if (!vsub) {
797 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
798 gsm48_mi_type_name(mi_type), mi_string);
799 } else {
800 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100801
802 if (vsub->cs.is_paging)
803 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
804
Harald Welte2483f1b2016-06-19 18:06:02 +0200805 vlr_subscr_rx_imsi_detach(vsub);
806 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
807 vlr_subscr_put(vsub);
808 }
Harald Welte2a139372009-02-22 21:14:55 +0000809
Harald Welte2483f1b2016-06-19 18:06:02 +0200810 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000811 return 0;
812}
813
Harald Welted2a7f5a2009-06-05 20:08:20 +0000814static int gsm48_rx_mm_status(struct msgb *msg)
815{
816 struct gsm48_hdr *gh = msgb_l3(msg);
817
818 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
819
820 return 0;
821}
822
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100823static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
824 struct gsm_subscriber_connection *conn,
825 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100826{
827 struct gsm48_hdr *gh = msgb_l3(msg);
828 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100829
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100830 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
831 LOGP(DMM, LOGL_ERROR,
832 "%s: MM AUTHENTICATION RESPONSE:"
833 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200834 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100835 return -EINVAL;
836 }
837
838 *res_len = sizeof(ar->sres);
839 memcpy(res, ar->sres, sizeof(ar->sres));
840 return 0;
841}
842
843static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
844 struct gsm_subscriber_connection *conn,
845 struct msgb *msg)
846{
847 struct gsm48_hdr *gh;
848 uint8_t *data;
849 uint8_t iei;
850 uint8_t ie_len;
851 unsigned int data_len;
852
853 /* First parse the GSM part */
854 if (parse_gsm_auth_resp(res, res_len, conn, msg))
855 return -EINVAL;
856 OSMO_ASSERT(*res_len == 4);
857
858 /* Then add the extended res part */
859 gh = msgb_l3(msg);
860 data = gh->data + sizeof(struct gsm48_auth_resp);
861 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
862
863 if (data_len < 3) {
864 LOGP(DMM, LOGL_ERROR,
865 "%s: MM AUTHENTICATION RESPONSE:"
866 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200867 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100868 return -EINVAL;
869 }
870
871 iei = data[0];
872 ie_len = data[1];
873 if (iei != GSM48_IE_AUTH_RES_EXT) {
874 LOGP(DMM, LOGL_ERROR,
875 "%s: MM R99 AUTHENTICATION RESPONSE:"
876 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200877 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100878 GSM48_IE_AUTH_RES_EXT, iei);
879 return -EINVAL;
880 }
881
882 if (ie_len > 12) {
883 LOGP(DMM, LOGL_ERROR,
884 "%s: MM R99 AUTHENTICATION RESPONSE:"
885 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200886 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100887 return -EINVAL;
888 }
889
890 *res_len += ie_len;
891 memcpy(res + 4, &data[2], ie_len);
892 return 0;
893}
894
895/* Chapter 9.2.3: Authentication Response */
896static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
897{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100898 uint8_t res[16];
899 uint8_t res_len;
900 int rc;
901 bool is_r99;
902
Harald Welte2483f1b2016-06-19 18:06:02 +0200903 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100904 LOGP(DMM, LOGL_ERROR,
905 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200906 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100907 return -EINVAL;
908 }
909
910 if (msgb_l3len(msg) >
911 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
912 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
913 is_r99 = true;
914 } else {
915 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
916 is_r99 = false;
917 }
918
919 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200920 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100921 return -EINVAL;
922 }
923
924 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200925 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100926 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
927 osmo_hexdump_nospc(res, res_len));
928
Harald Welte2483f1b2016-06-19 18:06:02 +0200929 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
930 conn->via_ran == RAN_UTRAN_IU,
931 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100932}
933
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100934static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
935{
936 struct gsm48_hdr *gh = msgb_l3(msg);
937 uint8_t cause;
938 uint8_t auts_tag;
939 uint8_t auts_len;
940 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100941
Harald Welte2483f1b2016-06-19 18:06:02 +0200942 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100943 LOGP(DMM, LOGL_ERROR,
944 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200945 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100946 return -EINVAL;
947 }
948
949 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
950 LOGP(DMM, LOGL_ERROR,
951 "%s: MM R99 AUTHENTICATION FAILURE:"
952 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
954 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100955 return -EINVAL;
956 }
957
958 cause = gh->data[0];
959
960 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
961 LOGP(DMM, LOGL_INFO,
962 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200963 vlr_subscr_name(conn->vsub), cause);
964 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
965 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100966 }
967
968 /* This is a Synch Failure procedure, which should pass an AUTS to
969 * resynchronize the sequence nr with the HLR. Expecting exactly one
970 * TLV with 14 bytes of AUTS. */
971
972 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
973 LOGP(DMM, LOGL_INFO,
974 "%s: MM R99 AUTHENTICATION FAILURE:"
975 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200976 vlr_subscr_name(conn->vsub));
977 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100978 return -EINVAL;
979 }
980
981 auts_tag = gh->data[1];
982 auts_len = gh->data[2];
983 auts = &gh->data[3];
984
985 if (auts_tag != GSM48_IE_AUTS
986 || auts_len != 14) {
987 LOGP(DMM, LOGL_INFO,
988 "%s: MM R99 AUTHENTICATION FAILURE:"
989 " invalid Synch Failure:"
990 " expected AUTS IE 0x%02x of 14 bytes,"
991 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200992 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100993 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200994 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100995 return -EINVAL;
996 }
997
998 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
999 LOGP(DMM, LOGL_INFO,
1000 "%s: MM R99 AUTHENTICATION FAILURE:"
1001 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001002 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1003 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001004 return -EINVAL;
1005 }
1006
1007 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1008 * large enough. */
1009
1010 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012
Harald Welte2483f1b2016-06-19 18:06:02 +02001013 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1014}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001015
Harald Welte2483f1b2016-06-19 18:06:02 +02001016static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1017{
1018 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1019 vlr_subscr_name(conn->vsub));
1020 if (!conn->vsub) {
1021 LOGP(DMM, LOGL_ERROR,
1022 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1023 return -EINVAL;
1024 }
1025 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001026}
1027
Harald Weltebf5e8df2009-02-03 12:59:45 +00001028/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001029static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001030{
1031 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001032 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001033
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001034 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001035 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001036 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001037 break;
1038 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001039 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001040 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001041 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001042 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001043 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001044 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001045 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001046 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001047 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001048 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001049 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001050 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001051 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001052 break;
1053 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001054 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001055 break;
1056 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001057 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001058 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001059 case GSM48_MT_MM_AUTH_FAIL:
1060 rc = gsm48_rx_mm_auth_fail(conn, msg);
1061 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001062 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001063 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001064 gh->msg_type);
1065 break;
1066 }
1067
1068 return rc;
1069}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001070
Harald Welte2483f1b2016-06-19 18:06:02 +02001071static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1072{
1073 /* Check the size for the classmark */
1074 if (tot_len < 1 + *classmark2_lv)
1075 return NULL;
1076
1077 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1078 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1079 return NULL;
1080
1081 return mi_lv;
1082}
1083
Harald Welte2d35ae62009-02-06 12:02:13 +00001084/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001085static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001086{
Harald Welte2483f1b2016-06-19 18:06:02 +02001087 static const enum subscr_conn_from conn_from_paging_resp =
1088 SUBSCR_CONN_FROM_PAGING_RESP;
1089 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001090 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001091 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001092 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001093 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001094 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001095 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001096 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001097 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001098 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001099
1100 lai.plmn.mcc = conn->network->country_code;
1101 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001102 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001103
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001104 resp = (struct gsm48_pag_resp *) &gh->data[0];
1105 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1106 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001107 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1108 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001109
Harald Welte2483f1b2016-06-19 18:06:02 +02001110 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1111 if (!mi_lv) {
1112 /* FIXME */
1113 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001114 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001115
Harald Welte2483f1b2016-06-19 18:06:02 +02001116 rc = msc_create_conn_fsm(conn, mi_string);
1117 if (rc)
1118 /* logging already happened in msc_create_conn_fsm() */
1119 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001120
Harald Welte2483f1b2016-06-19 18:06:02 +02001121 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1122 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001123
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001124 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001125 vlr_proc_acc_req(conn->conn_fsm,
1126 SUBSCR_CONN_E_ACCEPTED,
1127 SUBSCR_CONN_E_CN_CLOSE,
1128 (void*)&conn_from_paging_resp,
1129 net->vlr, conn,
1130 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001131 is_utran || conn->network->authentication_required,
1132 is_utran? VLR_CIPH_A5_3
1133 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +02001134 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001135 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001136
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001137 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001138}
1139
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001140static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001141{
1142 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001143 uint8_t apdu_id_flags;
1144 uint8_t apdu_len;
1145 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001146
1147 apdu_id_flags = gh->data[0];
1148 apdu_len = gh->data[1];
1149 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001150
Max8dc8f232017-02-09 19:13:02 +01001151 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001152 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001153
Harald Welte2483f1b2016-06-19 18:06:02 +02001154 /* we're not using the app info blob anywhere, so ignore. */
1155#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001156 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001157#else
1158 return 0;
1159#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001160}
1161
Harald Weltebf5e8df2009-02-03 12:59:45 +00001162/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001163static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001164{
1165 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001166 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001167
1168 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001169 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001170 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001171 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001172 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001173 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001174 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001175 default:
Philippd37981e2016-10-10 15:11:41 +02001176 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1177 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001178 break;
1179 }
1180
Harald Welte2d35ae62009-02-06 12:02:13 +00001181 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001182}
1183
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001184int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1185 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001186{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001187 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001188 struct gsm48_hdr *gh;
1189
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001190 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1191 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001192
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001193 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1194 gh->proto_discr = GSM48_PDISC_RR;
1195 gh->msg_type = GSM48_MT_RR_APP_INFO;
1196 gh->data[0] = apdu_id;
1197 gh->data[1] = apdu_len;
1198 memcpy(gh->data+2, apdu, apdu_len);
1199
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001200 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001201}
1202
Alexander Couzens92f552f2016-08-23 07:32:27 +02001203/* FIXME: this count_statistics is a state machine behaviour. we should convert
1204 * the complete call control into a state machine. Afterwards we can move this
1205 * code into state transitions.
1206 */
1207static void count_statistics(struct gsm_trans *trans, int new_state)
1208{
1209 int old_state = trans->cc.state;
1210 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1211
1212 if (old_state == new_state)
1213 return;
1214
1215 /* state incoming */
1216 switch (new_state) {
1217 case GSM_CSTATE_ACTIVE:
1218 osmo_counter_inc(trans->net->active_calls);
1219 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1220 break;
1221 }
1222
1223 /* state outgoing */
1224 switch (old_state) {
1225 case GSM_CSTATE_ACTIVE:
1226 osmo_counter_dec(trans->net->active_calls);
1227 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1228 new_state == GSM_CSTATE_DISCONNECT_IND)
1229 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1230 else
1231 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1232 break;
1233 }
1234}
1235
Harald Welte4bc90a12008-12-27 16:32:52 +00001236/* Call Control */
1237
Harald Welte7584aea2009-02-11 11:44:12 +00001238/* The entire call control code is written in accordance with Figure 7.10c
1239 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1240 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1241 * it for voice */
1242
Harald Welte4bfdfe72009-06-10 23:11:52 +08001243static void new_cc_state(struct gsm_trans *trans, int state)
1244{
1245 if (state > 31 || state < 0)
1246 return;
1247
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001248 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1249 trans->transaction_id,
1250 vlr_subscr_name(trans->vsub),
1251 gsm48_cc_state_name(trans->cc.state),
1252 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001253
Alexander Couzens92f552f2016-08-23 07:32:27 +02001254 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001255 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001256}
1257
1258static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001259{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001260 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001261 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001262 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001263
Harald Welte4bc90a12008-12-27 16:32:52 +00001264 gh->msg_type = GSM48_MT_CC_STATUS;
1265
1266 cause = msgb_put(msg, 3);
1267 cause[0] = 2;
1268 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1269 cause[2] = 0x80 | 30; /* response to status inquiry */
1270
1271 call_state = msgb_put(msg, 1);
1272 call_state[0] = 0xc0 | 0x00;
1273
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001274 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001275}
1276
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001277static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001278 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001279{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001280 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001281 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1282
Harald Welte6f4b7532008-12-29 00:39:37 +00001283 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001284 gh->msg_type = msg_type;
1285
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001286 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001287}
1288
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1290{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001291 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001292 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001293 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001294 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295 }
1296}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001297
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1299 int msg_type, struct gsm_mncc *mncc)
1300{
1301 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001302 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303
Philipp Maierfbf66102017-04-09 12:32:51 +02001304 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1305
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001306#if BEFORE_MSCSPLIT
1307 /* Re-enable this log output once we can obtain this information via
1308 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001310 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001311 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001312 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001313 trans->conn->lchan->ts->trx->bts->nr,
1314 trans->conn->lchan->ts->trx->nr,
1315 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001316 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001317 get_mncc_name(msg_type));
1318 else
1319 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1320 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001321 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322 get_mncc_name(msg_type));
1323 else
1324 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1325 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001326#else
1327 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1328#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329
1330 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001331
Harald Welte966636f2009-06-26 19:39:35 +02001332 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333 if (!msg)
1334 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001335
1336 data = msgb_put(msg, sizeof(struct gsm_mncc));
1337 memcpy(data, mncc, sizeof(struct gsm_mncc));
1338
Harald Welte54209c22010-12-22 23:43:29 +01001339 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340
1341 return 0;
1342}
1343
1344int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001345 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346{
1347 struct gsm_mncc rel;
1348
Harald Welte92f70c52009-06-12 01:54:08 +08001349 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001351 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001352 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1353 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1355}
1356
Harald Weltedcaf5652009-07-23 18:56:43 +02001357/* Call Control Specific transaction release.
1358 * gets called by trans_free, DO NOT CALL YOURSELF! */
1359void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 gsm48_stop_cc_timer(trans);
1362
Philipp Maierfbf66102017-04-09 12:32:51 +02001363 /* Make sure call also gets released on the mgcp side */
1364 msc_call_release(trans);
1365
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 /* send release to L4, if callref still exists */
1367 if (trans->callref) {
1368 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001369 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001370 GSM48_CAUSE_LOC_PRN_S_LU,
1371 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001373 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375}
1376
1377static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001378
Harald Welte09e38af2009-02-16 22:52:23 +00001379/* call-back from paging the B-end of the connection */
1380static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001381 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001382{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001383 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001384 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001385
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001386 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001387
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001388 switch (event) {
1389 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001390 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1391 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001392 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001393 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001394 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001395 /* send SETUP request to called party */
1396 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1397 break;
1398 case GSM_PAGING_EXPIRED:
1399 case GSM_PAGING_BUSY:
1400 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001401 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001402 /* Temporarily out of order */
1403 mncc_release_ind(transt->net, transt,
1404 transt->callref,
1405 GSM48_CAUSE_LOC_PRN_S_LU,
1406 GSM48_CC_CAUSE_DEST_OOO);
1407 transt->callref = 0;
1408 transt->paging_request = NULL;
1409 trans_free(transt);
1410 break;
1411 default:
1412 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1413 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001415
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001416 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001417 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001418}
Harald Welte7584aea2009-02-11 11:44:12 +00001419
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001421static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001422{
Harald Welte53d51f52015-12-03 14:59:04 +01001423 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1424 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001425
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001427 return -EIO;
1428
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001429 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001430 return -EIO;
1431
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001432 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001433 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001434
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001435 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001436}
1437
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1439{
1440 DEBUGP(DCC, "-> STATUS ENQ\n");
1441 return gsm48_cc_tx_status(trans, msg);
1442}
1443
1444static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1445static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1446
1447static void gsm48_cc_timeout(void *arg)
1448{
1449 struct gsm_trans *trans = arg;
1450 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001451 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1452 int mo_location = GSM48_CAUSE_LOC_USER;
1453 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1454 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001455 struct gsm_mncc mo_rel, l4_rel;
1456
1457 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1458 mo_rel.callref = trans->callref;
1459 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1460 l4_rel.callref = trans->callref;
1461
Harald Weltedcaf5652009-07-23 18:56:43 +02001462 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001463 case 0x303:
1464 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001465 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001466 break;
1467 case 0x310:
1468 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001469 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001470 break;
1471 case 0x313:
1472 disconnect = 1;
1473 /* unknown, did not find it in the specs */
1474 break;
1475 case 0x301:
1476 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001477 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001478 break;
1479 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001480 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001481 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001482 gsm48_cc_tx_release(trans, &trans->cc.msg);
1483 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001484 break; /* stay in release state */
1485 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001486 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001487 return;
1488// release = 1;
1489// l4_cause = 14;
1490// break;
1491 case 0x306:
1492 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001493 mo_cause = trans->cc.msg.cause.value;
1494 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001495 break;
1496 case 0x323:
1497 disconnect = 1;
1498 break;
1499 default:
1500 release = 1;
1501 }
1502
1503 if (release && trans->callref) {
1504 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001505 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001506 l4_location, l4_cause);
1507 trans->callref = 0;
1508 }
1509
1510 if (disconnect && trans->callref) {
1511 /* process disconnect towards layer 4 */
1512 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001513 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001514 }
1515
1516 /* process disconnect towards mobile station */
1517 if (disconnect || release) {
1518 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001519 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1520 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1521 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522 mo_rel.cause.diag_len = 3;
1523
1524 if (disconnect)
1525 gsm48_cc_tx_disconnect(trans, &mo_rel);
1526 if (release)
1527 gsm48_cc_tx_release(trans, &mo_rel);
1528 }
1529
1530}
1531
Max3ffce192016-04-25 15:22:00 +02001532/* disconnect both calls from the bridge */
1533static inline void disconnect_bridge(struct gsm_network *net,
1534 struct gsm_mncc_bridge *bridge, int err)
1535{
1536 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1537 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1538 struct gsm_mncc mx_rel;
1539 if (!trans0 || !trans1)
1540 return;
1541
1542 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1543 trans0->callref, trans1->callref, strerror(err));
1544
1545 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1546 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1547 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1548
1549 mx_rel.callref = trans0->callref;
1550 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1551
1552 mx_rel.callref = trans1->callref;
1553 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1554}
1555
Harald Welte4bfdfe72009-06-10 23:11:52 +08001556static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1557 int sec, int micro)
1558{
1559 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001560 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001561 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001562 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001563}
1564
1565static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1566{
1567 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001568 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1570 struct tlv_parsed tp;
1571 struct gsm_mncc setup;
1572
1573 memset(&setup, 0, sizeof(struct gsm_mncc));
1574 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001575
Harald Welte474d19f2010-03-02 23:18:30 +01001576 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001578 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1579 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001580 setup.emergency = 1;
Harald Welte0dd01372018-01-24 11:06:19 +01001581 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582
1583 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001584 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001585 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1586 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001587
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 /* bearer capability */
1589 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1590 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001591 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001593
1594 /* Create a copy of the bearer capability
1595 * in the transaction struct, so we can use
1596 * this information later */
1597 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1598 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001599 }
1600 /* facility */
1601 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1602 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001603 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1605 }
1606 /* called party bcd number */
1607 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1608 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001609 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1611 }
1612 /* user-user */
1613 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1614 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001615 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001616 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1617 }
1618 /* ss-version */
1619 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1620 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001621 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001622 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1623 }
1624 /* CLIR suppression */
1625 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1626 setup.clir.sup = 1;
1627 /* CLIR invocation */
1628 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1629 setup.clir.inv = 1;
1630 /* cc cap */
1631 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1632 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001633 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1635 }
1636
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 new_cc_state(trans, GSM_CSTATE_INITIATED);
1638
Harald Welte79826e12018-01-24 10:50:24 +01001639 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1640 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001641 setup.called.number);
1642
Alexander Couzensb847a212016-08-02 11:34:11 +02001643 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001644
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001646 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647
Harald Welte13cac662009-07-29 12:10:35 +02001648 /* MNCC code will modify the channel asynchronously, we should
1649 * ipaccess-bind only after the modification has been made to the
1650 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 return 0;
1652}
1653
1654static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001655{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001656 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001657 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001659 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001660
Harald Welte7ccf7782009-02-17 01:43:01 +00001661 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001662
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 /* transaction id must not be assigned */
1664 if (trans->transaction_id != 0xff) { /* unasssigned */
1665 DEBUGP(DCC, "TX Setup with assigned transaction. "
1666 "This is not allowed!\n");
1667 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001668 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001669 GSM48_CAUSE_LOC_PRN_S_LU,
1670 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001672 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 return rc;
1674 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001675
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001677 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001678 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001679 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001681 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001682 GSM48_CAUSE_LOC_PRN_S_LU,
1683 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001685 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 return rc;
1687 }
Harald Welte78283ef2009-07-23 21:36:44 +02001688 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001689
Harald Welte65e74cc2008-12-29 01:55:35 +00001690 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001691
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001693
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 /* bearer capability */
1695 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001696 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 /* facility */
1698 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001699 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 /* progress */
1701 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001702 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 /* calling party BCD number */
1704 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001705 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 /* called party BCD number */
1707 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001708 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 /* user-user */
1710 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 /* redirecting party BCD number */
1713 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001714 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 /* signal */
1716 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001717 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001718
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001720
Alexander Couzensb847a212016-08-02 11:34:11 +02001721 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001722
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001723 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001724}
1725
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1727{
1728 struct gsm48_hdr *gh = msgb_l3(msg);
1729 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1730 struct tlv_parsed tp;
1731 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001732 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733
1734 gsm48_stop_cc_timer(trans);
1735 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1736
1737 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1738 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001739
Harald Welte474d19f2010-03-02 23:18:30 +01001740 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741#if 0
1742 /* repeat */
1743 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1744 call_conf.repeat = 1;
1745 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1746 call_conf.repeat = 2;
1747#endif
1748 /* bearer capability */
1749 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1750 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001751 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001753
1754 /* Create a copy of the bearer capability
1755 * in the transaction struct, so we can use
1756 * this information later */
1757 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1758 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 }
1760 /* cause */
1761 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1762 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001763 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1765 }
1766 /* cc cap */
1767 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1768 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001769 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1771 }
1772
Andreas Eversberg035b8742013-09-19 09:27:06 +02001773 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001774 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001775
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1777
Philipp Maierfbf66102017-04-09 12:32:51 +02001778 /* Assign call (if not done yet) */
1779 if (trans->assignment_done == false) {
1780 rc = msc_call_assignment(trans);
1781 trans->assignment_done = true;
1782 }
1783 else
1784 rc = 0;
1785
1786 /* don't continue, if there were problems with
1787 * the call assignment. */
1788 if (rc)
1789 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001790
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001791 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001792 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793}
1794
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001795static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796{
1797 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001798 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001800 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1803
1804 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1805
1806 /* bearer capability */
1807 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001808 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 /* facility */
1810 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001811 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 /* progress */
1813 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001814 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001816 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1817 if (rc)
1818 return rc;
1819
Philipp Maierfbf66102017-04-09 12:32:51 +02001820 /* Assign call (if not done yet) */
1821 if (trans->assignment_done == false) {
1822 rc = msc_call_assignment(trans);
1823 trans->assignment_done = true;
1824 }
1825 else
1826 rc = 0;
1827
1828 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829}
1830
1831static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1832{
1833 struct gsm48_hdr *gh = msgb_l3(msg);
1834 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1835 struct tlv_parsed tp;
1836 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001837
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 gsm48_stop_cc_timer(trans);
1839 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1840
1841 memset(&alerting, 0, sizeof(struct gsm_mncc));
1842 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001843 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 /* facility */
1845 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1846 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001847 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1849 }
1850
1851 /* progress */
1852 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1853 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001854 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1856 }
1857 /* ss-version */
1858 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1859 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001860 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1862 }
1863
1864 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1865
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001866 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001867 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868}
1869
1870static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1871{
1872 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001873 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1875
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 gh->msg_type = GSM48_MT_CC_ALERTING;
1877
1878 /* facility */
1879 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* progress */
1882 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* user-user */
1885 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887
1888 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001889
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001890 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891}
1892
1893static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1894{
1895 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001896 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1898
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 gh->msg_type = GSM48_MT_CC_PROGRESS;
1900
1901 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903 /* user-user */
1904 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001905 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001906
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001907 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908}
1909
1910static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1911{
1912 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001913 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1915
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 gh->msg_type = GSM48_MT_CC_CONNECT;
1917
1918 gsm48_stop_cc_timer(trans);
1919 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1920
1921 /* facility */
1922 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 /* progress */
1925 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 /* connected number */
1928 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001929 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 /* user-user */
1931 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001932 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933
1934 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1935
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001936 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937}
1938
1939static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1940{
1941 struct gsm48_hdr *gh = msgb_l3(msg);
1942 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1943 struct tlv_parsed tp;
1944 struct gsm_mncc connect;
1945
1946 gsm48_stop_cc_timer(trans);
1947
1948 memset(&connect, 0, sizeof(struct gsm_mncc));
1949 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001950 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001952 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001953 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1954 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001955
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 /* facility */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1958 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1961 }
1962 /* user-user */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1964 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1967 }
1968 /* ss-version */
1969 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1970 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1973 }
1974
1975 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001976 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001978 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979}
1980
1981
1982static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1983{
1984 struct gsm_mncc connect_ack;
1985
1986 gsm48_stop_cc_timer(trans);
1987
1988 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001989 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001990
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1992 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001993
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001994 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 &connect_ack);
1996}
1997
1998static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1999{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002000 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2002
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2004
2005 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2006
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002007 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008}
2009
2010static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2011{
2012 struct gsm48_hdr *gh = msgb_l3(msg);
2013 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2014 struct tlv_parsed tp;
2015 struct gsm_mncc disc;
2016
2017 gsm48_stop_cc_timer(trans);
2018
2019 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2020
2021 memset(&disc, 0, sizeof(struct gsm_mncc));
2022 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002023 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 /* cause */
2025 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2026 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2029 }
2030 /* facility */
2031 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2032 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002033 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2035 }
2036 /* user-user */
2037 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2038 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002039 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2041 }
2042 /* ss-version */
2043 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2044 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002045 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2047 }
2048
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002049 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050
2051}
2052
Harald Weltec66b71c2009-06-11 14:23:20 +08002053static struct gsm_mncc_cause default_cause = {
2054 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2055 .coding = 0,
2056 .rec = 0,
2057 .rec_val = 0,
2058 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2059 .diag_len = 0,
2060 .diag = { 0 },
2061};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062
2063static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2064{
2065 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002066 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2068
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2070
2071 gsm48_stop_cc_timer(trans);
2072 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2073
2074 /* cause */
2075 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002076 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 else
Harald Welte55c8f352010-03-07 23:40:35 +01002078 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079
2080 /* facility */
2081 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002082 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* progress */
2084 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002085 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 /* user-user */
2087 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002088 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089
2090 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002091 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092
2093 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2094
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002095 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096}
2097
2098static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2099{
2100 struct gsm48_hdr *gh = msgb_l3(msg);
2101 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2102 struct tlv_parsed tp;
2103 struct gsm_mncc rel;
2104 int rc;
2105
2106 gsm48_stop_cc_timer(trans);
2107
2108 memset(&rel, 0, sizeof(struct gsm_mncc));
2109 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002110 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 /* cause */
2112 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2113 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002114 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2116 }
2117 /* facility */
2118 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2119 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002120 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2122 }
2123 /* user-user */
2124 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2125 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002126 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2128 }
2129 /* ss-version */
2130 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2131 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002132 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2134 }
2135
Harald Weltedcaf5652009-07-23 18:56:43 +02002136 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002138 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002140 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002141 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002142 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002143 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 }
2145
2146 new_cc_state(trans, GSM_CSTATE_NULL);
2147
2148 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002149 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150
2151 return rc;
2152}
2153
2154static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2155{
2156 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002157 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2159
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 gh->msg_type = GSM48_MT_CC_RELEASE;
2161
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 gsm48_stop_cc_timer(trans);
2163 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2164
2165 /* cause */
2166 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002167 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 /* facility */
2169 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 /* user-user */
2172 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174
Harald Weltedcaf5652009-07-23 18:56:43 +02002175 trans->cc.T308_second = 0;
2176 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177
Harald Weltedcaf5652009-07-23 18:56:43 +02002178 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2180
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002181 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182}
2183
2184static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2185{
2186 struct gsm48_hdr *gh = msgb_l3(msg);
2187 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2188 struct tlv_parsed tp;
2189 struct gsm_mncc rel;
2190 int rc = 0;
2191
2192 gsm48_stop_cc_timer(trans);
2193
2194 memset(&rel, 0, sizeof(struct gsm_mncc));
2195 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002196 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 /* cause */
2198 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2199 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002200 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2202 }
2203 /* facility */
2204 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2205 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002206 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2208 }
2209 /* user-user */
2210 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2211 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002212 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2214 }
2215 /* ss-version */
2216 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2217 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002218 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2220 }
2221
2222 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002223 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002225 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226 MNCC_REJ_IND, &rel);
2227 break;
2228 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002229 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 MNCC_REL_CNF, &rel);
2231 break;
2232 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002233 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 MNCC_REL_IND, &rel);
2235 }
2236 }
2237
2238 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002239 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240
2241 return rc;
2242}
2243
2244static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2245{
2246 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002247 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002249 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2252
2253 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002254
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 gsm48_stop_cc_timer(trans);
2256
2257 /* cause */
2258 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 /* facility */
2261 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 /* user-user */
2264 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266
Harald Weltec7782de2010-12-21 19:31:41 +01002267 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2268
Harald Weltedcaf5652009-07-23 18:56:43 +02002269 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270
Harald Weltec7782de2010-12-21 19:31:41 +01002271 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272}
2273
2274static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2275{
2276 struct gsm48_hdr *gh = msgb_l3(msg);
2277 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2278 struct tlv_parsed tp;
2279 struct gsm_mncc fac;
2280
2281 memset(&fac, 0, sizeof(struct gsm_mncc));
2282 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002283 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 /* facility */
2285 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2286 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002287 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2289 }
2290 /* ss-version */
2291 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2292 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002293 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2295 }
2296
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002297 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298}
2299
2300static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2301{
2302 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002303 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2305
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 gh->msg_type = GSM48_MT_CC_FACILITY;
2307
2308 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002311 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312}
2313
2314static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2315{
2316 struct gsm_mncc hold;
2317
2318 memset(&hold, 0, sizeof(struct gsm_mncc));
2319 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002320 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321}
2322
2323static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2324{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002325 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2327
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2329
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002330 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331}
2332
2333static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2334{
2335 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002336 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2338
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2340
2341 /* cause */
2342 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 else
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002347 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348}
2349
2350static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2351{
2352 struct gsm_mncc retrieve;
2353
2354 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2355 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002356 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002357 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358}
2359
2360static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2361{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002362 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2364
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2366
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002367 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368}
2369
2370static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2371{
2372 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002373 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2375
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2377
2378 /* cause */
2379 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002380 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 else
Harald Welte55c8f352010-03-07 23:40:35 +01002382 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002384 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385}
2386
2387static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2388{
2389 struct gsm48_hdr *gh = msgb_l3(msg);
2390 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2391 struct tlv_parsed tp;
2392 struct gsm_mncc dtmf;
2393
2394 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2395 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002396 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397 /* keypad facility */
2398 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2399 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2402 }
2403
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002404 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2408{
2409 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002410 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2412
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2414
2415 /* keypad */
2416 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002417 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002419 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420}
2421
2422static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2423{
2424 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002425 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2427
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2429
2430 /* cause */
2431 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002432 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 else
Harald Welte55c8f352010-03-07 23:40:35 +01002434 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002436 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437}
2438
2439static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2440{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002441 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2443
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2445
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002446 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2450{
2451 struct gsm_mncc dtmf;
2452
2453 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2454 dtmf.callref = trans->callref;
2455
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002456 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457}
2458
2459static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2460{
2461 struct gsm48_hdr *gh = msgb_l3(msg);
2462 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2463 struct tlv_parsed tp;
2464 struct gsm_mncc modify;
2465
2466 memset(&modify, 0, sizeof(struct gsm_mncc));
2467 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002468 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 /* bearer capability */
2470 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2471 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002472 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002474
2475 /* Create a copy of the bearer capability
2476 * in the transaction struct, so we can use
2477 * this information later */
2478 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2479 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480 }
2481
2482 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2483
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002484 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485}
2486
2487static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2488{
2489 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002490 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2492
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 gh->msg_type = GSM48_MT_CC_MODIFY;
2494
2495 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2496
2497 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002498 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499
2500 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2501
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002502 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503}
2504
2505static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2506{
2507 struct gsm48_hdr *gh = msgb_l3(msg);
2508 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2509 struct tlv_parsed tp;
2510 struct gsm_mncc modify;
2511
2512 gsm48_stop_cc_timer(trans);
2513
2514 memset(&modify, 0, sizeof(struct gsm_mncc));
2515 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002516 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 /* bearer capability */
2518 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2519 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002520 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002522
2523 /* Create a copy of the bearer capability
2524 * in the transaction struct, so we can use
2525 * this information later */
2526 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2527 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 }
2529
2530 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2531
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002532 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533}
2534
2535static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2536{
2537 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002538 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2540
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2542
2543 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545
2546 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2547
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002548 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549}
2550
2551static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2552{
2553 struct gsm48_hdr *gh = msgb_l3(msg);
2554 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2555 struct tlv_parsed tp;
2556 struct gsm_mncc modify;
2557
2558 gsm48_stop_cc_timer(trans);
2559
2560 memset(&modify, 0, sizeof(struct gsm_mncc));
2561 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002562 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 /* bearer capability */
2564 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2565 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002568
2569 /* Create a copy of the bearer capability
2570 * in the transaction struct, so we can use
2571 * this information later */
2572 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2573 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 }
2575 /* cause */
2576 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2577 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2580 }
2581
2582 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2583
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002584 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585}
2586
2587static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2588{
2589 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002590 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2592
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2594
2595 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002598 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599
2600 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2601
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002602 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2606{
2607 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002608 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2610
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 gh->msg_type = GSM48_MT_CC_NOTIFY;
2612
2613 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002614 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002616 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617}
2618
2619static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2620{
2621 struct gsm48_hdr *gh = msgb_l3(msg);
2622 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2623// struct tlv_parsed tp;
2624 struct gsm_mncc notify;
2625
2626 memset(&notify, 0, sizeof(struct gsm_mncc));
2627 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002628// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002630 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002632 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002638 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_USER_INFO;
2642
2643 /* user-user */
2644 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 /* more data */
2647 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002648 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002650 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651}
2652
2653static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2654{
2655 struct gsm48_hdr *gh = msgb_l3(msg);
2656 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2657 struct tlv_parsed tp;
2658 struct gsm_mncc user;
2659
2660 memset(&user, 0, sizeof(struct gsm_mncc));
2661 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002662 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663 /* user-user */
2664 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2665 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002666 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2668 }
2669 /* more data */
2670 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2671 user.more = 1;
2672
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002673 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674}
2675
Philipp Maierfbf66102017-04-09 12:32:51 +02002676static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2677 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2678 uint32_t payload_msg_type)
2679{
2680 uint8_t data[sizeof(struct gsm_mncc)];
2681 struct gsm_mncc_rtp *rtp;
2682
2683 memset(&data, 0, sizeof(data));
2684 rtp = (struct gsm_mncc_rtp *) &data[0];
2685
2686 rtp->callref = callref;
2687 rtp->msg_type = cmd;
2688 rtp->ip = addr;
2689 rtp->port = port;
2690 rtp->payload_type = payload_type;
2691 rtp->payload_msg_type = payload_msg_type;
2692 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2693}
2694
2695static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2696{
2697 int msg_type;
2698
2699 /* FIXME This has to be set to some meaningful value.
2700 * Possible options are:
2701 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2702 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2703 * (0 if unknown) */
2704 msg_type = GSM_TCHF_FRAME;
2705
Neels Hofmeyr6c8afe12017-09-04 01:03:58 +02002706 uint32_t addr = mgcp_client_remote_addr_n(net->mgw.client);
Philipp Maierfbf66102017-04-09 12:32:51 +02002707 uint16_t port = trans->conn->rtp.port_cn;
2708
2709 /* FIXME: This has to be set to some meaningful value,
2710 * before the MSC-Split, this value was pulled from
2711 * lchan->abis_ip.rtp_payload */
2712 uint32_t payload_type = 0;
2713
2714 return mncc_recv_rtp(net, trans->callref, cmd,
2715 addr,
2716 port,
2717 payload_type,
2718 msg_type);
2719}
2720
2721static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2722{
2723 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2724}
2725
2726static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2727{
2728 struct gsm_trans *trans;
2729 int rc;
2730
2731 /* Find callref */
2732 trans = trans_find_by_callref(net, callref);
2733 if (!trans) {
2734 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2735 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2736 return -EIO;
2737 }
2738 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2739 if (!trans->conn) {
2740 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2741 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2742 return 0;
2743 }
2744
2745 trans->conn->mncc_rtp_bridge = 1;
2746
2747 /* When we call msc_call_assignment() we will trigger, depending
2748 * on the RAN type the call assignment on the A or Iu interface.
2749 * msc_call_assignment() also takes care about sending the CRCX
2750 * command to the MGCP-GW. The CRCX will return the port number,
2751 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2752 * have to return this port number back to the MNCC by sending
2753 * it back with the TCH_RTP_CREATE message. To make sure that
2754 * this message is sent AFTER the response to CRCX from the
2755 * MGCP-GW has arrived, we need will instruct msc_call_assignment()
2756 * to take care of this by setting trans->tch_rtp_create to true.
2757 * This will make sure that gsm48_tch_rtp_create() (below) is
2758 * called as soon as the local port number has become known. */
2759 trans->tch_rtp_create = true;
2760
2761 /* Assign call (if not done yet) */
2762 if (trans->assignment_done == false) {
2763 rc = msc_call_assignment(trans);
2764 trans->assignment_done = true;
2765 }
2766 else
2767 rc = 0;
2768
2769 return rc;
2770}
2771
2772/* Trigger TCH_RTP_CREATE acknowledgement */
2773int gsm48_tch_rtp_create(struct gsm_trans *trans)
2774{
2775 /* This function is called as soon as the port, on which the
2776 * mgcp-gw expects the incoming RTP stream from the remote
2777 * end (e.g. Asterisk) is known. */
2778
2779 struct gsm_subscriber_connection *conn = trans->conn;
2780 struct gsm_network *network = conn->network;
2781
2782 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2783 return 0;
2784}
2785
2786static int tch_rtp_connect(struct gsm_network *net, void *arg)
2787{
2788 struct gsm_trans *trans;
2789 struct gsm_mncc_rtp *rtp = arg;
2790
2791 /* Find callref */
2792 trans = trans_find_by_callref(net, rtp->callref);
2793 if (!trans) {
2794 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2795 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2796 return -EIO;
2797 }
2798 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2799 if (!trans->conn) {
2800 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2801 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2802 return 0;
2803 }
2804
Philipp Maier2f108b02017-11-23 13:38:02 +01002805 msc_call_connect(trans, rtp->port, rtp->ip);
Philipp Maierfbf66102017-04-09 12:32:51 +02002806 return 0;
2807}
2808
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002810 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811 int type;
2812 int (*rout) (struct gsm_trans *trans, void *arg);
2813} downstatelist[] = {
2814 /* mobile originating call establishment */
2815 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002816 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2818 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2819 {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 */
2820 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2821 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2822 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2823 /* mobile terminating call establishment */
2824 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2825 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2826 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2827 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2828 /* signalling during call */
2829 {SBIT(GSM_CSTATE_ACTIVE),
2830 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2831 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2832 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2833 {ALL_STATES,
2834 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2835 {ALL_STATES,
2836 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2837 {ALL_STATES,
2838 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2839 {SBIT(GSM_CSTATE_ACTIVE),
2840 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2841 {SBIT(GSM_CSTATE_ACTIVE),
2842 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2843 {SBIT(GSM_CSTATE_ACTIVE),
2844 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2845 {SBIT(GSM_CSTATE_ACTIVE),
2846 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2847 {SBIT(GSM_CSTATE_ACTIVE),
2848 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2849 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2850 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2851 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2852 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2853 {SBIT(GSM_CSTATE_ACTIVE),
2854 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2855 /* clearing */
2856 {SBIT(GSM_CSTATE_INITIATED),
2857 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2858 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2859 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2860 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2861 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862};
2863
2864#define DOWNSLLEN \
2865 (sizeof(downstatelist) / sizeof(struct downstate))
2866
2867
Harald Welte76556372010-12-22 23:57:45 +01002868int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869{
Harald Welte1a6f7982009-08-09 18:52:33 +02002870 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002872 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873 struct gsm_mncc *data = arg, rel;
2874
Harald Welte04dc88f2010-12-22 21:45:05 +01002875 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2876
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877 /* handle special messages */
2878 switch(msg_type) {
2879 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002880 rc = tch_bridge(net, arg);
2881 if (rc < 0)
2882 disconnect_bridge(net, arg, -rc);
2883 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002884 case MNCC_RTP_CREATE:
2885 return tch_rtp_create(net, data->callref);
2886 case MNCC_RTP_CONNECT:
2887 return tch_rtp_connect(net, arg);
2888 case MNCC_RTP_FREE:
2889 /* unused right now */
2890 return -EIO;
2891
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002894 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002895 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002896 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002897 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002898 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2899 get_mncc_name(msg_type));
2900 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 }
2902
2903 memset(&rel, 0, sizeof(struct gsm_mncc));
2904 rel.callref = data->callref;
2905
2906 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002907 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908
2909 /* Callref unknown */
2910 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002911 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002912
Harald Welte4a3464c2009-07-04 10:11:24 +02002913 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002914 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2915 "Received '%s' from MNCC with "
2916 "unknown callref %d\n", data->called.number,
2917 get_mncc_name(msg_type), data->callref);
2918 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002919 return mncc_release_ind(net, NULL, data->callref,
2920 GSM48_CAUSE_LOC_PRN_S_LU,
2921 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002923 if (!data->called.number[0] && !data->imsi[0]) {
2924 DEBUGP(DCC, "(bts - trx - ts - ti) "
2925 "Received '%s' from MNCC with "
2926 "no number or IMSI\n", get_mncc_name(msg_type));
2927 /* Invalid number */
2928 return mncc_release_ind(net, NULL, data->callref,
2929 GSM48_CAUSE_LOC_PRN_S_LU,
2930 GSM48_CC_CAUSE_INV_NR_FORMAT);
2931 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002933 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002934 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2935 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002936 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002937 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002938
2939 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002940 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002941
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002943 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2945 "Received '%s' from MNCC with "
2946 "unknown subscriber %s\n", data->called.number,
2947 get_mncc_name(msg_type), data->called.number);
2948 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002949 return mncc_release_ind(net, NULL, data->callref,
2950 GSM48_CAUSE_LOC_PRN_S_LU,
2951 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 }
2953 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002954 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2956 "Received '%s' from MNCC with "
2957 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01002958 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02002959 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002960 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002961 return mncc_release_ind(net, NULL, data->callref,
2962 GSM48_CAUSE_LOC_PRN_S_LU,
2963 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 }
2965 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002966 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002967 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002969 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002971 mncc_release_ind(net, NULL, data->callref,
2972 GSM48_CAUSE_LOC_PRN_S_LU,
2973 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974 return -ENOMEM;
2975 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002976
2977 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002978 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002979
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002980 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002981 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 /* find transaction with this subscriber already paging */
2983 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002984 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002986 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002988 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 "Received '%s' from MNCC with "
2990 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002991 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02002993 get_mncc_name(msg_type), vsub->lac);
2994 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002995 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 return 0;
2997 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002998 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002999 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003000
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003001 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003002 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003003 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003004 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003005 trans,
3006 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003007 if (!trans->paging_request) {
3008 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003009 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003010 trans_free(trans);
3011 return 0;
3012 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003013 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003014 return 0;
3015 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003016
3017 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003018 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003019 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003020 } else {
3021 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003022 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003024
3025 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003026 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027
3028 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003029 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003030 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003031 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003032 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003034 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3035 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 if (msg_type == MNCC_REL_REQ)
3037 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3038 else
3039 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3040 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003041 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003043 } else {
3044 DEBUGP(DCC, "(ti %02x sub %s) "
3045 "Received '%s' from MNCC in state %d (%s)\n",
3046 trans->transaction_id,
3047 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3048 get_mncc_name(msg_type), trans->cc.state,
3049 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 }
3051
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052 /* Find function for current state and message */
3053 for (i = 0; i < DOWNSLLEN; i++)
3054 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003055 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 break;
3057 if (i == DOWNSLLEN) {
3058 DEBUGP(DCC, "Message unhandled at this state.\n");
3059 return 0;
3060 }
3061
3062 rc = downstatelist[i].rout(trans, arg);
3063
3064 return rc;
3065}
3066
3067
3068static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003069 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 int type;
3071 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3072} datastatelist[] = {
3073 /* mobile originating call establishment */
3074 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3075 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3076 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3077 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3078 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3079 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3080 /* mobile terminating call establishment */
3081 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3082 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3083 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3084 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003085 {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 +08003086 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3087 /* signalling during call */
3088 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3089 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3090 {SBIT(GSM_CSTATE_ACTIVE),
3091 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3092 {ALL_STATES,
3093 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3094 {ALL_STATES,
3095 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3096 {ALL_STATES,
3097 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3098 {SBIT(GSM_CSTATE_ACTIVE),
3099 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3100 {SBIT(GSM_CSTATE_ACTIVE),
3101 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3102 {SBIT(GSM_CSTATE_ACTIVE),
3103 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3104 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3105 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3106 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3107 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3108 {SBIT(GSM_CSTATE_ACTIVE),
3109 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3110 /* clearing */
3111 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3112 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3113 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3114 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3115 {ALL_STATES, /* 5.4.3.4 */
3116 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3117};
3118
3119#define DATASLLEN \
3120 (sizeof(datastatelist) / sizeof(struct datastate))
3121
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003122static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003123{
3124 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003125 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003126 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003127 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003129
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 if (msg_type & 0x80) {
3131 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3132 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003133 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003134
Harald Welte2483f1b2016-06-19 18:06:02 +02003135 if (!conn->vsub) {
3136 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003137 return -EINVAL;
3138 }
3139
Harald Welte4bfdfe72009-06-10 23:11:52 +08003140 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003141 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003142
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003143#if BEFORE_MSCSPLIT
3144 /* Re-enable this log output once we can obtain this information via
3145 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003146 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003147 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003148 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003149 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003150 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3151 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003152#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153
3154 /* Create transaction */
3155 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003156 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 "creating new trans.\n", transaction_id);
3158 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003159 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003160 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003161 transaction_id, new_callref++);
3162 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003163 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003164 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003165 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 GSM48_MT_CC_RELEASE_COMPL);
3167 return -ENOMEM;
3168 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003170 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte2483f1b2016-06-19 18:06:02 +02003171 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172 }
3173
3174 /* find function for current state and message */
3175 for (i = 0; i < DATASLLEN; i++)
3176 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003177 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178 break;
3179 if (i == DATASLLEN) {
3180 DEBUGP(DCC, "Message unhandled at this state.\n");
3181 return 0;
3182 }
3183
Harald Welte2483f1b2016-06-19 18:06:02 +02003184 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003185
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003187
Harald Welte2483f1b2016-06-19 18:06:02 +02003188 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003189 return rc;
3190}
3191
Harald Welte2483f1b2016-06-19 18:06:02 +02003192static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003193{
Harald Welte2483f1b2016-06-19 18:06:02 +02003194 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3195 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003196
Harald Welte2483f1b2016-06-19 18:06:02 +02003197 switch (pdisc) {
3198 case GSM48_PDISC_MM:
3199 switch (msg_type) {
3200 case GSM48_MT_MM_LOC_UPD_REQUEST:
3201 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003202 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003203 case GSM48_MT_MM_AUTH_RESP:
3204 case GSM48_MT_MM_AUTH_FAIL:
3205 case GSM48_MT_MM_ID_RESP:
3206 case GSM48_MT_MM_TMSI_REALL_COMPL:
3207 case GSM48_MT_MM_IMSI_DETACH_IND:
3208 return true;
3209 default:
3210 break;
3211 }
3212 break;
3213 case GSM48_PDISC_RR:
3214 switch (msg_type) {
3215 case GSM48_MT_RR_CIPH_M_COMPL:
3216 case GSM48_MT_RR_PAG_RESP:
3217 return true;
3218 default:
3219 break;
3220 }
3221 break;
3222 default:
3223 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003224 }
3225
Harald Welte2483f1b2016-06-19 18:06:02 +02003226 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003227}
3228
Harald Welte2483f1b2016-06-19 18:06:02 +02003229void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3230 struct msgb *msg)
3231{
3232
3233 /* If a CM Service Request was received before, this is the request the
3234 * conn was opened for. No need to wait for further messages. */
3235
3236 if (!conn->received_cm_service_request)
3237 return;
3238
3239 if (log_check_level(DMM, LOGL_DEBUG)) {
3240 struct gsm48_hdr *gh = msgb_l3(msg);
3241 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3242 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3243
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003244 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003245 " received_cm_service_request changes to false\n",
3246 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003247 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003248 }
3249 conn->received_cm_service_request = false;
3250}
3251
3252
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003253/* 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 +08003254int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003255{
Philipp Maier91f10c72017-04-20 18:40:37 +02003256 struct gsm48_hdr *gh;
3257 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003258 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003259
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003260 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003261 OSMO_ASSERT(conn);
3262 OSMO_ASSERT(msg);
3263
Philipp Maier91f10c72017-04-20 18:40:37 +02003264 gh = msgb_l3(msg);
3265 pdisc = gsm48_hdr_pdisc(gh);
3266
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003267 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3268 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3269 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003270
3271 if (!msc_subscr_conn_is_accepted(conn)
3272 && !msg_is_initially_permitted(gh)) {
3273 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003274 "subscr %s: Message not permitted for initial conn: %s\n",
3275 vlr_subscr_name(conn->vsub),
3276 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003277 return -EACCES;
3278 }
3279
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003280 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3281 LOGP(DMM, LOGL_ERROR,
3282 "%s: Illegal situation: RAN type mismatch:"
3283 " attached via %s, received message via %s\n",
3284 vlr_subscr_name(conn->vsub),
3285 ran_type_name(conn->vsub->cs.attached_via_ran),
3286 ran_type_name(conn->via_ran));
3287 return -EACCES;
3288 }
3289
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003290#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003291 if (silent_call_reroute(conn, msg))
3292 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003293#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003294
Harald Welte52b1f982008-12-23 20:25:15 +00003295 switch (pdisc) {
3296 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003297 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003298 break;
3299 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003300 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003301 break;
3302 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003303 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003304 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003305 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003306 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003307 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003308 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003309 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003310 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3311 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003312 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003313 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003314 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003315 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003316 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003317 case GSM48_PDISC_TEST:
3318 rc = gsm0414_rcv_test(conn, msg);
3319 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003320 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003321 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3322 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003323 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003324 break;
3325 }
3326
3327 return rc;
3328}
Harald Welte8470bf22008-12-25 23:28:35 +00003329
Harald Welte2483f1b2016-06-19 18:06:02 +02003330/***********************************************************************
3331 * VLR integration
3332 ***********************************************************************/
3333
3334/* VLR asks us to send an authentication request */
3335static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3336 bool send_autn)
3337{
3338 struct gsm_subscriber_connection *conn = msc_conn_ref;
3339 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3340 send_autn? at->vec.autn : NULL,
3341 at->key_seq);
3342}
3343
3344/* VLR asks us to send an authentication reject */
3345static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3346{
3347 struct gsm_subscriber_connection *conn = msc_conn_ref;
3348 return gsm48_tx_mm_auth_rej(conn);
3349}
3350
3351/* VLR asks us to transmit an Identity Request of given type */
3352static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3353{
3354 struct gsm_subscriber_connection *conn = msc_conn_ref;
3355 return mm_tx_identity_req(conn, mi_type);
3356}
3357
3358/* VLR asks us to transmit a Location Update Accept */
3359static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3360{
3361 struct gsm_subscriber_connection *conn = msc_conn_ref;
3362 return gsm0408_loc_upd_acc(conn, send_tmsi);
3363}
3364
3365/* VLR asks us to transmit a Location Update Reject */
3366static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3367{
3368 struct gsm_subscriber_connection *conn = msc_conn_ref;
3369 return gsm0408_loc_upd_rej(conn, cause);
3370}
3371
3372/* VLR asks us to transmit a CM Service Accept */
3373static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3374{
3375 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003376 return msc_gsm48_tx_mm_serv_ack(conn);
3377}
3378
3379static int msc_vlr_tx_common_id(void *msc_conn_ref)
3380{
3381 struct gsm_subscriber_connection *conn = msc_conn_ref;
3382 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003383}
3384
3385/* VLR asks us to transmit a CM Service Reject */
3386static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3387{
3388 uint8_t cause;
3389 struct gsm_subscriber_connection *conn = msc_conn_ref;
3390 conn->received_cm_service_request = false;
3391
3392 switch (result) {
3393 default:
3394 case VLR_PR_ARQ_RES_NONE:
3395 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3396 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3397 cause = GSM48_REJECT_NETWORK_FAILURE;
3398 break;
3399 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3400 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3401 break;
3402 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3403 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3404 break;
3405 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3406 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3407 break;
3408 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3409 cause = GSM48_REJECT_ILLEGAL_MS;
3410 break;
3411 case VLR_PR_ARQ_RES_TIMEOUT:
3412 cause = GSM48_REJECT_CONGESTION;
3413 break;
3414 };
3415
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003416 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003417}
3418
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003419/* For msc_vlr_set_ciph_mode() */
3420osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3421 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3422
Harald Welte2483f1b2016-06-19 18:06:02 +02003423/* VLR asks us to start using ciphering */
3424static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3425 enum vlr_ciph ciph,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01003426 bool umts_aka,
Harald Welte2483f1b2016-06-19 18:06:02 +02003427 bool retrieve_imeisv)
3428{
3429 struct gsm_subscriber_connection *conn = msc_conn_ref;
3430 struct vlr_subscr *vsub;
3431 struct gsm_auth_tuple *tuple;
3432
3433 if (!conn || !conn->vsub) {
3434 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3435 " NULL conn/subscriber");
3436 return -EINVAL;
3437 }
3438
3439 vsub = conn->vsub;
3440 tuple = vsub->last_tuple;
3441
3442 if (!tuple) {
3443 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3444 " Command: no auth tuple available\n",
3445 vlr_subscr_name(vsub));
3446 return -EINVAL;
3447 }
3448
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003449 switch (conn->via_ran) {
3450 case RAN_GERAN_A:
3451 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3452 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003453 {
3454 struct gsm0808_encrypt_info ei;
3455
3456 ei.perm_algo[0] = vlr_ciph_to_gsm0808_alg_id(ciph);
3457 ei.perm_algo_len = 1;
3458
Neels Hofmeyr52821712017-12-18 01:23:42 +01003459 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3460 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3461 * necessarily a match for the UMTS AKA tokens. */
3462 if (umts_aka)
3463 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3464 else
3465 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003466 ei.key_len = sizeof(tuple->vec.kc);
3467
3468 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3469 }
3470
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003471 case RAN_UTRAN_IU:
3472#ifdef BUILD_IU
3473 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3474 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003475 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003476#else
3477 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3478 " built without Iu support\n");
3479 return -ENOTSUP;
3480#endif
3481
3482 default:
3483 break;
3484 }
3485 LOGP(DMM, LOGL_ERROR,
3486 "%s: cannot start ciphering, unknown RAN type %d\n",
3487 vlr_subscr_name(conn->vsub), conn->via_ran);
3488 return -ENOTSUP;
3489}
3490
3491void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3492{
3493 struct vlr_ciph_result vlr_res = {};
3494
3495 if (!conn || !conn->vsub) {
3496 LOGP(DMM, LOGL_ERROR,
3497 "Rx Security Mode Complete for invalid conn\n");
3498 return;
3499 }
3500
3501 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3502 vlr_subscr_name(conn->vsub));
3503
3504 vlr_res.cause = VLR_CIPH_COMPL;
3505 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003506}
3507
3508/* VLR informs us that the subscriber data has somehow been modified */
3509static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3510{
Maxa263bb22017-12-27 13:23:44 +01003511 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3512 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003513}
3514
3515/* VLR informs us that the subscriber has been associated with a conn */
3516static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3517 struct vlr_subscr *vsub)
3518{
3519 struct gsm_subscriber_connection *conn = msc_conn_ref;
3520 OSMO_ASSERT(!conn->vsub);
3521 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003522 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003523}
3524
3525/* operations that we need to implement for libvlr */
3526static const struct vlr_ops msc_vlr_ops = {
3527 .tx_auth_req = msc_vlr_tx_auth_req,
3528 .tx_auth_rej = msc_vlr_tx_auth_rej,
3529 .tx_id_req = msc_vlr_tx_id_req,
3530 .tx_lu_acc = msc_vlr_tx_lu_acc,
3531 .tx_lu_rej = msc_vlr_tx_lu_rej,
3532 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3533 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3534 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003535 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003536 .subscr_update = msc_vlr_subscr_update,
3537 .subscr_assoc = msc_vlr_subscr_assoc,
3538};
3539
3540/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3541int msc_vlr_alloc(struct gsm_network *net)
3542{
3543 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3544 if (!net->vlr)
3545 return -ENOMEM;
3546 net->vlr->user_ctx = net;
3547 return 0;
3548}
3549
3550/* Launch the VLR, i.e. its GSUP connection */
3551int msc_vlr_start(struct gsm_network *net)
3552{
3553 OSMO_ASSERT(net->vlr);
3554 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3555 net->gsup_server_port);
3556}