blob: e5ddb44d482b2388365da80b54f57dfbe41af90d [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>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.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 Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080082static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000084
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080086
Neels Hofmeyr9d744252018-03-22 16:09:50 +010087static bool classmark_is_r99(struct gsm_classmark *cm)
88{
89 int rev_lev = 0;
90 if (cm->classmark1_set)
91 rev_lev = cm->classmark1.rev_lev;
92 else if (cm->classmark2_len > 0)
93 rev_lev = (cm->classmark2[0] >> 5) & 0x3;
94 return rev_lev >= 2;
95}
96
Harald Welte71330722017-12-23 19:59:02 +010097/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
98static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
99{
100 switch (a5) {
101 case 0:
102 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
103 return true;
104 case 1:
105 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
106 * "The network shall not provide service to an MS which indicates that it does not
107 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
108 * on policy here */
109 /* See 3GPP TS 24.008 10.5.1.7 */
110 if (!cm->classmark1_set) {
111 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
112 return true;
113 } else {
114 if (cm->classmark1.a5_1)
115 return false; /* Inverted logic for this bit! */
116 else
117 return true;
118 }
119 break;
120 case 2:
121 case 3:
122 /* See 3GPP TS 24.008 10.5.1.6 */
123 if (cm->classmark2_len < 3) {
124 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
125 return false;
126 } else {
127 if (cm->classmark2[2] & (1 << (a5-2)))
128 return true;
129 else
130 return false;
131 }
132 break;
133 case 4:
134 case 5:
135 case 6:
136 case 7:
137 /* See 3GPP TS 24.008 10.5.1.7 */
138 if (cm->classmark3_len < 1) {
139 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
140 return false;
141 } else {
142 if (cm->classmark3[0] & (1 << (a5-4)))
143 return true;
144 else
145 return false;
146 }
147 break;
148 default:
149 return false;
150 }
151}
152
Harald Welte31c00f72011-03-03 23:29:05 +0100153void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
154{
155 net->mncc_recv(net, msg);
156}
157
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800158static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
159 struct gsm_trans *trans)
160{
161 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
162
163 /* if we get passed a transaction reference, do some common
164 * work that the caller no longer has to do */
165 if (trans) {
166 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200167 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800168 }
169
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200170 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800171}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100172
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800173int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
174{
175 struct gsm48_hdr *gh;
176 struct msgb *ss_notify;
177
178 ss_notify = gsm0480_create_notifySS(message);
179 if (!ss_notify)
180 return -1;
181
182 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
183 uint8_t *data = msgb_push(ss_notify, 1);
184 data[0] = ss_notify->len - 1;
185 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
186 gh->msg_type = GSM48_MT_CC_FACILITY;
187 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
188}
189
Harald Welte2483f1b2016-06-19 18:06:02 +0200190/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100191void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
192{
193 struct gsm_trans *trans, *temp;
194
195 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
196
197 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100198 if (trans->protocol == protocol) {
199 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100200 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100201 }
Harald Welte371efe52010-12-22 23:17:50 +0100202 }
203}
204
Holger Freyther429e7762008-12-30 13:28:30 +0000205/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200206static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000207{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800208 struct msgb *msg;
209
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800210 msg = gsm48_create_loc_upd_rej(cause);
211 if (!msg) {
212 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
213 return -1;
214 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200215
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200216 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
217 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100218
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800219 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000220}
221
222/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200223static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
224 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000225{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100226 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000227 struct gsm48_hdr *gh;
228 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200229 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100230 struct osmo_location_area_id laid = {
231 .plmn = conn->network->plmn,
232 .lac = conn->lac,
233 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200234
Harald Welte52b1f982008-12-23 20:25:15 +0000235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
236 gh->proto_discr = GSM48_PDISC_MM;
237 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
238
239 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100240 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000241
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 if (send_tmsi == GSM_RESERVED_TMSI) {
243 /* we did not allocate a TMSI to the MS, so we need to
244 * include the IMSI in order for the MS to delete any
245 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200246 uint8_t mi[10];
247 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200248 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200249 mid = msgb_put(msg, len);
250 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200251 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
252 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200253 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200254 /* Include the TMSI, which means that the MS will send a
255 * TMSI REALLOCATION COMPLETE, and we should wait for
256 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200257 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200258 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200259 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
260 vlr_subscr_name(conn->vsub),
261 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200262 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200263 /* TODO: Follow-on proceed */
264 /* TODO: CTS permission */
265 /* TODO: Equivalent PLMNs */
266 /* TODO: Emergency Number List */
267 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000268
Harald Welte52b1f982008-12-23 20:25:15 +0000269
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800270 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000271}
272
Harald Weltebf5e8df2009-02-03 12:59:45 +0000273/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200274static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000275{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100276 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000277 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000278
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
280 gh->proto_discr = GSM48_PDISC_MM;
281 gh->msg_type = GSM48_MT_MM_ID_REQ;
282 gh->data[0] = id_type;
283
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800284 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000285}
286
Harald Weltebf5e8df2009-02-03 12:59:45 +0000287/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800288static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000289{
290 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200291 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200292 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000293
Harald Welte2483f1b2016-06-19 18:06:02 +0200294 if (!conn->vsub) {
295 LOGP(DMM, LOGL_ERROR,
296 "Rx MM Identity Response: invalid: no subscriber\n");
297 return -EINVAL;
298 }
299
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200300 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200301 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
302 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000303
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200304 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100305
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000307}
308
Harald Welte2483f1b2016-06-19 18:06:02 +0200309/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200310static const struct value_string lupd_names[] = {
311 { GSM48_LUPD_NORMAL, "NORMAL" },
312 { GSM48_LUPD_PERIODIC, "PERIODIC" },
313 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
314 { 0, NULL }
315};
Harald Welte2a139372009-02-22 21:14:55 +0000316
Harald Welte2483f1b2016-06-19 18:06:02 +0200317/* Chapter 9.2.15: Receive Location Updating Request.
318 * Keep this function non-static for direct invocation by unit tests. */
319int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000320{
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000322 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000323 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200324 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200325 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200327 uint32_t tmsi;
328 char *imsi;
329 struct osmo_location_area_id old_lai, new_lai;
330 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200331 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200332 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000333
Harald Welte8470bf22008-12-25 23:28:35 +0000334 lu = (struct gsm48_loc_upd_req *) gh->data;
335
336 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000337
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200338 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000339
Harald Welte2483f1b2016-06-19 18:06:02 +0200340 rc = msc_create_conn_fsm(conn, mi_string);
341 if (rc)
342 /* logging already happened in msc_create_conn_fsm() */
343 return rc;
344
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200345 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
346
Harald Welte2483f1b2016-06-19 18:06:02 +0200347 conn->classmark.classmark1 = lu->classmark1;
348 conn->classmark.classmark1_set = true;
349
350 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
351 gsm48_mi_type_name(mi_type), mi_string,
352 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000353
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200354 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100355
Harald Welte24ff6ee2009-12-22 00:41:05 +0100356 switch (lu->type) {
357 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200358 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200359 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100360 break;
361 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200362 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200363 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100364 break;
365 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200366 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200367 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100368 break;
369 }
370
Harald Welte2483f1b2016-06-19 18:06:02 +0200371 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
372 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
373 /* TODO: 10.5.7.8 Device properties */
374 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200375
Harald Welte52b1f982008-12-23 20:25:15 +0000376 switch (mi_type) {
377 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200378 tmsi = GSM_RESERVED_TMSI;
379 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000380 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000381 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200382 tmsi = tmsi_from_string(mi_string);
383 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000384 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200385 default:
Harald Weltea0368542009-06-27 02:58:43 +0200386 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200387 tmsi = GSM_RESERVED_TMSI;
388 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000389 break;
390 }
391
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100392 gsm48_decode_lai2(&lu->lai, &old_lai);
393 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200394 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200395 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200396
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200397 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200398 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200399 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200400 net->vlr, conn, vlr_lu_type, tmsi, imsi,
401 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200402 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100403 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200404 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200405 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200406 net->vlr->cfg.assign_tmsi);
407 if (!lu_fsm) {
408 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
409 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800410 }
411
Harald Welte2483f1b2016-06-19 18:06:02 +0200412 /* From vlr_loc_update() we expect an implicit dispatch of
413 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
414 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000415
Harald Welte2483f1b2016-06-19 18:06:02 +0200416 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
417 LOGP(DRR, LOGL_ERROR,
418 "%s: internal error during Location Updating attempt\n",
419 mi_string);
420 return -EIO;
421 }
422
423 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000424}
425
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100426/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200427/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100428static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800429{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100430 uint8_t ret;
431
432 ret = value / 10;
433 ret |= (value % 10) << 4;
434
435 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800436}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100437
Harald Welte4bfdfe72009-06-10 23:11:52 +0800438
Harald Weltedb253af2008-12-30 17:56:55 +0000439/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800440int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000441{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100442 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000443 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200444 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200445 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200446 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100447
Harald Welte4bfdfe72009-06-10 23:11:52 +0800448 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100449 struct tm* gmt_time;
450 struct tm* local_time;
451 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100452 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000453
Harald Weltedb253af2008-12-30 17:56:55 +0000454 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
455 gh->proto_discr = GSM48_PDISC_MM;
456 gh->msg_type = GSM48_MT_MM_INFO;
457
458 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200459#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000460 name_len = strlen(net->name_long);
461 /* 10.5.3.5a */
462 ptr8 = msgb_put(msg, 3);
463 ptr8[0] = GSM48_IE_NAME_LONG;
464 ptr8[1] = name_len*2 +1;
465 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
466
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200467 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000468 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000469 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000470
471 /* FIXME: Use Cell Broadcast, not UCS-2, since
472 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200473#endif
474 name_len = (strlen(net->name_long)*7)/8;
475 name_pad = (8 - strlen(net->name_long)*7)%8;
476 if (name_pad > 0)
477 name_len++;
478 /* 10.5.3.5a */
479 ptr8 = msgb_put(msg, 3);
480 ptr8[0] = GSM48_IE_NAME_LONG;
481 ptr8[1] = name_len +1;
482 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
483
484 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100485 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200486
Harald Weltedb253af2008-12-30 17:56:55 +0000487 }
488
489 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200490#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000491 name_len = strlen(net->name_short);
492 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200493 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200494 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000495 ptr8[1] = name_len*2 + 1;
496 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
497
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200498 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000499 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000500 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200501#endif
502 name_len = (strlen(net->name_short)*7)/8;
503 name_pad = (8 - strlen(net->name_short)*7)%8;
504 if (name_pad > 0)
505 name_len++;
506 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200507 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200508 ptr8[0] = GSM48_IE_NAME_SHORT;
509 ptr8[1] = name_len +1;
510 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
511
512 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100513 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200514
Harald Weltedb253af2008-12-30 17:56:55 +0000515 }
516
Harald Weltedb253af2008-12-30 17:56:55 +0000517 /* Section 10.5.3.9 */
518 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100519 gmt_time = gmtime(&cur_t);
520
Harald Weltedb253af2008-12-30 17:56:55 +0000521 ptr8 = msgb_put(msg, 8);
522 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100523 ptr8[1] = bcdify(gmt_time->tm_year % 100);
524 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
525 ptr8[3] = bcdify(gmt_time->tm_mday);
526 ptr8[4] = bcdify(gmt_time->tm_hour);
527 ptr8[5] = bcdify(gmt_time->tm_min);
528 ptr8[6] = bcdify(gmt_time->tm_sec);
529
Neels Hofmeyr73983952016-05-10 13:29:33 +0200530 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200531 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200532 if (net->tz.hr < 0) {
533 tzunits = ((net->tz.hr/-1)*4);
534 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100535 ptr8[7] = bcdify(tzunits);
536 /* Set negative time */
537 ptr8[7] |= 0x08;
538 }
539 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200540 tzunits = net->tz.hr*4;
541 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100542 ptr8[7] = bcdify(tzunits);
543 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100544 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200545 if (net->tz.dst >= 0 && net->tz.dst <= 2)
546 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100547 }
548 else {
549 /* Need to get GSM offset and convert into 15 min units */
550 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200551#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200552 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100553 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200554#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200555 /* find timezone offset */
556 time_t utc;
557 double offsetFromUTC;
558 utc = mktime(gmt_time);
559 local_time = localtime(&cur_t);
560 offsetFromUTC = difftime(cur_t, utc);
561 if (local_time->tm_isdst)
562 offsetFromUTC += 3600.0;
563 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200564#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100565 if (tzunits < 0) {
566 tzunits = tzunits/-1;
567 ptr8[7] = bcdify(tzunits);
568 /* Flip it to negative */
569 ptr8[7] |= 0x08;
570 }
571 else
572 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100573
574 /* Does not support DST +2 */
575 if (local_time->tm_isdst)
576 dst = 1;
577 }
578
579 if (dst) {
580 ptr8 = msgb_put(msg, 3);
581 ptr8[0] = GSM48_IE_NET_DST;
582 ptr8[1] = 1;
583 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100584 }
Harald Weltedb253af2008-12-30 17:56:55 +0000585
Daniel Willmanneea93372009-08-13 03:42:07 +0200586 DEBUGP(DMM, "-> MM INFO\n");
587
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800588 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000589}
590
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100591/*! Send an Authentication Request to MS on the given subscriber connection
592 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
593 * \param[in] conn Subscriber connection to send on.
594 * \param[in] rand Random challenge token to send, must be 16 bytes long.
595 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
596 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
597 * \param[in] key_seq auth tuple's sequence number.
598 */
599int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
600 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200601{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100602 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200603 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200604 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200605
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100606 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100607 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100608 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200609
Harald Welte7984d5c2009-08-12 22:56:50 +0200610 gh->proto_discr = GSM48_PDISC_MM;
611 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
612
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100613 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200614
Harald Welte7984d5c2009-08-12 22:56:50 +0200615 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100616 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200617 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200618 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200619
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100620
621 /* 16 bytes AUTN */
622 if (autn)
623 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
624
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800625 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200626}
627
628/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800629int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200630{
631 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800632 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200633}
634
Harald Welte2483f1b2016-06-19 18:06:02 +0200635static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
636static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
637
638static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100639{
Harald Welte2483f1b2016-06-19 18:06:02 +0200640 uint8_t mi_type;
641 char mi_string[GSM48_MI_SIZE];
642 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100643
Harald Welte2483f1b2016-06-19 18:06:02 +0200644 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
645 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100646
Harald Welte2483f1b2016-06-19 18:06:02 +0200647 switch (mi_type) {
648 case GSM_MI_TYPE_IMSI:
649 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
650 goto accept_reuse;
651 break;
652 case GSM_MI_TYPE_TMSI:
653 tmsi = osmo_load32be(mi_lv+2);
654 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
655 goto accept_reuse;
656 break;
657 case GSM_MI_TYPE_IMEI:
658 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
659 goto accept_reuse;
660 break;
661 default:
662 break;
663 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100664
Harald Welte2483f1b2016-06-19 18:06:02 +0200665 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
666 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
667 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
668 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100669
Harald Welte2483f1b2016-06-19 18:06:02 +0200670accept_reuse:
671 DEBUGP(DMM, "%s: re-using already accepted connection\n",
672 vlr_subscr_name(conn->vsub));
673 conn->received_cm_service_request = true;
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200674 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200675 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100676}
677
Harald Welte4ed0e922009-01-10 03:17:30 +0000678/*
679 * Handle CM Service Requests
680 * a) Verify that the packet is long enough to contain the information
681 * we require otherwsie reject with INCORRECT_MESSAGE
682 * b) Try to parse the TMSI. If we do not have one reject
683 * c) Check that we know the subscriber with the TMSI otherwise reject
684 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200685 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200686 *
687 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000688 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200689int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000690{
Harald Welte2483f1b2016-06-19 18:06:02 +0200691 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200692 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200693 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000694
Harald Welteba4cf162009-01-10 01:49:35 +0000695 struct gsm48_hdr *gh = msgb_l3(msg);
696 struct gsm48_service_request *req =
697 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800698 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200699 uint8_t classmark2_len = gh->data[1];
700 uint8_t *classmark2 = gh->data+2;
701 uint8_t mi_len = *(classmark2 + classmark2_len);
702 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200703 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200704 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200705 int rc;
706
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100707 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200708 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000709
Harald Weltec9e02182009-05-01 19:07:53 +0000710 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000711 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000712 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200713 return msc_gsm48_tx_mm_serv_rej(conn,
714 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000715 }
716
717 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000718 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200719 return msc_gsm48_tx_mm_serv_rej(conn,
720 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000721 }
722
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200723 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000724 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welte3995d2e2018-01-24 11:23:54 +0100725 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
726 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200727
Harald Welte3995d2e2018-01-24 11:23:54 +0100728 switch (mi_type) {
729 case GSM_MI_TYPE_IMSI:
730 case GSM_MI_TYPE_TMSI:
731 /* continue below */
732 break;
733 case GSM_MI_TYPE_IMEI:
734 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
735 /* We don't do emergency calls by IMEI */
736 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
737 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
738 }
739 /* fall-through for non-emergency setup */
740 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200741 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200742 return msc_gsm48_tx_mm_serv_rej(conn,
743 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000744 }
745
Harald Welte37382ec2018-01-24 12:49:34 +0100746 switch (req->cm_service_type) {
747 case GSM48_CMSERV_MO_CALL_PACKET:
748 case GSM48_CMSERV_EMERGENCY:
749 case GSM48_CMSERV_SMS:
750 case GSM48_CMSERV_SUP_SERV:
751 /* continue below */
752 break;
753 default:
754 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
755 }
756
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200757 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200758 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
759 conn->classmark.classmark2_len = classmark2_len;
760
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200761 if (conn->fi) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200762 if (msc_subscr_conn_is_accepted(conn))
763 return cm_serv_reuse_conn(conn, mi-1);
764 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
765 vlr_subscr_name(conn->vsub));
766 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
767 return -EINVAL;
768 }
769
770 rc = msc_create_conn_fsm(conn, mi_string);
771 if (rc) {
772 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
773 /* logging already happened in msc_create_conn_fsm() */
774 return rc;
775 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200776 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100777
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200778 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200779 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200780 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200781 net->vlr, conn,
782 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200783 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100784 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200785 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200786 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000787
Harald Welte2483f1b2016-06-19 18:06:02 +0200788 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000789}
790
Harald Welte49f8fcb2018-01-24 21:40:19 +0100791/* Receive a CM Re-establish Request */
792static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
793{
794 uint8_t mi_type;
795 char mi_string[GSM48_MI_SIZE];
796 struct gsm48_hdr *gh = msgb_l3(msg);
797
798 uint8_t classmark2_len = gh->data[1];
799 uint8_t *classmark2 = gh->data+2;
800 uint8_t mi_len = *(classmark2 + classmark2_len);
801 uint8_t *mi = (classmark2 + classmark2_len + 1);
802
803 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
804 mi_type = mi[0] & GSM_MI_TYPE_MASK;
805 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
806
807 /* we don't support CM call re-establishment */
808 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
809}
810
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200811static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000812{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200813 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000814 struct gsm48_hdr *gh = msgb_l3(msg);
815 struct gsm48_imsi_detach_ind *idi =
816 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200817 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200818 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200819 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000820
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200821 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200822 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
823 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000824
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200825 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100826
Harald Welte2a139372009-02-22 21:14:55 +0000827 switch (mi_type) {
828 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200829 vsub = vlr_subscr_find_by_tmsi(network->vlr,
830 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000831 break;
832 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200833 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000834 break;
835 case GSM_MI_TYPE_IMEI:
836 case GSM_MI_TYPE_IMEISV:
837 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200838 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
839 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000840 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200841 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200842 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
843 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000844 break;
845 }
846
Harald Welte2483f1b2016-06-19 18:06:02 +0200847 /* TODO? We used to remember the subscriber's classmark1 here and
848 * stored it in the old sqlite db, but now we store it in a conn that
849 * will be discarded anyway: */
850 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200851
Harald Welte2483f1b2016-06-19 18:06:02 +0200852 if (!vsub) {
853 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
854 gsm48_mi_type_name(mi_type), mi_string);
855 } else {
856 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100857
858 if (vsub->cs.is_paging)
859 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
860
Harald Welte2483f1b2016-06-19 18:06:02 +0200861 vlr_subscr_rx_imsi_detach(vsub);
862 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
863 vlr_subscr_put(vsub);
864 }
Harald Welte2a139372009-02-22 21:14:55 +0000865
Harald Welte2483f1b2016-06-19 18:06:02 +0200866 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000867 return 0;
868}
869
Harald Welted2a7f5a2009-06-05 20:08:20 +0000870static int gsm48_rx_mm_status(struct msgb *msg)
871{
872 struct gsm48_hdr *gh = msgb_l3(msg);
873
874 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
875
876 return 0;
877}
878
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100879static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
880 struct gsm_subscriber_connection *conn,
881 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100885
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100886 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
887 LOGP(DMM, LOGL_ERROR,
888 "%s: MM AUTHENTICATION RESPONSE:"
889 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200890 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100891 return -EINVAL;
892 }
893
894 *res_len = sizeof(ar->sres);
895 memcpy(res, ar->sres, sizeof(ar->sres));
896 return 0;
897}
898
899static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
900 struct gsm_subscriber_connection *conn,
901 struct msgb *msg)
902{
903 struct gsm48_hdr *gh;
904 uint8_t *data;
905 uint8_t iei;
906 uint8_t ie_len;
907 unsigned int data_len;
908
909 /* First parse the GSM part */
910 if (parse_gsm_auth_resp(res, res_len, conn, msg))
911 return -EINVAL;
912 OSMO_ASSERT(*res_len == 4);
913
914 /* Then add the extended res part */
915 gh = msgb_l3(msg);
916 data = gh->data + sizeof(struct gsm48_auth_resp);
917 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
918
919 if (data_len < 3) {
920 LOGP(DMM, LOGL_ERROR,
921 "%s: MM AUTHENTICATION RESPONSE:"
922 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200923 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100924 return -EINVAL;
925 }
926
927 iei = data[0];
928 ie_len = data[1];
929 if (iei != GSM48_IE_AUTH_RES_EXT) {
930 LOGP(DMM, LOGL_ERROR,
931 "%s: MM R99 AUTHENTICATION RESPONSE:"
932 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200933 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100934 GSM48_IE_AUTH_RES_EXT, iei);
935 return -EINVAL;
936 }
937
938 if (ie_len > 12) {
939 LOGP(DMM, LOGL_ERROR,
940 "%s: MM R99 AUTHENTICATION RESPONSE:"
941 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200942 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100943 return -EINVAL;
944 }
945
946 *res_len += ie_len;
947 memcpy(res + 4, &data[2], ie_len);
948 return 0;
949}
950
951/* Chapter 9.2.3: Authentication Response */
952static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
953{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 uint8_t res[16];
955 uint8_t res_len;
956 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100957 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100958
Harald Welte2483f1b2016-06-19 18:06:02 +0200959 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100960 LOGP(DMM, LOGL_ERROR,
961 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200962 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 return -EINVAL;
964 }
965
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100966 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
967
968 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100970 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100972
973 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100974 LOGP(DMM, LOGL_ERROR,
975 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
976 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100977 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100978 memset(res, 0, sizeof(res));
979 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 }
981
982 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200983 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100984 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100985 osmo_hexdump_nospc(res, res_len));
986
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100987 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +0200988 conn->via_ran == RAN_UTRAN_IU,
989 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100990}
991
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100992static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
993{
994 struct gsm48_hdr *gh = msgb_l3(msg);
995 uint8_t cause;
996 uint8_t auts_tag;
997 uint8_t auts_len;
998 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100999
Harald Welte2483f1b2016-06-19 18:06:02 +02001000 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001001 LOGP(DMM, LOGL_ERROR,
1002 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001003 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001004 return -EINVAL;
1005 }
1006
1007 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1008 LOGP(DMM, LOGL_ERROR,
1009 "%s: MM R99 AUTHENTICATION FAILURE:"
1010 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1012 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001013 return -EINVAL;
1014 }
1015
1016 cause = gh->data[0];
1017
1018 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1019 LOGP(DMM, LOGL_INFO,
1020 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001021 vlr_subscr_name(conn->vsub), cause);
1022 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1023 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024 }
1025
1026 /* This is a Synch Failure procedure, which should pass an AUTS to
1027 * resynchronize the sequence nr with the HLR. Expecting exactly one
1028 * TLV with 14 bytes of AUTS. */
1029
1030 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1031 LOGP(DMM, LOGL_INFO,
1032 "%s: MM R99 AUTHENTICATION FAILURE:"
1033 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001034 vlr_subscr_name(conn->vsub));
1035 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001036 return -EINVAL;
1037 }
1038
1039 auts_tag = gh->data[1];
1040 auts_len = gh->data[2];
1041 auts = &gh->data[3];
1042
1043 if (auts_tag != GSM48_IE_AUTS
1044 || auts_len != 14) {
1045 LOGP(DMM, LOGL_INFO,
1046 "%s: MM R99 AUTHENTICATION FAILURE:"
1047 " invalid Synch Failure:"
1048 " expected AUTS IE 0x%02x of 14 bytes,"
1049 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001050 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001051 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001052 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001053 return -EINVAL;
1054 }
1055
1056 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1057 LOGP(DMM, LOGL_INFO,
1058 "%s: MM R99 AUTHENTICATION FAILURE:"
1059 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001060 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1061 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001062 return -EINVAL;
1063 }
1064
1065 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1066 * large enough. */
1067
1068 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001069 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001070
Harald Welte2483f1b2016-06-19 18:06:02 +02001071 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1072}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001073
Harald Welte2483f1b2016-06-19 18:06:02 +02001074static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1075{
1076 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1077 vlr_subscr_name(conn->vsub));
1078 if (!conn->vsub) {
1079 LOGP(DMM, LOGL_ERROR,
1080 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1081 return -EINVAL;
1082 }
1083 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001084}
1085
Harald Weltebf5e8df2009-02-03 12:59:45 +00001086/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001087static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001088{
1089 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001090 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001091
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001092 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001093 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001094 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001095 break;
1096 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001097 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001098 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001099 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001100 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001101 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001102 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001103 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001104 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001105 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001106 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001107 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001108 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001109 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001110 break;
1111 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001112 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001113 break;
1114 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001115 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001116 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001117 case GSM48_MT_MM_AUTH_FAIL:
1118 rc = gsm48_rx_mm_auth_fail(conn, msg);
1119 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001120 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001121 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001122 gh->msg_type);
1123 break;
1124 }
1125
1126 return rc;
1127}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001128
Harald Welte2483f1b2016-06-19 18:06:02 +02001129static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1130{
1131 /* Check the size for the classmark */
1132 if (tot_len < 1 + *classmark2_lv)
1133 return NULL;
1134
1135 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1136 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1137 return NULL;
1138
1139 return mi_lv;
1140}
1141
Harald Welte2d35ae62009-02-06 12:02:13 +00001142/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001143static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001144{
Harald Welte2483f1b2016-06-19 18:06:02 +02001145 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001147 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001148 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001149 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001150 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001151 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001152 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001153 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001154 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001155
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001156 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001157 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001158
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001159 resp = (struct gsm48_pag_resp *) &gh->data[0];
1160 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1161 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001162 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1163 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001164
Harald Welte2483f1b2016-06-19 18:06:02 +02001165 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1166 if (!mi_lv) {
1167 /* FIXME */
1168 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001169 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001170
Harald Welte2483f1b2016-06-19 18:06:02 +02001171 rc = msc_create_conn_fsm(conn, mi_string);
1172 if (rc)
1173 /* logging already happened in msc_create_conn_fsm() */
1174 return rc;
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001175 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001176
Harald Welte2483f1b2016-06-19 18:06:02 +02001177 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1178 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001179
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001180 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001181 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001182 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001183 net->vlr, conn,
1184 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001185 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001186 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001187 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001188 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001189
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001190 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001191}
1192
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001193static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001194{
1195 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001196 uint8_t apdu_id_flags;
1197 uint8_t apdu_len;
1198 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001199
1200 apdu_id_flags = gh->data[0];
1201 apdu_len = gh->data[1];
1202 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001203
Max8dc8f232017-02-09 19:13:02 +01001204 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001205 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001206
Harald Welte2483f1b2016-06-19 18:06:02 +02001207 /* we're not using the app info blob anywhere, so ignore. */
1208#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001209 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001210#else
1211 return 0;
1212#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001213}
1214
Harald Weltebf5e8df2009-02-03 12:59:45 +00001215/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001216static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001217{
1218 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001219 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001220
1221 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001222 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001223 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001224 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001225 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001226 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001228 default:
Philippd37981e2016-10-10 15:11:41 +02001229 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1230 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001231 break;
1232 }
1233
Harald Welte2d35ae62009-02-06 12:02:13 +00001234 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001235}
1236
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001237int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1238 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001239{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001240 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001241 struct gsm48_hdr *gh;
1242
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1244 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001245
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001246 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1247 gh->proto_discr = GSM48_PDISC_RR;
1248 gh->msg_type = GSM48_MT_RR_APP_INFO;
1249 gh->data[0] = apdu_id;
1250 gh->data[1] = apdu_len;
1251 memcpy(gh->data+2, apdu, apdu_len);
1252
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001253 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254}
1255
Alexander Couzens92f552f2016-08-23 07:32:27 +02001256/* FIXME: this count_statistics is a state machine behaviour. we should convert
1257 * the complete call control into a state machine. Afterwards we can move this
1258 * code into state transitions.
1259 */
1260static void count_statistics(struct gsm_trans *trans, int new_state)
1261{
1262 int old_state = trans->cc.state;
1263 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1264
1265 if (old_state == new_state)
1266 return;
1267
1268 /* state incoming */
1269 switch (new_state) {
1270 case GSM_CSTATE_ACTIVE:
1271 osmo_counter_inc(trans->net->active_calls);
1272 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1273 break;
1274 }
1275
1276 /* state outgoing */
1277 switch (old_state) {
1278 case GSM_CSTATE_ACTIVE:
1279 osmo_counter_dec(trans->net->active_calls);
1280 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1281 new_state == GSM_CSTATE_DISCONNECT_IND)
1282 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1283 else
1284 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1285 break;
1286 }
1287}
1288
Harald Welte4bc90a12008-12-27 16:32:52 +00001289/* Call Control */
1290
Harald Welte7584aea2009-02-11 11:44:12 +00001291/* The entire call control code is written in accordance with Figure 7.10c
1292 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1293 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1294 * it for voice */
1295
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296static void new_cc_state(struct gsm_trans *trans, int state)
1297{
1298 if (state > 31 || state < 0)
1299 return;
1300
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001301 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1302 trans->transaction_id,
1303 vlr_subscr_name(trans->vsub),
1304 gsm48_cc_state_name(trans->cc.state),
1305 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306
Alexander Couzens92f552f2016-08-23 07:32:27 +02001307 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001308 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309}
1310
1311static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001312{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001313 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001314 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001315 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001316
Harald Welte4bc90a12008-12-27 16:32:52 +00001317 gh->msg_type = GSM48_MT_CC_STATUS;
1318
1319 cause = msgb_put(msg, 3);
1320 cause[0] = 2;
1321 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1322 cause[2] = 0x80 | 30; /* response to status inquiry */
1323
1324 call_state = msgb_put(msg, 1);
1325 call_state[0] = 0xc0 | 0x00;
1326
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001327 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001328}
1329
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001330static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001331 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001332{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001333 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1335
Harald Welte6f4b7532008-12-29 00:39:37 +00001336 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001337 gh->msg_type = msg_type;
1338
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001339 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001340}
1341
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1343{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001344 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001345 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001346 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001347 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 }
1349}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001350
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1352 int msg_type, struct gsm_mncc *mncc)
1353{
1354 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001355 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356
Philipp Maierfbf66102017-04-09 12:32:51 +02001357 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1358
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001359#if BEFORE_MSCSPLIT
1360 /* Re-enable this log output once we can obtain this information via
1361 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001363 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001364 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001366 trans->conn->lchan->ts->trx->bts->nr,
1367 trans->conn->lchan->ts->trx->nr,
1368 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001369 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370 get_mncc_name(msg_type));
1371 else
1372 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1373 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001374 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 get_mncc_name(msg_type));
1376 else
1377 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1378 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001379#else
1380 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1381#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382
1383 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001384
Harald Welte966636f2009-06-26 19:39:35 +02001385 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 if (!msg)
1387 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001388
1389 data = msgb_put(msg, sizeof(struct gsm_mncc));
1390 memcpy(data, mncc, sizeof(struct gsm_mncc));
1391
Harald Welte54209c22010-12-22 23:43:29 +01001392 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393
1394 return 0;
1395}
1396
1397int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001398 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399{
1400 struct gsm_mncc rel;
1401
Harald Welte92f70c52009-06-12 01:54:08 +08001402 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001404 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001405 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1406 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1408}
1409
Harald Weltedcaf5652009-07-23 18:56:43 +02001410/* Call Control Specific transaction release.
1411 * gets called by trans_free, DO NOT CALL YOURSELF! */
1412void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001413{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 gsm48_stop_cc_timer(trans);
1415
Philipp Maier621ba032017-11-07 17:19:25 +01001416 /* Initiate the teardown of the related connections on the MGW */
1417 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001418
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419 /* send release to L4, if callref still exists */
1420 if (trans->callref) {
1421 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001422 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001423 GSM48_CAUSE_LOC_PRN_S_LU,
1424 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001426 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001428}
1429
1430static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001431
Harald Welte09e38af2009-02-16 22:52:23 +00001432/* call-back from paging the B-end of the connection */
1433static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001434 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001435{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001436 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001437 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001438
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001439 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001440
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001441 switch (event) {
1442 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001443 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1444 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001445 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001446 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001447 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001448 /* send SETUP request to called party */
1449 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1450 break;
1451 case GSM_PAGING_EXPIRED:
1452 case GSM_PAGING_BUSY:
1453 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001454 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001455 /* Temporarily out of order */
1456 mncc_release_ind(transt->net, transt,
1457 transt->callref,
1458 GSM48_CAUSE_LOC_PRN_S_LU,
1459 GSM48_CC_CAUSE_DEST_OOO);
1460 transt->callref = 0;
1461 transt->paging_request = NULL;
1462 trans_free(transt);
1463 break;
1464 default:
1465 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1466 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001468
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001469 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001470 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001471}
Harald Welte7584aea2009-02-11 11:44:12 +00001472
Harald Welte4bfdfe72009-06-10 23:11:52 +08001473/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001474static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001475{
Harald Welte53d51f52015-12-03 14:59:04 +01001476 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1477 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001478 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001479
Harald Welte4bfdfe72009-06-10 23:11:52 +08001480 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001481 return -EIO;
1482
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001483 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001484 return -EIO;
1485
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001486 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001487 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001488
Philipp Maier621ba032017-11-07 17:19:25 +01001489 /* Bridge RTP streams */
1490 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1491 trans2->conn->rtp.local_addr_cn);
1492 if (rc)
1493 return -EINVAL;
1494
1495 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1496 trans1->conn->rtp.local_addr_cn);
1497 if (rc)
1498 return -EINVAL;
1499
1500 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001501}
1502
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1504{
1505 DEBUGP(DCC, "-> STATUS ENQ\n");
1506 return gsm48_cc_tx_status(trans, msg);
1507}
1508
1509static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1510static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1511
1512static void gsm48_cc_timeout(void *arg)
1513{
1514 struct gsm_trans *trans = arg;
1515 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001516 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1517 int mo_location = GSM48_CAUSE_LOC_USER;
1518 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1519 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520 struct gsm_mncc mo_rel, l4_rel;
1521
1522 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1523 mo_rel.callref = trans->callref;
1524 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1525 l4_rel.callref = trans->callref;
1526
Harald Weltedcaf5652009-07-23 18:56:43 +02001527 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001528 case 0x303:
1529 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001530 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001531 break;
1532 case 0x310:
1533 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001534 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001535 break;
1536 case 0x313:
1537 disconnect = 1;
1538 /* unknown, did not find it in the specs */
1539 break;
1540 case 0x301:
1541 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001542 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001543 break;
1544 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001545 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001546 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001547 gsm48_cc_tx_release(trans, &trans->cc.msg);
1548 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549 break; /* stay in release state */
1550 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001551 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001552 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553 case 0x306:
1554 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001555 mo_cause = trans->cc.msg.cause.value;
1556 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001557 break;
1558 case 0x323:
1559 disconnect = 1;
1560 break;
1561 default:
1562 release = 1;
1563 }
1564
1565 if (release && trans->callref) {
1566 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001567 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568 l4_location, l4_cause);
1569 trans->callref = 0;
1570 }
1571
1572 if (disconnect && trans->callref) {
1573 /* process disconnect towards layer 4 */
1574 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001575 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001576 }
1577
1578 /* process disconnect towards mobile station */
1579 if (disconnect || release) {
1580 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001581 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1582 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1583 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001584 mo_rel.cause.diag_len = 3;
1585
1586 if (disconnect)
1587 gsm48_cc_tx_disconnect(trans, &mo_rel);
1588 if (release)
1589 gsm48_cc_tx_release(trans, &mo_rel);
1590 }
1591
1592}
1593
Max3ffce192016-04-25 15:22:00 +02001594/* disconnect both calls from the bridge */
1595static inline void disconnect_bridge(struct gsm_network *net,
1596 struct gsm_mncc_bridge *bridge, int err)
1597{
1598 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1599 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1600 struct gsm_mncc mx_rel;
1601 if (!trans0 || !trans1)
1602 return;
1603
1604 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1605 trans0->callref, trans1->callref, strerror(err));
1606
1607 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1608 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1609 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1610
1611 mx_rel.callref = trans0->callref;
1612 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1613
1614 mx_rel.callref = trans1->callref;
1615 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1616}
1617
Harald Welte4bfdfe72009-06-10 23:11:52 +08001618static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1619 int sec, int micro)
1620{
1621 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001622 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001623 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001624 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001625}
1626
1627static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1628{
1629 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001630 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001631 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1632 struct tlv_parsed tp;
1633 struct gsm_mncc setup;
1634
1635 memset(&setup, 0, sizeof(struct gsm_mncc));
1636 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001637
Harald Welte474d19f2010-03-02 23:18:30 +01001638 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001640 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1641 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001643 /* use destination number as configured by user (if any) */
1644 if (trans->net->emergency.route_to_msisdn) {
1645 setup.fields |= MNCC_F_CALLED;
1646 setup.called.type = 0; /* unknown */
1647 setup.called.plan = 0; /* unknown */
1648 OSMO_STRLCPY_ARRAY(setup.called.number,
1649 trans->net->emergency.route_to_msisdn);
1650 }
Harald Welte0dd01372018-01-24 11:06:19 +01001651 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652
1653 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001654 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001655 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1656 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001657
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 /* bearer capability */
1659 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1660 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001661 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001663
1664 /* Create a copy of the bearer capability
1665 * in the transaction struct, so we can use
1666 * this information later */
1667 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1668 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 }
1670 /* facility */
1671 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1672 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001673 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1675 }
1676 /* called party bcd number */
1677 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1678 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001679 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1681 }
1682 /* user-user */
1683 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1684 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001685 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1687 }
1688 /* ss-version */
1689 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1690 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001691 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1693 }
1694 /* CLIR suppression */
1695 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1696 setup.clir.sup = 1;
1697 /* CLIR invocation */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1699 setup.clir.inv = 1;
1700 /* cc cap */
1701 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1702 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001703 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001704 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1705 }
1706
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707 new_cc_state(trans, GSM_CSTATE_INITIATED);
1708
Harald Welte79826e12018-01-24 10:50:24 +01001709 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1710 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001711 setup.called.number);
1712
Alexander Couzensb847a212016-08-02 11:34:11 +02001713 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001714
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001716 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001717
Harald Welte13cac662009-07-29 12:10:35 +02001718 /* MNCC code will modify the channel asynchronously, we should
1719 * ipaccess-bind only after the modification has been made to the
1720 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 return 0;
1722}
1723
1724static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001725{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001726 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001727 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001729 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001730
Harald Welte7ccf7782009-02-17 01:43:01 +00001731 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001732
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 /* transaction id must not be assigned */
1734 if (trans->transaction_id != 0xff) { /* unasssigned */
1735 DEBUGP(DCC, "TX Setup with assigned transaction. "
1736 "This is not allowed!\n");
1737 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001738 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001739 GSM48_CAUSE_LOC_PRN_S_LU,
1740 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001742 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001743 return rc;
1744 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001745
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001747 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001748 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001749 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001751 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001752 GSM48_CAUSE_LOC_PRN_S_LU,
1753 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001755 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 return rc;
1757 }
Harald Welte78283ef2009-07-23 21:36:44 +02001758 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001759
Harald Welte65e74cc2008-12-29 01:55:35 +00001760 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001761
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001763
Harald Welte4bfdfe72009-06-10 23:11:52 +08001764 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001765 if (setup->fields & MNCC_F_BEARER_CAP) {
1766 /* Create a copy of the bearer capability in the transaction struct, so we
1767 * can use this information later */
1768 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001769 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001770 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* facility */
1772 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 /* progress */
1775 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001776 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 /* calling party BCD number */
1778 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001779 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 /* called party BCD number */
1781 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001782 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 /* user-user */
1784 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001785 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 /* redirecting party BCD number */
1787 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001788 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 /* signal */
1790 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001792
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001794
Alexander Couzensb847a212016-08-02 11:34:11 +02001795 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001796
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001797 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001798}
1799
Harald Welte4bfdfe72009-06-10 23:11:52 +08001800static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1801{
1802 struct gsm48_hdr *gh = msgb_l3(msg);
1803 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1804 struct tlv_parsed tp;
1805 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001806 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807
1808 gsm48_stop_cc_timer(trans);
1809 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1810
1811 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1812 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001813
Harald Welte474d19f2010-03-02 23:18:30 +01001814 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815#if 0
1816 /* repeat */
1817 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1818 call_conf.repeat = 1;
1819 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1820 call_conf.repeat = 2;
1821#endif
1822 /* bearer capability */
1823 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1824 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001825 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001827
1828 /* Create a copy of the bearer capability
1829 * in the transaction struct, so we can use
1830 * this information later */
1831 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1832 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833 }
1834 /* cause */
1835 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1836 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001837 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1839 }
1840 /* cc cap */
1841 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1842 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001843 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1845 }
1846
Andreas Eversberg035b8742013-09-19 09:27:06 +02001847 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001848 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001849
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1851
Philipp Maierfbf66102017-04-09 12:32:51 +02001852 /* Assign call (if not done yet) */
1853 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001854 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001855 trans->assignment_done = true;
1856 }
1857 else
1858 rc = 0;
1859
1860 /* don't continue, if there were problems with
1861 * the call assignment. */
1862 if (rc)
1863 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001864
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001865 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001866 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867}
1868
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001869static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870{
1871 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001872 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001874 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1877
1878 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1879
1880 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001881 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001882 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001883 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1884 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001885 /* facility */
1886 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001887 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 /* progress */
1889 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001890 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001892 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1893 if (rc)
1894 return rc;
1895
Philipp Maierfbf66102017-04-09 12:32:51 +02001896 /* Assign call (if not done yet) */
1897 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001898 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001899 trans->assignment_done = true;
1900 }
1901 else
1902 rc = 0;
1903
1904 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1908{
1909 struct gsm48_hdr *gh = msgb_l3(msg);
1910 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1911 struct tlv_parsed tp;
1912 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001913
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 gsm48_stop_cc_timer(trans);
1915 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1916
1917 memset(&alerting, 0, sizeof(struct gsm_mncc));
1918 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001919 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 /* facility */
1921 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1922 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1925 }
1926
1927 /* progress */
1928 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1929 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1932 }
1933 /* ss-version */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1935 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001936 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1938 }
1939
1940 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1941
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001942 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001943 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944}
1945
1946static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1947{
1948 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001949 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1951
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 gh->msg_type = GSM48_MT_CC_ALERTING;
1953
1954 /* facility */
1955 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* progress */
1958 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* user-user */
1961 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963
1964 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001965
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001966 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967}
1968
1969static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1970{
1971 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001972 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 gh->msg_type = GSM48_MT_CC_PROGRESS;
1976
1977 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* user-user */
1980 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001983 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984}
1985
1986static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1987{
1988 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001989 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1991
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 gh->msg_type = GSM48_MT_CC_CONNECT;
1993
1994 gsm48_stop_cc_timer(trans);
1995 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1996
1997 /* facility */
1998 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 /* progress */
2001 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 /* connected number */
2004 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 /* user-user */
2007 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009
2010 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2011
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002012 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013}
2014
2015static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2016{
2017 struct gsm48_hdr *gh = msgb_l3(msg);
2018 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2019 struct tlv_parsed tp;
2020 struct gsm_mncc connect;
2021
2022 gsm48_stop_cc_timer(trans);
2023
2024 memset(&connect, 0, sizeof(struct gsm_mncc));
2025 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002026 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002028 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002029 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2030 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002031
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 /* facility */
2033 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2034 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002035 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2037 }
2038 /* user-user */
2039 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2040 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002041 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2043 }
2044 /* ss-version */
2045 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2046 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002047 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2049 }
2050
2051 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002052 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002054 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055}
2056
2057
2058static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2059{
2060 struct gsm_mncc connect_ack;
2061
2062 gsm48_stop_cc_timer(trans);
2063
2064 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002065 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002066
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2068 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002069
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002070 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 &connect_ack);
2072}
2073
2074static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2075{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002076 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2078
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2080
2081 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2082
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002083 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084}
2085
2086static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2087{
2088 struct gsm48_hdr *gh = msgb_l3(msg);
2089 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2090 struct tlv_parsed tp;
2091 struct gsm_mncc disc;
2092
2093 gsm48_stop_cc_timer(trans);
2094
2095 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2096
2097 memset(&disc, 0, sizeof(struct gsm_mncc));
2098 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002099 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 /* cause */
2101 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2102 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002103 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2105 }
2106 /* facility */
2107 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2108 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002109 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2111 }
2112 /* user-user */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2114 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002115 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2117 }
2118 /* ss-version */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2120 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002121 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2123 }
2124
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002125 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126
2127}
2128
Harald Weltec66b71c2009-06-11 14:23:20 +08002129static struct gsm_mncc_cause default_cause = {
2130 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2131 .coding = 0,
2132 .rec = 0,
2133 .rec_val = 0,
2134 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2135 .diag_len = 0,
2136 .diag = { 0 },
2137};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138
2139static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2140{
2141 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002142 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2144
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2146
2147 gsm48_stop_cc_timer(trans);
2148 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2149
2150 /* cause */
2151 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 else
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155
2156 /* facility */
2157 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002158 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 /* progress */
2160 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002161 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 /* user-user */
2163 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165
2166 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002167 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168
2169 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2170
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002171 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172}
2173
2174static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2175{
2176 struct gsm48_hdr *gh = msgb_l3(msg);
2177 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2178 struct tlv_parsed tp;
2179 struct gsm_mncc rel;
2180 int rc;
2181
2182 gsm48_stop_cc_timer(trans);
2183
2184 memset(&rel, 0, sizeof(struct gsm_mncc));
2185 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002186 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* cause */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2189 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002190 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2192 }
2193 /* facility */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2195 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002196 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2198 }
2199 /* user-user */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2201 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002202 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2204 }
2205 /* ss-version */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2207 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002208 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2210 }
2211
Harald Weltedcaf5652009-07-23 18:56:43 +02002212 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002214 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002216 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002217 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002218 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002219 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 }
2221
2222 new_cc_state(trans, GSM_CSTATE_NULL);
2223
2224 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002225 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226
2227 return rc;
2228}
2229
2230static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2231{
2232 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002233 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2235
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 gh->msg_type = GSM48_MT_CC_RELEASE;
2237
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 gsm48_stop_cc_timer(trans);
2239 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2240
2241 /* cause */
2242 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002243 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244 /* facility */
2245 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002246 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 /* user-user */
2248 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002249 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250
Harald Weltedcaf5652009-07-23 18:56:43 +02002251 trans->cc.T308_second = 0;
2252 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253
Harald Weltedcaf5652009-07-23 18:56:43 +02002254 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2256
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002257 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258}
2259
2260static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2261{
2262 struct gsm48_hdr *gh = msgb_l3(msg);
2263 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2264 struct tlv_parsed tp;
2265 struct gsm_mncc rel;
2266 int rc = 0;
2267
2268 gsm48_stop_cc_timer(trans);
2269
2270 memset(&rel, 0, sizeof(struct gsm_mncc));
2271 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002272 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 /* cause */
2274 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2275 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002276 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2278 }
2279 /* facility */
2280 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2281 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002282 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2284 }
2285 /* user-user */
2286 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2287 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002288 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2290 }
2291 /* ss-version */
2292 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2293 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2296 }
2297
2298 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002299 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002301 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 MNCC_REJ_IND, &rel);
2303 break;
2304 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002305 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 MNCC_REL_CNF, &rel);
2307 break;
2308 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002309 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 MNCC_REL_IND, &rel);
2311 }
2312 }
2313
2314 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002315 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316
2317 return rc;
2318}
2319
2320static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2321{
2322 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002323 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002325 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2328
2329 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 gsm48_stop_cc_timer(trans);
2332
2333 /* cause */
2334 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002335 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 /* facility */
2337 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002338 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 /* user-user */
2340 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002341 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342
Harald Weltec7782de2010-12-21 19:31:41 +01002343 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2344
Harald Weltedcaf5652009-07-23 18:56:43 +02002345 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
Harald Weltec7782de2010-12-21 19:31:41 +01002347 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348}
2349
2350static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2351{
2352 struct gsm48_hdr *gh = msgb_l3(msg);
2353 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2354 struct tlv_parsed tp;
2355 struct gsm_mncc fac;
2356
2357 memset(&fac, 0, sizeof(struct gsm_mncc));
2358 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002359 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 /* facility */
2361 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2362 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002363 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2365 }
2366 /* ss-version */
2367 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2368 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002369 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2371 }
2372
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002373 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374}
2375
2376static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2377{
2378 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002379 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2381
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 gh->msg_type = GSM48_MT_CC_FACILITY;
2383
2384 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002385 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002387 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388}
2389
2390static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2391{
2392 struct gsm_mncc hold;
2393
2394 memset(&hold, 0, sizeof(struct gsm_mncc));
2395 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002396 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397}
2398
2399static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2400{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002401 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2403
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2405
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002406 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2410{
2411 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002412 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2414
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2416
2417 /* cause */
2418 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 else
Harald Welte55c8f352010-03-07 23:40:35 +01002421 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002423 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424}
2425
2426static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2427{
2428 struct gsm_mncc retrieve;
2429
2430 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2431 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002432 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002433 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434}
2435
2436static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2437{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002438 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2440
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2442
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002443 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2447{
2448 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002449 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2451
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2453
2454 /* cause */
2455 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 else
Harald Welte55c8f352010-03-07 23:40:35 +01002458 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002460 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461}
2462
2463static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2464{
2465 struct gsm48_hdr *gh = msgb_l3(msg);
2466 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2467 struct tlv_parsed tp;
2468 struct gsm_mncc dtmf;
2469
2470 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2471 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002472 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473 /* keypad facility */
2474 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2475 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002476 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2478 }
2479
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002480 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2484{
2485 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002486 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2488
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2490
2491 /* keypad */
2492 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002493 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002495 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496}
2497
2498static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2499{
2500 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002501 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2503
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2505
2506 /* cause */
2507 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002508 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509 else
Harald Welte55c8f352010-03-07 23:40:35 +01002510 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002512 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513}
2514
2515static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2516{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2519
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2521
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002522 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523}
2524
2525static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2526{
2527 struct gsm_mncc dtmf;
2528
2529 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2530 dtmf.callref = trans->callref;
2531
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002532 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533}
2534
2535static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2536{
2537 struct gsm48_hdr *gh = msgb_l3(msg);
2538 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2539 struct tlv_parsed tp;
2540 struct gsm_mncc modify;
2541
2542 memset(&modify, 0, sizeof(struct gsm_mncc));
2543 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002544 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 /* bearer capability */
2546 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2547 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002548 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002550
2551 /* Create a copy of the bearer capability
2552 * in the transaction struct, so we can use
2553 * this information later */
2554 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2555 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002556 }
2557
2558 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2559
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002560 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561}
2562
2563static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2564{
2565 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002566 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2568
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569 gh->msg_type = GSM48_MT_CC_MODIFY;
2570
2571 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2572
2573 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002575 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576
2577 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2578
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002579 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580}
2581
2582static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2583{
2584 struct gsm48_hdr *gh = msgb_l3(msg);
2585 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2586 struct tlv_parsed tp;
2587 struct gsm_mncc modify;
2588
2589 gsm48_stop_cc_timer(trans);
2590
2591 memset(&modify, 0, sizeof(struct gsm_mncc));
2592 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002593 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 /* bearer capability */
2595 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2596 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002597 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002599
2600 /* Create a copy of the bearer capability
2601 * in the transaction struct, so we can use
2602 * this information later */
2603 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2604 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 }
2606
2607 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2608
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002609 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610}
2611
2612static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2613{
2614 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002615 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2617
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2619
2620 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002621 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002622 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623
2624 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2625
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002626 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627}
2628
2629static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2630{
2631 struct gsm48_hdr *gh = msgb_l3(msg);
2632 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2633 struct tlv_parsed tp;
2634 struct gsm_mncc modify;
2635
2636 gsm48_stop_cc_timer(trans);
2637
2638 memset(&modify, 0, sizeof(struct gsm_mncc));
2639 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002640 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 /* bearer capability */
2642 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2643 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002644 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002646
2647 /* Create a copy of the bearer capability
2648 * in the transaction struct, so we can use
2649 * this information later */
2650 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2651 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002652 }
2653 /* cause */
2654 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2655 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002656 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2658 }
2659
2660 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2661
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002662 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663}
2664
2665static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2666{
2667 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002668 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2670
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2672
2673 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002675 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002677 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678
2679 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2680
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002681 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682}
2683
2684static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2685{
2686 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002687 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2689
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 gh->msg_type = GSM48_MT_CC_NOTIFY;
2691
2692 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002693 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002695 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2699{
2700 struct gsm48_hdr *gh = msgb_l3(msg);
2701 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2702// struct tlv_parsed tp;
2703 struct gsm_mncc notify;
2704
2705 memset(&notify, 0, sizeof(struct gsm_mncc));
2706 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002707// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002709 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002711 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712}
2713
2714static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2715{
2716 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002717 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2719
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 gh->msg_type = GSM48_MT_CC_USER_INFO;
2721
2722 /* user-user */
2723 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002724 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725 /* more data */
2726 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002727 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002729 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730}
2731
2732static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2733{
2734 struct gsm48_hdr *gh = msgb_l3(msg);
2735 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2736 struct tlv_parsed tp;
2737 struct gsm_mncc user;
2738
2739 memset(&user, 0, sizeof(struct gsm_mncc));
2740 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002741 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 /* user-user */
2743 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2744 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002745 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2747 }
2748 /* more data */
2749 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2750 user.more = 1;
2751
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002752 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753}
2754
Philipp Maierfbf66102017-04-09 12:32:51 +02002755static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2756 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2757 uint32_t payload_msg_type)
2758{
2759 uint8_t data[sizeof(struct gsm_mncc)];
2760 struct gsm_mncc_rtp *rtp;
2761
2762 memset(&data, 0, sizeof(data));
2763 rtp = (struct gsm_mncc_rtp *) &data[0];
2764
2765 rtp->callref = callref;
2766 rtp->msg_type = cmd;
2767 rtp->ip = addr;
2768 rtp->port = port;
2769 rtp->payload_type = payload_type;
2770 rtp->payload_msg_type = payload_msg_type;
2771 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2772}
2773
2774static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2775{
2776 int msg_type;
2777
2778 /* FIXME This has to be set to some meaningful value.
2779 * Possible options are:
2780 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2781 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2782 * (0 if unknown) */
2783 msg_type = GSM_TCHF_FRAME;
2784
Philipp Maier621ba032017-11-07 17:19:25 +01002785 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2786 uint16_t port = trans->conn->rtp.local_port_cn;
2787
2788 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2789
2790 if (addr == INADDR_NONE) {
2791 LOGP(DMNCC, LOGL_ERROR,
2792 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2793 vlr_subscr_name(trans->vsub));
2794 return;
2795 }
2796 if (port == 0) {
2797 LOGP(DMNCC, LOGL_ERROR,
2798 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2799 vlr_subscr_name(trans->vsub));
2800 return;
2801 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002802
2803 /* FIXME: This has to be set to some meaningful value,
2804 * before the MSC-Split, this value was pulled from
2805 * lchan->abis_ip.rtp_payload */
2806 uint32_t payload_type = 0;
2807
2808 return mncc_recv_rtp(net, trans->callref, cmd,
2809 addr,
2810 port,
2811 payload_type,
2812 msg_type);
2813}
2814
2815static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2816{
2817 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2818}
2819
2820static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2821{
2822 struct gsm_trans *trans;
2823 int rc;
2824
2825 /* Find callref */
2826 trans = trans_find_by_callref(net, callref);
2827 if (!trans) {
2828 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2829 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2830 return -EIO;
2831 }
2832 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2833 if (!trans->conn) {
2834 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2835 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2836 return 0;
2837 }
2838
2839 trans->conn->mncc_rtp_bridge = 1;
2840
Philipp Maier621ba032017-11-07 17:19:25 +01002841 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002842 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002843 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002844 * command to the MGCP-GW. The CRCX will return the port number,
2845 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2846 * have to return this port number back to the MNCC by sending
2847 * it back with the TCH_RTP_CREATE message. To make sure that
2848 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002849 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002850 * to take care of this by setting trans->tch_rtp_create to true.
2851 * This will make sure that gsm48_tch_rtp_create() (below) is
2852 * called as soon as the local port number has become known. */
2853 trans->tch_rtp_create = true;
2854
2855 /* Assign call (if not done yet) */
2856 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002857 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002858 trans->assignment_done = true;
2859 }
2860 else
2861 rc = 0;
2862
2863 return rc;
2864}
2865
2866/* Trigger TCH_RTP_CREATE acknowledgement */
2867int gsm48_tch_rtp_create(struct gsm_trans *trans)
2868{
2869 /* This function is called as soon as the port, on which the
2870 * mgcp-gw expects the incoming RTP stream from the remote
2871 * end (e.g. Asterisk) is known. */
2872
2873 struct gsm_subscriber_connection *conn = trans->conn;
2874 struct gsm_network *network = conn->network;
2875
2876 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2877 return 0;
2878}
2879
2880static int tch_rtp_connect(struct gsm_network *net, void *arg)
2881{
2882 struct gsm_trans *trans;
2883 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002884 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002885
2886 /* Find callref */
2887 trans = trans_find_by_callref(net, rtp->callref);
2888 if (!trans) {
2889 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2890 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2891 return -EIO;
2892 }
2893 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2894 if (!trans->conn) {
2895 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2896 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2897 return 0;
2898 }
2899
Philipp Maier621ba032017-11-07 17:19:25 +01002900 addr.s_addr = osmo_htonl(rtp->ip);
2901 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002902}
2903
Harald Welte4bfdfe72009-06-10 23:11:52 +08002904static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002905 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906 int type;
2907 int (*rout) (struct gsm_trans *trans, void *arg);
2908} downstatelist[] = {
2909 /* mobile originating call establishment */
2910 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002911 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2913 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2914 {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 */
2915 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2916 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2917 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2918 /* mobile terminating call establishment */
2919 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2920 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2921 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2922 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2923 /* signalling during call */
2924 {SBIT(GSM_CSTATE_ACTIVE),
2925 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2926 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2927 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2928 {ALL_STATES,
2929 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2930 {ALL_STATES,
2931 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2932 {ALL_STATES,
2933 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2934 {SBIT(GSM_CSTATE_ACTIVE),
2935 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2936 {SBIT(GSM_CSTATE_ACTIVE),
2937 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2938 {SBIT(GSM_CSTATE_ACTIVE),
2939 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2940 {SBIT(GSM_CSTATE_ACTIVE),
2941 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2942 {SBIT(GSM_CSTATE_ACTIVE),
2943 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2944 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2945 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2946 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2947 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2948 {SBIT(GSM_CSTATE_ACTIVE),
2949 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2950 /* clearing */
2951 {SBIT(GSM_CSTATE_INITIATED),
2952 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2953 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2954 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2955 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2956 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957};
2958
2959#define DOWNSLLEN \
2960 (sizeof(downstatelist) / sizeof(struct downstate))
2961
2962
Harald Welte76556372010-12-22 23:57:45 +01002963int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964{
Harald Welte1a6f7982009-08-09 18:52:33 +02002965 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002967 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 struct gsm_mncc *data = arg, rel;
2969
Harald Welte04dc88f2010-12-22 21:45:05 +01002970 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2971
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972 /* handle special messages */
2973 switch(msg_type) {
2974 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002975 rc = tch_bridge(net, arg);
2976 if (rc < 0)
2977 disconnect_bridge(net, arg, -rc);
2978 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002979 case MNCC_RTP_CREATE:
2980 return tch_rtp_create(net, data->callref);
2981 case MNCC_RTP_CONNECT:
2982 return tch_rtp_connect(net, arg);
2983 case MNCC_RTP_FREE:
2984 /* unused right now */
2985 return -EIO;
2986
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002988 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002989 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002990 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002991 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002992 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002993 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2994 get_mncc_name(msg_type));
2995 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 }
2997
2998 memset(&rel, 0, sizeof(struct gsm_mncc));
2999 rel.callref = data->callref;
3000
3001 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003002 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003
3004 /* Callref unknown */
3005 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003006 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003007
Harald Welte4a3464c2009-07-04 10:11:24 +02003008 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3010 "Received '%s' from MNCC with "
3011 "unknown callref %d\n", data->called.number,
3012 get_mncc_name(msg_type), data->callref);
3013 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003014 return mncc_release_ind(net, NULL, data->callref,
3015 GSM48_CAUSE_LOC_PRN_S_LU,
3016 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003018 if (!data->called.number[0] && !data->imsi[0]) {
3019 DEBUGP(DCC, "(bts - trx - ts - ti) "
3020 "Received '%s' from MNCC with "
3021 "no number or IMSI\n", get_mncc_name(msg_type));
3022 /* Invalid number */
3023 return mncc_release_ind(net, NULL, data->callref,
3024 GSM48_CAUSE_LOC_PRN_S_LU,
3025 GSM48_CC_CAUSE_INV_NR_FORMAT);
3026 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003028 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003029 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3030 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003031 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003032 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003033
3034 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003035 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003036
Harald Welte4bfdfe72009-06-10 23:11:52 +08003037 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003038 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3040 "Received '%s' from MNCC with "
3041 "unknown subscriber %s\n", data->called.number,
3042 get_mncc_name(msg_type), data->called.number);
3043 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003044 return mncc_release_ind(net, NULL, data->callref,
3045 GSM48_CAUSE_LOC_PRN_S_LU,
3046 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 }
3048 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003049 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3051 "Received '%s' from MNCC with "
3052 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003053 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003054 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003056 return mncc_release_ind(net, NULL, data->callref,
3057 GSM48_CAUSE_LOC_PRN_S_LU,
3058 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 }
3060 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003061 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003062 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003064 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003066 mncc_release_ind(net, NULL, data->callref,
3067 GSM48_CAUSE_LOC_PRN_S_LU,
3068 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 return -ENOMEM;
3070 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003071
3072 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003073 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003074
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003075 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003076 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003077 /* find transaction with this subscriber already paging */
3078 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003079 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003081 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003082 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003083 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 "Received '%s' from MNCC with "
3085 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003086 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003088 get_mncc_name(msg_type), vsub->lac);
3089 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003090 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003091 return 0;
3092 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003093 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003094 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003095
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003096 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003097 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003098 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003099 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003100 trans,
3101 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003102 if (!trans->paging_request) {
3103 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003104 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003105 trans_free(trans);
3106 return 0;
3107 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003108 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003109 return 0;
3110 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003111
3112 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003113 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003114 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Harald Welte2483f1b2016-06-19 18:06:02 +02003115 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003116 } else {
3117 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003118 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003120
3121 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003122 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123
3124 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003125 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003126 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003127 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003128 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003130 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3131 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 if (msg_type == MNCC_REL_REQ)
3133 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3134 else
3135 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3136 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003137 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003139 } else {
3140 DEBUGP(DCC, "(ti %02x sub %s) "
3141 "Received '%s' from MNCC in state %d (%s)\n",
3142 trans->transaction_id,
3143 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3144 get_mncc_name(msg_type), trans->cc.state,
3145 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146 }
3147
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 /* Find function for current state and message */
3149 for (i = 0; i < DOWNSLLEN; i++)
3150 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003151 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 break;
3153 if (i == DOWNSLLEN) {
3154 DEBUGP(DCC, "Message unhandled at this state.\n");
3155 return 0;
3156 }
3157
3158 rc = downstatelist[i].rout(trans, arg);
3159
3160 return rc;
3161}
3162
3163
3164static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003165 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 int type;
3167 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3168} datastatelist[] = {
3169 /* mobile originating call establishment */
3170 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3171 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3172 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3173 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3174 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3175 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3176 /* mobile terminating call establishment */
3177 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3178 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3179 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3180 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003181 {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 +08003182 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3183 /* signalling during call */
3184 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3185 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3186 {SBIT(GSM_CSTATE_ACTIVE),
3187 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3188 {ALL_STATES,
3189 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3190 {ALL_STATES,
3191 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3192 {ALL_STATES,
3193 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3194 {SBIT(GSM_CSTATE_ACTIVE),
3195 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3196 {SBIT(GSM_CSTATE_ACTIVE),
3197 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3198 {SBIT(GSM_CSTATE_ACTIVE),
3199 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3200 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3201 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3202 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3203 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3204 {SBIT(GSM_CSTATE_ACTIVE),
3205 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3206 /* clearing */
3207 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3208 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3209 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3210 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3211 {ALL_STATES, /* 5.4.3.4 */
3212 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3213};
3214
3215#define DATASLLEN \
3216 (sizeof(datastatelist) / sizeof(struct datastate))
3217
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003218static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003219{
3220 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003221 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003222 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003223 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003224 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003225
Harald Welte4bfdfe72009-06-10 23:11:52 +08003226 if (msg_type & 0x80) {
3227 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3228 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003229 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003230
Harald Welte2483f1b2016-06-19 18:06:02 +02003231 if (!conn->vsub) {
3232 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003233 return -EINVAL;
3234 }
3235
Harald Welte4bfdfe72009-06-10 23:11:52 +08003236 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003237 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003238
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003239#if BEFORE_MSCSPLIT
3240 /* Re-enable this log output once we can obtain this information via
3241 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003242 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003244 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003245 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003246 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3247 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003248#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003249
3250 /* Create transaction */
3251 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003252 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003253 "creating new trans.\n", transaction_id);
3254 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003255 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003256 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003257 transaction_id, new_callref++);
3258 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003260 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003261 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 GSM48_MT_CC_RELEASE_COMPL);
3263 return -ENOMEM;
3264 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003266 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003267 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Harald Welte2483f1b2016-06-19 18:06:02 +02003268 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003269 }
3270
3271 /* find function for current state and message */
3272 for (i = 0; i < DATASLLEN; i++)
3273 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003274 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275 break;
3276 if (i == DATASLLEN) {
3277 DEBUGP(DCC, "Message unhandled at this state.\n");
3278 return 0;
3279 }
3280
Harald Welte2483f1b2016-06-19 18:06:02 +02003281 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003282
Harald Welte4bfdfe72009-06-10 23:11:52 +08003283 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003284
Harald Welte2483f1b2016-06-19 18:06:02 +02003285 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003286 return rc;
3287}
3288
Harald Welte2483f1b2016-06-19 18:06:02 +02003289static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003290{
Harald Welte2483f1b2016-06-19 18:06:02 +02003291 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3292 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003293
Harald Welte2483f1b2016-06-19 18:06:02 +02003294 switch (pdisc) {
3295 case GSM48_PDISC_MM:
3296 switch (msg_type) {
3297 case GSM48_MT_MM_LOC_UPD_REQUEST:
3298 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003299 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003300 case GSM48_MT_MM_AUTH_RESP:
3301 case GSM48_MT_MM_AUTH_FAIL:
3302 case GSM48_MT_MM_ID_RESP:
3303 case GSM48_MT_MM_TMSI_REALL_COMPL:
3304 case GSM48_MT_MM_IMSI_DETACH_IND:
3305 return true;
3306 default:
3307 break;
3308 }
3309 break;
3310 case GSM48_PDISC_RR:
3311 switch (msg_type) {
3312 case GSM48_MT_RR_CIPH_M_COMPL:
3313 case GSM48_MT_RR_PAG_RESP:
3314 return true;
3315 default:
3316 break;
3317 }
3318 break;
3319 default:
3320 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003321 }
3322
Harald Welte2483f1b2016-06-19 18:06:02 +02003323 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003324}
3325
Harald Welte2483f1b2016-06-19 18:06:02 +02003326void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3327 struct msgb *msg)
3328{
3329
3330 /* If a CM Service Request was received before, this is the request the
3331 * conn was opened for. No need to wait for further messages. */
3332
3333 if (!conn->received_cm_service_request)
3334 return;
3335
3336 if (log_check_level(DMM, LOGL_DEBUG)) {
3337 struct gsm48_hdr *gh = msgb_l3(msg);
3338 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3339 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3340
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003341 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003342 " received_cm_service_request changes to false\n",
3343 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003344 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003345 }
3346 conn->received_cm_service_request = false;
3347}
3348
Harald Weltec2007852018-02-03 21:08:26 +01003349/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3350int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3351{
3352 switch (pdisc) {
3353 case GSM48_PDISC_MM:
3354 case GSM48_PDISC_CC:
3355 case GSM48_PDISC_NC_SS:
3356 return 0;
3357 case GSM48_PDISC_GROUP_CC:
3358 return 1;
3359 case GSM48_PDISC_BCAST_CC:
3360 return 2;
3361 case GSM48_PDISC_LOC:
3362 return 3;
3363 default:
3364 return -1;
3365 }
3366}
3367
3368/* extract the N(SD) and return the modulo value for a R98 message */
3369static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3370{
3371 switch (pdisc) {
3372 case GSM48_PDISC_MM:
3373 case GSM48_PDISC_CC:
3374 case GSM48_PDISC_NC_SS:
3375 *n_sd = (msg_type >> 6) & 0x3;
3376 return 4;
3377 case GSM48_PDISC_GROUP_CC:
3378 case GSM48_PDISC_BCAST_CC:
3379 case GSM48_PDISC_LOC:
3380 *n_sd = (msg_type >> 6) & 0x1;
3381 return 2;
3382 default:
3383 /* no sequence number, we cannot detect dups */
3384 return 0;
3385 }
3386}
3387
3388/* extract the N(SD) and return the modulo value for a R99 message */
3389static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3390{
3391 switch (pdisc) {
3392 case GSM48_PDISC_MM:
3393 case GSM48_PDISC_CC:
3394 case GSM48_PDISC_NC_SS:
3395 case GSM48_PDISC_GROUP_CC:
3396 case GSM48_PDISC_BCAST_CC:
3397 case GSM48_PDISC_LOC:
3398 *n_sd = (msg_type >> 6) & 0x1;
3399 return 2;
3400 default:
3401 /* no sequence number, we cannot detect dups */
3402 return 0;
3403 }
3404}
3405
3406/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3407static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3408{
3409 struct gsm48_hdr *gh;
3410 uint8_t pdisc;
3411 uint8_t n_sd, modulo, bin;
3412
3413 gh = msgb_l3(msg);
3414 pdisc = gsm48_hdr_pdisc(gh);
3415
3416 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3417 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3418 } else { /* R99 */
3419 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3420 }
3421 if (modulo == 0)
3422 return false;
3423 bin = gsm0407_pdisc_ctr_bin(pdisc);
3424 if (bin < 0)
3425 return false;
3426
3427 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3428 if (n_sd != conn->n_sd_next[bin]) {
3429 /* not what we expected: duplicate */
3430 return true;
3431 } else {
3432 /* as expected: no dup; update expected counter for next message */
3433 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3434 return false;
3435 }
3436}
Harald Welte2483f1b2016-06-19 18:06:02 +02003437
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003438/* 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 +08003439int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003440{
Philipp Maier91f10c72017-04-20 18:40:37 +02003441 struct gsm48_hdr *gh;
3442 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003443 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003444
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003445 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003446 OSMO_ASSERT(conn);
3447 OSMO_ASSERT(msg);
3448
Philipp Maier91f10c72017-04-20 18:40:37 +02003449 gh = msgb_l3(msg);
3450 pdisc = gsm48_hdr_pdisc(gh);
3451
Harald Weltec2007852018-02-03 21:08:26 +01003452 if (gsm0407_is_duplicate(conn, msg)) {
3453 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3454 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3455 return 0;
3456 }
3457
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003458 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3459 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3460 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003461
3462 if (!msc_subscr_conn_is_accepted(conn)
3463 && !msg_is_initially_permitted(gh)) {
3464 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003465 "subscr %s: Message not permitted for initial conn: %s\n",
3466 vlr_subscr_name(conn->vsub),
3467 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003468 return -EACCES;
3469 }
3470
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003471 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3472 LOGP(DMM, LOGL_ERROR,
3473 "%s: Illegal situation: RAN type mismatch:"
3474 " attached via %s, received message via %s\n",
3475 vlr_subscr_name(conn->vsub),
3476 ran_type_name(conn->vsub->cs.attached_via_ran),
3477 ran_type_name(conn->via_ran));
3478 return -EACCES;
3479 }
3480
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003481#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003482 if (silent_call_reroute(conn, msg))
3483 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003484#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003485
Harald Welte52b1f982008-12-23 20:25:15 +00003486 switch (pdisc) {
3487 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003488 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003489 break;
3490 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003491 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003492 break;
3493 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003494 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003495 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003496 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003497 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003498 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003499 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003500 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003501 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3502 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003503 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003504 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003505 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003506 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003507 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003508 case GSM48_PDISC_TEST:
3509 rc = gsm0414_rcv_test(conn, msg);
3510 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003511 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003512 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3513 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003514 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003515 break;
3516 }
3517
3518 return rc;
3519}
Harald Welte8470bf22008-12-25 23:28:35 +00003520
Harald Welte2483f1b2016-06-19 18:06:02 +02003521/***********************************************************************
3522 * VLR integration
3523 ***********************************************************************/
3524
3525/* VLR asks us to send an authentication request */
3526static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3527 bool send_autn)
3528{
3529 struct gsm_subscriber_connection *conn = msc_conn_ref;
3530 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3531 send_autn? at->vec.autn : NULL,
3532 at->key_seq);
3533}
3534
3535/* VLR asks us to send an authentication reject */
3536static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3537{
3538 struct gsm_subscriber_connection *conn = msc_conn_ref;
3539 return gsm48_tx_mm_auth_rej(conn);
3540}
3541
3542/* VLR asks us to transmit an Identity Request of given type */
3543static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3544{
3545 struct gsm_subscriber_connection *conn = msc_conn_ref;
3546 return mm_tx_identity_req(conn, mi_type);
3547}
3548
3549/* VLR asks us to transmit a Location Update Accept */
3550static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3551{
3552 struct gsm_subscriber_connection *conn = msc_conn_ref;
3553 return gsm0408_loc_upd_acc(conn, send_tmsi);
3554}
3555
3556/* VLR asks us to transmit a Location Update Reject */
3557static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3558{
3559 struct gsm_subscriber_connection *conn = msc_conn_ref;
3560 return gsm0408_loc_upd_rej(conn, cause);
3561}
3562
3563/* VLR asks us to transmit a CM Service Accept */
3564static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3565{
3566 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003567 return msc_gsm48_tx_mm_serv_ack(conn);
3568}
3569
3570static int msc_vlr_tx_common_id(void *msc_conn_ref)
3571{
3572 struct gsm_subscriber_connection *conn = msc_conn_ref;
3573 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003574}
3575
Stefan Sperling3a741282018-03-13 21:11:49 +01003576/* VLR asks us to transmit MM info. */
3577static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3578{
3579 struct gsm_subscriber_connection *conn = msc_conn_ref;
3580 if (!conn->network->send_mm_info)
3581 return 0;
3582 return gsm48_tx_mm_info(conn);
3583}
3584
Harald Welte2483f1b2016-06-19 18:06:02 +02003585/* VLR asks us to transmit a CM Service Reject */
3586static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3587{
3588 uint8_t cause;
3589 struct gsm_subscriber_connection *conn = msc_conn_ref;
3590 conn->received_cm_service_request = false;
3591
3592 switch (result) {
3593 default:
3594 case VLR_PR_ARQ_RES_NONE:
3595 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3596 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3597 cause = GSM48_REJECT_NETWORK_FAILURE;
3598 break;
3599 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3600 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3601 break;
3602 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3603 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3604 break;
3605 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3606 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3607 break;
3608 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3609 cause = GSM48_REJECT_ILLEGAL_MS;
3610 break;
3611 case VLR_PR_ARQ_RES_TIMEOUT:
3612 cause = GSM48_REJECT_CONGESTION;
3613 break;
3614 };
3615
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003616 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003617}
3618
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003619/* For msc_vlr_set_ciph_mode() */
3620osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3621 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3622
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003623/* VLR asks us to start using ciphering.
3624 * (Keep non-static to allow regression testing on this function.) */
3625int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3626 bool umts_aka,
3627 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003628{
3629 struct gsm_subscriber_connection *conn = msc_conn_ref;
3630 struct vlr_subscr *vsub;
3631 struct gsm_auth_tuple *tuple;
3632
3633 if (!conn || !conn->vsub) {
3634 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3635 " NULL conn/subscriber");
3636 return -EINVAL;
3637 }
3638
3639 vsub = conn->vsub;
3640 tuple = vsub->last_tuple;
3641
3642 if (!tuple) {
3643 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3644 " Command: no auth tuple available\n",
3645 vlr_subscr_name(vsub));
3646 return -EINVAL;
3647 }
3648
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003649 switch (conn->via_ran) {
3650 case RAN_GERAN_A:
3651 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3652 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003653 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003654 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003655 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003656 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003657
Harald Welte7b222aa2017-12-23 19:30:32 +01003658 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003659 if (net->a5_encryption_mask & (1 << i) &&
3660 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003661 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3662 }
3663 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003664
Harald Welte71330722017-12-23 19:59:02 +01003665 if (ei.perm_algo_len == 0) {
3666 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3667 "between MSC-configured and MS-supported A5 algorithms\n",
3668 vlr_subscr_name(conn->vsub));
3669 return -ENOTSUP;
3670 }
3671
Neels Hofmeyr52821712017-12-18 01:23:42 +01003672 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3673 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3674 * necessarily a match for the UMTS AKA tokens. */
3675 if (umts_aka)
3676 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3677 else
3678 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003679 ei.key_len = sizeof(tuple->vec.kc);
3680
3681 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3682 }
3683
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003684 case RAN_UTRAN_IU:
3685#ifdef BUILD_IU
3686 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3687 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003688 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003689#else
3690 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3691 " built without Iu support\n");
3692 return -ENOTSUP;
3693#endif
3694
3695 default:
3696 break;
3697 }
3698 LOGP(DMM, LOGL_ERROR,
3699 "%s: cannot start ciphering, unknown RAN type %d\n",
3700 vlr_subscr_name(conn->vsub), conn->via_ran);
3701 return -ENOTSUP;
3702}
3703
3704void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3705{
3706 struct vlr_ciph_result vlr_res = {};
3707
3708 if (!conn || !conn->vsub) {
3709 LOGP(DMM, LOGL_ERROR,
3710 "Rx Security Mode Complete for invalid conn\n");
3711 return;
3712 }
3713
3714 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3715 vlr_subscr_name(conn->vsub));
3716
3717 vlr_res.cause = VLR_CIPH_COMPL;
3718 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003719}
3720
3721/* VLR informs us that the subscriber data has somehow been modified */
3722static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3723{
Maxa263bb22017-12-27 13:23:44 +01003724 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3725 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003726}
3727
3728/* VLR informs us that the subscriber has been associated with a conn */
3729static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3730 struct vlr_subscr *vsub)
3731{
3732 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003733 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003734 OSMO_ASSERT(!conn->vsub);
3735 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003736 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003737 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003738}
3739
3740/* operations that we need to implement for libvlr */
3741static const struct vlr_ops msc_vlr_ops = {
3742 .tx_auth_req = msc_vlr_tx_auth_req,
3743 .tx_auth_rej = msc_vlr_tx_auth_rej,
3744 .tx_id_req = msc_vlr_tx_id_req,
3745 .tx_lu_acc = msc_vlr_tx_lu_acc,
3746 .tx_lu_rej = msc_vlr_tx_lu_rej,
3747 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3748 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3749 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003750 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003751 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003752 .subscr_update = msc_vlr_subscr_update,
3753 .subscr_assoc = msc_vlr_subscr_assoc,
3754};
3755
3756/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3757int msc_vlr_alloc(struct gsm_network *net)
3758{
3759 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3760 if (!net->vlr)
3761 return -ENOMEM;
3762 net->vlr->user_ctx = net;
3763 return 0;
3764}
3765
3766/* Launch the VLR, i.e. its GSUP connection */
3767int msc_vlr_start(struct gsm_network *net)
3768{
3769 OSMO_ASSERT(net->vlr);
3770 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3771 net->gsup_server_port);
3772}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01003773
3774struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
3775{
3776 struct msgb *msg;
3777 struct gsm48_hdr *gh;
3778
3779 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
3780 if (!msg)
3781 return NULL;
3782
3783 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3784 gh->proto_discr = GSM48_PDISC_MM;
3785 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
3786 gh->data[0] = value;
3787
3788 return msg;
3789}
3790
3791struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
3792{
3793 struct gsm48_hdr *gh;
3794 struct msgb *msg;
3795
3796 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
3797 if (!msg)
3798 return NULL;
3799
3800 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3801 gh->proto_discr = GSM48_PDISC_MM;
3802 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
3803 gh->data[0] = cause;
3804 return msg;
3805}
3806
3807int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
3808{
3809 /* Check the size for the classmark */
3810 if (length < 1 + *classmark2_lv)
3811 return -1;
3812
3813 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
3814 if (length < 2 + *classmark2_lv + mi_lv[0])
3815 return -2;
3816
3817 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
3818 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
3819}
3820
3821int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
3822 char *mi_string, uint8_t *mi_type)
3823{
3824 static const uint32_t classmark_offset =
3825 offsetof(struct gsm48_pag_resp, classmark2);
3826 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
3827 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
3828 mi_string, mi_type);
3829}