blob: fc82d780ab1a837f7043d58bd2e9557f2828f116 [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 <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000033
Neels Hofmeyr4ac80092019-03-04 02:46:37 +010034#include "config.h"
Harald Welte9c3dc902012-04-08 16:59:24 +020035
Neels Hofmeyr90843962017-09-04 15:04:35 +020036#include <osmocom/msc/debug.h>
37#include <osmocom/msc/gsm_data.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020038#include <osmocom/msc/gsm_04_08.h>
Andreas Eversberg06497192023-06-01 12:18:24 +020039#include <osmocom/msc/msc_vgcs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020040#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/transaction.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020042#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010043#include <osmocom/msc/msc_a.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080044
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010045#include <osmocom/gsm/gsm48.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010046#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020047#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010048#include <osmocom/core/msgb.h>
49#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010050#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010051#include <osmocom/core/byteswap.h>
Neels Hofmeyrae98b972021-07-27 03:46:49 +020052#include <osmocom/core/fsm.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010053#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020054#include <osmocom/crypt/auth.h>
Alexander Couzens8b7d7852021-11-05 01:52:05 +010055#include <osmocom/crypt/utran_cipher.h>
Harald Welte52b1f982008-12-23 20:25:15 +000056
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010057#include <osmocom/msc/msub.h>
58#include <osmocom/msc/msc_roles.h>
Neels Hofmeyrae98b972021-07-27 03:46:49 +020059#include <osmocom/msc/call_leg.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020060
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020061#include <assert.h>
62
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020063
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020064void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010065void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020066
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010067static int gsm0408_loc_upd_acc(struct msc_a *msc_a, uint32_t send_tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000068
Harald Welte27989d42018-06-21 20:39:20 +020069/*! Send a simple GSM 04.08 message without any payload
Neels Hofmeyrc036b792018-11-29 22:37:51 +010070 * \param conn Active RAN connection
Harald Welte27989d42018-06-21 20:39:20 +020071 * \param[in] pdisc Protocol discriminator
72 * \param[in] msg_type Message type
73 * \return result of \ref gsm48_conn_sendmsg
74 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075int gsm48_tx_simple(struct msc_a *msc_a, uint8_t pdisc, uint8_t msg_type)
Harald Welte27989d42018-06-21 20:39:20 +020076{
77 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
79
80 gh->proto_discr = pdisc;
81 gh->msg_type = msg_type;
82
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010083 return msc_a_tx_dtap_to_i(msc_a, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080084}
Sylvain Munaut30a15382009-12-24 00:27:26 +010085
Harald Welte2483f1b2016-06-19 18:06:02 +020086/* clear all transactions globally; used in case of MNCC socket disconnect */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010087void gsm0408_clear_all_trans(struct gsm_network *net, enum trans_type type)
Harald Welte371efe52010-12-22 23:17:50 +010088{
89 struct gsm_trans *trans, *temp;
90
91 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
92
93 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010094 if (trans->type == type) {
Harald Welteeb76c7a2010-12-23 02:47:53 +010095 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +010096 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +010097 }
Harald Welte371efe52010-12-22 23:17:50 +010098 }
99}
100
Holger Freyther429e7762008-12-30 13:28:30 +0000101/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100102static int gsm0408_loc_upd_rej(struct msc_a *msc_a, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000103{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800104 struct msgb *msg;
105
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800106 msg = gsm48_create_loc_upd_rej(cause);
107 if (!msg) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100108 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 +0800109 return -1;
110 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200111
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100112 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO, "LOCATION UPDATING REJECT\n");
Harald Welte24ff6ee2009-12-22 00:41:05 +0100113
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100114 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000115}
116
117/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100118static int gsm0408_loc_upd_acc(struct msc_a *msc_a, uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000119{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100120 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000121 struct gsm48_hdr *gh;
122 struct gsm48_loc_area_id *lai;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100123 struct gsm_network *net = msc_a_net(msc_a);
124 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100125 struct osmo_location_area_id laid = {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100126 .plmn = net->plmn,
127 .lac = vsub->cgi.lai.lac,
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100128 };
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200129 uint8_t *l;
130 int rc;
131 struct osmo_mobile_identity mi = {};
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200132
Harald Welte52b1f982008-12-23 20:25:15 +0000133 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
134 gh->proto_discr = GSM48_PDISC_MM;
135 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
136
137 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100138 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000139
Harald Welte2483f1b2016-06-19 18:06:02 +0200140 if (send_tmsi == GSM_RESERVED_TMSI) {
141 /* we did not allocate a TMSI to the MS, so we need to
142 * include the IMSI in order for the MS to delete any
143 * old TMSI that might still be allocated */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200144 mi.type = GSM_MI_TYPE_IMSI;
145 OSMO_STRLCPY_ARRAY(mi.imsi, vsub->imsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200146 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100147 vlr_subscr_name(vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200148 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200149 /* Include the TMSI, which means that the MS will send a
150 * TMSI REALLOCATION COMPLETE, and we should wait for
151 * that until T3250 expiration */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200152 mi.type = GSM_MI_TYPE_TMSI;
153 mi.tmsi = send_tmsi;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200154 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100155 vlr_subscr_name(vsub),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200156 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200157 }
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200158 l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);
159 rc = osmo_mobile_identity_encode_msgb(msg, &mi, false);
160 if (rc < 0) {
161 msgb_free(msg);
162 return -EINVAL;
163 }
164 *l = rc;
165
Harald Welte2483f1b2016-06-19 18:06:02 +0200166 /* TODO: Follow-on proceed */
167 /* TODO: CTS permission */
168 /* TODO: Equivalent PLMNs */
169 /* TODO: Emergency Number List */
170 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000171
Harald Welte52b1f982008-12-23 20:25:15 +0000172
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100173 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000174}
175
Harald Weltebf5e8df2009-02-03 12:59:45 +0000176/* Transmit Chapter 9.2.10 Identity Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100177static int mm_tx_identity_req(struct msc_a *msc_a, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000178{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100179 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000180 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000181
Harald Welte231ad4f2008-12-27 11:15:38 +0000182 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
183 gh->proto_discr = GSM48_PDISC_MM;
184 gh->msg_type = GSM48_MT_MM_ID_REQ;
185 gh->data[0] = id_type;
186
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100187 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000188}
189
Harald Weltebf5e8df2009-02-03 12:59:45 +0000190/* Parse Chapter 9.2.11 Identity Response */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100191static int mm_rx_id_resp(struct msc_a *msc_a, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000192{
193 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200194 uint8_t *mi_data = gh->data+1;
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100195 uint8_t mi_len = gh->data[0];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100196 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200197 struct osmo_mobile_identity mi;
198 int rc;
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
Vadim Yanitskiya79aac02019-12-28 01:19:19 +0100206 /* There muct be at least one octet with MI type */
207 if (!mi_len) {
208 LOGP(DMM, LOGL_NOTICE, "MM Identity Response contains "
209 "malformed Mobile Identity\n");
210 return -EINVAL;
211 }
212
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200213 rc = osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
214 if (rc) {
215 LOGP(DMM, LOGL_ERROR, "Failure to decode Mobile Identity in MM Identity Response (rc=%d)\n", rc);
Vadim Yanitskiya79aac02019-12-28 01:19:19 +0100216 return -EINVAL;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200217 }
218
219 /* Make sure we got what we expected */
220 if (mi.type != msc_a->mm_id_req_type) {
Vadim Yanitskiya79aac02019-12-28 01:19:19 +0100221 LOGP(DMM, LOGL_NOTICE, "MM Identity Response contains unexpected "
222 "Mobile Identity type %s (extected %s)\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200223 gsm48_mi_type_name(mi.type),
Vadim Yanitskiya79aac02019-12-28 01:19:19 +0100224 gsm48_mi_type_name(msc_a->mm_id_req_type));
225 return -EINVAL;
226 }
227
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200228 DEBUGP(DMM, "IDENTITY RESPONSE: %s\n", osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte231ad4f2008-12-27 11:15:38 +0000229
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200230 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100231
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200232 return vlr_subscr_rx_id_resp(vsub, &mi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100233}
234
235/* 9.2.5 CM service accept */
236static int msc_gsm48_tx_mm_serv_ack(struct msc_a *msc_a)
237{
238 struct msgb *msg;
239 struct gsm48_hdr *gh;
240
241 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV ACC");
242
243 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
244 gh->proto_discr = GSM48_PDISC_MM;
245 gh->msg_type = GSM48_MT_MM_CM_SERV_ACC;
246
247 return msc_a_tx_dtap_to_i(msc_a, msg);
248}
249
Neels Hofmeyr5d53c602022-04-24 23:37:07 +0200250/* 9.2.6 CM service reject.
251 * For an active and valid CM Service Request, instead use msc_vlr_tx_cm_serv_rej(), which also takes care of
252 * decrementing the use token for that service type. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100253static int msc_gsm48_tx_mm_serv_rej(struct msc_a *msc_a,
254 enum gsm48_reject_value value)
255{
256 struct msgb *msg;
257
258 msg = gsm48_create_mm_serv_rej(value);
259 if (!msg) {
260 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
261 return -1;
262 }
263
264 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "-> CM SERVICE Reject cause: %d\n", value);
265
266 return msc_a_tx_dtap_to_i(msc_a, msg);
267}
268
269
270/* Extract the R99 flag from various Complete Level 3 messages. Depending on the Message Type, extract R99 from the
271 * Classmark Type 1 or the Classmark Type 2. Return 1 for R99, 0 for pre-R99, negative if not a valid Complete Level 3
272 * message. */
273int compl_l3_msg_is_r99(const struct msgb *msg)
274{
275 const struct gsm48_hdr *gh = msgb_l3(msg);
276 uint8_t pdisc = gsm48_hdr_pdisc(gh);
277 const struct gsm48_loc_upd_req *lu;
278 const struct gsm48_imsi_detach_ind *idi;
279 uint8_t cm2_len;
280 const struct gsm48_classmark2 *cm2;
281
282 switch (pdisc) {
283 case GSM48_PDISC_MM:
284 switch (gsm48_hdr_msg_type(gh)) {
285 case GSM48_MT_MM_LOC_UPD_REQUEST:
286 lu = (const struct gsm48_loc_upd_req *) gh->data;
287 return osmo_gsm48_classmark1_is_r99(&lu->classmark1) ? 1 : 0;
288
289 case GSM48_MT_MM_CM_SERV_REQ:
290 case GSM48_MT_MM_CM_REEST_REQ:
291 break;
292
293 case GSM48_MT_MM_IMSI_DETACH_IND:
294 idi = (const struct gsm48_imsi_detach_ind *) gh->data;
295 return osmo_gsm48_classmark1_is_r99(&idi->classmark1) ? 1 : 0;
296
297 default:
298 return -1;
299 }
300 break;
301
302 case GSM48_PDISC_RR:
303 switch (gsm48_hdr_msg_type(gh)) {
304 case GSM48_MT_RR_PAG_RESP:
305 break;
306
307 default:
308 return -1;
309 }
310 break;
311
312 default:
313 return -1;
314 }
315
316 /* Both CM Service Request and Paging Response have Classmark Type 2 at the same location: */
317 cm2_len = gh->data[1];
318 cm2 = (void*)gh->data+2;
319 return osmo_gsm48_classmark2_is_r99(cm2, cm2_len) ? 1 : 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000320}
321
Harald Welte2483f1b2016-06-19 18:06:02 +0200322/* Chapter 9.2.15: Receive Location Updating Request.
323 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100324static int mm_rx_loc_upd_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000325{
Harald Welte8470bf22008-12-25 23:28:35 +0000326 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000327 struct gsm48_loc_upd_req *lu;
Harald Welte2483f1b2016-06-19 18:06:02 +0200328 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200329 uint32_t tmsi;
330 char *imsi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100331 struct osmo_location_area_id old_lai;
Harald Welte2483f1b2016-06-19 18:06:02 +0200332 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200333 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334 struct gsm_network *net = msc_a_net(msc_a);
335 struct vlr_subscr *vsub;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200336 struct osmo_mobile_identity mi;
337 int rc;
338
339 rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false);
340 if (rc) {
341 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
342 "Failed to decode Mobile Identity in Location Updating Request\n");
343 return -EINVAL;
344 }
Harald Welte52b1f982008-12-23 20:25:15 +0000345
Harald Welte8470bf22008-12-25 23:28:35 +0000346 lu = (struct gsm48_loc_upd_req *) gh->data;
347
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100348 if (msc_a_is_establishing_auth_ciph(msc_a)) {
349 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
350 "Cannot accept another LU, conn already busy establishing authenticity;"
351 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200352 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200353 return -EINVAL;
354 }
355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100356 if (msc_a_is_accepted(msc_a)) {
357 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
358 "Cannot accept another LU, conn already established;"
359 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200360 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200361 return -EINVAL;
362 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200363
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100364 msc_a->complete_layer3_type = COMPLETE_LAYER3_LU;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200365
366 msub_update_id_from_mi(msc_a->c.msub, &mi);
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200367
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100368 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200369 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi), osmo_lu_type_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000370
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200371 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &mi);
Harald Welte7659de12009-12-13 12:39:18 +0100372
Harald Welte24ff6ee2009-12-22 00:41:05 +0100373 switch (lu->type) {
374 case GSM48_LUPD_NORMAL:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200375 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_LOC_UPDATE_TYPE_NORMAL));
Harald Welte2483f1b2016-06-19 18:06:02 +0200376 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100377 break;
378 case GSM48_LUPD_IMSI_ATT:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200379 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_LOC_UPDATE_TYPE_ATTACH));
Harald Welte2483f1b2016-06-19 18:06:02 +0200380 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100381 break;
382 case GSM48_LUPD_PERIODIC:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200383 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_LOC_UPDATE_TYPE_PERIODIC));
Harald Welte2483f1b2016-06-19 18:06:02 +0200384 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100385 break;
386 }
387
Harald Welte2483f1b2016-06-19 18:06:02 +0200388 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
Martin Hauke3f07dac2019-11-14 17:49:08 +0100389 /* TODO: 10.5.3.14 Additional update parameters (CS fallback calls) */
Harald Welte2483f1b2016-06-19 18:06:02 +0200390 /* TODO: 10.5.7.8 Device properties */
391 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200392
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200393 switch (mi.type) {
Harald Welte52b1f982008-12-23 20:25:15 +0000394 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200395 tmsi = GSM_RESERVED_TMSI;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200396 imsi = mi.imsi;
Harald Welte4b634542008-12-27 01:55:51 +0000397 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000398 case GSM_MI_TYPE_TMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200399 tmsi = mi.tmsi;
Harald Welte2483f1b2016-06-19 18:06:02 +0200400 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000401 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200402 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100403 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200404 tmsi = GSM_RESERVED_TMSI;
405 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000406 break;
407 }
408
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100409 gsm48_decode_lai2(&lu->lai, &old_lai);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100410 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 +0200411
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100412 msc_a_get(msc_a, __func__);
413 msc_a_get(msc_a, MSC_A_USE_LOCATION_UPDATING);
414
415 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
416 lu_fsm = vlr_loc_update(msc_a->c.fi,
417 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
418 net->vlr, msc_a, vlr_lu_type, tmsi, imsi,
419 &old_lai, &msc_a->via_cell.lai,
420 is_utran || net->authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200421 msc_a_is_ciphering_to_be_attempted(msc_a),
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200422 msc_a_is_ciphering_required(msc_a),
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100423 lu->key_seq,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100424 osmo_gsm48_classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200425 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200426 net->vlr->cfg.assign_tmsi);
427 if (!lu_fsm) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100428 LOG_MSC_A(msc_a, LOGL_ERROR, "Can't start LU FSM\n");
429 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
430 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200431 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432 }
433
Harald Welte2483f1b2016-06-19 18:06:02 +0200434 /* From vlr_loc_update() we expect an implicit dispatch of
435 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
Thorsten Alteholz3a357de2019-07-16 20:51:16 +0200436 * already have been called and completed. Has an error occurred? */
Harald Welte255539c2008-12-28 02:26:27 +0000437
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100438 vsub = msc_a_vsub(msc_a);
439 if (!vsub) {
440 msc_a_put(msc_a, __func__);
441 return 0;
442 }
443
444 if (vsub->lu_fsm != lu_fsm) {
445 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal Error during Location Updating attempt\n");
446 msc_a_release_cn(msc_a);
447 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200448 return -EIO;
449 }
450
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100451 vsub->classmark.classmark1 = lu->classmark1;
452 vsub->classmark.classmark1_set = true;
453 msc_a_put(msc_a, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +0200454 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000455}
456
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100457/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200458/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100459static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800460{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100461 uint8_t ret;
462
463 ret = value / 10;
464 ret |= (value % 10) << 4;
465
466 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800467}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100468
Harald Welte0df904d2018-12-03 11:00:04 +0100469/* Generate a message buffer that contains a valid MM info message,
470 * See also 3GPP TS 24.008, chapter 9.2.15a */
471struct msgb *gsm48_create_mm_info(struct gsm_network *net)
Harald Weltedb253af2008-12-30 17:56:55 +0000472{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100473 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000474 struct gsm48_hdr *gh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200475 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200476 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100477
Harald Welte4bfdfe72009-06-10 23:11:52 +0800478 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100479 struct tm* gmt_time;
480 struct tm* local_time;
481 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100482 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000483
Harald Weltedb253af2008-12-30 17:56:55 +0000484 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
485 gh->proto_discr = GSM48_PDISC_MM;
486 gh->msg_type = GSM48_MT_MM_INFO;
487
488 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200489#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000490 name_len = strlen(net->name_long);
491 /* 10.5.3.5a */
492 ptr8 = msgb_put(msg, 3);
493 ptr8[0] = GSM48_IE_NAME_LONG;
494 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_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000500
501 /* FIXME: Use Cell Broadcast, not UCS-2, since
502 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200503#endif
504 name_len = (strlen(net->name_long)*7)/8;
505 name_pad = (8 - strlen(net->name_long)*7)%8;
506 if (name_pad > 0)
507 name_len++;
508 /* 10.5.3.5a */
509 ptr8 = msgb_put(msg, 3);
510 ptr8[0] = GSM48_IE_NAME_LONG;
511 ptr8[1] = name_len +1;
512 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
513
514 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100515 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200516
Harald Weltedb253af2008-12-30 17:56:55 +0000517 }
518
519 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200520#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000521 name_len = strlen(net->name_short);
522 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200523 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200524 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000525 ptr8[1] = name_len*2 + 1;
526 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
527
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200528 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000529 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000530 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200531#endif
532 name_len = (strlen(net->name_short)*7)/8;
533 name_pad = (8 - strlen(net->name_short)*7)%8;
534 if (name_pad > 0)
535 name_len++;
536 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200537 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200538 ptr8[0] = GSM48_IE_NAME_SHORT;
539 ptr8[1] = name_len +1;
540 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
541
542 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100543 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200544
Harald Weltedb253af2008-12-30 17:56:55 +0000545 }
546
Harald Weltedb253af2008-12-30 17:56:55 +0000547 /* Section 10.5.3.9 */
548 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100549 gmt_time = gmtime(&cur_t);
550
Harald Weltedb253af2008-12-30 17:56:55 +0000551 ptr8 = msgb_put(msg, 8);
552 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100553 ptr8[1] = bcdify(gmt_time->tm_year % 100);
554 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
555 ptr8[3] = bcdify(gmt_time->tm_mday);
556 ptr8[4] = bcdify(gmt_time->tm_hour);
557 ptr8[5] = bcdify(gmt_time->tm_min);
558 ptr8[6] = bcdify(gmt_time->tm_sec);
559
Neels Hofmeyr73983952016-05-10 13:29:33 +0200560 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200561 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200562 if (net->tz.hr < 0) {
563 tzunits = ((net->tz.hr/-1)*4);
564 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100565 ptr8[7] = bcdify(tzunits);
566 /* Set negative time */
567 ptr8[7] |= 0x08;
568 }
569 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200570 tzunits = net->tz.hr*4;
571 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100572 ptr8[7] = bcdify(tzunits);
573 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100574 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200575 if (net->tz.dst >= 0 && net->tz.dst <= 2)
576 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100577 }
578 else {
579 /* Need to get GSM offset and convert into 15 min units */
580 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200581#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200582 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100583 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200584#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200585 /* find timezone offset */
586 time_t utc;
587 double offsetFromUTC;
588 utc = mktime(gmt_time);
589 local_time = localtime(&cur_t);
590 offsetFromUTC = difftime(cur_t, utc);
591 if (local_time->tm_isdst)
592 offsetFromUTC += 3600.0;
593 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200594#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100595 if (tzunits < 0) {
596 tzunits = tzunits/-1;
597 ptr8[7] = bcdify(tzunits);
598 /* Flip it to negative */
599 ptr8[7] |= 0x08;
600 }
601 else
602 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100603
604 /* Does not support DST +2 */
605 if (local_time->tm_isdst)
606 dst = 1;
607 }
608
609 if (dst) {
610 ptr8 = msgb_put(msg, 3);
611 ptr8[0] = GSM48_IE_NET_DST;
612 ptr8[1] = 1;
613 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100614 }
Harald Weltedb253af2008-12-30 17:56:55 +0000615
Harald Welte0df904d2018-12-03 11:00:04 +0100616 return msg;
617}
Daniel Willmanneea93372009-08-13 03:42:07 +0200618
Harald Welte0df904d2018-12-03 11:00:04 +0100619/* Section 9.2.15a */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100620int gsm48_tx_mm_info(struct msc_a *msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100621{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100622 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +0100623 struct msgb *msg;
624
625 msg = gsm48_create_mm_info(net);
626
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100627 LOG_MSC_A(msc_a, LOGL_DEBUG, "Tx MM INFO\n");
628 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Weltedb253af2008-12-30 17:56:55 +0000629}
630
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100631/*! Send an Authentication Request to MS on the given RAN connection
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100632 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
633 * \param[in] conn Subscriber connection to send on.
634 * \param[in] rand Random challenge token to send, must be 16 bytes long.
635 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
636 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
637 * \param[in] key_seq auth tuple's sequence number.
638 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100639int gsm48_tx_mm_auth_req(struct msc_a *msc_a, uint8_t *rand,
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100640 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200641{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100642 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200644 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200645
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100646 DEBUGP(DMM, "Tx AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100647 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100648 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200649
Harald Welte7984d5c2009-08-12 22:56:50 +0200650 gh->proto_discr = GSM48_PDISC_MM;
651 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
652
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100653 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200654
Harald Welte7984d5c2009-08-12 22:56:50 +0200655 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100656 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200657 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200658 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200659
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100660
661 /* 16 bytes AUTN */
662 if (autn)
663 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
664
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100665 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte7984d5c2009-08-12 22:56:50 +0200666}
667
668/* Section 9.2.1 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100669int gsm48_tx_mm_auth_rej(struct msc_a *msc_a)
Harald Welte7984d5c2009-08-12 22:56:50 +0200670{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100671 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "-> AUTH REJECT\n");
672 return gsm48_tx_simple(msc_a, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200673}
674
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100675static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
676 enum gsm48_reject_value cause);
Harald Welte2483f1b2016-06-19 18:06:02 +0200677
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200678static int cm_serv_reuse_conn(struct msc_a *msc_a, const struct osmo_mobile_identity *mi, enum osmo_cm_service_type cm_serv_type)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100679{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100680 struct gsm_network *net = msc_a_net(msc_a);
681 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100682
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200683 switch (mi->type) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200684 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200685 if (vlr_subscr_matches_imsi(vsub, mi->imsi))
Harald Welte2483f1b2016-06-19 18:06:02 +0200686 goto accept_reuse;
687 break;
688 case GSM_MI_TYPE_TMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200689 if (vlr_subscr_matches_tmsi(vsub, mi->tmsi))
Harald Welte2483f1b2016-06-19 18:06:02 +0200690 goto accept_reuse;
691 break;
692 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200693 if (vlr_subscr_matches_imei(vsub, mi->imei))
Harald Welte2483f1b2016-06-19 18:06:02 +0200694 goto accept_reuse;
695 break;
696 default:
697 break;
698 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100699
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200700 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "CM Service Request with mismatching mobile identity: %s\n",
701 osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100702 msc_vlr_tx_cm_serv_rej(msc_a, cm_serv_type, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200703 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100704
Harald Welte2483f1b2016-06-19 18:06:02 +0200705accept_reuse:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "re-using already accepted connection\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200707
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100708 msub_update_id(msc_a->c.msub);
709 return net->vlr->ops.tx_cm_serv_acc(msc_a, cm_serv_type);
Sylvain Munautba87f452009-12-24 00:28:46 +0100710}
711
Harald Welte4ed0e922009-01-10 03:17:30 +0000712/*
713 * Handle CM Service Requests
714 * a) Verify that the packet is long enough to contain the information
Martin Hauke3f07dac2019-11-14 17:49:08 +0100715 * we require otherwise reject with INCORRECT_MESSAGE
Harald Welte4ed0e922009-01-10 03:17:30 +0000716 * b) Try to parse the TMSI. If we do not have one reject
717 * c) Check that we know the subscriber with the TMSI otherwise reject
718 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200719 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200720 *
721 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000722 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100723int gsm48_rx_mm_serv_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000724{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100725 struct gsm_network *net = msc_a_net(msc_a);
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700726 struct gsm48_hdr *gh;
727 struct gsm48_service_request *req;
728 struct gsm48_classmark2 *cm2;
729 uint8_t *cm2_buf, cm2_len;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200730 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100731 struct vlr_subscr *vsub;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200732 struct osmo_mobile_identity mi;
733 int rc;
Harald Welteba4cf162009-01-10 01:49:35 +0000734
Neels Hofmeyr5d53c602022-04-24 23:37:07 +0200735 /* There are two ways to respond with a CM Service Reject:
736 * Directly and only send the CM Service Reject with msc_gsm48_tx_mm_serv_rej().
737 * Decrement the CM Service use count token and send the message with msc_vlr_tx_cm_serv_rej().
738 *
739 * Until we accept the CM Service Request message as such, there is no use count placed for the service type.
740 * So in here use msc_gsm48_tx_mm_serv_rej() to respond.
741 */
742
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700743 /* Make sure that both header and CM Service Request fit into the buffer */
744 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*req)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100745 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: wrong message size (%u < %zu)\n",
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700746 msgb_l3len(msg), sizeof(*gh) + sizeof(*req));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100747 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000748 }
749
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200750 rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false);
751 if (rc) {
752 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: unable to decode Mobile Identity\n");
753 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
754 }
755
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700756 gh = (struct gsm48_hdr *) msgb_l3(msg);
757 req = (struct gsm48_service_request *) gh->data;
758
759 /* Unfortunately in Phase1 the Classmark2 length is variable, so we cannot
760 * just use gsm48_service_request struct, and need to parse it manually. */
761 cm2_len = gh->data[1];
762 cm2_buf = gh->data + 2;
763 cm2 = (struct gsm48_classmark2 *) cm2_buf;
764
765 /* Prevent buffer overrun: check the length of Classmark2 */
766 if (cm2_buf + cm2_len > msg->tail) {
767 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: Classmark2 "
768 "length=%u is too big\n", cm2_len);
769 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
770 }
771
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100772 if (msc_a_is_establishing_auth_ciph(msc_a)) {
773 LOG_MSC_A(msc_a, LOGL_ERROR,
774 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
775 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_CONGESTION);
Neels Hofmeyr7ce21dc2019-01-04 17:42:05 +0100776 /* or should we accept and note down the service request anyway? */
777 }
778
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100779 msc_a->complete_layer3_type = COMPLETE_LAYER3_CM_SERVICE_REQ;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200780 msub_update_id_from_mi(msc_a->c.msub, &mi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100781 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "Rx CM SERVICE REQUEST cm_service_type=%s\n",
782 osmo_cm_service_type_name(req->cm_service_type));
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100783
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200784 switch (mi.type) {
Harald Welte3995d2e2018-01-24 11:23:54 +0100785 case GSM_MI_TYPE_IMSI:
786 case GSM_MI_TYPE_TMSI:
787 /* continue below */
788 break;
789 case GSM_MI_TYPE_IMEI:
790 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
791 /* We don't do emergency calls by IMEI */
Vadim Yanitskiy44560652019-05-09 21:05:41 +0700792 LOG_MSC_A(msc_a, LOGL_NOTICE, "Tx CM SERVICE REQUEST REJECT: "
793 "emergency services by IMEI are not supported\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100794 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_IMEI_NOT_ACCEPTED);
Harald Welte3995d2e2018-01-24 11:23:54 +0100795 }
796 /* fall-through for non-emergency setup */
797 default:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200798 LOG_MSC_A(msc_a, LOGL_ERROR, "MI type is not expected: %s\n", gsm48_mi_type_name(mi.type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100799 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000800 }
801
Andreas Eversbergcd8bd452023-07-11 14:16:24 +0200802 if (!msc_a_cm_service_type_to_use(msc_a, req->cm_service_type))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100803 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
Harald Welte37382ec2018-01-24 12:49:34 +0100804
Neels Hofmeyr5d53c602022-04-24 23:37:07 +0200805 /* At this point, the CM Service Request message is being accepted.
806 * Increment the matching use token, and from here on use msc_vlr_tx_cm_serv_rej() to respond in case of
807 * failure. */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +0200808 msc_a_get(msc_a, msc_a_cm_service_type_to_use(msc_a, req->cm_service_type));
Neels Hofmeyr5d53c602022-04-24 23:37:07 +0200809
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100810 if (msc_a_is_accepted(msc_a))
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200811 return cm_serv_reuse_conn(msc_a, &mi, req->cm_service_type);
Harald Welte2483f1b2016-06-19 18:06:02 +0200812
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200813 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &mi);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200814
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100815 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
816 vlr_proc_acc_req(msc_a->c.fi,
817 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
818 net->vlr, msc_a,
819 VLR_PR_ARQ_T_CM_SERV_REQ,
820 req->cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200821 &mi, &msc_a->via_cell.lai,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100822 is_utran || net->authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200823 msc_a_is_ciphering_to_be_attempted(msc_a),
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200824 msc_a_is_ciphering_required(msc_a),
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100825 req->cipher_key_seq,
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700826 osmo_gsm48_classmark2_is_r99(cm2, cm2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200827 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000828
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200829 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
Thorsten Alteholz3a357de2019-07-16 20:51:16 +0200830 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occurred? */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100831 vsub = msc_a_vsub(msc_a);
832 if (!vsub) {
833 LOG_MSC_A(msc_a, LOGL_ERROR, "subscriber not allowed to do a CM Service Request\n");
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200834 return -EIO;
835 }
836
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100837 vsub->classmark.classmark2 = *cm2;
Vadim Yanitskiyb380a8c2019-05-08 16:11:29 +0700838 vsub->classmark.classmark2_len = cm2_len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200839 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000840}
841
Harald Welte49f8fcb2018-01-24 21:40:19 +0100842/* Receive a CM Re-establish Request */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100843static int gsm48_rx_cm_reest_req(struct msc_a *msc_a, struct msgb *msg)
Harald Welte49f8fcb2018-01-24 21:40:19 +0100844{
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200845 struct gsm_network *net = msc_a_net(msc_a);
846 struct gsm48_hdr *gh;
847 struct gsm48_service_request *req;
848 struct gsm48_classmark2 *cm2;
849 uint8_t *cm2_buf, cm2_len;
850 bool is_utran;
851 struct vlr_subscr *vsub;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200852 struct osmo_mobile_identity mi;
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200853 struct msub *prev_msub;
854 struct msc_a *prev_msc_a;
855
Philipp Maier9194b332020-09-11 21:26:23 +0200856 int rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false);
857 if (rc) {
858 LOGP(DMM, LOGL_ERROR, "CM RE-ESTABLISH REQUEST: cannot decode Mobile Identity\n");
859 return -EINVAL;
860 }
861
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200862 msc_a->complete_layer3_type = COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ;
863 msub_update_id_from_mi(msc_a->c.msub, &mi);
864
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200865 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST %s\n", osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte49f8fcb2018-01-24 21:40:19 +0100866
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200867 gh = (struct gsm48_hdr *) msgb_l3(msg);
868 req = (struct gsm48_service_request *) gh->data;
869
870 /* Unfortunately in Phase1 the Classmark2 length is variable, so we cannot
871 * just use gsm48_service_request struct, and need to parse it manually. */
872 cm2_len = gh->data[1];
873 cm2_buf = gh->data + 2;
874 cm2 = (struct gsm48_classmark2 *) cm2_buf;
875
876 /* Prevent buffer overrun: check the length of Classmark2 */
877 if (cm2_buf + cm2_len > msg->tail) {
878 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx CM SERVICE REQUEST: Classmark2 "
879 "length=%u is too big\n", cm2_len);
880 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_INCORRECT_MESSAGE);
881 }
882
883 /* Look up the other, previously active connection for this subscriber */
884 vsub = vlr_subscr_find_by_mi(net->vlr, &mi, __func__);
885 prev_msub = msub_for_vsub(vsub);
886 prev_msc_a = msub_msc_a(prev_msub);
887 if (!vsub || !prev_msub || !prev_msc_a) {
888 LOG_MSC_A(msc_a, LOGL_ERROR, "CM Re-Establish Request for unknown subscriber: %s\n",
889 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
890 if (vsub)
891 vlr_subscr_put(vsub, __func__);
892 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED);
893 }
894
895 LOG_MSC_A(msc_a, LOGL_NOTICE, "New conn requesting Re-Establishment\n");
896 LOG_MSC_A(prev_msc_a, LOGL_NOTICE, "Old conn matching Re-Establishment request (%s)\n",
897 osmo_use_count_to_str_c(OTC_SELECT, &prev_msc_a->use_count));
898
899 if (!prev_msc_a->cc.call_leg || !prev_msc_a->cc.active_trans) {
900 LOG_MSC_A(msc_a, LOGL_ERROR, "CM Re-Establish Request only supported for voice calls\n");
901 if (vsub)
902 vlr_subscr_put(vsub, __func__);
903 return msc_gsm48_tx_mm_serv_rej(msc_a, GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED);
904 }
905
906 msc_a_get(prev_msc_a, __func__);
907
908 /* Move the call_leg and active CC trans over to the new msc_a */
909 call_leg_reparent(prev_msc_a->cc.call_leg,
910 msc_a->c.fi,
911 MSC_EV_CALL_LEG_TERM,
912 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
913 MSC_EV_CALL_LEG_RTP_COMPLETE);
914 msc_a->cc.call_leg = prev_msc_a->cc.call_leg;
915 prev_msc_a->cc.call_leg = NULL;
916
917 msc_a->cc.active_trans = prev_msc_a->cc.active_trans;
918 msc_a->cc.active_trans->msc_a = msc_a;
919 msc_a_get(msc_a, MSC_A_USE_CC);
920 prev_msc_a->cc.active_trans = NULL;
921 msc_a_put(prev_msc_a, MSC_A_USE_CC);
922
923 /* Dis-associate the VLR subscriber from the previous msc_a, so that we can start a new Process Access Request
924 * on the new msc_a. */
925 if (vsub->proc_arq_fsm) {
926 osmo_fsm_inst_term(vsub->proc_arq_fsm, OSMO_FSM_TERM_REGULAR, NULL);
927 vsub->proc_arq_fsm = NULL;
928 }
929 if (prev_msub->vsub) {
930 vlr_subscr_put(prev_msub->vsub, VSUB_USE_MSUB);
931 prev_msub->vsub = NULL;
932 }
933
934 /* Clear the previous conn.
935 * FIXME: we are clearing the previous conn before having authenticated the new conn. That means anyone can send
936 * CM Re-Establishing requests with arbitrary mobile identities without having to authenticate, and can freely
937 * Clear any connections at will. */
938 msc_a_release_cn(prev_msc_a);
939 msc_a_put(prev_msc_a, __func__);
940 prev_msc_a = NULL;
941
942 /* Kick off Authentication and Ciphering for the new conn. */
943 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
944 vlr_proc_acc_req(msc_a->c.fi,
945 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
946 net->vlr, msc_a,
947 VLR_PR_ARQ_T_CM_RE_ESTABLISH_REQ, 0,
948 &mi, &msc_a->via_cell.lai,
949 is_utran || net->authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200950 msc_a_is_ciphering_to_be_attempted(msc_a),
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200951 msc_a_is_ciphering_required(msc_a),
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200952 req->cipher_key_seq,
953 osmo_gsm48_classmark2_is_r99(cm2, cm2_len),
954 is_utran);
955 vlr_subscr_put(vsub, __func__);
956
957 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START, and we expect
958 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occurred? */
959 vsub = msc_a_vsub(msc_a);
960 if (!vsub) {
961 LOG_MSC_A(msc_a, LOGL_ERROR, "subscriber not allowed to do a CM Service Request\n");
962 return -EIO;
963 }
964
965 vsub->classmark.classmark2 = *cm2;
966 vsub->classmark.classmark2_len = cm2_len;
967 return 0;
Harald Welte49f8fcb2018-01-24 21:40:19 +0100968}
969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100970static int gsm48_rx_mm_imsi_detach_ind(struct msc_a *msc_a, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000971{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100972 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte2a139372009-02-22 21:14:55 +0000973 struct gsm48_hdr *gh = msgb_l3(msg);
974 struct gsm48_imsi_detach_ind *idi =
975 (struct gsm48_imsi_detach_ind *) gh->data;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200976 struct osmo_mobile_identity mi;
Harald Welte2483f1b2016-06-19 18:06:02 +0200977 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000978
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200979 int rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false);
980 if (rc) {
981 LOGP(DMM, LOGL_ERROR, "IMSI DETACH INDICATION: cannot decode Mobile Identity\n");
982 return -EINVAL;
983 }
984
985 DEBUGP(DMM, "IMSI DETACH INDICATION: %s\n", osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte2a139372009-02-22 21:14:55 +0000986
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200987 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, MSC_CTR_LOC_UPDATE_TYPE_DETACH));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100988
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200989 switch (mi.type) {
Harald Welte2a139372009-02-22 21:14:55 +0000990 case GSM_MI_TYPE_TMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200991 vsub = vlr_subscr_find_by_tmsi(net->vlr, mi.tmsi, __func__);
Harald Welte2a139372009-02-22 21:14:55 +0000992 break;
993 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200994 vsub = vlr_subscr_find_by_imsi(net->vlr, mi.imsi, __func__);
Harald Welte2a139372009-02-22 21:14:55 +0000995 break;
996 case GSM_MI_TYPE_IMEI:
997 case GSM_MI_TYPE_IMEISV:
998 /* no sim card... FIXME: what to do ? */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200999 LOGP(DMM, LOGL_ERROR, "%s: unimplemented mobile identity type\n",
1000 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte2a139372009-02-22 21:14:55 +00001001 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001002 default:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001003 LOGP(DMM, LOGL_ERROR, "%s: unknown mobile identity type\n",
1004 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte2a139372009-02-22 21:14:55 +00001005 break;
1006 }
1007
Harald Welte2483f1b2016-06-19 18:06:02 +02001008 if (!vsub) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001009 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber %s\n",
1010 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 } else {
1012 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001013 msub_set_vsub(msc_a->c.msub, vsub);
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +01001014
1015 if (vsub->cs.is_paging)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001016 paging_expired(vsub);
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +01001017
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001018 vsub->classmark.classmark1 = idi->classmark1;
1019
Harald Welte2483f1b2016-06-19 18:06:02 +02001020 vlr_subscr_rx_imsi_detach(vsub);
1021 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001022 vlr_subscr_put(vsub, __func__);
Harald Welte2483f1b2016-06-19 18:06:02 +02001023 }
Harald Welte2a139372009-02-22 21:14:55 +00001024
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001025 msc_a_release_cn(msc_a);
Harald Welte2a139372009-02-22 21:14:55 +00001026 return 0;
1027}
1028
Harald Welted2a7f5a2009-06-05 20:08:20 +00001029static int gsm48_rx_mm_status(struct msgb *msg)
1030{
1031 struct gsm48_hdr *gh = msgb_l3(msg);
1032
1033 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1034
1035 return 0;
1036}
1037
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001038static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001039 struct msc_a *msc_a,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001040 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001041{
1042 struct gsm48_hdr *gh = msgb_l3(msg);
1043 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001044
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001045 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001046 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: l3 length invalid: %u\n",
1047 msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001048 return -EINVAL;
1049 }
1050
1051 *res_len = sizeof(ar->sres);
1052 memcpy(res, ar->sres, sizeof(ar->sres));
1053 return 0;
1054}
1055
1056static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001057 struct msc_a *msc_a,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001058 struct msgb *msg)
1059{
1060 struct gsm48_hdr *gh;
1061 uint8_t *data;
1062 uint8_t iei;
1063 uint8_t ie_len;
1064 unsigned int data_len;
1065
1066 /* First parse the GSM part */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001067 if (parse_gsm_auth_resp(res, res_len, msc_a, msg))
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001068 return -EINVAL;
1069 OSMO_ASSERT(*res_len == 4);
1070
1071 /* Then add the extended res part */
1072 gh = msgb_l3(msg);
1073 data = gh->data + sizeof(struct gsm48_auth_resp);
1074 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
1075
1076 if (data_len < 3) {
Vadim Yanitskiy2fca80e2019-05-08 16:53:23 +07001077 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: "
1078 "message length=%u is too short\n", data_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001079 return -EINVAL;
1080 }
1081
1082 iei = data[0];
1083 ie_len = data[1];
1084 if (iei != GSM48_IE_AUTH_RES_EXT) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001085 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION RESPONSE: expected IEI 0x%02x, got 0x%02x\n",
1086 GSM48_IE_AUTH_RES_EXT, iei);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001087 return -EINVAL;
1088 }
1089
1090 if (ie_len > 12) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001091 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
1092 "MM R99 AUTHENTICATION RESPONSE: extended Auth Resp IE 0x%02x is too large: %u bytes\n",
1093 GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001094 return -EINVAL;
1095 }
1096
1097 *res_len += ie_len;
1098 memcpy(res + 4, &data[2], ie_len);
1099 return 0;
1100}
1101
1102/* Chapter 9.2.3: Authentication Response */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001103static int gsm48_rx_mm_auth_resp(struct msc_a *msc_a, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001104{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001105 uint8_t res[16];
1106 uint8_t res_len;
1107 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001108 bool is_umts;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001109 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001110
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001111 if (!vsub) {
1112 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
1113 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001114 return -EINVAL;
1115 }
1116
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001117 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
1118
1119 if (is_umts)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001120 rc = parse_umts_auth_resp(res, &res_len, msc_a, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001121 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001122 rc = parse_gsm_auth_resp(res, &res_len, msc_a, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001123
1124 if (rc) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001125 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR,
1126 "MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
1127 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
1128 is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +01001129 memset(res, 0, sizeof(res));
1130 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001131 }
1132
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001133 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
1134 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
1135 osmo_hexdump_nospc(res, res_len));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001136
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001137 return vlr_subscr_rx_auth_resp(vsub, osmo_gsm48_classmark_is_r99(&vsub->classmark),
1138 msc_a->c.ran->type == OSMO_RAT_UTRAN_IU,
Harald Welte2483f1b2016-06-19 18:06:02 +02001139 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001140}
1141
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001142static int gsm48_rx_mm_auth_fail(struct msc_a *msc_a, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001143{
1144 struct gsm48_hdr *gh = msgb_l3(msg);
1145 uint8_t cause;
1146 uint8_t auts_tag;
1147 uint8_t auts_len;
1148 uint8_t *auts;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001149 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001150
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001151 if (!vsub) {
1152 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
1153 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001154 return -EINVAL;
1155 }
1156
1157 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001158 LOG_MSC_A_CAT(msc_a, DMM, LOGL_ERROR, "MM R99 AUTHENTICATION FAILURE: l3 length invalid: %u\n",
1159 msgb_l3len(msg));
1160 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001161 return -EINVAL;
1162 }
1163
1164 cause = gh->data[0];
1165
1166 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001167 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO, "MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n", cause);
1168 vlr_subscr_rx_auth_fail(vsub, NULL);
Harald Welte2483f1b2016-06-19 18:06:02 +02001169 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001170 }
1171
1172 /* This is a Synch Failure procedure, which should pass an AUTS to
1173 * resynchronize the sequence nr with the HLR. Expecting exactly one
1174 * TLV with 14 bytes of AUTS. */
1175
1176 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001177 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1178 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure: missing AUTS IE\n");
1179 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001180 return -EINVAL;
1181 }
1182
1183 auts_tag = gh->data[1];
1184 auts_len = gh->data[2];
1185 auts = &gh->data[3];
1186
1187 if (auts_tag != GSM48_IE_AUTS
1188 || auts_len != 14) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001189 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1190 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure:"
1191 " expected AUTS IE 0x%02x of 14 bytes,"
1192 " got IE 0x%02x of %u bytes\n",
1193 GSM48_IE_AUTS, auts_tag, auts_len);
1194 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001195 return -EINVAL;
1196 }
1197
1198 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001199 LOG_MSC_A_CAT(msc_a, DMM, LOGL_INFO,
1200 "MM R99 AUTHENTICATION FAILURE: invalid Synch Failure msg: message truncated (%u)\n",
1201 msgb_l3len(msg));
1202 msc_a_release_mo(msc_a, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001203 return -EINVAL;
1204 }
1205
1206 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1207 * large enough. */
1208
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001209 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
1210 osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001211
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001212 return vlr_subscr_rx_auth_fail(vsub, auts);
Harald Welte2483f1b2016-06-19 18:06:02 +02001213}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001214
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001215static int gsm48_rx_mm_tmsi_reall_compl(struct msc_a *msc_a)
Harald Welte2483f1b2016-06-19 18:06:02 +02001216{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001217 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1218 if (!vsub) {
1219 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 +02001220 return -EINVAL;
1221 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001222 LOG_MSC_A_CAT(msc_a, DMM, LOGL_DEBUG, "TMSI Reallocation Completed\n");
1223 return vlr_subscr_rx_tmsi_reall_compl(vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001224}
1225
Harald Weltebf5e8df2009-02-03 12:59:45 +00001226/* Receive a GSM 04.08 Mobility Management (MM) message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001227int gsm0408_rcv_mm(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001228{
1229 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001230 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001231
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001232 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001233 case GSM48_MT_MM_LOC_UPD_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001234 rc = mm_rx_loc_upd_req(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001235 break;
1236 case GSM48_MT_MM_ID_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001237 rc = mm_rx_id_resp(msc_a, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001238 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001239 case GSM48_MT_MM_CM_SERV_REQ:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001240 rc = gsm48_rx_mm_serv_req(msc_a, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001241 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001242 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001243 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001244 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001245 case GSM48_MT_MM_TMSI_REALL_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001246 rc = gsm48_rx_mm_tmsi_reall_compl(msc_a);
Harald Welte69b2af22009-01-06 19:47:00 +00001247 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001248 case GSM48_MT_MM_IMSI_DETACH_IND:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001249 rc = gsm48_rx_mm_imsi_detach_ind(msc_a, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001250 break;
1251 case GSM48_MT_MM_CM_REEST_REQ:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001252 rc = gsm48_rx_cm_reest_req(msc_a, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001253 break;
1254 case GSM48_MT_MM_AUTH_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001255 rc = gsm48_rx_mm_auth_resp(msc_a, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001256 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001257 case GSM48_MT_MM_AUTH_FAIL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001258 rc = gsm48_rx_mm_auth_fail(msc_a, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001259 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001260 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001261 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001262 gh->msg_type);
1263 break;
1264 }
1265
1266 return rc;
1267}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001268
Harald Welte2d35ae62009-02-06 12:02:13 +00001269/* Receive a PAGING RESPONSE message from the MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001270static int gsm48_rx_rr_pag_resp(struct msc_a *msc_a, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001271{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001272 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte2d35ae62009-02-06 12:02:13 +00001273 struct gsm48_hdr *gh = msgb_l3(msg);
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001274 struct gsm48_pag_resp *pr =
1275 (struct gsm48_pag_resp *)gh->data;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001276 uint8_t classmark2_len = gh->data[1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001277 uint8_t *classmark2_buf = gh->data+2;
1278 struct gsm48_classmark2 *cm2 = (void*)classmark2_buf;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001279 bool is_utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001280 struct vlr_subscr *vsub;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001281 struct osmo_mobile_identity mi;
1282 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001283
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001284 if (msc_a_is_establishing_auth_ciph(msc_a)) {
Vadim Yanitskiy5b19f6e2020-01-25 07:02:47 +07001285 LOG_MSC_A_CAT(msc_a, DRR, LOGL_ERROR,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001286 "Ignoring Paging Response, conn already busy establishing authenticity\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001287 return 0;
1288 }
1289
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001290 if (msc_a_is_accepted(msc_a)) {
Vadim Yanitskiy5b19f6e2020-01-25 07:02:47 +07001291 LOG_MSC_A_CAT(msc_a, DRR, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001292 return 0;
1293 }
1294
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001295 rc = osmo_mobile_identity_decode_from_l3(&mi, msg, false);
1296 if (rc) {
1297 LOG_MSC_A_CAT(msc_a, DRR, LOGL_ERROR, "Paging Response: cannot decode Mobile Identity\n");
1298 return -EINVAL;
1299 }
1300
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001301 msc_a->complete_layer3_type = COMPLETE_LAYER3_PAGING_RESP;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001302 msub_update_id_from_mi(msc_a->c.msub, &mi);
1303 LOG_MSC_A_CAT(msc_a, DRR, LOGL_DEBUG, "Rx PAGING RESPONSE %s\n", osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001305 msc_a_get(msc_a, MSC_A_USE_PAGING_RESPONSE);
1306
1307 is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
1308 vlr_proc_acc_req(msc_a->c.fi,
1309 MSC_A_EV_AUTHENTICATED, MSC_A_EV_CN_CLOSE, NULL,
1310 net->vlr, msc_a,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001311 VLR_PR_ARQ_T_PAGING_RESP, 0, &mi, &msc_a->via_cell.lai,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001312 is_utran || net->authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +02001313 msc_a_is_ciphering_to_be_attempted(msc_a),
Neels Hofmeyr2ea72642022-10-10 23:35:47 +02001314 msc_a_is_ciphering_required(msc_a),
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001315 pr->key_seq,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001316 osmo_gsm48_classmark2_is_r99(cm2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001317 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001318
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001319 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
Thorsten Alteholz3a357de2019-07-16 20:51:16 +02001320 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occurred? */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001321 vsub = msc_a_vsub(msc_a);
1322 if (!vsub) {
Vadim Yanitskiy5b19f6e2020-01-25 07:02:47 +07001323 LOG_MSC_A_CAT(msc_a, DRR, LOGL_ERROR, "subscriber not allowed to do a Paging Response\n");
Neels Hofmeyr667d5e02020-08-19 13:40:33 +00001324
1325 /* Above MSC_A_USE_PAGING_RESPONSE may already have been removed by a forced release, put that use only
1326 * if it still exists. (see msc_a_fsm_releasing_onenter()) */
1327 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_PAGING_RESPONSE))
1328 msc_a_put(msc_a, MSC_A_USE_PAGING_RESPONSE);
1329
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001330 return -EIO;
1331 }
1332
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333 vsub->classmark.classmark2 = *cm2;
1334 vsub->classmark.classmark2_len = classmark2_len;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001335 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001336}
1337
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001338static int gsm48_rx_rr_ciphering_mode_complete(struct msc_a *msc_a, struct msgb *msg)
1339{
1340 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1341 struct gsm48_hdr *gh = msgb_l3(msg);
1342 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1343 struct tlv_parsed tp;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001344 struct tlv_p_entry *mi_tlv;
1345 struct osmo_mobile_identity mi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001346
1347 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001348 mi_tlv = TLVP_GET(&tp, GSM48_IE_MOBILE_ID);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001349
Vadim Yanitskiya79aac02019-12-28 01:19:19 +01001350 /* IMEI(SV) is optional for this message */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001351 if (!mi_tlv)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001352 return 0;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001353 if (!mi_tlv->len)
Vadim Yanitskiya79aac02019-12-28 01:19:19 +01001354 return -EINVAL;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001355
1356 if (osmo_mobile_identity_decode(&mi, mi_tlv->val, mi_tlv->len, false)) {
1357 LOGP(DMM, LOGL_ERROR, "RR Ciphering Mode Complete contains invalid Mobile Identity %s\n",
1358 osmo_hexdump(mi_tlv->val, mi_tlv->len));
1359 return -EINVAL;
1360 }
1361 if (mi.type != GSM_MI_TYPE_IMEISV) {
Vadim Yanitskiya79aac02019-12-28 01:19:19 +01001362 LOGP(DMM, LOGL_ERROR, "RR Ciphering Mode Complete contains "
1363 "unexpected Mobile Identity type %s\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001364 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Vadim Yanitskiya79aac02019-12-28 01:19:19 +01001365 return -EINVAL;
1366 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001367
Neels Hofmeyr38816da2019-08-29 00:43:18 +02001368 LOG_MSC_A(msc_a, LOGL_DEBUG, "RR Ciphering Mode Complete contains Mobile Identity: %s\n",
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001369 osmo_mobile_identity_to_str_c(OTC_SELECT, &mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001370
1371 if (!vsub)
1372 return 0;
1373
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001374 return vlr_subscr_rx_id_resp(vsub, &mi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001375}
1376
1377static int gsm48_rx_rr_app_info(struct msc_a *msc_a, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001378{
1379 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001380 uint8_t apdu_id_flags;
1381 uint8_t apdu_len;
1382 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001383
1384 apdu_id_flags = gh->data[0];
1385 apdu_len = gh->data[1];
1386 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001387
Vadim Yanitskiy6a9a3a72020-01-25 06:53:50 +07001388 LOG_MSC_A_CAT(msc_a, DRR, LOGL_DEBUG, "Rx RR APPLICATION INFO "
1389 "(id/flags=0x%02x apdu_len=%u apdu=%s)\n",
1390 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001391
Harald Welte2483f1b2016-06-19 18:06:02 +02001392 /* we're not using the app info blob anywhere, so ignore. */
1393#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001394 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001395#else
1396 return 0;
1397#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001398}
1399
Harald Weltebf5e8df2009-02-03 12:59:45 +00001400/* Receive a GSM 04.08 Radio Resource (RR) message */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001401int gsm0408_rcv_rr(struct msc_a *msc_a, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001402{
1403 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001404 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001405
1406 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001407 case GSM48_MT_RR_PAG_RESP:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001408 rc = gsm48_rx_rr_pag_resp(msc_a, msg);
1409 break;
1410 case GSM48_MT_RR_CIPH_M_COMPL:
1411 rc = gsm48_rx_rr_ciphering_mode_complete(msc_a, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001412 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001413 case GSM48_MT_RR_APP_INFO:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001414 rc = gsm48_rx_rr_app_info(msc_a, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001415 break;
Andreas Eversberg06497192023-06-01 12:18:24 +02001416 case GSM48_MT_RR_UPLINK_RELEASE:
1417 rc = gsm44068_rcv_rr(msc_a, msg);
1418 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001419 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001420 LOG_MSC_A_CAT(msc_a, DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1421 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001422 break;
1423 }
1424
Harald Welte2d35ae62009-02-06 12:02:13 +00001425 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001426}
1427
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001428int gsm48_send_rr_app_info(struct msc_a *msc_a, uint8_t apdu_id,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001429 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001430{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001431 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001432 struct gsm48_hdr *gh;
1433
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001434 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1435 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001436
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001437 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1438 gh->proto_discr = GSM48_PDISC_RR;
1439 gh->msg_type = GSM48_MT_RR_APP_INFO;
1440 gh->data[0] = apdu_id;
1441 gh->data[1] = apdu_len;
1442 memcpy(gh->data+2, apdu, apdu_len);
1443
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001444 return msc_a_tx_dtap_to_i(msc_a, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001445}
1446
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001447extern int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg);
Harald Welte8470bf22008-12-25 23:28:35 +00001448
Harald Welte2483f1b2016-06-19 18:06:02 +02001449/***********************************************************************
1450 * VLR integration
1451 ***********************************************************************/
1452
1453/* VLR asks us to send an authentication request */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001454static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Harald Welte2483f1b2016-06-19 18:06:02 +02001455 bool send_autn)
1456{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001457 struct msc_a *msc_a = msc_conn_ref;
1458 return gsm48_tx_mm_auth_req(msc_a, at->vec.rand,
Harald Welte2483f1b2016-06-19 18:06:02 +02001459 send_autn? at->vec.autn : NULL,
1460 at->key_seq);
1461}
1462
1463/* VLR asks us to send an authentication reject */
1464static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1465{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001466 struct msc_a *msc_a = msc_conn_ref;
1467 return gsm48_tx_mm_auth_rej(msc_a);
Harald Welte2483f1b2016-06-19 18:06:02 +02001468}
1469
1470/* VLR asks us to transmit an Identity Request of given type */
1471static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1472{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001473 struct msc_a *msc_a = msc_conn_ref;
Vadim Yanitskiya79aac02019-12-28 01:19:19 +01001474
1475 /* Store requested MI type, so we can check the response */
1476 msc_a->mm_id_req_type = mi_type;
1477
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001478 return mm_tx_identity_req(msc_a, mi_type);
Harald Welte2483f1b2016-06-19 18:06:02 +02001479}
1480
1481/* VLR asks us to transmit a Location Update Accept */
1482static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1483{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001484 struct msc_a *msc_a = msc_conn_ref;
1485 return gsm0408_loc_upd_acc(msc_a, send_tmsi);
Harald Welte2483f1b2016-06-19 18:06:02 +02001486}
1487
1488/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001489static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001490{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001491 struct msc_a *msc_a = msc_conn_ref;
1492 return gsm0408_loc_upd_rej(msc_a, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02001493}
1494
1495/* VLR asks us to transmit a CM Service Accept */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001496int 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 +02001497{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001498 struct msc_a *msc_a = msc_conn_ref;
1499 return msc_gsm48_tx_mm_serv_ack(msc_a);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001500}
1501
1502static int msc_vlr_tx_common_id(void *msc_conn_ref)
1503{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001504 struct msc_a *msc_a = msc_conn_ref;
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001505 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001506 struct ran_msg msg = {
1507 .msg_type = RAN_MSG_COMMON_ID,
1508 .common_id = {
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001509 .imsi = vsub->imsi,
1510 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001511 },
1512 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001513 if (vsub->sgs.last_eutran_plmn_present) {
1514 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
1515 sizeof(vsub->sgs.last_eutran_plmn));
1516 }
1517
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001518 return msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Harald Welte2483f1b2016-06-19 18:06:02 +02001519}
1520
Stefan Sperling3a741282018-03-13 21:11:49 +01001521/* VLR asks us to transmit MM info. */
1522static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1523{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001524 struct msc_a *msc_a = msc_conn_ref;
1525 struct gsm_network *net = msc_a_net(msc_a);
1526 if (!net->send_mm_info)
Stefan Sperling3a741282018-03-13 21:11:49 +01001527 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001528 return gsm48_tx_mm_info(msc_a);
Stefan Sperling3a741282018-03-13 21:11:49 +01001529}
1530
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02001531/* VLR asks us to transmit a CM Service Reject.
1532 * Decrement the CM Service type's use token and send the CM Service Reject message. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001533static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
1534 enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001535{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001536 struct msc_a *msc_a = msc_conn_ref;
1537 msc_gsm48_tx_mm_serv_rej(msc_a, cause);
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02001538 msc_a_put(msc_a, msc_a_cm_service_type_to_use(msc_a, cm_service_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001539 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001540}
1541
1542/* VLR informs us that the subscriber data has somehow been modified */
1543static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1544{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001545 struct msub *msub = msub_for_vsub(subscr);
1546 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s)%s\n",
1547 subscr->imsi, subscr->msisdn, msub ? "" : " (NO CONN!)");
1548 msub_update_id(msub);
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001549}
1550
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001551/* VLR informs us that the subscriber has been associated with a conn.
1552 * The subscriber has *not* been authenticated yet, so the vsub should be protected from potentially invalid information
1553 * from the msc_a. */
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001554static int msc_vlr_subscr_assoc(void *msc_conn_ref,
Harald Welte2483f1b2016-06-19 18:06:02 +02001555 struct vlr_subscr *vsub)
1556{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001557 struct msc_a *msc_a = msc_conn_ref;
1558 struct msub *msub = msc_a->c.msub;
Max48d4ec02018-02-12 16:51:03 +01001559 OSMO_ASSERT(vsub);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001560
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001561 if (msub_set_vsub(msub, vsub))
1562 return -EINVAL;
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001563
1564 /* FIXME: would be better to modify vsub->* only after the subscriber is authenticated, in
1565 * evaluate_acceptance_outcome(conn_accepted == true). */
1566
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001567 vsub->cs.attached_via_ran = msc_a->c.ran->type;
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001568
1569 /* In case we have already received Classmark Information before the VLR Subscriber was
1570 * associated with the conn: merge the new Classmark into vsub->classmark. Don't overwrite valid
1571 * vsub->classmark with unset classmark, though. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001572 osmo_gsm48_classmark_update(&vsub->classmark, &msc_a->temporary_classmark);
1573
1574 msub_update_id(msub);
1575
1576 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_COMPLETE_LAYER_3_OK, NULL);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001577 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001578}
1579
1580/* operations that we need to implement for libvlr */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001581const struct vlr_ops msc_vlr_ops = {
Harald Welte2483f1b2016-06-19 18:06:02 +02001582 .tx_auth_req = msc_vlr_tx_auth_req,
1583 .tx_auth_rej = msc_vlr_tx_auth_rej,
1584 .tx_id_req = msc_vlr_tx_id_req,
1585 .tx_lu_acc = msc_vlr_tx_lu_acc,
1586 .tx_lu_rej = msc_vlr_tx_lu_rej,
1587 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1588 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001589 .set_ciph_mode = msc_a_vlr_set_cipher_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001590 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001591 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001592 .subscr_update = msc_vlr_subscr_update,
1593 .subscr_assoc = msc_vlr_subscr_assoc,
1594};
1595
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001596struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1597{
1598 struct msgb *msg;
1599 struct gsm48_hdr *gh;
1600
1601 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1602 if (!msg)
1603 return NULL;
1604
1605 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1606 gh->proto_discr = GSM48_PDISC_MM;
1607 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1608 gh->data[0] = value;
1609
1610 return msg;
1611}
1612
1613struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1614{
1615 struct gsm48_hdr *gh;
1616 struct msgb *msg;
1617
1618 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1619 if (!msg)
1620 return NULL;
1621
1622 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1623 gh->proto_discr = GSM48_PDISC_MM;
1624 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1625 gh->data[0] = cause;
1626 return msg;
1627}