blob: 79488cda5310ce653e1cfc741aa673c7ee924385 [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
Neels Hofmeyr4ac80092019-03-04 02:46:37 +010035#include "config.h"
Harald Welte9c3dc902012-04-08 16:59:24 +020036
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>
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070045#include <osmocom/msc/gsm_09_11.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020047#include <osmocom/msc/transaction.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020048#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020050#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010051#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020052#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010053#include <osmocom/msc/msc_a.h>
54#include <osmocom/msc/paging.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>
Harald Welte52b1f982008-12-23 20:25:15 +000066
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010067#include <osmocom/msc/msub.h>
68#include <osmocom/msc/msc_roles.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020069
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020070#include <assert.h>
71
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020072
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020073void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010074void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020075
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010076static int gsm0408_loc_upd_acc(struct msc_a *msc_a, uint32_t send_tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000077
Harald Welte27989d42018-06-21 20:39:20 +020078/*! Send a simple GSM 04.08 message without any payload
Neels Hofmeyrc036b792018-11-29 22:37:51 +010079 * \param conn Active RAN connection
Harald Welte27989d42018-06-21 20:39:20 +020080 * \param[in] pdisc Protocol discriminator
81 * \param[in] msg_type Message type
82 * \return result of \ref gsm48_conn_sendmsg
83 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010084int gsm48_tx_simple(struct msc_a *msc_a, uint8_t pdisc, uint8_t msg_type)
Harald Welte27989d42018-06-21 20:39:20 +020085{
86 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
87 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
88
89 gh->proto_discr = pdisc;
90 gh->msg_type = msg_type;
91
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010092 return msc_a_tx_dtap_to_i(msc_a, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080093}
Sylvain Munaut30a15382009-12-24 00:27:26 +010094
Harald Welte2483f1b2016-06-19 18:06:02 +020095/* clear all transactions globally; used in case of MNCC socket disconnect */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010096void gsm0408_clear_all_trans(struct gsm_network *net, enum trans_type type)
Harald Welte371efe52010-12-22 23:17:50 +010097{
98 struct gsm_trans *trans, *temp;
99
100 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
101
102 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100103 if (trans->type == type) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100104 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100105 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100106 }
Harald Welte371efe52010-12-22 23:17:50 +0100107 }
108}
109
Holger Freyther429e7762008-12-30 13:28:30 +0000110/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100111static int gsm0408_loc_upd_rej(struct msc_a *msc_a, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000112{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800113 struct msgb *msg;
114
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800115 msg = gsm48_create_loc_upd_rej(cause);
116 if (!msg) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100117 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800118 return -1;
119 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200120
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100121 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO, "LOCATION UPDATING REJECT\n");
Harald Welte24ff6ee2009-12-22 00:41:05 +0100122
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100123 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000124}
125
126/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100127static int gsm0408_loc_upd_acc(struct msc_a *msc_a, uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000128{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100129 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000130 struct gsm48_hdr *gh;
131 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200132 uint8_t *mid;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100133 struct gsm_network *net = msc_a_net(msc_a);
134 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100135 struct osmo_location_area_id laid = {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100136 .plmn = net->plmn,
137 .lac = vsub->cgi.lai.lac,
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100138 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200139
Harald Welte52b1f982008-12-23 20:25:15 +0000140 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
141 gh->proto_discr = GSM48_PDISC_MM;
142 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
143
144 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100145 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000146
Harald Welte2483f1b2016-06-19 18:06:02 +0200147 if (send_tmsi == GSM_RESERVED_TMSI) {
148 /* we did not allocate a TMSI to the MS, so we need to
149 * include the IMSI in order for the MS to delete any
150 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200151 uint8_t mi[10];
152 int len;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100153 len = gsm48_generate_mid_from_imsi(mi, vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200154 mid = msgb_put(msg, len);
155 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200156 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100157 vlr_subscr_name(vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200158 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200159 /* Include the TMSI, which means that the MS will send a
160 * TMSI REALLOCATION COMPLETE, and we should wait for
161 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200162 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200163 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200164 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100165 vlr_subscr_name(vsub),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200166 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200167 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200168 /* TODO: Follow-on proceed */
169 /* TODO: CTS permission */
170 /* TODO: Equivalent PLMNs */
171 /* TODO: Emergency Number List */
172 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000173
Harald Welte52b1f982008-12-23 20:25:15 +0000174
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100175 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000176}
177
Harald Weltebf5e8df2009-02-03 12:59:45 +0000178/* Transmit Chapter 9.2.10 Identity Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100179static int mm_tx_identity_req(struct msc_a *msc_a, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000180{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100181 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000182 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000183
Harald Welte231ad4f2008-12-27 11:15:38 +0000184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
185 gh->proto_discr = GSM48_PDISC_MM;
186 gh->msg_type = GSM48_MT_MM_ID_REQ;
187 gh->data[0] = id_type;
188
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100189 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000190}
191
Harald Weltebf5e8df2009-02-03 12:59:45 +0000192/* Parse Chapter 9.2.11 Identity Response */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100193static int mm_rx_id_resp(struct msc_a *msc_a, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000194{
195 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100196 uint8_t *mi = gh->data+1;
197 uint8_t mi_len = gh->data[0];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100198 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte231ad4f2008-12-27 11:15:38 +0000199
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100200 if (!vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200201 LOGP(DMM, LOGL_ERROR,
202 "Rx MM Identity Response: invalid: no subscriber\n");
203 return -EINVAL;
204 }
205
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100206 DEBUGP(DMM, "IDENTITY RESPONSE: MI=%s\n", osmo_mi_name(mi, mi_len));
Harald Welte231ad4f2008-12-27 11:15:38 +0000207
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200208 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100209
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100210 return vlr_subscr_rx_id_resp(vsub, mi, mi_len);
211}
212
213/* 9.2.5 CM service accept */
214static int msc_gsm48_tx_mm_serv_ack(struct msc_a *msc_a)
215{
216 struct msgb *msg;
217 struct gsm48_hdr *gh;
218
219 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACC");
220
221 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
222 gh->proto_discr = GSM48_PDISC_MM;
223 gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
224
225 return msc_a_tx_dtap_to_i(msc_a, msg);
226}
227
228/* 9.2.6 CM service reject */
229static int msc_gsm48_tx_mm_serv_rej(struct msc_a *msc_a,
230 enum gsm48_reject_value value)
231{
232 struct msgb *msg;
233
234 msg = gsm48_create_mm_serv_rej(value);
235 if (!msg) {
236 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
237 return -1;
238 }
239
240 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "-> CM SERVICE Reject cause: %d\n", value);
241
242 return msc_a_tx_dtap_to_i(msc_a, msg);
243}
244
245
246/* Extract the R99 flag from various Complete Level 3 messages. Depending on the Message Type, extract R99 from the
247 * Classmark Type 1 or the Classmark Type 2. Return 1 for R99, 0 for pre-R99, negative if not a valid Complete Level 3
248 * message. */
249int compl_l3_msg_is_r99(const struct msgb *msg)
250{
251 const struct gsm48_hdr *gh = msgb_l3(msg);
252 uint8_t pdisc = gsm48_hdr_pdisc(gh);
253 const struct gsm48_loc_upd_req *lu;
254 const struct gsm48_imsi_detach_ind *idi;
255 uint8_t cm2_len;
256 const struct gsm48_classmark2 *cm2;
257
258 switch (pdisc) {
259 case GSM48_PDISC_MM:
260 switch (gsm48_hdr_msg_type(gh)) {
261 case GSM48_MT_MM_LOC_UPD_REQUEST:
262 lu = (const struct gsm48_loc_upd_req *) gh->data;
263 return osmo_gsm48_classmark1_is_r99(&lu->classmark1) ? 1 : 0;
264
265 case GSM48_MT_MM_CM_SERV_REQ:
266 case GSM48_MT_MM_CM_REEST_REQ:
267 break;
268
269 case GSM48_MT_MM_IMSI_DETACH_IND:
270 idi = (const struct gsm48_imsi_detach_ind *) gh->data;
271 return osmo_gsm48_classmark1_is_r99(&idi->classmark1) ? 1 : 0;
272
273 default:
274 return -1;
275 }
276 break;
277
278 case GSM48_PDISC_RR:
279 switch (gsm48_hdr_msg_type(gh)) {
280 case GSM48_MT_RR_PAG_RESP:
281 break;
282
283 default:
284 return -1;
285 }
286 break;
287
288 default:
289 return -1;
290 }
291
292 /* Both CM Service Request and Paging Response have Classmark Type 2 at the same location: */
293 cm2_len = gh->data[1];
294 cm2 = (void*)gh->data+2;
295 return osmo_gsm48_classmark2_is_r99(cm2, cm2_len) ? 1 : 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000296}
297
Harald Welte2483f1b2016-06-19 18:06:02 +0200298/* Chapter 9.2.15: Receive Location Updating Request.
299 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300static int mm_rx_loc_upd_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000301{
Harald Welte8470bf22008-12-25 23:28:35 +0000302 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000303 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200304 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200305 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200307 uint32_t tmsi;
308 char *imsi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100309 struct osmo_location_area_id old_lai;
Harald Welte2483f1b2016-06-19 18:06:02 +0200310 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200311 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100312 struct gsm_network *net = msc_a_net(msc_a);
313 struct vlr_subscr *vsub;
Harald Welte52b1f982008-12-23 20:25:15 +0000314
Harald Welte8470bf22008-12-25 23:28:35 +0000315 lu = (struct gsm48_loc_upd_req *) gh->data;
316
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100317 if (msc_a_is_establishing_auth_ciph(msc_a)) {
318 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
319 "Cannot accept another LU, conn already busy establishing authenticity;"
320 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
321 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200322 return -EINVAL;
323 }
324
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100325 if (msc_a_is_accepted(msc_a)) {
326 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
327 "Cannot accept another LU, conn already established;"
328 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
329 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200330 return -EINVAL;
331 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200332
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100333 msc_a->complete_layer3_type = COMPLETE_LAYER3_LU;
334 msub_update_id_from_mi(msc_a->c.msub, lu->mi, lu->mi_len);
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200335
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100336 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
337 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000338
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200339 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100340
Harald Welte24ff6ee2009-12-22 00:41:05 +0100341 switch (lu->type) {
342 case GSM48_LUPD_NORMAL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100343 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200344 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100345 break;
346 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100347 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200348 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100349 break;
350 case GSM48_LUPD_PERIODIC:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100351 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200352 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100353 break;
354 }
355
Harald Welte2483f1b2016-06-19 18:06:02 +0200356 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
357 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
358 /* TODO: 10.5.7.8 Device properties */
359 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200360
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100361 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
362 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte52b1f982008-12-23 20:25:15 +0000363 switch (mi_type) {
364 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 tmsi = GSM_RESERVED_TMSI;
366 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000367 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000368 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200369 tmsi = tmsi_from_string(mi_string);
370 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000371 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200372 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100373 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200374 tmsi = GSM_RESERVED_TMSI;
375 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000376 break;
377 }
378
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100379 gsm48_decode_lai2(&lu->lai, &old_lai);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100380 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "USIM: old LAI: %s\n", osmo_lai_name(&old_lai));
Harald Welte24516ea2009-07-04 10:18:00 +0200381
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100382 msc_a_get(msc_a, __func__);
383 msc_a_get(msc_a, MSC_A_USE_LOCATION_UPDATING);
384
385 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
386 lu_fsm = vlr_loc_update(msc_a->c.fi,
387 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
388 net->vlr, msc_a, vlr_lu_type, tmsi, imsi,
389 &old_lai, &msc_a->via_cell.lai,
390 is_utran || net->authentication_required,
391 is_utran || net->a5_encryption_mask > 0x01,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100392 lu->key_seq,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100393 osmo_gsm48_classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200394 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200395 net->vlr->cfg.assign_tmsi);
396 if (!lu_fsm) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100397 LOG_MSC_A(msc_a, LOGL_ERROR, "Can't start LU FSM\n");
398 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
399 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200400 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800401 }
402
Harald Welte2483f1b2016-06-19 18:06:02 +0200403 /* From vlr_loc_update() we expect an implicit dispatch of
404 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
405 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000406
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100407 vsub = msc_a_vsub(msc_a);
408 if (!vsub) {
409 msc_a_put(msc_a, __func__);
410 return 0;
411 }
412
413 if (vsub->lu_fsm != lu_fsm) {
414 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal Error during Location Updating attempt\n");
415 msc_a_release_cn(msc_a);
416 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200417 return -EIO;
418 }
419
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100420 vsub->classmark.classmark1 = lu->classmark1;
421 vsub->classmark.classmark1_set = true;
422 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200423 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 Welte0df904d2018-12-03 11:00:04 +0100438/* Generate a message buffer that contains a valid MM info message,
439 * See also 3GPP TS 24.008, chapter 9.2.15a */
440struct msgb *gsm48_create_mm_info(struct gsm_network *net)
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;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200444 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200445 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100446
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100448 struct tm* gmt_time;
449 struct tm* local_time;
450 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100451 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000452
Harald Weltedb253af2008-12-30 17:56:55 +0000453 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
454 gh->proto_discr = GSM48_PDISC_MM;
455 gh->msg_type = GSM48_MT_MM_INFO;
456
457 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200458#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000459 name_len = strlen(net->name_long);
460 /* 10.5.3.5a */
461 ptr8 = msgb_put(msg, 3);
462 ptr8[0] = GSM48_IE_NAME_LONG;
463 ptr8[1] = name_len*2 +1;
464 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
465
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200466 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000467 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000468 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000469
470 /* FIXME: Use Cell Broadcast, not UCS-2, since
471 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200472#endif
473 name_len = (strlen(net->name_long)*7)/8;
474 name_pad = (8 - strlen(net->name_long)*7)%8;
475 if (name_pad > 0)
476 name_len++;
477 /* 10.5.3.5a */
478 ptr8 = msgb_put(msg, 3);
479 ptr8[0] = GSM48_IE_NAME_LONG;
480 ptr8[1] = name_len +1;
481 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
482
483 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100484 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200485
Harald Weltedb253af2008-12-30 17:56:55 +0000486 }
487
488 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200489#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000490 name_len = strlen(net->name_short);
491 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200492 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200493 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000494 ptr8[1] = name_len*2 + 1;
495 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
496
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200497 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000498 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000499 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200500#endif
501 name_len = (strlen(net->name_short)*7)/8;
502 name_pad = (8 - strlen(net->name_short)*7)%8;
503 if (name_pad > 0)
504 name_len++;
505 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200506 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200507 ptr8[0] = GSM48_IE_NAME_SHORT;
508 ptr8[1] = name_len +1;
509 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
510
511 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100512 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200513
Harald Weltedb253af2008-12-30 17:56:55 +0000514 }
515
Harald Weltedb253af2008-12-30 17:56:55 +0000516 /* Section 10.5.3.9 */
517 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100518 gmt_time = gmtime(&cur_t);
519
Harald Weltedb253af2008-12-30 17:56:55 +0000520 ptr8 = msgb_put(msg, 8);
521 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100522 ptr8[1] = bcdify(gmt_time->tm_year % 100);
523 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
524 ptr8[3] = bcdify(gmt_time->tm_mday);
525 ptr8[4] = bcdify(gmt_time->tm_hour);
526 ptr8[5] = bcdify(gmt_time->tm_min);
527 ptr8[6] = bcdify(gmt_time->tm_sec);
528
Neels Hofmeyr73983952016-05-10 13:29:33 +0200529 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200530 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200531 if (net->tz.hr < 0) {
532 tzunits = ((net->tz.hr/-1)*4);
533 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100534 ptr8[7] = bcdify(tzunits);
535 /* Set negative time */
536 ptr8[7] |= 0x08;
537 }
538 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200539 tzunits = net->tz.hr*4;
540 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100541 ptr8[7] = bcdify(tzunits);
542 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100543 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200544 if (net->tz.dst >= 0 && net->tz.dst <= 2)
545 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 }
547 else {
548 /* Need to get GSM offset and convert into 15 min units */
549 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200550#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200551 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100552 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200553#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200554 /* find timezone offset */
555 time_t utc;
556 double offsetFromUTC;
557 utc = mktime(gmt_time);
558 local_time = localtime(&cur_t);
559 offsetFromUTC = difftime(cur_t, utc);
560 if (local_time->tm_isdst)
561 offsetFromUTC += 3600.0;
562 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200563#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100564 if (tzunits < 0) {
565 tzunits = tzunits/-1;
566 ptr8[7] = bcdify(tzunits);
567 /* Flip it to negative */
568 ptr8[7] |= 0x08;
569 }
570 else
571 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100572
573 /* Does not support DST +2 */
574 if (local_time->tm_isdst)
575 dst = 1;
576 }
577
578 if (dst) {
579 ptr8 = msgb_put(msg, 3);
580 ptr8[0] = GSM48_IE_NET_DST;
581 ptr8[1] = 1;
582 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100583 }
Harald Weltedb253af2008-12-30 17:56:55 +0000584
Harald Welte0df904d2018-12-03 11:00:04 +0100585 return msg;
586}
Daniel Willmanneea93372009-08-13 03:42:07 +0200587
Harald Welte0df904d2018-12-03 11:00:04 +0100588/* Section 9.2.15a */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100589int gsm48_tx_mm_info(struct msc_a *msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100590{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100591 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +0100592 struct msgb *msg;
593
594 msg = gsm48_create_mm_info(net);
595
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100596 LOG_MSC_A(msc_a, LOGL_DEBUG, "Tx MM INFO\n");
597 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Weltedb253af2008-12-30 17:56:55 +0000598}
599
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100600/*! Send an Authentication Request to MS on the given RAN connection
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100601 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
602 * \param[in] conn Subscriber connection to send on.
603 * \param[in] rand Random challenge token to send, must be 16 bytes long.
604 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
605 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
606 * \param[in] key_seq auth tuple's sequence number.
607 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100608int gsm48_tx_mm_auth_req(struct msc_a *msc_a, uint8_t *rand,
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100609 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200610{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100611 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200612 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200613 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200614
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100615 DEBUGP(DMM, "Tx AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100616 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100617 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200618
Harald Welte7984d5c2009-08-12 22:56:50 +0200619 gh->proto_discr = GSM48_PDISC_MM;
620 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
621
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100622 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200623
Harald Welte7984d5c2009-08-12 22:56:50 +0200624 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100625 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200626 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200627 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200628
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100629
630 /* 16 bytes AUTN */
631 if (autn)
632 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
633
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100634 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte7984d5c2009-08-12 22:56:50 +0200635}
636
637/* Section 9.2.1 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100638int gsm48_tx_mm_auth_rej(struct msc_a *msc_a)
Harald Welte7984d5c2009-08-12 22:56:50 +0200639{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100640 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "-> AUTH REJECT\n");
641 return gsm48_tx_simple(msc_a, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200642}
643
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100644static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
645 enum gsm48_reject_value cause);
Harald Welte2483f1b2016-06-19 18:06:02 +0200646
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100647static int cm_serv_reuse_conn(struct msc_a *msc_a, const uint8_t *mi_lv, enum osmo_cm_service_type cm_serv_type)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100648{
Harald Welte2483f1b2016-06-19 18:06:02 +0200649 uint8_t mi_type;
650 char mi_string[GSM48_MI_SIZE];
651 uint32_t tmsi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100652 struct gsm_network *net = msc_a_net(msc_a);
653 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100654
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
656 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100657
Harald Welte2483f1b2016-06-19 18:06:02 +0200658 switch (mi_type) {
659 case GSM_MI_TYPE_IMSI:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100660 if (vlr_subscr_matches_imsi(vsub, mi_string))
Harald Welte2483f1b2016-06-19 18:06:02 +0200661 goto accept_reuse;
662 break;
663 case GSM_MI_TYPE_TMSI:
664 tmsi = osmo_load32be(mi_lv+2);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100665 if (vlr_subscr_matches_tmsi(vsub, tmsi))
Harald Welte2483f1b2016-06-19 18:06:02 +0200666 goto accept_reuse;
667 break;
668 case GSM_MI_TYPE_IMEI:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100669 if (vlr_subscr_matches_imei(vsub, mi_string))
Harald Welte2483f1b2016-06-19 18:06:02 +0200670 goto accept_reuse;
671 break;
672 default:
673 break;
674 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100675
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100676 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "CM Service Request with mismatching mobile identity: %s %s\n",
677 gsm48_mi_type_name(mi_type), mi_string);
678 msc_vlr_tx_cm_serv_rej(msc_a, cm_serv_type, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200679 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100680
Harald Welte2483f1b2016-06-19 18:06:02 +0200681accept_reuse:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100682 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "re-using already accepted connection\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200683
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100684 msc_a_get(msc_a, msc_a_cm_service_type_to_use(cm_serv_type));
685 msub_update_id(msc_a->c.msub);
686 return net->vlr->ops.tx_cm_serv_acc(msc_a, cm_serv_type);
Sylvain Munautba87f452009-12-24 00:28:46 +0100687}
688
Harald Welte4ed0e922009-01-10 03:17:30 +0000689/*
690 * Handle CM Service Requests
691 * a) Verify that the packet is long enough to contain the information
692 * we require otherwsie reject with INCORRECT_MESSAGE
693 * b) Try to parse the TMSI. If we do not have one reject
694 * c) Check that we know the subscriber with the TMSI otherwise reject
695 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200696 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200697 *
698 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000699 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100700int gsm48_rx_mm_serv_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000701{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100702 struct gsm_network *net = msc_a_net(msc_a);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200703 uint8_t mi_type;
Harald Welteba4cf162009-01-10 01:49:35 +0000704 struct gsm48_hdr *gh = msgb_l3(msg);
705 struct gsm48_service_request *req =
706 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800707 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200708 uint8_t classmark2_len = gh->data[1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100709 uint8_t *classmark2_buf = gh->data+2;
710 struct gsm48_classmark2 *cm2 = (void*)classmark2_buf;
711 uint8_t *mi_p = classmark2_buf + classmark2_len;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200712 uint8_t mi_len = *mi_p;
713 uint8_t *mi = mi_p + 1;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200714 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100715 struct vlr_subscr *vsub;
Harald Welteba4cf162009-01-10 01:49:35 +0000716
717 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100718 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: wrong message size (%u < %zu)\n",
719 msg->data_len, sizeof(struct gsm48_service_request*));
720 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000721 }
722
723 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100724 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: message does not fit in packet\n");
725 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000726 }
727
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100728 if (msc_a_is_establishing_auth_ciph(msc_a)) {
729 LOG_MSC_A(msc_a, LOGL_ERROR,
730 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
731 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_CONGESTION);
Neels Hofmeyr7ce21dc2019-01-04 17:42:05 +0100732 /* or should we accept and note down the service request anyway? */
733 }
734
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100735 msc_a->complete_layer3_type = COMPLETE_LAYER3_CM_SERVICE_REQ;
736 msub_update_id_from_mi(msc_a->c.msub, mi, mi_len);
737 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "Rx CM SERVICE REQUEST cm_service_type=%s\n",
738 osmo_cm_service_type_name(req->cm_service_type));
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100739
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100740 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte3995d2e2018-01-24 11:23:54 +0100741 switch (mi_type) {
742 case GSM_MI_TYPE_IMSI:
743 case GSM_MI_TYPE_TMSI:
744 /* continue below */
745 break;
746 case GSM_MI_TYPE_IMEI:
747 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
748 /* We don't do emergency calls by IMEI */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100749 LOG_MSC_A(msc_a, LOGL_NOTICE, "Tx CM SERVICE REQUEST REJECT\n");
750 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_IMEI_NOT_ACCEPTED);
Harald Welte3995d2e2018-01-24 11:23:54 +0100751 }
752 /* fall-through for non-emergency setup */
753 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100754 LOG_MSC_A(msc_a, LOGL_ERROR, "MI type is not expected: %s\n", gsm48_mi_type_name(mi_type));
755 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000756 }
757
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100758 if (!msc_a_cm_service_type_to_use(req->cm_service_type))
759 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
Harald Welte37382ec2018-01-24 12:49:34 +0100760
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100761 if (msc_a_is_accepted(msc_a))
762 return cm_serv_reuse_conn(msc_a, mi_p, req->cm_service_type);
Harald Welte2483f1b2016-06-19 18:06:02 +0200763
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200764 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200765
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100766 msc_a_get(msc_a, msc_a_cm_service_type_to_use(req->cm_service_type));
767
768 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
769 vlr_proc_acc_req(msc_a->c.fi,
770 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
771 net->vlr, msc_a,
772 VLR_PR_ARQ_T_CM_SERV_REQ,
773 req->cm_service_type,
774 mi-1, &msc_a->via_cell.lai,
775 is_utran || net->authentication_required,
776 is_utran || net->a5_encryption_mask > 0x01,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100777 req->cipher_key_seq,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100778 osmo_gsm48_classmark2_is_r99(cm2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200779 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000780
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200781 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
782 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100783 vsub = msc_a_vsub(msc_a);
784 if (!vsub) {
785 LOG_MSC_A(msc_a, LOGL_ERROR, "subscriber not allowed to do a CM Service Request\n");
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200786 return -EIO;
787 }
788
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100789 vsub->classmark.classmark2 = *cm2;
790 vsub->classmark.classmark2_len = classmark2_len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000792}
793
Harald Welte49f8fcb2018-01-24 21:40:19 +0100794/* Receive a CM Re-establish Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100795static int gsm48_rx_cm_reest_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte49f8fcb2018-01-24 21:40:19 +0100796{
797 uint8_t mi_type;
798 char mi_string[GSM48_MI_SIZE];
799 struct gsm48_hdr *gh = msgb_l3(msg);
800
801 uint8_t classmark2_len = gh->data[1];
802 uint8_t *classmark2 = gh->data+2;
803 uint8_t mi_len = *(classmark2 + classmark2_len);
804 uint8_t *mi = (classmark2 + classmark2_len + 1);
805
806 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
807 mi_type = mi[0] & GSM_MI_TYPE_MASK;
808 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
809
810 /* we don't support CM call re-establishment */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100811 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
Harald Welte49f8fcb2018-01-24 21:40:19 +0100812}
813
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100814static int gsm48_rx_mm_imsi_detach_ind(struct msc_a *msc_a, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000815{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100816 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte2a139372009-02-22 21:14:55 +0000817 struct gsm48_hdr *gh = msgb_l3(msg);
818 struct gsm48_imsi_detach_ind *idi =
819 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200820 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200821 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200822 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000823
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200824 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200825 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
826 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000827
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100828 rate_ctr_inc(&net->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100829
Harald Welte2a139372009-02-22 21:14:55 +0000830 switch (mi_type) {
831 case GSM_MI_TYPE_TMSI:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100832 vsub = vlr_subscr_find_by_tmsi(net->vlr,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100833 tmsi_from_string(mi_string), __func__);
Harald Welte2a139372009-02-22 21:14:55 +0000834 break;
835 case GSM_MI_TYPE_IMSI:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100836 vsub = vlr_subscr_find_by_imsi(net->vlr, mi_string, __func__);
Harald Welte2a139372009-02-22 21:14:55 +0000837 break;
838 case GSM_MI_TYPE_IMEI:
839 case GSM_MI_TYPE_IMEISV:
840 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200841 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
842 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000843 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200844 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200845 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
846 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000847 break;
848 }
849
Harald Welte2483f1b2016-06-19 18:06:02 +0200850 if (!vsub) {
851 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
852 gsm48_mi_type_name(mi_type), mi_string);
853 } else {
854 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100855 msub_set_vsub(msc_a->c.msub, vsub);
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100856
857 if (vsub->cs.is_paging)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100858 paging_expired(vsub);
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100859
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200860 vsub->classmark.classmark1 = idi->classmark1;
861
Harald Welte2483f1b2016-06-19 18:06:02 +0200862 vlr_subscr_rx_imsi_detach(vsub);
863 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100864 vlr_subscr_put(vsub, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200865 }
Harald Welte2a139372009-02-22 21:14:55 +0000866
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100867 msc_a_release_cn(msc_a);
Harald Welte2a139372009-02-22 21:14:55 +0000868 return 0;
869}
870
Harald Welted2a7f5a2009-06-05 20:08:20 +0000871static int gsm48_rx_mm_status(struct msgb *msg)
872{
873 struct gsm48_hdr *gh = msgb_l3(msg);
874
875 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
876
877 return 0;
878}
879
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100880static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100881 struct msc_a *msc_a,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100882 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100883{
884 struct gsm48_hdr *gh = msgb_l3(msg);
885 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100886
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100887 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: l3 length invalid: %u\n",
889 msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100890 return -EINVAL;
891 }
892
893 *res_len = sizeof(ar->sres);
894 memcpy(res, ar->sres, sizeof(ar->sres));
895 return 0;
896}
897
898static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100899 struct msc_a *msc_a,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100900 struct msgb *msg)
901{
902 struct gsm48_hdr *gh;
903 uint8_t *data;
904 uint8_t iei;
905 uint8_t ie_len;
906 unsigned int data_len;
907
908 /* First parse the GSM part */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100909 if (parse_gsm_auth_resp(res, res_len, msc_a, msg))
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100910 return -EINVAL;
911 OSMO_ASSERT(*res_len == 4);
912
913 /* Then add the extended res part */
914 gh = msgb_l3(msg);
915 data = gh->data + sizeof(struct gsm48_auth_resp);
916 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
917
918 if (data_len < 3) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100919 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: l3 length invalid: %u\n",
920 msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100921 return -EINVAL;
922 }
923
924 iei = data[0];
925 ie_len = data[1];
926 if (iei != GSM48_IE_AUTH_RES_EXT) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100927 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION RESPONSE: expected IEI 0x%02x, got 0x%02x\n",
928 GSM48_IE_AUTH_RES_EXT, iei);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100929 return -EINVAL;
930 }
931
932 if (ie_len > 12) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100933 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
934 "MM R99 AUTHENTICATION RESPONSE: extended Auth Resp IE 0x%02x is too large: %u bytes\n",
935 GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100936 return -EINVAL;
937 }
938
939 *res_len += ie_len;
940 memcpy(res + 4, &data[2], ie_len);
941 return 0;
942}
943
944/* Chapter 9.2.3: Authentication Response */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100945static int gsm48_rx_mm_auth_resp(struct msc_a *msc_a, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100946{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100947 uint8_t res[16];
948 uint8_t res_len;
949 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100950 bool is_umts;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100951 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100952
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100953 if (!vsub) {
954 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
955 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100956 return -EINVAL;
957 }
958
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100959 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
960
961 if (is_umts)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100962 rc = parse_umts_auth_resp(res, &res_len, msc_a, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100963 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100964 rc = parse_gsm_auth_resp(res, &res_len, msc_a, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965
966 if (rc) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100967 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
968 "MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
969 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
970 is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100971 memset(res, 0, sizeof(res));
972 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100973 }
974
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100975 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
976 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
977 osmo_hexdump_nospc(res, res_len));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100978
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100979 return vlr_subscr_rx_auth_resp(vsub, osmo_gsm48_classmark_is_r99(&vsub->classmark),
980 msc_a->c.ran->type == OSMO_RAT_UTRAN_IU,
Harald Welte2483f1b2016-06-19 18:06:02 +0200981 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100982}
983
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100984static int gsm48_rx_mm_auth_fail(struct msc_a *msc_a, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100985{
986 struct gsm48_hdr *gh = msgb_l3(msg);
987 uint8_t cause;
988 uint8_t auts_tag;
989 uint8_t auts_len;
990 uint8_t *auts;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100991 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100992
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100993 if (!vsub) {
994 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
995 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100996 return -EINVAL;
997 }
998
999 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001000 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION FAILURE: l3 length invalid: %u\n",
1001 msgb_l3len(msg));
1002 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001003 return -EINVAL;
1004 }
1005
1006 cause = gh->data[0];
1007
1008 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001009 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO, "MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n", cause);
1010 vlr_subscr_rx_auth_fail(vsub, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 }
1013
1014 /* This is a Synch Failure procedure, which should pass an AUTS to
1015 * resynchronize the sequence nr with the HLR. Expecting exactly one
1016 * TLV with 14 bytes of AUTS. */
1017
1018 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001019 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1020 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure: missing AUTS IE\n");
1021 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001022 return -EINVAL;
1023 }
1024
1025 auts_tag = gh->data[1];
1026 auts_len = gh->data[2];
1027 auts = &gh->data[3];
1028
1029 if (auts_tag != GSM48_IE_AUTS
1030 || auts_len != 14) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001031 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1032 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure:"
1033 " expected AUTS IE 0x%02x of 14 bytes,"
1034 " got IE 0x%02x of %u bytes\n",
1035 GSM48_IE_AUTS, auts_tag, auts_len);
1036 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001037 return -EINVAL;
1038 }
1039
1040 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001041 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1042 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure msg: message truncated (%u)\n",
1043 msgb_l3len(msg));
1044 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001045 return -EINVAL;
1046 }
1047
1048 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1049 * large enough. */
1050
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001051 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
1052 osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001053
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001054 return vlr_subscr_rx_auth_fail(vsub, auts);
Harald Welte2483f1b2016-06-19 18:06:02 +02001055}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001056
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001057static int gsm48_rx_mm_tmsi_reall_compl(struct msc_a *msc_a)
Harald Welte2483f1b2016-06-19 18:06:02 +02001058{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001059 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1060 if (!vsub) {
1061 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001062 return -EINVAL;
1063 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001064 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "TMSI Reallocation Completed\n");
1065 return vlr_subscr_rx_tmsi_reall_compl(vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001066}
1067
Harald Weltebf5e8df2009-02-03 12:59:45 +00001068/* Receive a GSM 04.08 Mobility Management (MM) message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001069int gsm0408_rcv_mm(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001070{
1071 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001072 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001073
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001074 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001075 case GSM48_MT_MM_LOC_UPD_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 rc = mm_rx_loc_upd_req(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001077 break;
1078 case GSM48_MT_MM_ID_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001079 rc = mm_rx_id_resp(msc_a, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001080 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001081 case GSM48_MT_MM_CM_SERV_REQ:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001082 rc = gsm48_rx_mm_serv_req(msc_a, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001083 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001084 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001085 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001086 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001087 case GSM48_MT_MM_TMSI_REALL_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001088 rc = gsm48_rx_mm_tmsi_reall_compl(msc_a);
Harald Welte69b2af22009-01-06 19:47:00 +00001089 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001090 case GSM48_MT_MM_IMSI_DETACH_IND:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001091 rc = gsm48_rx_mm_imsi_detach_ind(msc_a, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001092 break;
1093 case GSM48_MT_MM_CM_REEST_REQ:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001094 rc = gsm48_rx_cm_reest_req(msc_a, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001095 break;
1096 case GSM48_MT_MM_AUTH_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001097 rc = gsm48_rx_mm_auth_resp(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001098 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001099 case GSM48_MT_MM_AUTH_FAIL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001100 rc = gsm48_rx_mm_auth_fail(msc_a, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001101 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001102 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001103 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001104 gh->msg_type);
1105 break;
1106 }
1107
1108 return rc;
1109}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001110
Harald Welte2d35ae62009-02-06 12:02:13 +00001111/* Receive a PAGING RESPONSE message from the MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001112static int gsm48_rx_rr_pag_resp(struct msc_a *msc_a, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001113{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001114 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte2d35ae62009-02-06 12:02:13 +00001115 struct gsm48_hdr *gh = msgb_l3(msg);
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001116 struct gsm48_pag_resp *pr =
1117 (struct gsm48_pag_resp *)gh->data;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001118 uint8_t classmark2_len = gh->data[1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001119 uint8_t *classmark2_buf = gh->data+2;
1120 struct gsm48_classmark2 *cm2 = (void*)classmark2_buf;
1121 uint8_t *mi_lv = classmark2_buf + classmark2_len;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001122 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001123 struct vlr_subscr *vsub;
Harald Welte2483f1b2016-06-19 18:06:02 +02001124
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001125 if (msc_a_is_establishing_auth_ciph(msc_a)) {
1126 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
1127 "Ignoring Paging Response, conn already busy establishing authenticity\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001128 return 0;
1129 }
1130
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001131 if (msc_a_is_accepted(msc_a)) {
1132 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001133 return 0;
1134 }
1135
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001136 msc_a->complete_layer3_type = COMPLETE_LAYER3_PAGING_RESP;
1137 msub_update_id_from_mi(msc_a->c.msub, mi_lv + 1, *mi_lv);
1138 LOG_MSC_A_CAT(msc_a, DRR, LOGL_DEBUG, "Rx PAGING RESPONSE\n");
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001139
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001140 msc_a_get(msc_a, MSC_A_USE_PAGING_RESPONSE);
1141
1142 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
1143 vlr_proc_acc_req(msc_a->c.fi,
1144 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
1145 net->vlr, msc_a,
1146 VLR_PR_ARQ_T_PAGING_RESP, 0, mi_lv, &msc_a->via_cell.lai,
1147 is_utran || net->authentication_required,
1148 is_utran || net->a5_encryption_mask > 0x01,
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001149 pr->key_seq,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001150 osmo_gsm48_classmark2_is_r99(cm2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001151 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001152
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001153 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
1154 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001155 vsub = msc_a_vsub(msc_a);
1156 if (!vsub) {
1157 LOG_MSC_A(msc_a, LOGL_ERROR, "subscriber not allowed to do a Paging Response\n");
1158 msc_a_put(msc_a, MSC_A_USE_PAGING_RESPONSE);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001159 return -EIO;
1160 }
1161
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001162 vsub->classmark.classmark2 = *cm2;
1163 vsub->classmark.classmark2_len = classmark2_len;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001164 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001165}
1166
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001167static int gsm48_rx_rr_ciphering_mode_complete(struct msc_a *msc_a, struct msgb *msg)
1168{
1169 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1170 struct gsm48_hdr *gh = msgb_l3(msg);
1171 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1172 struct tlv_parsed tp;
1173 struct tlv_p_entry *mi;
1174
1175 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1176 mi = TLVP_GET(&tp, GSM48_IE_MOBILE_ID);
1177
1178 if (!mi)
1179 return 0;
1180
1181 LOG_MSC_A(msc_a, LOGL_DEBUG, "Ciphering Mode Complete contains Mobile Identity: %s\n",
1182 osmo_mi_name(mi->val, mi->len));
1183
1184 if (!vsub)
1185 return 0;
1186
1187 return vlr_subscr_rx_id_resp(vsub, mi->val, mi->len);
1188}
1189
1190static int gsm48_rx_rr_app_info(struct msc_a *msc_a, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001191{
1192 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001193 uint8_t apdu_id_flags;
1194 uint8_t apdu_len;
1195 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001196
1197 apdu_id_flags = gh->data[0];
1198 apdu_len = gh->data[1];
1199 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001200
Max8dc8f232017-02-09 19:13:02 +01001201 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001202 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001203
Harald Welte2483f1b2016-06-19 18:06:02 +02001204 /* we're not using the app info blob anywhere, so ignore. */
1205#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001206 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001207#else
1208 return 0;
1209#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210}
1211
Harald Weltebf5e8df2009-02-03 12:59:45 +00001212/* Receive a GSM 04.08 Radio Resource (RR) message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001213int gsm0408_rcv_rr(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001214{
1215 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001216 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001217
1218 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001219 case GSM48_MT_RR_PAG_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001220 rc = gsm48_rx_rr_pag_resp(msc_a, msg);
1221 break;
1222 case GSM48_MT_RR_CIPH_M_COMPL:
1223 rc = gsm48_rx_rr_ciphering_mode_complete(msc_a, 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:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001226 rc = gsm48_rx_rr_app_info(msc_a, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001228 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001229 LOG_MSC_A_CAT(msc_a, 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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001237int gsm48_send_rr_app_info(struct msc_a *msc_a, uint8_t apdu_id,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001238 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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001253 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254}
1255
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001256extern int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001257
Harald Welte2483f1b2016-06-19 18:06:02 +02001258/***********************************************************************
1259 * VLR integration
1260 ***********************************************************************/
1261
1262/* VLR asks us to send an authentication request */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001263static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Harald Welte2483f1b2016-06-19 18:06:02 +02001264 bool send_autn)
1265{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001266 struct msc_a *msc_a = msc_conn_ref;
1267 return gsm48_tx_mm_auth_req(msc_a, at->vec.rand,
Harald Welte2483f1b2016-06-19 18:06:02 +02001268 send_autn? at->vec.autn : NULL,
1269 at->key_seq);
1270}
1271
1272/* VLR asks us to send an authentication reject */
1273static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1274{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001275 struct msc_a *msc_a = msc_conn_ref;
1276 return gsm48_tx_mm_auth_rej(msc_a);
Harald Welte2483f1b2016-06-19 18:06:02 +02001277}
1278
1279/* VLR asks us to transmit an Identity Request of given type */
1280static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1281{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001282 struct msc_a *msc_a = msc_conn_ref;
1283 return mm_tx_identity_req(msc_a, mi_type);
Harald Welte2483f1b2016-06-19 18:06:02 +02001284}
1285
1286/* VLR asks us to transmit a Location Update Accept */
1287static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1288{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001289 struct msc_a *msc_a = msc_conn_ref;
1290 return gsm0408_loc_upd_acc(msc_a, send_tmsi);
Harald Welte2483f1b2016-06-19 18:06:02 +02001291}
1292
1293/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001294static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001295{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001296 struct msc_a *msc_a = msc_conn_ref;
1297 return gsm0408_loc_upd_rej(msc_a, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02001298}
1299
1300/* VLR asks us to transmit a CM Service Accept */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001301int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type)
Harald Welte2483f1b2016-06-19 18:06:02 +02001302{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001303 struct msc_a *msc_a = msc_conn_ref;
1304 return msc_gsm48_tx_mm_serv_ack(msc_a);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001305}
1306
1307static int msc_vlr_tx_common_id(void *msc_conn_ref)
1308{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001309 struct msc_a *msc_a = msc_conn_ref;
1310 struct ran_msg msg = {
1311 .msg_type = RAN_MSG_COMMON_ID,
1312 .common_id = {
1313 .imsi = msc_a_vsub(msc_a)->imsi,
1314 },
1315 };
1316 return msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Harald Welte2483f1b2016-06-19 18:06:02 +02001317}
1318
Stefan Sperling3a741282018-03-13 21:11:49 +01001319/* VLR asks us to transmit MM info. */
1320static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1321{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001322 struct msc_a *msc_a = msc_conn_ref;
1323 struct gsm_network *net = msc_a_net(msc_a);
1324 if (!net->send_mm_info)
Stefan Sperling3a741282018-03-13 21:11:49 +01001325 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001326 return gsm48_tx_mm_info(msc_a);
Stefan Sperling3a741282018-03-13 21:11:49 +01001327}
1328
Harald Welte2483f1b2016-06-19 18:06:02 +02001329/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001330static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
1331 enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001332{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333 struct msc_a *msc_a = msc_conn_ref;
1334 msc_gsm48_tx_mm_serv_rej(msc_a, cause);
1335 msc_a_put(msc_a, msc_a_cm_service_type_to_use(cm_service_type));
1336 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001337}
1338
1339/* VLR informs us that the subscriber data has somehow been modified */
1340static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1341{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001342 struct msub *msub = msub_for_vsub(subscr);
1343 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s)%s\n",
1344 subscr->imsi, subscr->msisdn, msub ? "" : " (NO CONN!)");
1345 msub_update_id(msub);
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001346}
1347
Harald Welte2483f1b2016-06-19 18:06:02 +02001348/* VLR informs us that the subscriber has been associated with a conn */
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001349static int msc_vlr_subscr_assoc(void *msc_conn_ref,
Harald Welte2483f1b2016-06-19 18:06:02 +02001350 struct vlr_subscr *vsub)
1351{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001352 struct msc_a *msc_a = msc_conn_ref;
1353 struct msub *msub = msc_a->c.msub;
Max48d4ec02018-02-12 16:51:03 +01001354 OSMO_ASSERT(vsub);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001356 if (msub_set_vsub(msub, vsub))
1357 return -EINVAL;
1358 vsub->cs.attached_via_ran = msc_a->c.ran->type;
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001359
1360 /* In case we have already received Classmark Information before the VLR Subscriber was
1361 * associated with the conn: merge the new Classmark into vsub->classmark. Don't overwrite valid
1362 * vsub->classmark with unset classmark, though. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001363 osmo_gsm48_classmark_update(&vsub->classmark, &msc_a->temporary_classmark);
1364
1365 msub_update_id(msub);
1366
1367 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_COMPLETE_LAYER_3_OK, NULL);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001368 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001369}
1370
1371/* operations that we need to implement for libvlr */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001372const struct vlr_ops msc_vlr_ops = {
Harald Welte2483f1b2016-06-19 18:06:02 +02001373 .tx_auth_req = msc_vlr_tx_auth_req,
1374 .tx_auth_rej = msc_vlr_tx_auth_rej,
1375 .tx_id_req = msc_vlr_tx_id_req,
1376 .tx_lu_acc = msc_vlr_tx_lu_acc,
1377 .tx_lu_rej = msc_vlr_tx_lu_rej,
1378 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1379 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001380 .set_ciph_mode = msc_a_vlr_set_cipher_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001381 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001382 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001383 .subscr_update = msc_vlr_subscr_update,
1384 .subscr_assoc = msc_vlr_subscr_assoc,
1385};
1386
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001387struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1388{
1389 struct msgb *msg;
1390 struct gsm48_hdr *gh;
1391
1392 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1393 if (!msg)
1394 return NULL;
1395
1396 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1397 gh->proto_discr = GSM48_PDISC_MM;
1398 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1399 gh->data[0] = value;
1400
1401 return msg;
1402}
1403
1404struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1405{
1406 struct gsm48_hdr *gh;
1407 struct msgb *msg;
1408
1409 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1410 if (!msg)
1411 return NULL;
1412
1413 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1414 gh->proto_discr = GSM48_PDISC_MM;
1415 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1416 gh->data[0] = cause;
1417 return msg;
1418}
1419
1420int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
1421{
1422 /* Check the size for the classmark */
1423 if (length < 1 + *classmark2_lv)
1424 return -1;
1425
1426 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1427 if (length < 2 + *classmark2_lv + mi_lv[0])
1428 return -2;
1429
1430 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
1431 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
1432}
1433
1434int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
1435 char *mi_string, uint8_t *mi_type)
1436{
1437 static const uint32_t classmark_offset =
1438 offsetof(struct gsm48_pag_resp, classmark2);
1439 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
1440 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
1441 mi_string, mi_type);
1442}