blob: a1489f2192e2e7560fb9296caa5f6fbb305e1323 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) code base */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020025#include <osmocom/core/timer.h>
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070026#include <osmocom/core/tdef.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
28#include <osmocom/gsm/gsup.h>
29#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020030#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010031#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020033#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010034#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020035#include <osmocom/msc/vlr.h>
36#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010037#include <osmocom/msc/gsup_client_mux.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020038
Harald Welteb8b85a12016-06-17 00:06:42 +020039#include <netinet/in.h>
40#include <arpa/inet.h>
41#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020042#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020043
44#include "vlr_core.h"
45#include "vlr_auth_fsm.h"
46#include "vlr_lu_fsm.h"
47#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010048#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020049
50#define SGSN_SUBSCR_MAX_RETRIES 3
51#define SGSN_SUBSCR_RETRY_INTERVAL 10
52
53/***********************************************************************
54 * Convenience functions
55 ***********************************************************************/
56
57const struct value_string vlr_ciph_names[] = {
58 OSMO_VALUE_STRING(VLR_CIPH_NONE),
59 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
60 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
61 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
62 { 0, NULL }
63};
64
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070065/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
66struct osmo_tdef msc_tdefs_vlr[] = {
67 /* TODO: also define T3212 here */
68 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
69 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
70 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
71 { /* terminator */ }
72};
73
74/* This is just a wrapper around the osmo_tdef API.
75 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +020076uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
77{
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070078 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +020079}
80
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010081/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010082const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010083{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010084 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010085 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010086 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010087 if (!vsub)
88 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010089 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010090 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010091 present = true;
92 }
93 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010094 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010095 present = true;
96 }
97 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010098 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010099 present = true;
100 }
101 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100102 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100103 present = true;
104 }
105 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100106 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100107
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100108 return buf;
109}
110
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100111const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
112{
113 /* cast away the const so we can shorten the string within the static buffer */
114 char *name = (char*)vlr_subscr_name(vsub);
115 size_t len = strlen(name);
116 if (maxlen < 2)
117 return "-";
118 if (len > maxlen)
119 strcpy(name + maxlen - 2, "..");
120 return name;
121}
122
Oliver Smith5598aae2019-01-08 11:47:21 +0100123const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100124{
125 if (!vsub || !vsub->msisdn[0])
126 return vlr_subscr_name(vsub);
127 return vsub->msisdn;
128}
129
Harald Welteb8b85a12016-06-17 00:06:42 +0200130struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
131 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100132 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200133 const char *file, int line)
134{
135 struct vlr_subscr *vsub;
136
137 if (!imsi || !*imsi)
138 return NULL;
139
140 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100141 if (vlr_subscr_matches_imsi(vsub, imsi)) {
142 vlr_subscr_get_src(vsub, use, file, line);
143 return vsub;
144 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200145 }
146 return NULL;
147}
148
149struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
150 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100151 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200152 const char *file, int line)
153{
154 struct vlr_subscr *vsub;
155
156 if (tmsi == GSM_RESERVED_TMSI)
157 return NULL;
158
159 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100160 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
161 vlr_subscr_get_src(vsub, use, file, line);
162 return vsub;
163 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200164 }
165 return NULL;
166}
167
168struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
169 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100170 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200171 const char *file, int line)
172{
173 struct vlr_subscr *vsub;
174
175 if (!msisdn || !*msisdn)
176 return NULL;
177
178 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100179 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
180 vlr_subscr_get_src(vsub, use, file, line);
181 return vsub;
182 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200183 }
184 return NULL;
185}
186
Harald Welteb8b85a12016-06-17 00:06:42 +0200187/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100188static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200189 struct osmo_gsup_message *gsup_msg)
190{
191 struct vlr_instance *vlr = vsub->vlr;
192
193 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100194 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200195
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100196 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200197
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100198 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200199}
200
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100201static 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 +0200202{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100203 struct vlr_subscr *vsub = e->use_count->talloc_object;
204 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100205 int32_t total;
206 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200207
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100208 if (!e->use)
209 return -EINVAL;
210
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100211 total = osmo_use_count_total(&vsub->use_count);
212
213 if (total == 0
214 || (total == 1 && old_use_count == 0 && e->count == 1))
215 level = LOGL_INFO;
216 else
217 level = LOGL_DEBUG;
218
219 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100220 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
221 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
222
223 if (e->count < 0)
224 return -ERANGE;
225
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100226 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
227
228 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100229 vlr_subscr_free(vsub);
230 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200231}
232
233/* Allocate a new subscriber and insert it into list */
234static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
235{
236 struct vlr_subscr *vsub;
237 int i;
238
239 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100240 *vsub = (struct vlr_subscr){
241 .vlr = vlr,
242 .tmsi = GSM_RESERVED_TMSI,
243 .tmsi_new = GSM_RESERVED_TMSI,
244 .use_count = (struct osmo_use_count){
245 .talloc_object = vsub,
246 .use_cb = vlr_subscr_use_cb,
247 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100248 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100249 };
250 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 +0200251
252 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100253 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200254
255 INIT_LLIST_HEAD(&vsub->cs.requests);
256 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
257
Harald Welte0df904d2018-12-03 11:00:04 +0100258 /* Create an SGs FSM, which is needed to control CSFB,
259 * in cases where CSFB/SGs is not in use, this FSM will
260 * just do nothing. (see also: sgs_iface.c) */
261 vlr_sgs_fsm_create(vsub);
262
Harald Welteb8b85a12016-06-17 00:06:42 +0200263 llist_add_tail(&vsub->list, &vlr->subscribers);
264 return vsub;
265}
266
Harald Welteb8b85a12016-06-17 00:06:42 +0200267/* Send a GSUP Purge MS request.
268 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
269 * this subscriber, not to the HLR? */
270int vlr_subscr_purge(struct vlr_subscr *vsub)
271{
272 struct osmo_gsup_message gsup_msg = {0};
273
274 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
275
276 /* provide HLR number in case we know it */
277 gsup_msg.hlr_enc_len = vsub->hlr.len;
278 gsup_msg.hlr_enc = vsub->hlr.buf;
279
280 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
281}
282
Neels Hofmeyr15809592018-04-06 02:57:51 +0200283void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
284 enum osmo_fsm_term_cause fsm_cause,
285 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200286{
287 if (!vsub)
288 return;
289
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100290 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200291 if (vsub->lu_fsm)
292 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200293 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200294 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100295 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200296}
297
298/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
299void vlr_subscr_free(struct vlr_subscr *vsub)
300{
301 llist_del(&vsub->list);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100302 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
303 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100304
305 /* Make sure SGs timer Ts5 is removed */
306 osmo_timer_del(&vsub->sgs.Ts5);
307
308 /* Remove SGs FSM (see also: sgs_iface.c) */
309 vlr_sgs_fsm_remove(vsub);
310
Harald Welteb8b85a12016-06-17 00:06:42 +0200311 talloc_free(vsub);
312}
313
314/* Generate a new TMSI and store in vsub->tmsi_new.
315 * Search all known subscribers to ensure that the TMSI is unique. */
316int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
317{
318 struct vlr_instance *vlr = vsub->vlr;
319 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100320 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100321 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200322
323 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100324 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
325 if (rc < 0) {
326 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
327 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200328 }
329 /* throw the dice again, if the TSMI doesn't fit */
330 if (tmsi == GSM_RESERVED_TMSI)
331 continue;
332
333 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
334 if (vlr->cfg.is_ps) {
335 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200336 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200337 } else {
338 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200339 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
340 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200341 }
342
343 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100344 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
345 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200346 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100347 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200348
349 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100350 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200351 return 0;
352 }
353
354 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
355 " after %d tries\n", vlr_subscr_name(vsub), tried);
356 return -1;
357}
358
359/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100360 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200361 * \param[in] imsi IMSI string.
362 * \param[out] created if non-NULL, returns whether a new entry was created. */
363struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
364 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100365 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200366 bool *created,
367 const char *file,
368 int line)
369{
370 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100371 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200372 if (vsub) {
373 if (created)
374 *created = false;
375 return vsub;
376 }
377
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100378 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200379 if (!vsub)
380 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100381 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200382 vlr_subscr_set_imsi(vsub, imsi);
383 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
384 if (created)
385 *created = true;
386 return vsub;
387}
388
389/* Find subscriber by TMSI, 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] tmsi TMSI.
392 * \param[out] created if non-NULL, returns whether a new entry was created. */
393struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
394 uint32_t tmsi,
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_tmsi(vlr, tmsi, 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 vsub->tmsi = tmsi;
413 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
414 if (created)
415 *created = true;
416 return vsub;
417}
418
419void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
420{
421 if (!vsub)
422 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200423
424 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
425 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
426 imsi, vsub->imsi);
427 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
428 }
429
Harald Welteb8b85a12016-06-17 00:06:42 +0200430 vsub->id = atoll(vsub->imsi);
431 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
432 vsub->imsi, vsub->id);
433}
434
435void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
436{
437 if (!vsub)
438 return;
Max98f74672018-02-05 12:57:06 +0100439 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200440 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
441 vsub->imsi, vsub->imei);
442}
443
444void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
445{
446 if (!vsub)
447 return;
Max98f74672018-02-05 12:57:06 +0100448 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200449 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
450 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200451
452 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
453 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200454}
455
456/* Safely copy the given MSISDN string to vsub->msisdn */
457void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
458{
459 if (!vsub)
460 return;
Max98f74672018-02-05 12:57:06 +0100461 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200462 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
463 vsub->imsi, vsub->msisdn);
464}
465
466bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
467{
468 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
469}
470
471bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
472{
473 return vsub && tmsi != GSM_RESERVED_TMSI
474 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
475}
476
477bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
478{
479 return vsub && msisdn && vsub->msisdn[0]
480 && !strcmp(vsub->msisdn, msisdn);
481}
482
483bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
484{
485 return vsub && imei && vsub->imei[0]
486 && !strcmp(vsub->imei, imei);
487}
488
489/* Send updated subscriber information to HLR */
490int vlr_subscr_changed(struct vlr_subscr *vsub)
491{
492 /* FIXME */
493 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
494 return 0;
495}
496
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200497void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
498{
499 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
500 struct timespec now;
501
502 /* The T3212 timeout value field is coded as the binary representation of the timeout
503 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
504 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
505 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
506 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
507 } else {
508 LOGP(DVLR, LOGL_ERROR,
509 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
510 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
511 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
512 }
513}
514
515void vlr_subscr_expire_lu(void *data)
516{
517 struct vlr_instance *vlr = data;
518 struct vlr_subscr *vsub, *vsub_tmp;
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700519 struct gsm_network *net;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200520 struct timespec now;
521
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700522 /* Periodic location update might be disabled from the VTY,
523 * so we shall not expire subscribers until explicit IMSI Detach. */
524 net = vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
525 if (!net->t3212)
526 goto done;
527
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200528 if (llist_empty(&vlr->subscribers))
529 goto done;
530
531 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
532 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
533 goto done;
534 }
535
536 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
537 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
538 continue;
539
540 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
541 vlr_subscr_rx_imsi_detach(vsub);
542 }
543
544done:
545 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
546}
547
Harald Welteb8b85a12016-06-17 00:06:42 +0200548/***********************************************************************
549 * PDP context data
550 ***********************************************************************/
551
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200552#define GSM_APN_LENGTH 102
553
554/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
555/* see GSM 09.02, B.1, gprsSubscriptionData */
556struct sgsn_subscriber_pdp_data {
557 struct llist_head list;
558
559 unsigned int context_id;
560 uint16_t pdp_type;
561 char apn_str[GSM_APN_LENGTH];
562 uint8_t qos_subscribed[20];
563 size_t qos_subscribed_len;
564};
565
Harald Welteb8b85a12016-06-17 00:06:42 +0200566struct sgsn_subscriber_pdp_data *
567vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
568{
569 struct sgsn_subscriber_pdp_data* pdata;
570
571 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
572
573 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
574
575 return pdata;
576}
577
578static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
579{
580 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
581 int count = 0;
582
583 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
584 llist_del(&pdp->list);
585 talloc_free(pdp);
586 count += 1;
587 }
588
589 return count;
590}
591
592static struct sgsn_subscriber_pdp_data *
593vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
594{
595 struct sgsn_subscriber_pdp_data *pdp;
596
597 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
598 if (pdp->context_id == context_id)
599 return pdp;
600 }
601
602 return NULL;
603}
604
605/***********************************************************************
606 * Actual Implementation
607 ***********************************************************************/
608
609static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100610 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200611{
612 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200613 LOGP(DVLR, LOGL_NOTICE,
614 "Unknown IMSI %s, discarding GSUP request "
615 "of type 0x%02x\n",
616 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100617 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200618 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
619 LOGP(DVLR, LOGL_NOTICE,
620 "Unknown IMSI %s, discarding GSUP error "
621 "of type 0x%02x, cause '%s' (%d)\n",
622 gsup_msg->imsi, gsup_msg->message_type,
623 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
624 gsup_msg->cause);
625 } else {
626 LOGP(DVLR, LOGL_NOTICE,
627 "Unknown IMSI %s, discarding GSUP response "
628 "of type 0x%02x\n",
629 gsup_msg->imsi, gsup_msg->message_type);
630 }
631
632 return -GMM_CAUSE_IMSI_UNKNOWN;
633}
634
635static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100636 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200637{
638 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
639 LOGGSUPP(LOGL_NOTICE, gsup_msg,
640 "Purge MS has failed with cause '%s' (%d)\n",
641 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
642 gsup_msg->cause);
643 return -gsup_msg->cause;
644 }
645 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
646 return 0;
647}
648
649/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100650int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200651{
652 struct osmo_gsup_message gsup_msg = {0};
653 int rc;
654
655 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200656 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 +0200657 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
658
659 return rc;
660}
661
662/* VLR internal call to request tuples from HLR */
663int vlr_subscr_req_sai(struct vlr_subscr *vsub,
664 const uint8_t *auts, const uint8_t *auts_rand)
665{
666 struct osmo_gsup_message gsup_msg = {0};
667
668 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
669 gsup_msg.auts = auts;
670 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100671 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200672
673 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
674}
675
Oliver Smith7d053092018-12-14 17:37:38 +0100676/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
677int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
678{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100679 struct osmo_gsup_message gsup_msg = {
680 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200681 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100682 };
Oliver Smith7d053092018-12-14 17:37:38 +0100683 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
684 int len;
685
686 /* Encode IMEI */
687 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
688 if (len < 1) {
689 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
690 return -ENOSPC;
691 }
692 gsup_msg.imei_enc = imei_enc;
693 gsup_msg.imei_enc_len = len;
694
695 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100696 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100697 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100698}
699
Harald Welteb8b85a12016-06-17 00:06:42 +0200700/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100701int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200702{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100703 struct osmo_gsup_message gsup_msg = {
704 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200705 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200707
Max98f74672018-02-05 12:57:06 +0100708 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100709 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200710}
711
712/* Update the subscriber with GSUP-received auth tuples */
713void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
714 const struct osmo_gsup_message *gsup)
715{
716 unsigned int i;
717 unsigned int got_tuples;
718
719 if (gsup->num_auth_vectors) {
720 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
721 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100722 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200723 }
724
725 got_tuples = 0;
726 for (i = 0; i < gsup->num_auth_vectors; i++) {
727 size_t key_seq = i;
728
729 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
730 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100731 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200732 key_seq);
733 continue;
734 }
735 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
736 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100737 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200738 }
739
740 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
741
742 if (!got_tuples) {
743 /* FIXME what now? */
744 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
745 }
746
747 /* New tuples means last_tuple becomes invalid */
748 vsub->last_tuple = NULL;
749}
750
751/* Handle SendAuthInfo Result/Error from HLR */
752static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
753 const struct osmo_gsup_message *gsup)
754{
755 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
756 void *data = (void *) gsup;
757
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200758 if (!auth_fi) {
759 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
760 osmo_gsup_message_type_name(gsup->message_type));
761 return -1;
762 }
763
Harald Welteb8b85a12016-06-17 00:06:42 +0200764 switch (gsup->message_type) {
765 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
766 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
767 break;
768 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
769 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
770 break;
771 default:
772 return -1;
773 }
774
775 return 0;
776}
777
Harald Welteb8b85a12016-06-17 00:06:42 +0200778static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
779 const struct osmo_gsup_message *gsup_msg)
780{
781 unsigned idx;
782 int rc;
783
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100784 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100785 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
786 gsup_msg->msisdn_enc,
787 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200788 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
789 vsub->imsi, vsub->msisdn);
790 }
791
792 if (gsup_msg->hlr_enc) {
793 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
794 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
795 gsup_msg->hlr_enc_len);
796 vsub->hlr.len = 0;
797 } else {
798 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
799 gsup_msg->hlr_enc_len);
800 vsub->hlr.len = gsup_msg->hlr_enc_len;
801 }
802 }
803
804 if (gsup_msg->pdp_info_compl) {
805 rc = vlr_subscr_pdp_data_clear(vsub);
806 if (rc > 0)
807 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
808 }
809
810 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
811 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
812 size_t ctx_id = pdp_info->context_id;
813 struct sgsn_subscriber_pdp_data *pdp_data;
814
815 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
816 LOGVSUBP(LOGL_ERROR, vsub,
817 "APN too long, context id = %zu, APN = %s\n",
818 ctx_id, osmo_hexdump(pdp_info->apn_enc,
819 pdp_info->apn_enc_len));
820 continue;
821 }
822
823 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
824 LOGVSUBP(LOGL_ERROR, vsub,
825 "QoS info too long (%zu)\n",
826 pdp_info->qos_enc_len);
827 continue;
828 }
829
830 LOGVSUBP(LOGL_INFO, vsub,
831 "Will set PDP info, context id = %zu, APN = %s\n",
832 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
833
834 /* Set PDP info [ctx_id] */
835 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
836 if (!pdp_data) {
837 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
838 pdp_data->context_id = ctx_id;
839 }
840
841 OSMO_ASSERT(pdp_data != NULL);
842 pdp_data->pdp_type = pdp_info->pdp_type;
843 osmo_apn_to_str(pdp_data->apn_str,
844 pdp_info->apn_enc, pdp_info->apn_enc_len);
845 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
846 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
847 }
848}
849
850
851/* Handle InsertSubscrData Result from HLR */
852static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
853 const struct osmo_gsup_message *gsup)
854{
855 struct osmo_gsup_message gsup_reply = {0};
856
857 vlr_subscr_gsup_insert_data(vsub, gsup);
858 vsub->vlr->ops.subscr_update(vsub);
859
860 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
861 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
862}
863
864/* Handle UpdateLocation Result from HLR */
865static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
866 const struct osmo_gsup_message *gsup)
867{
Philipp Maier483cea82019-04-03 16:23:29 +0200868 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100869 bool sgs_lu_in_progress = false;
870
871 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
872 sgs_lu_in_progress = true;
873
874 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200875 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
876 "without LU in progress\n");
877 return -ENODEV;
878 }
879
880 /* contrary to MAP, we allow piggy-backing subscriber data onto the
881 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
882 * nested INSERT SUBSCRIBER DATA transaction */
883 vlr_subscr_gsup_insert_data(vsub, gsup);
884
Harald Welte0df904d2018-12-03 11:00:04 +0100885 if (sgs_lu_in_progress) {
886 sgs_lu_response.accepted = true;
887 sgs_lu_response.vsub = vsub;
888 vsub->sgs.response_cb(&sgs_lu_response);
889 } else
890 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200891
892 return 0;
893}
894
895/* Handle UpdateLocation Result from HLR */
896static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
897 const struct osmo_gsup_message *gsup)
898{
Philipp Maier483cea82019-04-03 16:23:29 +0200899 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +0100900 bool sgs_lu_in_progress = false;
901
902 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
903 sgs_lu_in_progress = true;
904
905 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200906 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
907 "without LU in progress\n");
908 return -ENODEV;
909 }
910
911 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
912 get_value_string(gsm48_gmm_cause_names, gsup->cause));
913
Harald Welte0df904d2018-12-03 11:00:04 +0100914 if (sgs_lu_in_progress) {
915 sgs_lu_response.accepted = false;
916 sgs_lu_response.vsub = vsub;
917 vsub->sgs.response_cb(&sgs_lu_response);
918 } else
919 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
920 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200921 return 0;
922}
923
Alexander Couzens7312b152019-08-19 15:30:12 +0200924void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
925 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200926{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200927 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
928 switch (gmm_cause) {
929 case GMM_CAUSE_IMSI_UNKNOWN:
930 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
931 break;
932 case GMM_CAUSE_ILLEGAL_MS:
933 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
934 break;
935 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
936 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
937 break;
938 case GMM_CAUSE_ILLEGAL_ME:
939 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
940 break;
941 case GMM_CAUSE_GPRS_NOTALLOWED:
942 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
943 break;
944 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
945 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
946 break;
947 case GMM_CAUSE_MS_ID_NOT_DERIVED:
948 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
949 break;
950 case GMM_CAUSE_IMPL_DETACHED:
951 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
952 break;
953 case GMM_CAUSE_PLMN_NOTALLOWED:
954 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
955 break;
956 case GMM_CAUSE_LA_NOTALLOWED:
957 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
958 break;
959 case GMM_CAUSE_ROAMING_NOTALLOWED:
960 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
961 break;
962 case GMM_CAUSE_NO_GPRS_PLMN:
963 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
964 break;
965 case GMM_CAUSE_MSC_TEMP_NOTREACH:
966 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
967 break;
968 case GMM_CAUSE_SYNC_FAIL:
969 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
970 break;
971 case GMM_CAUSE_CONGESTION:
972 gsm48_rej = GSM48_REJECT_CONGESTION;
973 break;
974 case GMM_CAUSE_SEM_INCORR_MSG:
975 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
976 break;
977 case GMM_CAUSE_INV_MAND_INFO:
978 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
979 break;
980 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
981 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
982 break;
983 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
984 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
985 break;
986 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
987 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
988 break;
989 case GMM_CAUSE_COND_IE_ERR:
990 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
991 break;
992 case GMM_CAUSE_MSG_INCOMP_P_STATE:
993 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
994 break;
995 case GMM_CAUSE_PROTO_ERR_UNSPEC:
996 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
997 break;
998
999 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1000 case GMM_CAUSE_MAC_FAIL:
1001 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1002 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1003 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1004 case GMM_CAUSE_NO_PDP_ACTIVATED:
1005 case GMM_CAUSE_NET_FAIL:
1006 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1007 break;
1008 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001009
1010 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001011}
1012
Harald Welteb8b85a12016-06-17 00:06:42 +02001013/* Handle LOCATION CANCEL request from HLR */
1014static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001015 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001016{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001017 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001018 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001019 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001020 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001021 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1022
1023 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1024 is_update_procedure ?
1025 "update procedure" : "subscription withdraw");
1026
1027 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001028 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001029
Alexander Couzens7312b152019-08-19 15:30:12 +02001030 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001031 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001032
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001033 vlr_subscr_rx_imsi_detach(vsub);
1034
Max770fbd22018-01-24 12:48:33 +01001035 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001036}
1037
Oliver Smith7d053092018-12-14 17:37:38 +01001038/* Handle Check_IMEI_VLR result and error from HLR */
1039static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1040{
1041 if (!vsub->lu_fsm) {
1042 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1043 osmo_gsup_message_type_name(gsup->message_type));
1044 return -ENODEV;
1045 }
1046
Oliver Smithcbf2c932019-05-06 13:09:55 +02001047 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1048 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001049 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1050 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1051 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1052 else
1053 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1054 } else {
1055 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1056 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1057 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1058 }
1059
1060 return 0;
1061}
1062
Harald Welteb8b85a12016-06-17 00:06:42 +02001063/* Incoming handler for GSUP from HLR.
1064 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001065int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001066{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001067 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001068 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001069 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001070
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001071 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001072 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001073 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001074 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1075 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001077 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001078 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001079 }
1080 }
1081
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001082 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001083 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1084 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001085 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001086 break;
1087 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001088 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001089 break;
1090 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001091 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001092 break;
1093 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001094 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001095 break;
1096 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001097 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001098 break;
1099 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1100 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1101 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1102 LOGVSUBP(LOGL_ERROR, vsub,
1103 "Rx GSUP msg_type=%d not yet implemented\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001104 gsup->message_type);
Harald Welteb8b85a12016-06-17 00:06:42 +02001105 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1106 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001107 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1108 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001109 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001110 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001111 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001112 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1113 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001114 break;
1115 }
1116
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001117 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001118 return rc;
1119}
1120
1121/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1122int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1123 const uint8_t *mi, size_t mi_len)
1124{
1125 char mi_string[GSM48_MI_SIZE];
1126 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1127
1128 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1129
1130 /* update the vlr_subscr with the given identity */
1131 switch (mi_type) {
1132 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001133 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1134 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1135 sizeof(vsub->imsi) - 1, mi_string);
1136 return -ENOSPC; /* ignore message; do not avance LU FSM */
1137 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001138 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1139 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1140 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001141 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001142 } else
1143 vlr_subscr_set_imsi(vsub, mi_string);
1144 break;
1145 case GSM_MI_TYPE_IMEI:
1146 vlr_subscr_set_imei(vsub, mi_string);
1147 break;
1148 case GSM_MI_TYPE_IMEISV:
1149 vlr_subscr_set_imeisv(vsub, mi_string);
1150 break;
1151 }
1152
1153 if (vsub->auth_fsm) {
1154 switch (mi_type) {
1155 case GSM_MI_TYPE_IMSI:
1156 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1157 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1158 break;
1159 }
1160 }
1161
1162 if (vsub->lu_fsm) {
1163 uint32_t event = 0;
1164 switch (mi_type) {
1165 case GSM_MI_TYPE_IMSI:
1166 event = VLR_ULA_E_ID_IMSI;
1167 break;
1168 case GSM_MI_TYPE_IMEI:
1169 event = VLR_ULA_E_ID_IMEI;
1170 break;
1171 case GSM_MI_TYPE_IMEISV:
1172 event = VLR_ULA_E_ID_IMEISV;
1173 break;
1174 default:
1175 OSMO_ASSERT(0);
1176 break;
1177 }
1178 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
Harald Welteb8b85a12016-06-17 00:06:42 +02001179 }
1180
1181 return 0;
1182}
1183
1184/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1185int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1186{
1187 if (vsub->lu_fsm) {
1188 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1189 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1190 } else if (vsub->proc_arq_fsm) {
1191 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1192 PR_ARQ_E_TMSI_ACK, NULL);
1193 } else {
1194 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001195 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001196 return -EINVAL;
1197 }
1198}
1199
Maxdcc193d2017-12-27 19:34:15 +01001200bool vlr_subscr_expire(struct vlr_subscr *vsub)
1201{
1202 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001203 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001204 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001205 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001206
1207 return true;
1208 }
1209
1210 return false;
1211}
1212
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001213/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001214int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1215{
1216 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001217 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001218
1219 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001220 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001221
Harald Welte0df904d2018-12-03 11:00:04 +01001222 /* Inform the UE-SGs FSM that the subscriber has been detached */
1223 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1224
Maxdcc193d2017-12-27 19:34:15 +01001225 vlr_subscr_expire(vsub);
1226
Harald Welteb8b85a12016-06-17 00:06:42 +02001227 return 0;
1228}
1229
1230/* Tear down any running FSMs due to MSC connection timeout.
1231 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1232 * message before the entire connection is torn down.
1233 * \param[in] vsub subscriber to tear down
1234 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001235void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001236{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001237 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001238}
1239
1240struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1241{
1242 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1243 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001244
1245 /* Some of these are needed only on UTRAN, but in case the caller wants
1246 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001247 OSMO_ASSERT(ops->tx_auth_req);
1248 OSMO_ASSERT(ops->tx_auth_rej);
1249 OSMO_ASSERT(ops->tx_id_req);
1250 OSMO_ASSERT(ops->tx_lu_acc);
1251 OSMO_ASSERT(ops->tx_lu_rej);
1252 OSMO_ASSERT(ops->tx_cm_serv_acc);
1253 OSMO_ASSERT(ops->tx_cm_serv_rej);
1254 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001255 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001256 OSMO_ASSERT(ops->subscr_update);
1257 OSMO_ASSERT(ops->subscr_assoc);
1258
1259 INIT_LLIST_HEAD(&vlr->subscribers);
1260 INIT_LLIST_HEAD(&vlr->operations);
1261 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1262
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001263 /* defaults */
1264 vlr->cfg.assign_tmsi = true;
1265
Harald Welteb8b85a12016-06-17 00:06:42 +02001266 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001267 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001268 /* osmo_lu_fsm.c */
1269 vlr_lu_fsm_init();
1270 /* vlr_access_request_fsm.c */
1271 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001272 /* vlr_sgs_fsm.c */
1273 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001274
1275 return vlr;
1276}
1277
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001278int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001279{
1280 OSMO_ASSERT(vlr);
1281
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001282 vlr->gcm = gcm;
1283 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1284 .func = vlr_gsup_rx,
1285 .data = vlr,
1286 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001287
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001288 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1289 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001290 return 0;
1291}
1292
1293/* MSC->VLR: Subscribre has disconnected */
1294int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1295{
1296 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1297 * interface */
1298 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1299 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1300 vsub->msc_conn_ref = NULL;
1301
1302 return 0;
1303}
1304
1305/* MSC->VLR: Receive Authentication Failure from Subscriber */
1306int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1307{
1308 struct vlr_auth_resp_par par = {0};
1309 par.auts = auts;
1310
1311 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1312 return 0;
1313}
1314
1315/* MSC->VLR: Receive Authentication Response from MS
1316 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1317int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1318 bool is_utran, const uint8_t *res, uint8_t res_len)
1319{
1320 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1321 struct vlr_auth_resp_par par;
1322
1323 par.is_r99 = is_r99;
1324 par.is_utran = is_utran;
1325 par.res = res;
1326 par.res_len = res_len;
1327 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1328
1329 return 0;
1330}
1331
1332/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001334{
1335 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001336 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001337 if (vsub->proc_arq_fsm
1338 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001339 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001340}
1341
1342/* Internal evaluation of requested ciphering mode.
1343 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1344 * \param[in] vlr VLR instance.
1345 * \param[in] fi Calling FSM instance, for logging.
1346 * \param[in] msc_conn_ref MSC conn to send to.
1347 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1348 * \returns 0 if no ciphering is needed or message was sent successfully,
1349 * or a negative value if ciph_mode is invalid or sending failed.
1350 */
1351int vlr_set_ciph_mode(struct vlr_instance *vlr,
1352 struct osmo_fsm_inst *fi,
1353 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001354 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001355 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001356 bool retrieve_imeisv)
1357{
Harald Welte71c51df2017-12-23 18:51:48 +01001358 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001359 return 0;
1360
Harald Welte71c51df2017-12-23 18:51:48 +01001361 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1362 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001363}
1364
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001365/* Decide whether UMTS AKA should be used.
1366 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1367 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1368 * GERAN are R99 capable and UMTS AKA tokens are available.
1369 * \param[in] vec Auth tokens (received from the HLR).
1370 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1371 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1372 */
1373bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1374{
1375 if (!is_r99)
1376 return false;
1377 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1378 return false;
1379 return true;
1380}
1381
Harald Welteb8b85a12016-06-17 00:06:42 +02001382void log_set_filter_vlr_subscr(struct log_target *target,
1383 struct vlr_subscr *vlr_subscr)
1384{
1385 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001386 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001387
1388 /* free the old data */
1389 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001390 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001391 *fsub = NULL;
1392 }
1393
1394 if (vlr_subscr) {
1395 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001396 vlr_subscr_get(vlr_subscr, use);
1397 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001398 } else
1399 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1400}