blob: 02aceef8ea2c52fae3c637814d13255fa2137781 [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>
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +020028#include <osmocom/gsm/gsm23236.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020029#include <osmocom/gsm/gsup.h>
30#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020031#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010032#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020034#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010035#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020036#include <osmocom/msc/vlr.h>
37#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038#include <osmocom/msc/gsup_client_mux.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020039
Harald Welteb8b85a12016-06-17 00:06:42 +020040#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020043#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020044
45#include "vlr_core.h"
46#include "vlr_auth_fsm.h"
47#include "vlr_lu_fsm.h"
48#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010049#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020050
51#define SGSN_SUBSCR_MAX_RETRIES 3
52#define SGSN_SUBSCR_RETRY_INTERVAL 10
53
54/***********************************************************************
55 * Convenience functions
56 ***********************************************************************/
57
58const struct value_string vlr_ciph_names[] = {
59 OSMO_VALUE_STRING(VLR_CIPH_NONE),
60 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
61 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
62 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
63 { 0, NULL }
64};
65
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070066/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
67struct osmo_tdef msc_tdefs_vlr[] = {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070068 { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" },
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070069 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
70 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
71 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
72 { /* terminator */ }
73};
74
75/* This is just a wrapper around the osmo_tdef API.
76 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +020077uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
78{
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070079 /* NOTE: since we usually do not need more than one instance of the VLR,
80 * and since libosmocore's osmo_tdef API does not (yet) support dynamic
81 * configuration, we always use the global instance of msc_tdefs_vlr. */
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070082 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +020083}
84
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010085/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010086const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010087{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010088 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010089 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010090 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010091 if (!vsub)
92 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010093 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010094 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010095 present = true;
96 }
97 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010098 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010099 present = true;
100 }
101 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100102 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100103 present = true;
104 }
105 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100106 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100107 present = true;
108 }
109 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100110 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100111
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100112 return buf;
113}
114
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100115const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
116{
117 /* cast away the const so we can shorten the string within the static buffer */
118 char *name = (char*)vlr_subscr_name(vsub);
119 size_t len = strlen(name);
120 if (maxlen < 2)
121 return "-";
122 if (len > maxlen)
123 strcpy(name + maxlen - 2, "..");
124 return name;
125}
126
Oliver Smith5598aae2019-01-08 11:47:21 +0100127const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100128{
129 if (!vsub || !vsub->msisdn[0])
130 return vlr_subscr_name(vsub);
131 return vsub->msisdn;
132}
133
Harald Welteb8b85a12016-06-17 00:06:42 +0200134struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
135 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100136 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200137 const char *file, int line)
138{
139 struct vlr_subscr *vsub;
140
141 if (!imsi || !*imsi)
142 return NULL;
143
144 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100145 if (vlr_subscr_matches_imsi(vsub, imsi)) {
146 vlr_subscr_get_src(vsub, use, file, line);
147 return vsub;
148 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200149 }
150 return NULL;
151}
152
153struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
154 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100155 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200156 const char *file, int line)
157{
158 struct vlr_subscr *vsub;
159
160 if (tmsi == GSM_RESERVED_TMSI)
161 return NULL;
162
163 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100164 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
165 vlr_subscr_get_src(vsub, use, file, line);
166 return vsub;
167 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200168 }
169 return NULL;
170}
171
172struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
173 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100174 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200175 const char *file, int line)
176{
177 struct vlr_subscr *vsub;
178
179 if (!msisdn || !*msisdn)
180 return NULL;
181
182 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100183 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
184 vlr_subscr_get_src(vsub, use, file, line);
185 return vsub;
186 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200187 }
188 return NULL;
189}
190
Harald Welteb8b85a12016-06-17 00:06:42 +0200191/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100192static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200193 struct osmo_gsup_message *gsup_msg)
194{
195 struct vlr_instance *vlr = vsub->vlr;
196
197 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100198 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200199
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100200 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200201
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100202 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200203}
204
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100205static 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 +0200206{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100207 struct vlr_subscr *vsub = e->use_count->talloc_object;
208 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100209 int32_t total;
210 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200211
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100212 if (!e->use)
213 return -EINVAL;
214
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100215 total = osmo_use_count_total(&vsub->use_count);
216
217 if (total == 0
218 || (total == 1 && old_use_count == 0 && e->count == 1))
219 level = LOGL_INFO;
220 else
221 level = LOGL_DEBUG;
222
223 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100224 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
225 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
226
227 if (e->count < 0)
228 return -ERANGE;
229
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100230 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
231
232 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100233 vlr_subscr_free(vsub);
234 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200235}
236
237/* Allocate a new subscriber and insert it into list */
238static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
239{
240 struct vlr_subscr *vsub;
241 int i;
242
243 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100244 *vsub = (struct vlr_subscr){
245 .vlr = vlr,
246 .tmsi = GSM_RESERVED_TMSI,
247 .tmsi_new = GSM_RESERVED_TMSI,
248 .use_count = (struct osmo_use_count){
249 .talloc_object = vsub,
250 .use_cb = vlr_subscr_use_cb,
251 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100252 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100253 };
254 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 +0200255
256 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100257 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200258
259 INIT_LLIST_HEAD(&vsub->cs.requests);
260 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
261
Harald Welte0df904d2018-12-03 11:00:04 +0100262 /* Create an SGs FSM, which is needed to control CSFB,
263 * in cases where CSFB/SGs is not in use, this FSM will
264 * just do nothing. (see also: sgs_iface.c) */
265 vlr_sgs_fsm_create(vsub);
266
Harald Welteb8b85a12016-06-17 00:06:42 +0200267 llist_add_tail(&vsub->list, &vlr->subscribers);
268 return vsub;
269}
270
Harald Welteb8b85a12016-06-17 00:06:42 +0200271/* Send a GSUP Purge MS request.
272 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
273 * this subscriber, not to the HLR? */
274int vlr_subscr_purge(struct vlr_subscr *vsub)
275{
276 struct osmo_gsup_message gsup_msg = {0};
277
278 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
279
280 /* provide HLR number in case we know it */
281 gsup_msg.hlr_enc_len = vsub->hlr.len;
282 gsup_msg.hlr_enc = vsub->hlr.buf;
283
284 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
285}
286
Neels Hofmeyr15809592018-04-06 02:57:51 +0200287void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
288 enum osmo_fsm_term_cause fsm_cause,
289 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200290{
291 if (!vsub)
292 return;
293
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100294 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200295 if (vsub->lu_fsm)
296 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200297 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200298 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100299 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200300}
301
302/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
303void vlr_subscr_free(struct vlr_subscr *vsub)
304{
305 llist_del(&vsub->list);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100306 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
307 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100308
309 /* Make sure SGs timer Ts5 is removed */
310 osmo_timer_del(&vsub->sgs.Ts5);
311
312 /* Remove SGs FSM (see also: sgs_iface.c) */
313 vlr_sgs_fsm_remove(vsub);
314
Harald Welteb8b85a12016-06-17 00:06:42 +0200315 talloc_free(vsub);
316}
317
318/* Generate a new TMSI and store in vsub->tmsi_new.
319 * Search all known subscribers to ensure that the TMSI is unique. */
320int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
321{
322 struct vlr_instance *vlr = vsub->vlr;
323 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100324 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100325 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200326
327 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100328 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
329 if (rc < 0) {
330 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
331 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200332 }
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +0200333
334 if (!llist_empty(&vlr->cfg.nri_ranges->entries)) {
335 int16_t nri_v;
336 osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen);
337 osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen);
338 LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n",
339 osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi);
340 }
341
Harald Welteb8b85a12016-06-17 00:06:42 +0200342 /* throw the dice again, if the TSMI doesn't fit */
343 if (tmsi == GSM_RESERVED_TMSI)
344 continue;
345
346 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
347 if (vlr->cfg.is_ps) {
348 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200349 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200350 } else {
351 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200352 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
353 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200354 }
355
356 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100357 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
358 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200359 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100360 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200361
362 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100363 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200364 return 0;
365 }
366
367 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
368 " after %d tries\n", vlr_subscr_name(vsub), tried);
369 return -1;
370}
371
372/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100373 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200374 * \param[in] imsi IMSI string.
375 * \param[out] created if non-NULL, returns whether a new entry was created. */
376struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
377 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100378 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200379 bool *created,
380 const char *file,
381 int line)
382{
383 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100384 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200385 if (vsub) {
386 if (created)
387 *created = false;
388 return vsub;
389 }
390
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100391 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200392 if (!vsub)
393 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100394 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200395 vlr_subscr_set_imsi(vsub, imsi);
396 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
397 if (created)
398 *created = true;
399 return vsub;
400}
401
402/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100403 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200404 * \param[in] tmsi TMSI.
405 * \param[out] created if non-NULL, returns whether a new entry was created. */
406struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
407 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100408 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200409 bool *created,
410 const char *file,
411 int line)
412{
413 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100414 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200415 if (vsub) {
416 if (created)
417 *created = false;
418 return vsub;
419 }
420
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100421 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200422 if (!vsub)
423 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100424 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200425 vsub->tmsi = tmsi;
426 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
427 if (created)
428 *created = true;
429 return vsub;
430}
431
432void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
433{
434 if (!vsub)
435 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200436
437 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
438 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
439 imsi, vsub->imsi);
440 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
441 }
442
Harald Welteb8b85a12016-06-17 00:06:42 +0200443 vsub->id = atoll(vsub->imsi);
444 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
445 vsub->imsi, vsub->id);
446}
447
448void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
449{
450 if (!vsub)
451 return;
Max98f74672018-02-05 12:57:06 +0100452 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200453 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
454 vsub->imsi, vsub->imei);
455}
456
457void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
458{
459 if (!vsub)
460 return;
Max98f74672018-02-05 12:57:06 +0100461 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200462 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
463 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200464
465 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
466 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200467}
468
469/* Safely copy the given MSISDN string to vsub->msisdn */
470void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
471{
472 if (!vsub)
473 return;
Max98f74672018-02-05 12:57:06 +0100474 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200475 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
476 vsub->imsi, vsub->msisdn);
477}
478
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200479void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
480 const struct osmo_plmn_id *last_eutran_plmn)
481{
482 if (!vsub)
483 return;
484 if (last_eutran_plmn) {
485 vsub->sgs.last_eutran_plmn_present = true;
486 memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
487 } else {
488 vsub->sgs.last_eutran_plmn_present = false;
489 }
490 DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
491 vsub->sgs.last_eutran_plmn_present ?
492 osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
493 "(none)");
494}
495
Harald Welteb8b85a12016-06-17 00:06:42 +0200496bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
497{
498 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
499}
500
501bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
502{
503 return vsub && tmsi != GSM_RESERVED_TMSI
504 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
505}
506
507bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
508{
509 return vsub && msisdn && vsub->msisdn[0]
510 && !strcmp(vsub->msisdn, msisdn);
511}
512
513bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
514{
515 return vsub && imei && vsub->imei[0]
516 && !strcmp(vsub->imei, imei);
517}
518
519/* Send updated subscriber information to HLR */
520int vlr_subscr_changed(struct vlr_subscr *vsub)
521{
522 /* FIXME */
523 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
524 return 0;
525}
526
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200527void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
528{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200529 struct timespec now;
530
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700531 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200532 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700533 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200534 } else {
535 LOGP(DVLR, LOGL_ERROR,
536 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
537 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
538 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
539 }
540}
541
542void vlr_subscr_expire_lu(void *data)
543{
544 struct vlr_instance *vlr = data;
545 struct vlr_subscr *vsub, *vsub_tmp;
546 struct timespec now;
547
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700548 /* Periodic location update might be disabled from the VTY,
549 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700550 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700551 goto done;
552
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200553 if (llist_empty(&vlr->subscribers))
554 goto done;
555
556 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
557 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
558 goto done;
559 }
560
561 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
562 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
563 continue;
564
565 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
566 vlr_subscr_rx_imsi_detach(vsub);
567 }
568
569done:
570 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
571}
572
Harald Welteb8b85a12016-06-17 00:06:42 +0200573/***********************************************************************
574 * PDP context data
575 ***********************************************************************/
576
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200577#define GSM_APN_LENGTH 102
578
579/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
580/* see GSM 09.02, B.1, gprsSubscriptionData */
581struct sgsn_subscriber_pdp_data {
582 struct llist_head list;
583
584 unsigned int context_id;
585 uint16_t pdp_type;
586 char apn_str[GSM_APN_LENGTH];
587 uint8_t qos_subscribed[20];
588 size_t qos_subscribed_len;
589};
590
Harald Welteb8b85a12016-06-17 00:06:42 +0200591struct sgsn_subscriber_pdp_data *
592vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
593{
594 struct sgsn_subscriber_pdp_data* pdata;
595
596 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
597
598 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
599
600 return pdata;
601}
602
603static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
604{
605 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
606 int count = 0;
607
608 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
609 llist_del(&pdp->list);
610 talloc_free(pdp);
611 count += 1;
612 }
613
614 return count;
615}
616
617static struct sgsn_subscriber_pdp_data *
618vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
619{
620 struct sgsn_subscriber_pdp_data *pdp;
621
622 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
623 if (pdp->context_id == context_id)
624 return pdp;
625 }
626
627 return NULL;
628}
629
630/***********************************************************************
631 * Actual Implementation
632 ***********************************************************************/
633
634static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100635 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200636{
637 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200638 LOGP(DVLR, LOGL_NOTICE,
639 "Unknown IMSI %s, discarding GSUP request "
640 "of type 0x%02x\n",
641 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100642 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200643 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
644 LOGP(DVLR, LOGL_NOTICE,
645 "Unknown IMSI %s, discarding GSUP error "
646 "of type 0x%02x, cause '%s' (%d)\n",
647 gsup_msg->imsi, gsup_msg->message_type,
648 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
649 gsup_msg->cause);
650 } else {
651 LOGP(DVLR, LOGL_NOTICE,
652 "Unknown IMSI %s, discarding GSUP response "
653 "of type 0x%02x\n",
654 gsup_msg->imsi, gsup_msg->message_type);
655 }
656
657 return -GMM_CAUSE_IMSI_UNKNOWN;
658}
659
660static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100661 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200662{
663 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
664 LOGGSUPP(LOGL_NOTICE, gsup_msg,
665 "Purge MS has failed with cause '%s' (%d)\n",
666 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
667 gsup_msg->cause);
668 return -gsup_msg->cause;
669 }
670 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
671 return 0;
672}
673
674/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100675int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200676{
677 struct osmo_gsup_message gsup_msg = {0};
678 int rc;
679
680 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200681 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 +0200682 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
683
684 return rc;
685}
686
687/* VLR internal call to request tuples from HLR */
688int vlr_subscr_req_sai(struct vlr_subscr *vsub,
689 const uint8_t *auts, const uint8_t *auts_rand)
690{
691 struct osmo_gsup_message gsup_msg = {0};
692
693 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
694 gsup_msg.auts = auts;
695 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100696 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200697
698 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
699}
700
Oliver Smith7d053092018-12-14 17:37:38 +0100701/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
702int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
703{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100704 struct osmo_gsup_message gsup_msg = {
705 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200706 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100707 };
Oliver Smith7d053092018-12-14 17:37:38 +0100708 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
709 int len;
710
711 /* Encode IMEI */
712 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
713 if (len < 1) {
714 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
715 return -ENOSPC;
716 }
717 gsup_msg.imei_enc = imei_enc;
718 gsup_msg.imei_enc_len = len;
719
720 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100721 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100722 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100723}
724
Harald Welteb8b85a12016-06-17 00:06:42 +0200725/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100726int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200727{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100728 struct osmo_gsup_message gsup_msg = {
729 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200730 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100731 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200732
Max98f74672018-02-05 12:57:06 +0100733 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100734 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200735}
736
737/* Update the subscriber with GSUP-received auth tuples */
738void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
739 const struct osmo_gsup_message *gsup)
740{
741 unsigned int i;
742 unsigned int got_tuples;
743
744 if (gsup->num_auth_vectors) {
745 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
746 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100747 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200748 }
749
750 got_tuples = 0;
751 for (i = 0; i < gsup->num_auth_vectors; i++) {
752 size_t key_seq = i;
753
754 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
755 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100756 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200757 key_seq);
758 continue;
759 }
760 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
761 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100762 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200763 }
764
765 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
766
767 if (!got_tuples) {
768 /* FIXME what now? */
769 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
770 }
771
772 /* New tuples means last_tuple becomes invalid */
773 vsub->last_tuple = NULL;
774}
775
776/* Handle SendAuthInfo Result/Error from HLR */
777static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
778 const struct osmo_gsup_message *gsup)
779{
780 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
781 void *data = (void *) gsup;
782
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200783 if (!auth_fi) {
784 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
785 osmo_gsup_message_type_name(gsup->message_type));
786 return -1;
787 }
788
Harald Welteb8b85a12016-06-17 00:06:42 +0200789 switch (gsup->message_type) {
790 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
791 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
792 break;
793 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
794 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
795 break;
796 default:
797 return -1;
798 }
799
800 return 0;
801}
802
Harald Welteb8b85a12016-06-17 00:06:42 +0200803static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
804 const struct osmo_gsup_message *gsup_msg)
805{
806 unsigned idx;
807 int rc;
808
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100809 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100810 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
811 gsup_msg->msisdn_enc,
812 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200813 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
814 vsub->imsi, vsub->msisdn);
815 }
816
817 if (gsup_msg->hlr_enc) {
818 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
819 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
820 gsup_msg->hlr_enc_len);
821 vsub->hlr.len = 0;
822 } else {
823 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
824 gsup_msg->hlr_enc_len);
825 vsub->hlr.len = gsup_msg->hlr_enc_len;
826 }
827 }
828
829 if (gsup_msg->pdp_info_compl) {
830 rc = vlr_subscr_pdp_data_clear(vsub);
831 if (rc > 0)
832 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
833 }
834
835 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
836 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
837 size_t ctx_id = pdp_info->context_id;
838 struct sgsn_subscriber_pdp_data *pdp_data;
839
840 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
841 LOGVSUBP(LOGL_ERROR, vsub,
842 "APN too long, context id = %zu, APN = %s\n",
843 ctx_id, osmo_hexdump(pdp_info->apn_enc,
844 pdp_info->apn_enc_len));
845 continue;
846 }
847
848 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
849 LOGVSUBP(LOGL_ERROR, vsub,
850 "QoS info too long (%zu)\n",
851 pdp_info->qos_enc_len);
852 continue;
853 }
854
855 LOGVSUBP(LOGL_INFO, vsub,
856 "Will set PDP info, context id = %zu, APN = %s\n",
857 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
858
859 /* Set PDP info [ctx_id] */
860 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
861 if (!pdp_data) {
862 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
863 pdp_data->context_id = ctx_id;
864 }
865
866 OSMO_ASSERT(pdp_data != NULL);
867 pdp_data->pdp_type = pdp_info->pdp_type;
868 osmo_apn_to_str(pdp_data->apn_str,
869 pdp_info->apn_enc, pdp_info->apn_enc_len);
870 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
871 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
872 }
873}
874
875
876/* Handle InsertSubscrData Result from HLR */
877static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
878 const struct osmo_gsup_message *gsup)
879{
880 struct osmo_gsup_message gsup_reply = {0};
881
882 vlr_subscr_gsup_insert_data(vsub, gsup);
883 vsub->vlr->ops.subscr_update(vsub);
884
885 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
886 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
887}
888
889/* Handle UpdateLocation Result from HLR */
890static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
891 const struct osmo_gsup_message *gsup)
892{
Philipp Maier483cea82019-04-03 16:23:29 +0200893 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100894 bool sgs_lu_in_progress = false;
895
896 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
897 sgs_lu_in_progress = true;
898
899 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200900 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
901 "without LU in progress\n");
902 return -ENODEV;
903 }
904
905 /* contrary to MAP, we allow piggy-backing subscriber data onto the
906 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
907 * nested INSERT SUBSCRIBER DATA transaction */
908 vlr_subscr_gsup_insert_data(vsub, gsup);
909
Harald Welte0df904d2018-12-03 11:00:04 +0100910 if (sgs_lu_in_progress) {
911 sgs_lu_response.accepted = true;
912 sgs_lu_response.vsub = vsub;
913 vsub->sgs.response_cb(&sgs_lu_response);
914 } else
915 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200916
917 return 0;
918}
919
920/* Handle UpdateLocation Result from HLR */
921static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
922 const struct osmo_gsup_message *gsup)
923{
Philipp Maier483cea82019-04-03 16:23:29 +0200924 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100925 bool sgs_lu_in_progress = false;
926
927 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
928 sgs_lu_in_progress = true;
929
930 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200931 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
932 "without LU in progress\n");
933 return -ENODEV;
934 }
935
936 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
937 get_value_string(gsm48_gmm_cause_names, gsup->cause));
938
Harald Welte0df904d2018-12-03 11:00:04 +0100939 if (sgs_lu_in_progress) {
940 sgs_lu_response.accepted = false;
941 sgs_lu_response.vsub = vsub;
942 vsub->sgs.response_cb(&sgs_lu_response);
943 } else
944 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
945 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200946 return 0;
947}
948
Alexander Couzens7312b152019-08-19 15:30:12 +0200949void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
950 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200951{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200952 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
953 switch (gmm_cause) {
954 case GMM_CAUSE_IMSI_UNKNOWN:
955 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
956 break;
957 case GMM_CAUSE_ILLEGAL_MS:
958 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
959 break;
960 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
961 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
962 break;
963 case GMM_CAUSE_ILLEGAL_ME:
964 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
965 break;
966 case GMM_CAUSE_GPRS_NOTALLOWED:
967 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
968 break;
969 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
970 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
971 break;
972 case GMM_CAUSE_MS_ID_NOT_DERIVED:
973 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
974 break;
975 case GMM_CAUSE_IMPL_DETACHED:
976 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
977 break;
978 case GMM_CAUSE_PLMN_NOTALLOWED:
979 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
980 break;
981 case GMM_CAUSE_LA_NOTALLOWED:
982 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
983 break;
984 case GMM_CAUSE_ROAMING_NOTALLOWED:
985 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
986 break;
987 case GMM_CAUSE_NO_GPRS_PLMN:
988 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
989 break;
990 case GMM_CAUSE_MSC_TEMP_NOTREACH:
991 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
992 break;
993 case GMM_CAUSE_SYNC_FAIL:
994 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
995 break;
996 case GMM_CAUSE_CONGESTION:
997 gsm48_rej = GSM48_REJECT_CONGESTION;
998 break;
999 case GMM_CAUSE_SEM_INCORR_MSG:
1000 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1001 break;
1002 case GMM_CAUSE_INV_MAND_INFO:
1003 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1004 break;
1005 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1006 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1007 break;
1008 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1009 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1010 break;
1011 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1012 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1013 break;
1014 case GMM_CAUSE_COND_IE_ERR:
1015 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1016 break;
1017 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1018 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1019 break;
1020 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1021 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1022 break;
1023
1024 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1025 case GMM_CAUSE_MAC_FAIL:
1026 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1027 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1028 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1029 case GMM_CAUSE_NO_PDP_ACTIVATED:
1030 case GMM_CAUSE_NET_FAIL:
1031 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1032 break;
1033 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001034
1035 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001036}
1037
Harald Welteb8b85a12016-06-17 00:06:42 +02001038/* Handle LOCATION CANCEL request from HLR */
1039static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001040 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001041{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001042 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001043 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001044 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001045 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001046 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1047
1048 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1049 is_update_procedure ?
1050 "update procedure" : "subscription withdraw");
1051
1052 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001053 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001054
Alexander Couzens7312b152019-08-19 15:30:12 +02001055 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001056 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001057
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001058 vlr_subscr_rx_imsi_detach(vsub);
1059
Max770fbd22018-01-24 12:48:33 +01001060 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001061}
1062
Oliver Smith7d053092018-12-14 17:37:38 +01001063/* Handle Check_IMEI_VLR result and error from HLR */
1064static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1065{
1066 if (!vsub->lu_fsm) {
1067 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1068 osmo_gsup_message_type_name(gsup->message_type));
1069 return -ENODEV;
1070 }
1071
Oliver Smithcbf2c932019-05-06 13:09:55 +02001072 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1073 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001074 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1075 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1076 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1077 else
1078 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1079 } else {
1080 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1081 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1082 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1083 }
1084
1085 return 0;
1086}
1087
Harald Welteb8b85a12016-06-17 00:06:42 +02001088/* Incoming handler for GSUP from HLR.
1089 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001090int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001091{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001092 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001093 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001094 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001095
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001096 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001097 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001098 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001099 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1100 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001101 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001102 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001103 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001104 }
1105 }
1106
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001107 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001108 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1109 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001110 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001111 break;
1112 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001113 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001114 break;
1115 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001116 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001117 break;
1118 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001119 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001120 break;
1121 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001122 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001123 break;
1124 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1125 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1126 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1127 LOGVSUBP(LOGL_ERROR, vsub,
1128 "Rx GSUP msg_type=%d not yet implemented\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001129 gsup->message_type);
Harald Welteb8b85a12016-06-17 00:06:42 +02001130 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1131 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001132 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1133 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001134 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001135 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001136 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001137 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1138 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001139 break;
1140 }
1141
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001142 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001143 return rc;
1144}
1145
1146/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001147int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001148{
Harald Welteb8b85a12016-06-17 00:06:42 +02001149 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001150 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001151 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001152 if (vsub->imsi[0]
1153 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001154 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001155 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001156 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001157 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001158 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001159 break;
1160 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001161 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001162 break;
1163 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001164 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001165 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001166 default:
1167 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001168 }
1169
1170 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001171 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001172 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001173 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1174 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001175 break;
1176 }
1177 }
1178
1179 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001180 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001181 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001182 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001183 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001184 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001185 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001186 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001187 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001188 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001189 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001190 }
1191
1192 return 0;
1193}
1194
1195/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1196int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1197{
1198 if (vsub->lu_fsm) {
1199 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1200 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1201 } else if (vsub->proc_arq_fsm) {
1202 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1203 PR_ARQ_E_TMSI_ACK, NULL);
1204 } else {
1205 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001206 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001207 return -EINVAL;
1208 }
1209}
1210
Maxdcc193d2017-12-27 19:34:15 +01001211bool vlr_subscr_expire(struct vlr_subscr *vsub)
1212{
1213 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001214 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001215 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001216 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001217
1218 return true;
1219 }
1220
1221 return false;
1222}
1223
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001224/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001225int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1226{
1227 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001228 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001229
1230 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001231 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001232
Harald Welte0df904d2018-12-03 11:00:04 +01001233 /* Inform the UE-SGs FSM that the subscriber has been detached */
1234 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1235
Maxdcc193d2017-12-27 19:34:15 +01001236 vlr_subscr_expire(vsub);
1237
Harald Welteb8b85a12016-06-17 00:06:42 +02001238 return 0;
1239}
1240
1241/* Tear down any running FSMs due to MSC connection timeout.
1242 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1243 * message before the entire connection is torn down.
1244 * \param[in] vsub subscriber to tear down
1245 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001246void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001247{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001248 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001249}
1250
1251struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1252{
1253 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1254 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001255
1256 /* Some of these are needed only on UTRAN, but in case the caller wants
1257 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001258 OSMO_ASSERT(ops->tx_auth_req);
1259 OSMO_ASSERT(ops->tx_auth_rej);
1260 OSMO_ASSERT(ops->tx_id_req);
1261 OSMO_ASSERT(ops->tx_lu_acc);
1262 OSMO_ASSERT(ops->tx_lu_rej);
1263 OSMO_ASSERT(ops->tx_cm_serv_acc);
1264 OSMO_ASSERT(ops->tx_cm_serv_rej);
1265 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001266 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001267 OSMO_ASSERT(ops->subscr_update);
1268 OSMO_ASSERT(ops->subscr_assoc);
1269
1270 INIT_LLIST_HEAD(&vlr->subscribers);
1271 INIT_LLIST_HEAD(&vlr->operations);
1272 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1273
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001274 /* defaults */
1275 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001276 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1277 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001278
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001279 /* reset shared timer definitions */
1280 osmo_tdefs_reset(msc_tdefs_vlr);
1281
Harald Welteb8b85a12016-06-17 00:06:42 +02001282 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001283 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001284 /* osmo_lu_fsm.c */
1285 vlr_lu_fsm_init();
1286 /* vlr_access_request_fsm.c */
1287 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001288 /* vlr_sgs_fsm.c */
1289 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001290
1291 return vlr;
1292}
1293
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001294int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001295{
1296 OSMO_ASSERT(vlr);
1297
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001298 vlr->gcm = gcm;
1299 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1300 .func = vlr_gsup_rx,
1301 .data = vlr,
1302 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001303
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001304 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1305 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001306 return 0;
1307}
1308
1309/* MSC->VLR: Subscribre has disconnected */
1310int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1311{
1312 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1313 * interface */
1314 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1315 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1316 vsub->msc_conn_ref = NULL;
1317
1318 return 0;
1319}
1320
1321/* MSC->VLR: Receive Authentication Failure from Subscriber */
1322int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1323{
1324 struct vlr_auth_resp_par par = {0};
1325 par.auts = auts;
1326
1327 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1328 return 0;
1329}
1330
1331/* MSC->VLR: Receive Authentication Response from MS
1332 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1333int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1334 bool is_utran, const uint8_t *res, uint8_t res_len)
1335{
1336 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1337 struct vlr_auth_resp_par par;
1338
1339 par.is_r99 = is_r99;
1340 par.is_utran = is_utran;
1341 par.res = res;
1342 par.res_len = res_len;
1343 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1344
1345 return 0;
1346}
1347
1348/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001349void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001350{
1351 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001352 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001353 if (vsub->proc_arq_fsm
1354 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001355 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001356}
1357
1358/* Internal evaluation of requested ciphering mode.
1359 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1360 * \param[in] vlr VLR instance.
1361 * \param[in] fi Calling FSM instance, for logging.
1362 * \param[in] msc_conn_ref MSC conn to send to.
1363 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1364 * \returns 0 if no ciphering is needed or message was sent successfully,
1365 * or a negative value if ciph_mode is invalid or sending failed.
1366 */
1367int vlr_set_ciph_mode(struct vlr_instance *vlr,
1368 struct osmo_fsm_inst *fi,
1369 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001370 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001371 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001372 bool retrieve_imeisv)
1373{
Harald Welte71c51df2017-12-23 18:51:48 +01001374 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001375 return 0;
1376
Harald Welte71c51df2017-12-23 18:51:48 +01001377 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1378 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001379}
1380
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001381/* Decide whether UMTS AKA should be used.
1382 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1383 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1384 * GERAN are R99 capable and UMTS AKA tokens are available.
1385 * \param[in] vec Auth tokens (received from the HLR).
1386 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1387 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1388 */
1389bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1390{
1391 if (!is_r99)
1392 return false;
1393 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1394 return false;
1395 return true;
1396}
1397
Harald Welteb8b85a12016-06-17 00:06:42 +02001398void log_set_filter_vlr_subscr(struct log_target *target,
1399 struct vlr_subscr *vlr_subscr)
1400{
1401 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001402 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001403
1404 /* free the old data */
1405 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001406 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001407 *fsub = NULL;
1408 }
1409
1410 if (vlr_subscr) {
1411 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001412 vlr_subscr_get(vlr_subscr, use);
1413 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001414 } else
1415 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1416}