blob: f98fee6ea8b55d37992dd87eb192345d9f9d3c08 [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
Harald Welte173bdf32022-05-15 12:12:54 +020058static int vlr_subscr_detach(struct vlr_subscr *vsub);
59
Harald Welteb8b85a12016-06-17 00:06:42 +020060const struct value_string vlr_ciph_names[] = {
61 OSMO_VALUE_STRING(VLR_CIPH_NONE),
62 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
63 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
64 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
65 { 0, NULL }
66};
67
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070068/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
69struct osmo_tdef msc_tdefs_vlr[] = {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070070 { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" },
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070071 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
72 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
73 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
74 { /* terminator */ }
75};
76
77/* This is just a wrapper around the osmo_tdef API.
78 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +020079uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
80{
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +070081 /* NOTE: since we usually do not need more than one instance of the VLR,
82 * and since libosmocore's osmo_tdef API does not (yet) support dynamic
83 * configuration, we always use the global instance of msc_tdefs_vlr. */
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070084 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +020085}
86
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010087/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010088const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010089{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010090 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010091 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010092 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010093 if (!vsub)
94 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010095 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010096 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010097 present = true;
98 }
99 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100100 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100101 present = true;
102 }
103 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100104 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100105 present = true;
106 }
107 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100108 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100109 present = true;
110 }
111 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100112 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100113
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100114 return buf;
115}
116
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100117const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
118{
119 /* cast away the const so we can shorten the string within the static buffer */
120 char *name = (char*)vlr_subscr_name(vsub);
121 size_t len = strlen(name);
122 if (maxlen < 2)
123 return "-";
124 if (len > maxlen)
125 strcpy(name + maxlen - 2, "..");
126 return name;
127}
128
Oliver Smith5598aae2019-01-08 11:47:21 +0100129const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100130{
131 if (!vsub || !vsub->msisdn[0])
132 return vlr_subscr_name(vsub);
133 return vsub->msisdn;
134}
135
Harald Welteb8b85a12016-06-17 00:06:42 +0200136struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
137 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100138 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200139 const char *file, int line)
140{
141 struct vlr_subscr *vsub;
142
143 if (!imsi || !*imsi)
144 return NULL;
145
146 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100147 if (vlr_subscr_matches_imsi(vsub, imsi)) {
148 vlr_subscr_get_src(vsub, use, file, line);
149 return vsub;
150 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200151 }
152 return NULL;
153}
154
155struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
156 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100157 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200158 const char *file, int line)
159{
160 struct vlr_subscr *vsub;
161
162 if (tmsi == GSM_RESERVED_TMSI)
163 return NULL;
164
165 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100166 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
167 vlr_subscr_get_src(vsub, use, file, line);
168 return vsub;
169 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200170 }
171 return NULL;
172}
173
174struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
175 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100176 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200177 const char *file, int line)
178{
179 struct vlr_subscr *vsub;
180
181 if (!msisdn || !*msisdn)
182 return NULL;
183
184 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100185 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
186 vlr_subscr_get_src(vsub, use, file, line);
187 return vsub;
188 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200189 }
190 return NULL;
191}
192
Neels Hofmeyr5bdba0d2021-07-27 03:46:18 +0200193struct vlr_subscr *_vlr_subscr_find_by_mi(struct vlr_instance *vlr,
194 const struct osmo_mobile_identity *mi,
195 const char *use,
196 const char *file, int line)
197{
198 switch (mi->type) {
199 case GSM_MI_TYPE_IMSI:
200 return _vlr_subscr_find_by_imsi(vlr, mi->imsi, use, file, line);
201 case GSM_MI_TYPE_TMSI:
202 return _vlr_subscr_find_by_tmsi(vlr, mi->tmsi, use, file, line);
203 default:
204 return NULL;
205 }
206}
207
Harald Welteb8b85a12016-06-17 00:06:42 +0200208/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100209static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200210 struct osmo_gsup_message *gsup_msg)
211{
212 struct vlr_instance *vlr = vsub->vlr;
213
214 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100215 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100219 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200220}
221
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100222static 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 +0200223{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100224 struct vlr_subscr *vsub = e->use_count->talloc_object;
225 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100226 int32_t total;
227 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200228
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100229 if (!e->use)
230 return -EINVAL;
231
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100232 total = osmo_use_count_total(&vsub->use_count);
233
234 if (total == 0
235 || (total == 1 && old_use_count == 0 && e->count == 1))
236 level = LOGL_INFO;
237 else
238 level = LOGL_DEBUG;
239
240 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100241 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
242 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
243
244 if (e->count < 0)
245 return -ERANGE;
246
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100247 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
248
249 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100250 vlr_subscr_free(vsub);
251 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200252}
253
254/* Allocate a new subscriber and insert it into list */
255static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
256{
257 struct vlr_subscr *vsub;
258 int i;
259
260 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100261 *vsub = (struct vlr_subscr){
262 .vlr = vlr,
263 .tmsi = GSM_RESERVED_TMSI,
264 .tmsi_new = GSM_RESERVED_TMSI,
265 .use_count = (struct osmo_use_count){
266 .talloc_object = vsub,
267 .use_cb = vlr_subscr_use_cb,
268 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100269 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100270 };
271 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 +0200272
273 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100274 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200275
276 INIT_LLIST_HEAD(&vsub->cs.requests);
277 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
278
Harald Welte0df904d2018-12-03 11:00:04 +0100279 /* Create an SGs FSM, which is needed to control CSFB,
280 * in cases where CSFB/SGs is not in use, this FSM will
281 * just do nothing. (see also: sgs_iface.c) */
282 vlr_sgs_fsm_create(vsub);
283
Harald Welteb8b85a12016-06-17 00:06:42 +0200284 llist_add_tail(&vsub->list, &vlr->subscribers);
285 return vsub;
286}
287
Harald Welteb8b85a12016-06-17 00:06:42 +0200288/* Send a GSUP Purge MS request.
289 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
290 * this subscriber, not to the HLR? */
291int vlr_subscr_purge(struct vlr_subscr *vsub)
292{
293 struct osmo_gsup_message gsup_msg = {0};
294
295 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
296
297 /* provide HLR number in case we know it */
298 gsup_msg.hlr_enc_len = vsub->hlr.len;
299 gsup_msg.hlr_enc = vsub->hlr.buf;
300
301 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
302}
303
Neels Hofmeyr15809592018-04-06 02:57:51 +0200304void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
305 enum osmo_fsm_term_cause fsm_cause,
306 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200307{
308 if (!vsub)
309 return;
310
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100311 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200312 if (vsub->lu_fsm)
313 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200314 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200315 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100316 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200317}
318
319/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
320void vlr_subscr_free(struct vlr_subscr *vsub)
321{
322 llist_del(&vsub->list);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100323 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
324 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100325
326 /* Make sure SGs timer Ts5 is removed */
327 osmo_timer_del(&vsub->sgs.Ts5);
328
329 /* Remove SGs FSM (see also: sgs_iface.c) */
330 vlr_sgs_fsm_remove(vsub);
331
Harald Welteb8b85a12016-06-17 00:06:42 +0200332 talloc_free(vsub);
333}
334
335/* Generate a new TMSI and store in vsub->tmsi_new.
336 * Search all known subscribers to ensure that the TMSI is unique. */
337int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
338{
339 struct vlr_instance *vlr = vsub->vlr;
340 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100341 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100342 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200343
344 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100345 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
346 if (rc < 0) {
347 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
348 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200349 }
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +0200350
351 if (!llist_empty(&vlr->cfg.nri_ranges->entries)) {
352 int16_t nri_v;
353 osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen);
354 osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen);
355 LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n",
356 osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi);
357 }
358
Harald Welteb8b85a12016-06-17 00:06:42 +0200359 /* throw the dice again, if the TSMI doesn't fit */
360 if (tmsi == GSM_RESERVED_TMSI)
361 continue;
362
363 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
364 if (vlr->cfg.is_ps) {
365 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200366 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200367 } else {
368 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200369 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
370 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200371 }
372
373 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100374 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
375 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200376 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100377 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200378
379 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100380 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200381 return 0;
382 }
383
384 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
385 " after %d tries\n", vlr_subscr_name(vsub), tried);
386 return -1;
387}
388
389/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100390 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200391 * \param[in] imsi IMSI string.
392 * \param[out] created if non-NULL, returns whether a new entry was created. */
393struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
394 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100395 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200396 bool *created,
397 const char *file,
398 int line)
399{
400 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100401 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200402 if (vsub) {
403 if (created)
404 *created = false;
405 return vsub;
406 }
407
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100408 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200409 if (!vsub)
410 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100411 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200412 vlr_subscr_set_imsi(vsub, imsi);
413 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
414 if (created)
415 *created = true;
416 return vsub;
417}
418
419/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100420 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200421 * \param[in] tmsi TMSI.
422 * \param[out] created if non-NULL, returns whether a new entry was created. */
423struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
424 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100425 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200426 bool *created,
427 const char *file,
428 int line)
429{
430 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100431 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200432 if (vsub) {
433 if (created)
434 *created = false;
435 return vsub;
436 }
437
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100438 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200439 if (!vsub)
440 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100441 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200442 vsub->tmsi = tmsi;
443 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
444 if (created)
445 *created = true;
446 return vsub;
447}
448
449void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
450{
451 if (!vsub)
452 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200453
454 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
455 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
456 imsi, vsub->imsi);
457 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
458 }
459
Harald Welteb8b85a12016-06-17 00:06:42 +0200460 vsub->id = atoll(vsub->imsi);
461 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
462 vsub->imsi, vsub->id);
463}
464
465void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
466{
467 if (!vsub)
468 return;
Max98f74672018-02-05 12:57:06 +0100469 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200470 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
471 vsub->imsi, vsub->imei);
472}
473
474void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
475{
476 if (!vsub)
477 return;
Max98f74672018-02-05 12:57:06 +0100478 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200479 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
480 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200481
482 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
483 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200484}
485
486/* Safely copy the given MSISDN string to vsub->msisdn */
487void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
488{
489 if (!vsub)
490 return;
Max98f74672018-02-05 12:57:06 +0100491 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200492 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
493 vsub->imsi, vsub->msisdn);
494}
495
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200496void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
497 const struct osmo_plmn_id *last_eutran_plmn)
498{
499 if (!vsub)
500 return;
501 if (last_eutran_plmn) {
502 vsub->sgs.last_eutran_plmn_present = true;
503 memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
504 } else {
505 vsub->sgs.last_eutran_plmn_present = false;
506 }
507 DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
508 vsub->sgs.last_eutran_plmn_present ?
509 osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
510 "(none)");
511}
512
Harald Welteb8b85a12016-06-17 00:06:42 +0200513bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
514{
515 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
516}
517
518bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
519{
520 return vsub && tmsi != GSM_RESERVED_TMSI
521 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
522}
523
524bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
525{
526 return vsub && msisdn && vsub->msisdn[0]
527 && !strcmp(vsub->msisdn, msisdn);
528}
529
530bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
531{
532 return vsub && imei && vsub->imei[0]
533 && !strcmp(vsub->imei, imei);
534}
535
536/* Send updated subscriber information to HLR */
537int vlr_subscr_changed(struct vlr_subscr *vsub)
538{
539 /* FIXME */
540 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
541 return 0;
542}
543
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200544void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
545{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200546 struct timespec now;
547
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700548 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200549 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700550 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200551 } else {
552 LOGP(DVLR, LOGL_ERROR,
553 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
554 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
555 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
556 }
557}
558
559void vlr_subscr_expire_lu(void *data)
560{
561 struct vlr_instance *vlr = data;
562 struct vlr_subscr *vsub, *vsub_tmp;
563 struct timespec now;
564
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700565 /* Periodic location update might be disabled from the VTY,
566 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700567 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700568 goto done;
569
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200570 if (llist_empty(&vlr->subscribers))
571 goto done;
572
573 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
574 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
575 goto done;
576 }
577
578 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
579 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
580 continue;
581
582 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
Harald Welte173bdf32022-05-15 12:12:54 +0200583 vlr_subscr_detach(vsub);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200584 }
585
586done:
587 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
588}
589
Harald Welteb8b85a12016-06-17 00:06:42 +0200590/***********************************************************************
591 * PDP context data
592 ***********************************************************************/
593
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200594#define GSM_APN_LENGTH 102
595
596/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
597/* see GSM 09.02, B.1, gprsSubscriptionData */
598struct sgsn_subscriber_pdp_data {
599 struct llist_head list;
600
601 unsigned int context_id;
602 uint16_t pdp_type;
603 char apn_str[GSM_APN_LENGTH];
604 uint8_t qos_subscribed[20];
605 size_t qos_subscribed_len;
606};
607
Harald Welteb8b85a12016-06-17 00:06:42 +0200608struct sgsn_subscriber_pdp_data *
609vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
610{
611 struct sgsn_subscriber_pdp_data* pdata;
612
613 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
614
615 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
616
617 return pdata;
618}
619
620static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
621{
622 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
623 int count = 0;
624
625 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
626 llist_del(&pdp->list);
627 talloc_free(pdp);
628 count += 1;
629 }
630
631 return count;
632}
633
634static struct sgsn_subscriber_pdp_data *
635vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
636{
637 struct sgsn_subscriber_pdp_data *pdp;
638
639 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
640 if (pdp->context_id == context_id)
641 return pdp;
642 }
643
644 return NULL;
645}
646
647/***********************************************************************
648 * Actual Implementation
649 ***********************************************************************/
650
651static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100652 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200653{
654 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200655 LOGP(DVLR, LOGL_NOTICE,
656 "Unknown IMSI %s, discarding GSUP request "
657 "of type 0x%02x\n",
658 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100659 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200660 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
661 LOGP(DVLR, LOGL_NOTICE,
662 "Unknown IMSI %s, discarding GSUP error "
663 "of type 0x%02x, cause '%s' (%d)\n",
664 gsup_msg->imsi, gsup_msg->message_type,
665 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
666 gsup_msg->cause);
667 } else {
668 LOGP(DVLR, LOGL_NOTICE,
669 "Unknown IMSI %s, discarding GSUP response "
670 "of type 0x%02x\n",
671 gsup_msg->imsi, gsup_msg->message_type);
672 }
673
674 return -GMM_CAUSE_IMSI_UNKNOWN;
675}
676
677static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100678 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200679{
680 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
681 LOGGSUPP(LOGL_NOTICE, gsup_msg,
682 "Purge MS has failed with cause '%s' (%d)\n",
683 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
684 gsup_msg->cause);
685 return -gsup_msg->cause;
686 }
687 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
688 return 0;
689}
690
691/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100692int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200693{
694 struct osmo_gsup_message gsup_msg = {0};
695 int rc;
696
697 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200698 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 +0200699 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
700
701 return rc;
702}
703
704/* VLR internal call to request tuples from HLR */
705int vlr_subscr_req_sai(struct vlr_subscr *vsub,
706 const uint8_t *auts, const uint8_t *auts_rand)
707{
708 struct osmo_gsup_message gsup_msg = {0};
709
710 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
711 gsup_msg.auts = auts;
712 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100713 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200714
715 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
716}
717
Oliver Smith7d053092018-12-14 17:37:38 +0100718/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
719int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
720{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100721 struct osmo_gsup_message gsup_msg = {
722 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200723 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100724 };
Oliver Smith7d053092018-12-14 17:37:38 +0100725 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
726 int len;
727
728 /* Encode IMEI */
729 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
730 if (len < 1) {
731 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
732 return -ENOSPC;
733 }
734 gsup_msg.imei_enc = imei_enc;
735 gsup_msg.imei_enc_len = len;
736
737 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100738 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100739 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100740}
741
Harald Welteb8b85a12016-06-17 00:06:42 +0200742/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100743int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200744{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100745 struct osmo_gsup_message gsup_msg = {
746 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200747 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100748 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200749
Max98f74672018-02-05 12:57:06 +0100750 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100751 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200752}
753
754/* Update the subscriber with GSUP-received auth tuples */
755void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
756 const struct osmo_gsup_message *gsup)
757{
758 unsigned int i;
759 unsigned int got_tuples;
760
761 if (gsup->num_auth_vectors) {
762 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
763 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100764 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200765 }
766
767 got_tuples = 0;
768 for (i = 0; i < gsup->num_auth_vectors; i++) {
769 size_t key_seq = i;
770
771 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
772 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100773 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200774 key_seq);
775 continue;
776 }
777 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
778 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100779 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200780 }
781
782 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
783
784 if (!got_tuples) {
785 /* FIXME what now? */
786 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
787 }
788
789 /* New tuples means last_tuple becomes invalid */
790 vsub->last_tuple = NULL;
791}
792
793/* Handle SendAuthInfo Result/Error from HLR */
794static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
795 const struct osmo_gsup_message *gsup)
796{
797 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
798 void *data = (void *) gsup;
799
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200800 if (!auth_fi) {
801 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
802 osmo_gsup_message_type_name(gsup->message_type));
803 return -1;
804 }
805
Harald Welteb8b85a12016-06-17 00:06:42 +0200806 switch (gsup->message_type) {
807 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
808 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
809 break;
810 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
811 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
812 break;
813 default:
814 return -1;
815 }
816
817 return 0;
818}
819
Harald Welteb8b85a12016-06-17 00:06:42 +0200820static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
821 const struct osmo_gsup_message *gsup_msg)
822{
823 unsigned idx;
824 int rc;
825
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100826 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100827 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
828 gsup_msg->msisdn_enc,
829 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200830 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
831 vsub->imsi, vsub->msisdn);
832 }
833
834 if (gsup_msg->hlr_enc) {
835 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
836 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
837 gsup_msg->hlr_enc_len);
838 vsub->hlr.len = 0;
839 } else {
840 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
841 gsup_msg->hlr_enc_len);
842 vsub->hlr.len = gsup_msg->hlr_enc_len;
843 }
844 }
845
846 if (gsup_msg->pdp_info_compl) {
847 rc = vlr_subscr_pdp_data_clear(vsub);
848 if (rc > 0)
849 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
850 }
851
852 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
853 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
854 size_t ctx_id = pdp_info->context_id;
855 struct sgsn_subscriber_pdp_data *pdp_data;
856
857 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
858 LOGVSUBP(LOGL_ERROR, vsub,
859 "APN too long, context id = %zu, APN = %s\n",
860 ctx_id, osmo_hexdump(pdp_info->apn_enc,
861 pdp_info->apn_enc_len));
862 continue;
863 }
864
865 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
866 LOGVSUBP(LOGL_ERROR, vsub,
867 "QoS info too long (%zu)\n",
868 pdp_info->qos_enc_len);
869 continue;
870 }
871
872 LOGVSUBP(LOGL_INFO, vsub,
873 "Will set PDP info, context id = %zu, APN = %s\n",
874 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
875
876 /* Set PDP info [ctx_id] */
877 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
878 if (!pdp_data) {
879 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
880 pdp_data->context_id = ctx_id;
881 }
882
883 OSMO_ASSERT(pdp_data != NULL);
884 pdp_data->pdp_type = pdp_info->pdp_type;
885 osmo_apn_to_str(pdp_data->apn_str,
886 pdp_info->apn_enc, pdp_info->apn_enc_len);
887 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
888 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
889 }
890}
891
892
893/* Handle InsertSubscrData Result from HLR */
894static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
895 const struct osmo_gsup_message *gsup)
896{
897 struct osmo_gsup_message gsup_reply = {0};
898
899 vlr_subscr_gsup_insert_data(vsub, gsup);
900 vsub->vlr->ops.subscr_update(vsub);
901
902 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
903 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
904}
905
906/* Handle UpdateLocation Result from HLR */
907static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
908 const struct osmo_gsup_message *gsup)
909{
Philipp Maier483cea82019-04-03 16:23:29 +0200910 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100911 bool sgs_lu_in_progress = false;
912
913 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
914 sgs_lu_in_progress = true;
915
916 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200917 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
918 "without LU in progress\n");
919 return -ENODEV;
920 }
921
922 /* contrary to MAP, we allow piggy-backing subscriber data onto the
923 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
924 * nested INSERT SUBSCRIBER DATA transaction */
925 vlr_subscr_gsup_insert_data(vsub, gsup);
926
Harald Welte0df904d2018-12-03 11:00:04 +0100927 if (sgs_lu_in_progress) {
928 sgs_lu_response.accepted = true;
929 sgs_lu_response.vsub = vsub;
930 vsub->sgs.response_cb(&sgs_lu_response);
931 } else
932 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200933
934 return 0;
935}
936
937/* Handle UpdateLocation Result from HLR */
938static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
939 const struct osmo_gsup_message *gsup)
940{
Philipp Maier483cea82019-04-03 16:23:29 +0200941 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100942 bool sgs_lu_in_progress = false;
943
944 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
945 sgs_lu_in_progress = true;
946
947 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200948 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
949 "without LU in progress\n");
950 return -ENODEV;
951 }
952
953 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
954 get_value_string(gsm48_gmm_cause_names, gsup->cause));
955
Harald Welte0df904d2018-12-03 11:00:04 +0100956 if (sgs_lu_in_progress) {
957 sgs_lu_response.accepted = false;
958 sgs_lu_response.vsub = vsub;
959 vsub->sgs.response_cb(&sgs_lu_response);
960 } else
961 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
962 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200963 return 0;
964}
965
Alexander Couzens7312b152019-08-19 15:30:12 +0200966void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
967 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200968{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200969 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
970 switch (gmm_cause) {
971 case GMM_CAUSE_IMSI_UNKNOWN:
972 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
973 break;
974 case GMM_CAUSE_ILLEGAL_MS:
975 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
976 break;
977 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
978 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
979 break;
980 case GMM_CAUSE_ILLEGAL_ME:
981 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
982 break;
983 case GMM_CAUSE_GPRS_NOTALLOWED:
984 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
985 break;
986 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
987 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
988 break;
989 case GMM_CAUSE_MS_ID_NOT_DERIVED:
990 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
991 break;
992 case GMM_CAUSE_IMPL_DETACHED:
993 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
994 break;
995 case GMM_CAUSE_PLMN_NOTALLOWED:
996 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
997 break;
998 case GMM_CAUSE_LA_NOTALLOWED:
999 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
1000 break;
1001 case GMM_CAUSE_ROAMING_NOTALLOWED:
1002 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
1003 break;
1004 case GMM_CAUSE_NO_GPRS_PLMN:
1005 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
1006 break;
1007 case GMM_CAUSE_MSC_TEMP_NOTREACH:
1008 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
1009 break;
1010 case GMM_CAUSE_SYNC_FAIL:
1011 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
1012 break;
1013 case GMM_CAUSE_CONGESTION:
1014 gsm48_rej = GSM48_REJECT_CONGESTION;
1015 break;
1016 case GMM_CAUSE_SEM_INCORR_MSG:
1017 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1018 break;
1019 case GMM_CAUSE_INV_MAND_INFO:
1020 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1021 break;
1022 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1023 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1024 break;
1025 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1026 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1027 break;
1028 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1029 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1030 break;
1031 case GMM_CAUSE_COND_IE_ERR:
1032 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1033 break;
1034 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1035 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1036 break;
1037 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1038 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1039 break;
1040
1041 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1042 case GMM_CAUSE_MAC_FAIL:
1043 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1044 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1045 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1046 case GMM_CAUSE_NO_PDP_ACTIVATED:
1047 case GMM_CAUSE_NET_FAIL:
1048 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1049 break;
1050 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001051
1052 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001053}
1054
Harald Welteb8b85a12016-06-17 00:06:42 +02001055/* Handle LOCATION CANCEL request from HLR */
1056static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001057 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001058{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001059 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001060 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001061 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001062 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001063 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1064
1065 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1066 is_update_procedure ?
1067 "update procedure" : "subscription withdraw");
1068
1069 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001070 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001071
Alexander Couzens7312b152019-08-19 15:30:12 +02001072 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001073 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001074
Harald Welte173bdf32022-05-15 12:12:54 +02001075 vlr_subscr_detach(vsub);
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001076
Max770fbd22018-01-24 12:48:33 +01001077 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001078}
1079
Oliver Smith7d053092018-12-14 17:37:38 +01001080/* Handle Check_IMEI_VLR result and error from HLR */
1081static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1082{
1083 if (!vsub->lu_fsm) {
1084 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1085 osmo_gsup_message_type_name(gsup->message_type));
1086 return -ENODEV;
1087 }
1088
Oliver Smithcbf2c932019-05-06 13:09:55 +02001089 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1090 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001091 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1092 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1093 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1094 else
1095 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1096 } else {
1097 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1098 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1099 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1100 }
1101
1102 return 0;
1103}
1104
Harald Welteb8b85a12016-06-17 00:06:42 +02001105/* Incoming handler for GSUP from HLR.
1106 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001107int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001108{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001109 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001110 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001111 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001112
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001113 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001114 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001115 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001116 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1117 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001118 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001119 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001120 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001121 }
1122 }
1123
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001124 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001125 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1126 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001127 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001128 break;
1129 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001130 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001131 break;
1132 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001133 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001134 break;
1135 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001136 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001137 break;
1138 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001139 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001140 break;
1141 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1142 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1143 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1144 LOGVSUBP(LOGL_ERROR, vsub,
1145 "Rx GSUP msg_type=%d not yet implemented\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001146 gsup->message_type);
Harald Welteb8b85a12016-06-17 00:06:42 +02001147 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1148 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001149 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1150 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001151 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001152 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001153 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001154 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1155 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001156 break;
1157 }
1158
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001159 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001160 return rc;
1161}
1162
1163/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001164int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001165{
Harald Welteb8b85a12016-06-17 00:06:42 +02001166 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001167 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001168 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001169 if (vsub->imsi[0]
1170 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001171 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001172 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001173 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001174 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001175 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001176 break;
1177 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001178 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001179 break;
1180 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001181 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001182 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001183 default:
1184 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001185 }
1186
1187 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001188 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001189 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001190 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1191 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001192 break;
1193 }
1194 }
1195
1196 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001197 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001198 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001199 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001200 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001201 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001202 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001203 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001204 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001205 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001206 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001207 }
1208
1209 return 0;
1210}
1211
1212/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1213int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1214{
1215 if (vsub->lu_fsm) {
1216 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1217 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1218 } else if (vsub->proc_arq_fsm) {
1219 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1220 PR_ARQ_E_TMSI_ACK, NULL);
1221 } else {
1222 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001223 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001224 return -EINVAL;
1225 }
1226}
1227
Maxdcc193d2017-12-27 19:34:15 +01001228bool vlr_subscr_expire(struct vlr_subscr *vsub)
1229{
1230 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001231 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001232 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001233 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001234
1235 return true;
1236 }
1237
1238 return false;
1239}
1240
Harald Welte173bdf32022-05-15 12:12:54 +02001241static int vlr_subscr_detach(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001242{
1243 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001244 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001245
1246 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001247 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001248
Harald Welte0df904d2018-12-03 11:00:04 +01001249 /* Inform the UE-SGs FSM that the subscriber has been detached */
1250 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1251
Maxdcc193d2017-12-27 19:34:15 +01001252 vlr_subscr_expire(vsub);
1253
Harald Welteb8b85a12016-06-17 00:06:42 +02001254 return 0;
1255}
1256
Harald Welte173bdf32022-05-15 12:12:54 +02001257/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
1258int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1259{
1260 return vlr_subscr_detach(vsub);
1261}
1262
Harald Welteb8b85a12016-06-17 00:06:42 +02001263/* Tear down any running FSMs due to MSC connection timeout.
1264 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1265 * message before the entire connection is torn down.
1266 * \param[in] vsub subscriber to tear down
1267 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001268void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001269{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001270 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001271}
1272
1273struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1274{
1275 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1276 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001277
1278 /* Some of these are needed only on UTRAN, but in case the caller wants
1279 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001280 OSMO_ASSERT(ops->tx_auth_req);
1281 OSMO_ASSERT(ops->tx_auth_rej);
1282 OSMO_ASSERT(ops->tx_id_req);
1283 OSMO_ASSERT(ops->tx_lu_acc);
1284 OSMO_ASSERT(ops->tx_lu_rej);
1285 OSMO_ASSERT(ops->tx_cm_serv_acc);
1286 OSMO_ASSERT(ops->tx_cm_serv_rej);
1287 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001288 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001289 OSMO_ASSERT(ops->subscr_update);
1290 OSMO_ASSERT(ops->subscr_assoc);
1291
1292 INIT_LLIST_HEAD(&vlr->subscribers);
1293 INIT_LLIST_HEAD(&vlr->operations);
1294 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1295
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001296 /* defaults */
1297 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001298 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1299 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001300
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001301 /* reset shared timer definitions */
1302 osmo_tdefs_reset(msc_tdefs_vlr);
1303
Harald Welteb8b85a12016-06-17 00:06:42 +02001304 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001305 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001306 /* osmo_lu_fsm.c */
1307 vlr_lu_fsm_init();
1308 /* vlr_access_request_fsm.c */
1309 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001310 /* vlr_sgs_fsm.c */
1311 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001312
1313 return vlr;
1314}
1315
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001316int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001317{
1318 OSMO_ASSERT(vlr);
1319
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001320 vlr->gcm = gcm;
1321 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1322 .func = vlr_gsup_rx,
1323 .data = vlr,
1324 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001325
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001326 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1327 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001328 return 0;
1329}
1330
1331/* MSC->VLR: Subscribre has disconnected */
1332int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1333{
1334 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1335 * interface */
1336 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1337 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1338 vsub->msc_conn_ref = NULL;
1339
1340 return 0;
1341}
1342
1343/* MSC->VLR: Receive Authentication Failure from Subscriber */
1344int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1345{
1346 struct vlr_auth_resp_par par = {0};
1347 par.auts = auts;
1348
1349 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1350 return 0;
1351}
1352
1353/* MSC->VLR: Receive Authentication Response from MS
1354 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1355int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1356 bool is_utran, const uint8_t *res, uint8_t res_len)
1357{
1358 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1359 struct vlr_auth_resp_par par;
1360
1361 par.is_r99 = is_r99;
1362 par.is_utran = is_utran;
1363 par.res = res;
1364 par.res_len = res_len;
1365 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1366
1367 return 0;
1368}
1369
1370/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001371void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001372{
1373 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001374 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001375 if (vsub->proc_arq_fsm
1376 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001377 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001378}
1379
1380/* Internal evaluation of requested ciphering mode.
1381 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1382 * \param[in] vlr VLR instance.
1383 * \param[in] fi Calling FSM instance, for logging.
1384 * \param[in] msc_conn_ref MSC conn to send to.
1385 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1386 * \returns 0 if no ciphering is needed or message was sent successfully,
1387 * or a negative value if ciph_mode is invalid or sending failed.
1388 */
1389int vlr_set_ciph_mode(struct vlr_instance *vlr,
1390 struct osmo_fsm_inst *fi,
1391 void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001392 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001393 bool retrieve_imeisv)
1394{
Harald Welte71c51df2017-12-23 18:51:48 +01001395 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1396 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001397}
1398
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001399/* Decide whether UMTS AKA should be used.
1400 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1401 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1402 * GERAN are R99 capable and UMTS AKA tokens are available.
1403 * \param[in] vec Auth tokens (received from the HLR).
1404 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1405 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1406 */
1407bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1408{
1409 if (!is_r99)
1410 return false;
1411 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1412 return false;
1413 return true;
1414}
1415
Harald Welteb8b85a12016-06-17 00:06:42 +02001416void log_set_filter_vlr_subscr(struct log_target *target,
1417 struct vlr_subscr *vlr_subscr)
1418{
1419 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001420 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001421
1422 /* free the old data */
1423 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001424 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001425 *fsub = NULL;
1426 }
1427
1428 if (vlr_subscr) {
1429 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001430 vlr_subscr_get(vlr_subscr, use);
1431 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001432 } else
1433 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1434}