blob: 3a44d3090878025fa232d40153fdccee1c1a9379 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) code base */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020025#include <osmocom/core/timer.h>
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070026#include <osmocom/core/tdef.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
28#include <osmocom/gsm/gsup.h>
29#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020030#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010031#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020033#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010034#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020035#include <osmocom/msc/vlr.h>
36#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010037#include <osmocom/msc/gsup_client_mux.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020038
Harald Welteb8b85a12016-06-17 00:06:42 +020039#include <netinet/in.h>
40#include <arpa/inet.h>
41#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020042#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020043
44#include "vlr_core.h"
45#include "vlr_auth_fsm.h"
46#include "vlr_lu_fsm.h"
47#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010048#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020049
50#define SGSN_SUBSCR_MAX_RETRIES 3
51#define SGSN_SUBSCR_RETRY_INTERVAL 10
52
53/***********************************************************************
54 * Convenience functions
55 ***********************************************************************/
56
57const struct value_string vlr_ciph_names[] = {
58 OSMO_VALUE_STRING(VLR_CIPH_NONE),
59 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
60 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
61 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
62 { 0, NULL }
63};
64
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070065/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
66struct osmo_tdef msc_tdefs_vlr[] = {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070067 { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" },
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070068 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
69 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
70 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
71 { /* terminator */ }
72};
73
74/* This is just a wrapper around the osmo_tdef API.
75 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +020076uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
77{
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070078 /* NOTE: since we usually do not need more than one instance of the VLR,
79 * and since libosmocore's osmo_tdef API does not (yet) support dynamic
80 * configuration, we always use the global instance of msc_tdefs_vlr. */
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070081 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +020082}
83
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010084/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010085const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010086{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010087 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010088 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010089 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010090 if (!vsub)
91 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010092 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010093 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010094 present = true;
95 }
96 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010097 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010098 present = true;
99 }
100 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100101 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100102 present = true;
103 }
104 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100105 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100106 present = true;
107 }
108 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100109 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100110
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100111 return buf;
112}
113
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100114const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
115{
116 /* cast away the const so we can shorten the string within the static buffer */
117 char *name = (char*)vlr_subscr_name(vsub);
118 size_t len = strlen(name);
119 if (maxlen < 2)
120 return "-";
121 if (len > maxlen)
122 strcpy(name + maxlen - 2, "..");
123 return name;
124}
125
Oliver Smith5598aae2019-01-08 11:47:21 +0100126const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100127{
128 if (!vsub || !vsub->msisdn[0])
129 return vlr_subscr_name(vsub);
130 return vsub->msisdn;
131}
132
Harald Welteb8b85a12016-06-17 00:06:42 +0200133struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
134 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100135 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200136 const char *file, int line)
137{
138 struct vlr_subscr *vsub;
139
140 if (!imsi || !*imsi)
141 return NULL;
142
143 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100144 if (vlr_subscr_matches_imsi(vsub, imsi)) {
145 vlr_subscr_get_src(vsub, use, file, line);
146 return vsub;
147 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200148 }
149 return NULL;
150}
151
152struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
153 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100154 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200155 const char *file, int line)
156{
157 struct vlr_subscr *vsub;
158
159 if (tmsi == GSM_RESERVED_TMSI)
160 return NULL;
161
162 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100163 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
164 vlr_subscr_get_src(vsub, use, file, line);
165 return vsub;
166 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200167 }
168 return NULL;
169}
170
171struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
172 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100173 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200174 const char *file, int line)
175{
176 struct vlr_subscr *vsub;
177
178 if (!msisdn || !*msisdn)
179 return NULL;
180
181 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100182 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
183 vlr_subscr_get_src(vsub, use, file, line);
184 return vsub;
185 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200186 }
187 return NULL;
188}
189
Harald Welteb8b85a12016-06-17 00:06:42 +0200190/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100191static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200192 struct osmo_gsup_message *gsup_msg)
193{
194 struct vlr_instance *vlr = vsub->vlr;
195
196 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100197 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200198
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100199 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200200
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100201 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200202}
203
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100204static int vlr_subscr_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
Harald Welteb8b85a12016-06-17 00:06:42 +0200205{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100206 struct vlr_subscr *vsub = e->use_count->talloc_object;
207 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100208 int32_t total;
209 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200210
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100211 if (!e->use)
212 return -EINVAL;
213
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100214 total = osmo_use_count_total(&vsub->use_count);
215
216 if (total == 0
217 || (total == 1 && old_use_count == 0 && e->count == 1))
218 level = LOGL_INFO;
219 else
220 level = LOGL_DEBUG;
221
222 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100223 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
224 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
225
226 if (e->count < 0)
227 return -ERANGE;
228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100229 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
230
231 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100232 vlr_subscr_free(vsub);
233 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200234}
235
236/* Allocate a new subscriber and insert it into list */
237static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
238{
239 struct vlr_subscr *vsub;
240 int i;
241
242 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100243 *vsub = (struct vlr_subscr){
244 .vlr = vlr,
245 .tmsi = GSM_RESERVED_TMSI,
246 .tmsi_new = GSM_RESERVED_TMSI,
247 .use_count = (struct osmo_use_count){
248 .talloc_object = vsub,
249 .use_cb = vlr_subscr_use_cb,
250 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100251 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100252 };
253 osmo_use_count_make_static_entries(&vsub->use_count, vsub->use_count_buf, ARRAY_SIZE(vsub->use_count_buf));
Harald Welteb8b85a12016-06-17 00:06:42 +0200254
255 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100256 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200257
258 INIT_LLIST_HEAD(&vsub->cs.requests);
259 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
260
Harald Welte0df904d2018-12-03 11:00:04 +0100261 /* Create an SGs FSM, which is needed to control CSFB,
262 * in cases where CSFB/SGs is not in use, this FSM will
263 * just do nothing. (see also: sgs_iface.c) */
264 vlr_sgs_fsm_create(vsub);
265
Harald Welteb8b85a12016-06-17 00:06:42 +0200266 llist_add_tail(&vsub->list, &vlr->subscribers);
267 return vsub;
268}
269
Harald Welteb8b85a12016-06-17 00:06:42 +0200270/* Send a GSUP Purge MS request.
271 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
272 * this subscriber, not to the HLR? */
273int vlr_subscr_purge(struct vlr_subscr *vsub)
274{
275 struct osmo_gsup_message gsup_msg = {0};
276
277 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
278
279 /* provide HLR number in case we know it */
280 gsup_msg.hlr_enc_len = vsub->hlr.len;
281 gsup_msg.hlr_enc = vsub->hlr.buf;
282
283 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
284}
285
Neels Hofmeyr15809592018-04-06 02:57:51 +0200286void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
287 enum osmo_fsm_term_cause fsm_cause,
288 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200289{
290 if (!vsub)
291 return;
292
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100293 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200294 if (vsub->lu_fsm)
295 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200296 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200297 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100298 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200299}
300
301/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
302void vlr_subscr_free(struct vlr_subscr *vsub)
303{
304 llist_del(&vsub->list);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
306 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100307
308 /* Make sure SGs timer Ts5 is removed */
309 osmo_timer_del(&vsub->sgs.Ts5);
310
311 /* Remove SGs FSM (see also: sgs_iface.c) */
312 vlr_sgs_fsm_remove(vsub);
313
Harald Welteb8b85a12016-06-17 00:06:42 +0200314 talloc_free(vsub);
315}
316
317/* Generate a new TMSI and store in vsub->tmsi_new.
318 * Search all known subscribers to ensure that the TMSI is unique. */
319int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
320{
321 struct vlr_instance *vlr = vsub->vlr;
322 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100323 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100324 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200325
326 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100327 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
328 if (rc < 0) {
329 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
330 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200331 }
332 /* throw the dice again, if the TSMI doesn't fit */
333 if (tmsi == GSM_RESERVED_TMSI)
334 continue;
335
336 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
337 if (vlr->cfg.is_ps) {
338 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200339 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200340 } else {
341 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200342 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
343 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200344 }
345
346 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100347 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
348 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200349 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100350 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200351
352 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100353 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200354 return 0;
355 }
356
357 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
358 " after %d tries\n", vlr_subscr_name(vsub), tried);
359 return -1;
360}
361
362/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100363 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200364 * \param[in] imsi IMSI string.
365 * \param[out] created if non-NULL, returns whether a new entry was created. */
366struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
367 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100368 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200369 bool *created,
370 const char *file,
371 int line)
372{
373 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100374 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200375 if (vsub) {
376 if (created)
377 *created = false;
378 return vsub;
379 }
380
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100381 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200382 if (!vsub)
383 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100384 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200385 vlr_subscr_set_imsi(vsub, imsi);
386 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
387 if (created)
388 *created = true;
389 return vsub;
390}
391
392/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100393 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200394 * \param[in] tmsi TMSI.
395 * \param[out] created if non-NULL, returns whether a new entry was created. */
396struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
397 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100398 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200399 bool *created,
400 const char *file,
401 int line)
402{
403 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100404 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200405 if (vsub) {
406 if (created)
407 *created = false;
408 return vsub;
409 }
410
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100411 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200412 if (!vsub)
413 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100414 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200415 vsub->tmsi = tmsi;
416 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
417 if (created)
418 *created = true;
419 return vsub;
420}
421
422void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
423{
424 if (!vsub)
425 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200426
427 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
428 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
429 imsi, vsub->imsi);
430 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
431 }
432
Harald Welteb8b85a12016-06-17 00:06:42 +0200433 vsub->id = atoll(vsub->imsi);
434 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
435 vsub->imsi, vsub->id);
436}
437
438void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
439{
440 if (!vsub)
441 return;
Max98f74672018-02-05 12:57:06 +0100442 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200443 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
444 vsub->imsi, vsub->imei);
445}
446
447void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
448{
449 if (!vsub)
450 return;
Max98f74672018-02-05 12:57:06 +0100451 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200452 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
453 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200454
455 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
456 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200457}
458
459/* Safely copy the given MSISDN string to vsub->msisdn */
460void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
461{
462 if (!vsub)
463 return;
Max98f74672018-02-05 12:57:06 +0100464 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200465 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
466 vsub->imsi, vsub->msisdn);
467}
468
469bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
470{
471 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
472}
473
474bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
475{
476 return vsub && tmsi != GSM_RESERVED_TMSI
477 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
478}
479
480bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
481{
482 return vsub && msisdn && vsub->msisdn[0]
483 && !strcmp(vsub->msisdn, msisdn);
484}
485
486bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
487{
488 return vsub && imei && vsub->imei[0]
489 && !strcmp(vsub->imei, imei);
490}
491
492/* Send updated subscriber information to HLR */
493int vlr_subscr_changed(struct vlr_subscr *vsub)
494{
495 /* FIXME */
496 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
497 return 0;
498}
499
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200500void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
501{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200502 struct timespec now;
503
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700504 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200505 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700506 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200507 } else {
508 LOGP(DVLR, LOGL_ERROR,
509 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
510 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
511 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
512 }
513}
514
515void vlr_subscr_expire_lu(void *data)
516{
517 struct vlr_instance *vlr = data;
518 struct vlr_subscr *vsub, *vsub_tmp;
519 struct timespec now;
520
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700521 /* Periodic location update might be disabled from the VTY,
522 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700523 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700524 goto done;
525
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200526 if (llist_empty(&vlr->subscribers))
527 goto done;
528
529 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
530 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
531 goto done;
532 }
533
534 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
535 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
536 continue;
537
538 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
539 vlr_subscr_rx_imsi_detach(vsub);
540 }
541
542done:
543 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
544}
545
Harald Welteb8b85a12016-06-17 00:06:42 +0200546/***********************************************************************
547 * PDP context data
548 ***********************************************************************/
549
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200550#define GSM_APN_LENGTH 102
551
552/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
553/* see GSM 09.02, B.1, gprsSubscriptionData */
554struct sgsn_subscriber_pdp_data {
555 struct llist_head list;
556
557 unsigned int context_id;
558 uint16_t pdp_type;
559 char apn_str[GSM_APN_LENGTH];
560 uint8_t qos_subscribed[20];
561 size_t qos_subscribed_len;
562};
563
Harald Welteb8b85a12016-06-17 00:06:42 +0200564struct sgsn_subscriber_pdp_data *
565vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
566{
567 struct sgsn_subscriber_pdp_data* pdata;
568
569 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
570
571 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
572
573 return pdata;
574}
575
576static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
577{
578 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
579 int count = 0;
580
581 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
582 llist_del(&pdp->list);
583 talloc_free(pdp);
584 count += 1;
585 }
586
587 return count;
588}
589
590static struct sgsn_subscriber_pdp_data *
591vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
592{
593 struct sgsn_subscriber_pdp_data *pdp;
594
595 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
596 if (pdp->context_id == context_id)
597 return pdp;
598 }
599
600 return NULL;
601}
602
603/***********************************************************************
604 * Actual Implementation
605 ***********************************************************************/
606
607static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100608 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200609{
610 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200611 LOGP(DVLR, LOGL_NOTICE,
612 "Unknown IMSI %s, discarding GSUP request "
613 "of type 0x%02x\n",
614 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100615 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200616 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
617 LOGP(DVLR, LOGL_NOTICE,
618 "Unknown IMSI %s, discarding GSUP error "
619 "of type 0x%02x, cause '%s' (%d)\n",
620 gsup_msg->imsi, gsup_msg->message_type,
621 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
622 gsup_msg->cause);
623 } else {
624 LOGP(DVLR, LOGL_NOTICE,
625 "Unknown IMSI %s, discarding GSUP response "
626 "of type 0x%02x\n",
627 gsup_msg->imsi, gsup_msg->message_type);
628 }
629
630 return -GMM_CAUSE_IMSI_UNKNOWN;
631}
632
633static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100634 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200635{
636 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
637 LOGGSUPP(LOGL_NOTICE, gsup_msg,
638 "Purge MS has failed with cause '%s' (%d)\n",
639 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
640 gsup_msg->cause);
641 return -gsup_msg->cause;
642 }
643 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
644 return 0;
645}
646
647/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100648int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200649{
650 struct osmo_gsup_message gsup_msg = {0};
651 int rc;
652
653 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200654 gsup_msg.cn_domain = vsub->vlr->cfg.is_ps ? OSMO_GSUP_CN_DOMAIN_PS : OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200655 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
656
657 return rc;
658}
659
660/* VLR internal call to request tuples from HLR */
661int vlr_subscr_req_sai(struct vlr_subscr *vsub,
662 const uint8_t *auts, const uint8_t *auts_rand)
663{
664 struct osmo_gsup_message gsup_msg = {0};
665
666 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
667 gsup_msg.auts = auts;
668 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100669 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200670
671 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
672}
673
Oliver Smith7d053092018-12-14 17:37:38 +0100674/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
675int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
676{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100677 struct osmo_gsup_message gsup_msg = {
678 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200679 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100680 };
Oliver Smith7d053092018-12-14 17:37:38 +0100681 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
682 int len;
683
684 /* Encode IMEI */
685 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
686 if (len < 1) {
687 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
688 return -ENOSPC;
689 }
690 gsup_msg.imei_enc = imei_enc;
691 gsup_msg.imei_enc_len = len;
692
693 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100694 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100695 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100696}
697
Harald Welteb8b85a12016-06-17 00:06:42 +0200698/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100699int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200700{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100701 struct osmo_gsup_message gsup_msg = {
702 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200703 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100704 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200705
Max98f74672018-02-05 12:57:06 +0100706 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100707 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200708}
709
710/* Update the subscriber with GSUP-received auth tuples */
711void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
712 const struct osmo_gsup_message *gsup)
713{
714 unsigned int i;
715 unsigned int got_tuples;
716
717 if (gsup->num_auth_vectors) {
718 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
719 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100720 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200721 }
722
723 got_tuples = 0;
724 for (i = 0; i < gsup->num_auth_vectors; i++) {
725 size_t key_seq = i;
726
727 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
728 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100729 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200730 key_seq);
731 continue;
732 }
733 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
734 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100735 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200736 }
737
738 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
739
740 if (!got_tuples) {
741 /* FIXME what now? */
742 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
743 }
744
745 /* New tuples means last_tuple becomes invalid */
746 vsub->last_tuple = NULL;
747}
748
749/* Handle SendAuthInfo Result/Error from HLR */
750static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
751 const struct osmo_gsup_message *gsup)
752{
753 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
754 void *data = (void *) gsup;
755
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200756 if (!auth_fi) {
757 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
758 osmo_gsup_message_type_name(gsup->message_type));
759 return -1;
760 }
761
Harald Welteb8b85a12016-06-17 00:06:42 +0200762 switch (gsup->message_type) {
763 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
764 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
765 break;
766 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
767 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
768 break;
769 default:
770 return -1;
771 }
772
773 return 0;
774}
775
Harald Welteb8b85a12016-06-17 00:06:42 +0200776static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
777 const struct osmo_gsup_message *gsup_msg)
778{
779 unsigned idx;
780 int rc;
781
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100782 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100783 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
784 gsup_msg->msisdn_enc,
785 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200786 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
787 vsub->imsi, vsub->msisdn);
788 }
789
790 if (gsup_msg->hlr_enc) {
791 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
792 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
793 gsup_msg->hlr_enc_len);
794 vsub->hlr.len = 0;
795 } else {
796 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
797 gsup_msg->hlr_enc_len);
798 vsub->hlr.len = gsup_msg->hlr_enc_len;
799 }
800 }
801
802 if (gsup_msg->pdp_info_compl) {
803 rc = vlr_subscr_pdp_data_clear(vsub);
804 if (rc > 0)
805 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
806 }
807
808 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
809 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
810 size_t ctx_id = pdp_info->context_id;
811 struct sgsn_subscriber_pdp_data *pdp_data;
812
813 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
814 LOGVSUBP(LOGL_ERROR, vsub,
815 "APN too long, context id = %zu, APN = %s\n",
816 ctx_id, osmo_hexdump(pdp_info->apn_enc,
817 pdp_info->apn_enc_len));
818 continue;
819 }
820
821 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
822 LOGVSUBP(LOGL_ERROR, vsub,
823 "QoS info too long (%zu)\n",
824 pdp_info->qos_enc_len);
825 continue;
826 }
827
828 LOGVSUBP(LOGL_INFO, vsub,
829 "Will set PDP info, context id = %zu, APN = %s\n",
830 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
831
832 /* Set PDP info [ctx_id] */
833 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
834 if (!pdp_data) {
835 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
836 pdp_data->context_id = ctx_id;
837 }
838
839 OSMO_ASSERT(pdp_data != NULL);
840 pdp_data->pdp_type = pdp_info->pdp_type;
841 osmo_apn_to_str(pdp_data->apn_str,
842 pdp_info->apn_enc, pdp_info->apn_enc_len);
843 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
844 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
845 }
846}
847
848
849/* Handle InsertSubscrData Result from HLR */
850static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
851 const struct osmo_gsup_message *gsup)
852{
853 struct osmo_gsup_message gsup_reply = {0};
854
855 vlr_subscr_gsup_insert_data(vsub, gsup);
856 vsub->vlr->ops.subscr_update(vsub);
857
858 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
859 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
860}
861
862/* Handle UpdateLocation Result from HLR */
863static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
864 const struct osmo_gsup_message *gsup)
865{
Philipp Maier483cea82019-04-03 16:23:29 +0200866 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100867 bool sgs_lu_in_progress = false;
868
869 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
870 sgs_lu_in_progress = true;
871
872 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200873 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
874 "without LU in progress\n");
875 return -ENODEV;
876 }
877
878 /* contrary to MAP, we allow piggy-backing subscriber data onto the
879 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
880 * nested INSERT SUBSCRIBER DATA transaction */
881 vlr_subscr_gsup_insert_data(vsub, gsup);
882
Harald Welte0df904d2018-12-03 11:00:04 +0100883 if (sgs_lu_in_progress) {
884 sgs_lu_response.accepted = true;
885 sgs_lu_response.vsub = vsub;
886 vsub->sgs.response_cb(&sgs_lu_response);
887 } else
888 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200889
890 return 0;
891}
892
893/* Handle UpdateLocation Result from HLR */
894static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
895 const struct osmo_gsup_message *gsup)
896{
Philipp Maier483cea82019-04-03 16:23:29 +0200897 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100898 bool sgs_lu_in_progress = false;
899
900 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
901 sgs_lu_in_progress = true;
902
903 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200904 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
905 "without LU in progress\n");
906 return -ENODEV;
907 }
908
909 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
910 get_value_string(gsm48_gmm_cause_names, gsup->cause));
911
Harald Welte0df904d2018-12-03 11:00:04 +0100912 if (sgs_lu_in_progress) {
913 sgs_lu_response.accepted = false;
914 sgs_lu_response.vsub = vsub;
915 vsub->sgs.response_cb(&sgs_lu_response);
916 } else
917 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
918 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200919 return 0;
920}
921
Alexander Couzens7312b152019-08-19 15:30:12 +0200922void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
923 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200924{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200925 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
926 switch (gmm_cause) {
927 case GMM_CAUSE_IMSI_UNKNOWN:
928 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
929 break;
930 case GMM_CAUSE_ILLEGAL_MS:
931 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
932 break;
933 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
934 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
935 break;
936 case GMM_CAUSE_ILLEGAL_ME:
937 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
938 break;
939 case GMM_CAUSE_GPRS_NOTALLOWED:
940 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
941 break;
942 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
943 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
944 break;
945 case GMM_CAUSE_MS_ID_NOT_DERIVED:
946 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
947 break;
948 case GMM_CAUSE_IMPL_DETACHED:
949 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
950 break;
951 case GMM_CAUSE_PLMN_NOTALLOWED:
952 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
953 break;
954 case GMM_CAUSE_LA_NOTALLOWED:
955 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
956 break;
957 case GMM_CAUSE_ROAMING_NOTALLOWED:
958 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
959 break;
960 case GMM_CAUSE_NO_GPRS_PLMN:
961 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
962 break;
963 case GMM_CAUSE_MSC_TEMP_NOTREACH:
964 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
965 break;
966 case GMM_CAUSE_SYNC_FAIL:
967 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
968 break;
969 case GMM_CAUSE_CONGESTION:
970 gsm48_rej = GSM48_REJECT_CONGESTION;
971 break;
972 case GMM_CAUSE_SEM_INCORR_MSG:
973 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
974 break;
975 case GMM_CAUSE_INV_MAND_INFO:
976 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
977 break;
978 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
979 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
980 break;
981 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
982 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
983 break;
984 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
985 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
986 break;
987 case GMM_CAUSE_COND_IE_ERR:
988 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
989 break;
990 case GMM_CAUSE_MSG_INCOMP_P_STATE:
991 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
992 break;
993 case GMM_CAUSE_PROTO_ERR_UNSPEC:
994 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
995 break;
996
997 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
998 case GMM_CAUSE_MAC_FAIL:
999 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1000 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1001 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1002 case GMM_CAUSE_NO_PDP_ACTIVATED:
1003 case GMM_CAUSE_NET_FAIL:
1004 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1005 break;
1006 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001007
1008 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001009}
1010
Harald Welteb8b85a12016-06-17 00:06:42 +02001011/* Handle LOCATION CANCEL request from HLR */
1012static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001013 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001014{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001015 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001016 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001017 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001018 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001019 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1020
1021 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1022 is_update_procedure ?
1023 "update procedure" : "subscription withdraw");
1024
1025 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001026 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001027
Alexander Couzens7312b152019-08-19 15:30:12 +02001028 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001029 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001030
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001031 vlr_subscr_rx_imsi_detach(vsub);
1032
Max770fbd22018-01-24 12:48:33 +01001033 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001034}
1035
Oliver Smith7d053092018-12-14 17:37:38 +01001036/* Handle Check_IMEI_VLR result and error from HLR */
1037static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1038{
1039 if (!vsub->lu_fsm) {
1040 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1041 osmo_gsup_message_type_name(gsup->message_type));
1042 return -ENODEV;
1043 }
1044
Oliver Smithcbf2c932019-05-06 13:09:55 +02001045 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1046 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001047 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1048 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1049 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1050 else
1051 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1052 } else {
1053 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1054 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1055 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1056 }
1057
1058 return 0;
1059}
1060
Harald Welteb8b85a12016-06-17 00:06:42 +02001061/* Incoming handler for GSUP from HLR.
1062 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001063int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001064{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001065 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001066 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001067 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001068
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001069 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001070 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001071 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001072 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1073 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001074 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001075 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001077 }
1078 }
1079
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001080 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001081 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1082 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001083 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001084 break;
1085 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001086 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001087 break;
1088 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001089 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001090 break;
1091 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001092 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001093 break;
1094 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001095 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001096 break;
1097 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1098 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1099 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1100 LOGVSUBP(LOGL_ERROR, vsub,
1101 "Rx GSUP msg_type=%d not yet implemented\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001102 gsup->message_type);
Harald Welteb8b85a12016-06-17 00:06:42 +02001103 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1104 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001105 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1106 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001107 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001108 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001109 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001110 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1111 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001112 break;
1113 }
1114
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001115 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001116 return rc;
1117}
1118
1119/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001120int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001121{
Harald Welteb8b85a12016-06-17 00:06:42 +02001122 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001123 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001124 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001125 if (vsub->imsi[0]
1126 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001127 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001128 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001129 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001130 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001131 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001132 break;
1133 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001134 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001135 break;
1136 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001137 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001138 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001139 default:
1140 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001141 }
1142
1143 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001144 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001145 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001146 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1147 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001148 break;
1149 }
1150 }
1151
1152 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001153 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001154 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001155 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001156 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001157 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001158 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001159 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001160 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001161 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001162 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001163 }
1164
1165 return 0;
1166}
1167
1168/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1169int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1170{
1171 if (vsub->lu_fsm) {
1172 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1173 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1174 } else if (vsub->proc_arq_fsm) {
1175 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1176 PR_ARQ_E_TMSI_ACK, NULL);
1177 } else {
1178 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001179 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001180 return -EINVAL;
1181 }
1182}
1183
Maxdcc193d2017-12-27 19:34:15 +01001184bool vlr_subscr_expire(struct vlr_subscr *vsub)
1185{
1186 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001187 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001188 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001189 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001190
1191 return true;
1192 }
1193
1194 return false;
1195}
1196
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001197/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001198int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1199{
1200 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001201 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001202
1203 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001204 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001205
Harald Welte0df904d2018-12-03 11:00:04 +01001206 /* Inform the UE-SGs FSM that the subscriber has been detached */
1207 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1208
Maxdcc193d2017-12-27 19:34:15 +01001209 vlr_subscr_expire(vsub);
1210
Harald Welteb8b85a12016-06-17 00:06:42 +02001211 return 0;
1212}
1213
1214/* Tear down any running FSMs due to MSC connection timeout.
1215 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1216 * message before the entire connection is torn down.
1217 * \param[in] vsub subscriber to tear down
1218 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001219void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001220{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001221 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001222}
1223
1224struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1225{
1226 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1227 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001228
1229 /* Some of these are needed only on UTRAN, but in case the caller wants
1230 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001231 OSMO_ASSERT(ops->tx_auth_req);
1232 OSMO_ASSERT(ops->tx_auth_rej);
1233 OSMO_ASSERT(ops->tx_id_req);
1234 OSMO_ASSERT(ops->tx_lu_acc);
1235 OSMO_ASSERT(ops->tx_lu_rej);
1236 OSMO_ASSERT(ops->tx_cm_serv_acc);
1237 OSMO_ASSERT(ops->tx_cm_serv_rej);
1238 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001239 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001240 OSMO_ASSERT(ops->subscr_update);
1241 OSMO_ASSERT(ops->subscr_assoc);
1242
1243 INIT_LLIST_HEAD(&vlr->subscribers);
1244 INIT_LLIST_HEAD(&vlr->operations);
1245 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1246
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001247 /* defaults */
1248 vlr->cfg.assign_tmsi = true;
1249
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001250 /* reset shared timer definitions */
1251 osmo_tdefs_reset(msc_tdefs_vlr);
1252
Harald Welteb8b85a12016-06-17 00:06:42 +02001253 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001254 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001255 /* osmo_lu_fsm.c */
1256 vlr_lu_fsm_init();
1257 /* vlr_access_request_fsm.c */
1258 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001259 /* vlr_sgs_fsm.c */
1260 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001261
1262 return vlr;
1263}
1264
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001265int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001266{
1267 OSMO_ASSERT(vlr);
1268
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001269 vlr->gcm = gcm;
1270 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1271 .func = vlr_gsup_rx,
1272 .data = vlr,
1273 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001274
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001275 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1276 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001277 return 0;
1278}
1279
1280/* MSC->VLR: Subscribre has disconnected */
1281int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1282{
1283 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1284 * interface */
1285 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1286 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1287 vsub->msc_conn_ref = NULL;
1288
1289 return 0;
1290}
1291
1292/* MSC->VLR: Receive Authentication Failure from Subscriber */
1293int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1294{
1295 struct vlr_auth_resp_par par = {0};
1296 par.auts = auts;
1297
1298 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1299 return 0;
1300}
1301
1302/* MSC->VLR: Receive Authentication Response from MS
1303 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1304int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1305 bool is_utran, const uint8_t *res, uint8_t res_len)
1306{
1307 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1308 struct vlr_auth_resp_par par;
1309
1310 par.is_r99 = is_r99;
1311 par.is_utran = is_utran;
1312 par.res = res;
1313 par.res_len = res_len;
1314 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1315
1316 return 0;
1317}
1318
1319/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001320void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001321{
1322 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001323 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001324 if (vsub->proc_arq_fsm
1325 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001326 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001327}
1328
1329/* Internal evaluation of requested ciphering mode.
1330 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1331 * \param[in] vlr VLR instance.
1332 * \param[in] fi Calling FSM instance, for logging.
1333 * \param[in] msc_conn_ref MSC conn to send to.
1334 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1335 * \returns 0 if no ciphering is needed or message was sent successfully,
1336 * or a negative value if ciph_mode is invalid or sending failed.
1337 */
1338int vlr_set_ciph_mode(struct vlr_instance *vlr,
1339 struct osmo_fsm_inst *fi,
1340 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001341 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001342 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001343 bool retrieve_imeisv)
1344{
Harald Welte71c51df2017-12-23 18:51:48 +01001345 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001346 return 0;
1347
Harald Welte71c51df2017-12-23 18:51:48 +01001348 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1349 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001350}
1351
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001352/* Decide whether UMTS AKA should be used.
1353 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1354 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1355 * GERAN are R99 capable and UMTS AKA tokens are available.
1356 * \param[in] vec Auth tokens (received from the HLR).
1357 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1358 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1359 */
1360bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1361{
1362 if (!is_r99)
1363 return false;
1364 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1365 return false;
1366 return true;
1367}
1368
Harald Welteb8b85a12016-06-17 00:06:42 +02001369void log_set_filter_vlr_subscr(struct log_target *target,
1370 struct vlr_subscr *vlr_subscr)
1371{
1372 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001373 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001374
1375 /* free the old data */
1376 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001377 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001378 *fsub = NULL;
1379 }
1380
1381 if (vlr_subscr) {
1382 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001383 vlr_subscr_get(vlr_subscr, use);
1384 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001385 } else
1386 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1387}