blob: e8ff1b1eae55667d5978c7d2e09b8795eb581548 [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>
Alexander Couzens620c60f2024-09-12 00:56:27 +020024#include <osmocom/core/stats.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020025#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020026#include <osmocom/core/timer.h>
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070027#include <osmocom/core/tdef.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020028#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +020029#include <osmocom/gsm/gsm23236.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020030#include <osmocom/gsm/gsup.h>
31#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020032#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010033#include <osmocom/gsm/ipa.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>
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +010039#include <osmocom/msc/paging.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020040
Harald Welteb8b85a12016-06-17 00:06:42 +020041#include <netinet/in.h>
42#include <arpa/inet.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
Harald Welte123261e2022-05-15 11:43:35 +020054enum vlr_stat_item_idx {
55 VLR_STAT_SUBSCRIBER_COUNT,
56 VLR_STAT_PDP_COUNT,
57};
58
59static const struct osmo_stat_item_desc vlr_stat_item_desc[] = {
60 [VLR_STAT_SUBSCRIBER_COUNT] = { "subscribers",
61 "Number of subscribers present in VLR" },
62 [VLR_STAT_PDP_COUNT] = { "pdp",
63 "Number of PDP records present in VLR" },
64};
65
66static const struct osmo_stat_item_group_desc vlr_statg_desc = {
67 "vlr",
68 "visitor location register",
69 OSMO_STATS_CLASS_GLOBAL,
70 ARRAY_SIZE(vlr_stat_item_desc),
71 vlr_stat_item_desc,
72};
73
74enum vlr_rate_ctr_idx {
75 VLR_CTR_GSUP_RX_UNKNOWN_IMSI,
76 VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR,
77 VLR_CTR_GSUP_RX_TUPLES,
78 VLR_CTR_GSUP_RX_UL_RES,
79 VLR_CTR_GSUP_RX_UL_ERR,
80 VLR_CTR_GSUP_RX_SAI_RES,
81 VLR_CTR_GSUP_RX_SAI_ERR,
82 VLR_CTR_GSUP_RX_ISD_REQ,
83 VLR_CTR_GSUP_RX_CANCEL_REQ,
84 VLR_CTR_GSUP_RX_CHECK_IMEI_RES,
85 VLR_CTR_GSUP_RX_CHECK_IMEI_ERR,
86 VLR_CTR_GSUP_RX_PURGE_MS_RES,
87 VLR_CTR_GSUP_RX_PURGE_MS_ERR,
88 VLR_CTR_GSUP_RX_DELETE_DATA_REQ,
89 VLR_CTR_GSUP_RX_UNKNOWN,
90
91 VLR_CTR_GSUP_TX_UL_REQ,
92 VLR_CTR_GSUP_TX_ISD_RES,
93 VLR_CTR_GSUP_TX_SAI_REQ,
94 VLR_CTR_GSUP_TX_PURGE_MS_REQ,
95 VLR_CTR_GSUP_TX_CHECK_IMEI_REQ,
96 VLR_CTR_GSUP_TX_AUTH_FAIL_REP,
97 VLR_CTR_GSUP_TX_CANCEL_RES,
98
99 VLR_CTR_DETACH_BY_REQ,
100 VLR_CTR_DETACH_BY_CANCEL,
101 VLR_CTR_DETACH_BY_T3212,
102};
103
104static const struct rate_ctr_desc vlr_ctr_desc[] = {
105 [VLR_CTR_GSUP_RX_UNKNOWN_IMSI] = { "gsup:rx:unknown_imsi",
106 "Received GSUP messages for unknown IMSI" },
107 [VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR] = { "gsup:rx:purge_no_subscr",
108 "Received GSUP purge for unknown subscriber" },
109 [VLR_CTR_GSUP_RX_TUPLES] = { "gsup:rx:auth_tuples",
110 "Received GSUP authentication tuples" },
111 [VLR_CTR_GSUP_RX_UL_RES] = { "gsup:rx:upd_loc:res",
112 "Received GSUP Update Location Result messages" },
113 [VLR_CTR_GSUP_RX_UL_ERR] = { "gsup:rx:upd_loc:err",
114 "Received GSUP Update Location Error messages" },
115 [VLR_CTR_GSUP_RX_SAI_RES] = { "gsup:rx:send_auth_info:res",
116 "Received GSUP Send Auth Info Result messages" },
117 [VLR_CTR_GSUP_RX_SAI_ERR] = { "gsup:rx:send_auth_info:err",
118 "Received GSUP Send Auth Info Error messages" },
119 [VLR_CTR_GSUP_RX_ISD_REQ] = { "gsup:rx:ins_sub_data:req",
120 "Received GSUP Insert Subscriber Data Request messages" },
121 [VLR_CTR_GSUP_RX_CANCEL_REQ] = { "gsup:rx:cancel:req",
122 "Received GSUP Cancel Subscriber messages" },
123 [VLR_CTR_GSUP_RX_CHECK_IMEI_RES] = { "gsup:rx:check_imei:res",
124 "Received GSUP Check IMEI Result messages" },
125 [VLR_CTR_GSUP_RX_CHECK_IMEI_ERR] = { "gsup:rx:check_imei:err",
126 "Received GSUP Check IMEI Error messages" },
127 [VLR_CTR_GSUP_RX_PURGE_MS_RES] = { "gsup:rx:purge_ms:res",
128 "Received GSUP Purge MS Result messages" },
129 [VLR_CTR_GSUP_RX_PURGE_MS_ERR] = { "gsup:rx:purge_ms:err",
130 "Received GSUP Purge MS Error messages" },
131 [VLR_CTR_GSUP_RX_DELETE_DATA_REQ] = { "gsup:rx:del_sub_data:req",
132 "Received GSUP Delete Subscriber Data Request messages" },
133 [VLR_CTR_GSUP_RX_UNKNOWN] = { "gsup:rx:unknown_msgtype",
134 "Received GSUP message of unknown type" },
135
136 [VLR_CTR_GSUP_TX_UL_REQ] = { "gsup:tx:upd_loc:req",
137 "Transmitted GSUP Update Location Request messages" },
138 [VLR_CTR_GSUP_TX_ISD_RES] = { "gsup:tx:ins_sub_data:res",
139 "Transmitted GSUP Insert Subscriber Data Result messages" },
140 [VLR_CTR_GSUP_TX_SAI_REQ] = { "gsup:tx:send_auth_info:res",
141 "Transmitted GSUP Send Auth Info Request messages" },
142 [VLR_CTR_GSUP_TX_PURGE_MS_REQ] = { "gsup:tx:purge_ms:req",
143 "Transmitted GSUP Purge MS Request messages" },
144 [VLR_CTR_GSUP_TX_CHECK_IMEI_REQ] = { "gsup:tx:check_imei:req",
145 "Transmitted GSUP Check IMEI Request messages" },
146 [VLR_CTR_GSUP_TX_AUTH_FAIL_REP] = { "gsup:tx:auth_fail:rep",
147 "Transmitted GSUP Auth Fail Report messages" },
148 [VLR_CTR_GSUP_TX_CANCEL_RES] = { "gsup:tx:cancel:res",
149 "Transmitted GSUP Cancel Result messages" },
150
151 [VLR_CTR_DETACH_BY_REQ] = { "detach:imsi_det_req",
152 "VLR Subscriber Detach by IMSI DETACH REQ" },
153 [VLR_CTR_DETACH_BY_CANCEL] = { "detach:gsup_cancel_req",
154 "VLR Subscriber Detach by GSUP CANCEL REQ" },
155 [VLR_CTR_DETACH_BY_T3212] = { "detach:t3212_timeout",
156 "VLR Subscriber Detach by T3212 timeout" },
157};
158
159static const struct rate_ctr_group_desc vlr_ctrg_desc = {
160 "vlr",
161 "visitor location register",
162 OSMO_STATS_CLASS_GLOBAL,
163 ARRAY_SIZE(vlr_ctr_desc),
164 vlr_ctr_desc,
165};
166
167
168#define vlr_rate_ctr_inc(vlr, idx) \
169 rate_ctr_inc(rate_ctr_group_get_ctr((vlr)->ctrg, idx))
170#define vlr_rate_ctr_add(vlr, idx, val) \
171 rate_ctr_add(rate_ctr_group_get_ctr((vlr)->ctrg, idx), val)
172
173#define vlr_stat_item_inc(vlr, idx) \
174 osmo_stat_item_inc(osmo_stat_item_group_get_item((vlr)->statg, idx), 1)
175#define vlr_stat_item_dec(vlr, idx) \
176 osmo_stat_item_dec(osmo_stat_item_group_get_item((vlr)->statg, idx), 1)
177#define vlr_stat_item_set(vlr, idx, val) \
178 osmo_stat_item_set(osmo_stat_item_group_get_item((vlr)->statg, idx), val)
179
180
Harald Welteb8b85a12016-06-17 00:06:42 +0200181/***********************************************************************
182 * Convenience functions
183 ***********************************************************************/
184
Harald Welte173bdf32022-05-15 12:12:54 +0200185static int vlr_subscr_detach(struct vlr_subscr *vsub);
186
Harald Welteb8b85a12016-06-17 00:06:42 +0200187const struct value_string vlr_ciph_names[] = {
188 OSMO_VALUE_STRING(VLR_CIPH_NONE),
189 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
190 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
191 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
192 { 0, NULL }
193};
194
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700195/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
196struct osmo_tdef msc_tdefs_vlr[] = {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700197 { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" },
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700198 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
199 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
200 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
201 { /* terminator */ }
202};
203
204/* This is just a wrapper around the osmo_tdef API.
205 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +0200206uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
207{
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700208 /* NOTE: since we usually do not need more than one instance of the VLR,
209 * and since libosmocore's osmo_tdef API does not (yet) support dynamic
210 * configuration, we always use the global instance of msc_tdefs_vlr. */
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700211 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200212}
213
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100214/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +0100215const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100216{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100217 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100218 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100219 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100220 if (!vsub)
221 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100222 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100223 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100224 present = true;
225 }
226 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100227 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100228 present = true;
229 }
230 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100231 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100232 present = true;
233 }
234 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100235 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100236 present = true;
237 }
238 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100239 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100240
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100241 return buf;
242}
243
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100244const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
245{
246 /* cast away the const so we can shorten the string within the static buffer */
247 char *name = (char*)vlr_subscr_name(vsub);
248 size_t len = strlen(name);
249 if (maxlen < 2)
250 return "-";
251 if (len > maxlen)
252 strcpy(name + maxlen - 2, "..");
253 return name;
254}
255
Oliver Smith5598aae2019-01-08 11:47:21 +0100256const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100257{
258 if (!vsub || !vsub->msisdn[0])
259 return vlr_subscr_name(vsub);
260 return vsub->msisdn;
261}
262
Harald Welteb8b85a12016-06-17 00:06:42 +0200263struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
264 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100265 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200266 const char *file, int line)
267{
268 struct vlr_subscr *vsub;
269
270 if (!imsi || !*imsi)
271 return NULL;
272
273 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100274 if (vlr_subscr_matches_imsi(vsub, imsi)) {
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100275 if (use)
276 vlr_subscr_get_src(vsub, use, file, line);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100277 return vsub;
278 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200279 }
280 return NULL;
281}
282
283struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
284 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100285 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200286 const char *file, int line)
287{
288 struct vlr_subscr *vsub;
289
290 if (tmsi == GSM_RESERVED_TMSI)
291 return NULL;
292
293 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100294 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
295 vlr_subscr_get_src(vsub, use, file, line);
296 return vsub;
297 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200298 }
299 return NULL;
300}
301
302struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
303 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100304 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200305 const char *file, int line)
306{
307 struct vlr_subscr *vsub;
308
309 if (!msisdn || !*msisdn)
310 return NULL;
311
312 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100313 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
314 vlr_subscr_get_src(vsub, use, file, line);
315 return vsub;
316 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200317 }
318 return NULL;
319}
320
Neels Hofmeyr5bdba0d2021-07-27 03:46:18 +0200321struct vlr_subscr *_vlr_subscr_find_by_mi(struct vlr_instance *vlr,
322 const struct osmo_mobile_identity *mi,
323 const char *use,
324 const char *file, int line)
325{
326 switch (mi->type) {
327 case GSM_MI_TYPE_IMSI:
328 return _vlr_subscr_find_by_imsi(vlr, mi->imsi, use, file, line);
329 case GSM_MI_TYPE_TMSI:
330 return _vlr_subscr_find_by_tmsi(vlr, mi->tmsi, use, file, line);
331 default:
332 return NULL;
333 }
334}
335
Harald Welteb8b85a12016-06-17 00:06:42 +0200336/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100337static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200338 struct osmo_gsup_message *gsup_msg)
339{
340 struct vlr_instance *vlr = vsub->vlr;
341
342 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100343 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200344
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100345 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200346
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100347 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200348}
349
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100350static 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 +0200351{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100352 struct vlr_subscr *vsub = e->use_count->talloc_object;
353 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100354 int32_t total;
355 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200356
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100357 if (!e->use)
358 return -EINVAL;
359
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100360 total = osmo_use_count_total(&vsub->use_count);
361
362 if (total == 0
363 || (total == 1 && old_use_count == 0 && e->count == 1))
364 level = LOGL_INFO;
365 else
366 level = LOGL_DEBUG;
367
368 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100369 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
370 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
371
372 if (e->count < 0)
373 return -ERANGE;
374
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100375 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
376
377 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100378 vlr_subscr_free(vsub);
379 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200380}
381
382/* Allocate a new subscriber and insert it into list */
383static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
384{
385 struct vlr_subscr *vsub;
386 int i;
387
388 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100389 *vsub = (struct vlr_subscr){
390 .vlr = vlr,
391 .tmsi = GSM_RESERVED_TMSI,
392 .tmsi_new = GSM_RESERVED_TMSI,
393 .use_count = (struct osmo_use_count){
394 .talloc_object = vsub,
395 .use_cb = vlr_subscr_use_cb,
396 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100397 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100398 };
399 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 +0200400
401 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100402 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200403
404 INIT_LLIST_HEAD(&vsub->cs.requests);
405 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
406
Harald Welte0df904d2018-12-03 11:00:04 +0100407 /* Create an SGs FSM, which is needed to control CSFB,
408 * in cases where CSFB/SGs is not in use, this FSM will
409 * just do nothing. (see also: sgs_iface.c) */
410 vlr_sgs_fsm_create(vsub);
411
Harald Welteb8b85a12016-06-17 00:06:42 +0200412 llist_add_tail(&vsub->list, &vlr->subscribers);
Harald Welte123261e2022-05-15 11:43:35 +0200413 vlr_stat_item_inc(vlr, VLR_STAT_SUBSCRIBER_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200414 return vsub;
415}
416
Harald Welteb8b85a12016-06-17 00:06:42 +0200417/* Send a GSUP Purge MS request.
418 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
419 * this subscriber, not to the HLR? */
420int vlr_subscr_purge(struct vlr_subscr *vsub)
421{
422 struct osmo_gsup_message gsup_msg = {0};
423
Harald Welte123261e2022-05-15 11:43:35 +0200424 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_PURGE_MS_REQ);
425
Harald Welteb8b85a12016-06-17 00:06:42 +0200426 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
427
428 /* provide HLR number in case we know it */
429 gsup_msg.hlr_enc_len = vsub->hlr.len;
430 gsup_msg.hlr_enc = vsub->hlr.buf;
431
432 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
433}
434
Neels Hofmeyr15809592018-04-06 02:57:51 +0200435void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
436 enum osmo_fsm_term_cause fsm_cause,
437 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200438{
439 if (!vsub)
440 return;
441
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100442 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200443 if (vsub->lu_fsm)
444 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200445 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200446 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100447 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200448}
449
450/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
451void vlr_subscr_free(struct vlr_subscr *vsub)
452{
453 llist_del(&vsub->list);
Harald Welte123261e2022-05-15 11:43:35 +0200454 vlr_stat_item_dec(vsub->vlr, VLR_STAT_SUBSCRIBER_COUNT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
456 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100457
458 /* Make sure SGs timer Ts5 is removed */
459 osmo_timer_del(&vsub->sgs.Ts5);
460
461 /* Remove SGs FSM (see also: sgs_iface.c) */
462 vlr_sgs_fsm_remove(vsub);
463
Harald Welteb8b85a12016-06-17 00:06:42 +0200464 talloc_free(vsub);
465}
466
467/* Generate a new TMSI and store in vsub->tmsi_new.
468 * Search all known subscribers to ensure that the TMSI is unique. */
469int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
470{
471 struct vlr_instance *vlr = vsub->vlr;
472 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100473 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100474 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200475
476 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100477 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
478 if (rc < 0) {
479 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
480 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200481 }
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +0200482
483 if (!llist_empty(&vlr->cfg.nri_ranges->entries)) {
484 int16_t nri_v;
485 osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen);
486 osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen);
487 LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n",
488 osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi);
489 }
490
Harald Welteb8b85a12016-06-17 00:06:42 +0200491 /* throw the dice again, if the TSMI doesn't fit */
492 if (tmsi == GSM_RESERVED_TMSI)
493 continue;
494
495 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
496 if (vlr->cfg.is_ps) {
497 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200498 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200499 } else {
500 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200501 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
502 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200503 }
504
505 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100506 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
507 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200508 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100509 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200510
511 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100512 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200513 return 0;
514 }
515
516 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
517 " after %d tries\n", vlr_subscr_name(vsub), tried);
518 return -1;
519}
520
521/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100522 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200523 * \param[in] imsi IMSI string.
524 * \param[out] created if non-NULL, returns whether a new entry was created. */
525struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
526 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100527 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200528 bool *created,
529 const char *file,
530 int line)
531{
532 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100533 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200534 if (vsub) {
535 if (created)
536 *created = false;
537 return vsub;
538 }
539
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100540 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200541 if (!vsub)
542 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100543 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200544 vlr_subscr_set_imsi(vsub, imsi);
545 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
546 if (created)
547 *created = true;
548 return vsub;
549}
550
551/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100552 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200553 * \param[in] tmsi TMSI.
554 * \param[out] created if non-NULL, returns whether a new entry was created. */
555struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
556 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100557 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200558 bool *created,
559 const char *file,
560 int line)
561{
562 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100563 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200564 if (vsub) {
565 if (created)
566 *created = false;
567 return vsub;
568 }
569
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100570 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200571 if (!vsub)
572 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100573 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200574 vsub->tmsi = tmsi;
575 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
576 if (created)
577 *created = true;
578 return vsub;
579}
580
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100581static void dedup_vsub(struct vlr_subscr *exists, struct vlr_subscr *vsub)
582{
583 struct vlr_instance *vlr = exists->vlr;
584 int i;
585 int j;
586 LOGP(DVLR, LOGL_NOTICE,
587 "There is an existing subscriber for IMSI %s used by %s, replacing with new VLR subscr: %s used by %s\n",
588 exists->imsi, osmo_use_count_to_str_c(OTC_SELECT, &exists->use_count),
589 vlr_subscr_name(vsub),
590 osmo_use_count_to_str_c(OTC_SELECT, &vsub->use_count));
591
592 /* Take over some state from the previous vsub */
593 paging_request_join_vsub(vsub, exists);
594 if (!vsub->msisdn[0])
595 OSMO_STRLCPY_ARRAY(vsub->msisdn, exists->msisdn);
596 if (!vsub->name[0])
597 OSMO_STRLCPY_ARRAY(vsub->name, exists->name);
598 /* Copy valid auth tuples we may already have, to reduce the need to ask for new ones from the HLR */
599 for (i = 0; i < ARRAY_SIZE(exists->auth_tuples); i++) {
600 if (exists->auth_tuples[i].key_seq == VLR_KEY_SEQ_INVAL)
601 continue;
602 for (j = 0; j < ARRAY_SIZE(vsub->auth_tuples); j++) {
603 if (vsub->auth_tuples[j].key_seq != VLR_KEY_SEQ_INVAL)
604 continue;
605 vsub->auth_tuples[j] = exists->auth_tuples[i];
606 }
607 }
608
609 if (exists->msc_conn_ref)
610 LOGVSUBP(LOGL_ERROR, vsub,
611 "There is an existing VLR entry for this same subscriber with an active connection."
612 " That should not be possible. Discarding old subscriber entry %s.\n",
613 exists->imsi);
614
615 if (vlr->ops.subscr_inval)
616 vlr->ops.subscr_inval(exists->msc_conn_ref, exists);
617 vlr_subscr_free(exists);
618}
619
Harald Welteb8b85a12016-06-17 00:06:42 +0200620void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
621{
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100622 struct vlr_subscr *exists;
Harald Welteb8b85a12016-06-17 00:06:42 +0200623 if (!vsub)
624 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200625
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100626 /* If the same IMSI is already set, nothing changes. */
627 if (!strcmp(vsub->imsi, imsi))
628 return;
629
630 /* We've just learned about this new IMSI, our primary key in the VLR. make sure to invalidate any prior VLR
631 * entries for this IMSI. */
632 exists = vlr_subscr_find_by_imsi(vsub->vlr, imsi, NULL);
633
634 if (exists)
635 dedup_vsub(exists, vsub);
636
637 /* Set the IMSI on the new subscriber, here. */
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200638 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
639 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
640 imsi, vsub->imsi);
641 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
642 }
643
Harald Welteb8b85a12016-06-17 00:06:42 +0200644 vsub->id = atoll(vsub->imsi);
645 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
646 vsub->imsi, vsub->id);
647}
648
649void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
650{
651 if (!vsub)
652 return;
Max98f74672018-02-05 12:57:06 +0100653 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200654 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
655 vsub->imsi, vsub->imei);
656}
657
658void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
659{
660 if (!vsub)
661 return;
Max98f74672018-02-05 12:57:06 +0100662 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200663 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
664 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200665
666 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
667 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200668}
669
670/* Safely copy the given MSISDN string to vsub->msisdn */
671void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
672{
673 if (!vsub)
674 return;
Max98f74672018-02-05 12:57:06 +0100675 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200676 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
677 vsub->imsi, vsub->msisdn);
678}
679
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200680void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
681 const struct osmo_plmn_id *last_eutran_plmn)
682{
683 if (!vsub)
684 return;
685 if (last_eutran_plmn) {
686 vsub->sgs.last_eutran_plmn_present = true;
687 memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
688 } else {
689 vsub->sgs.last_eutran_plmn_present = false;
690 }
691 DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
692 vsub->sgs.last_eutran_plmn_present ?
693 osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
694 "(none)");
695}
696
Harald Welteb8b85a12016-06-17 00:06:42 +0200697bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
698{
699 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
700}
701
702bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
703{
704 return vsub && tmsi != GSM_RESERVED_TMSI
705 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
706}
707
708bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
709{
710 return vsub && msisdn && vsub->msisdn[0]
711 && !strcmp(vsub->msisdn, msisdn);
712}
713
714bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
715{
716 return vsub && imei && vsub->imei[0]
717 && !strcmp(vsub->imei, imei);
718}
719
720/* Send updated subscriber information to HLR */
721int vlr_subscr_changed(struct vlr_subscr *vsub)
722{
723 /* FIXME */
724 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
725 return 0;
726}
727
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200728void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
729{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200730 struct timespec now;
731
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700732 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200733 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700734 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200735 } else {
736 LOGP(DVLR, LOGL_ERROR,
737 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
738 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
739 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
740 }
741}
742
743void vlr_subscr_expire_lu(void *data)
744{
745 struct vlr_instance *vlr = data;
746 struct vlr_subscr *vsub, *vsub_tmp;
747 struct timespec now;
748
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700749 /* Periodic location update might be disabled from the VTY,
750 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700751 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700752 goto done;
753
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200754 if (llist_empty(&vlr->subscribers))
755 goto done;
756
757 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
758 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
759 goto done;
760 }
761
762 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
763 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
764 continue;
765
766 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
Harald Welte123261e2022-05-15 11:43:35 +0200767 vlr_rate_ctr_inc(vlr, VLR_CTR_DETACH_BY_T3212);
Harald Welte173bdf32022-05-15 12:12:54 +0200768 vlr_subscr_detach(vsub);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200769 }
770
771done:
772 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
773}
774
Harald Welteb8b85a12016-06-17 00:06:42 +0200775/***********************************************************************
776 * PDP context data
777 ***********************************************************************/
778
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200779#define GSM_APN_LENGTH 102
780
781/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
782/* see GSM 09.02, B.1, gprsSubscriptionData */
783struct sgsn_subscriber_pdp_data {
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100784 struct llist_head list;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200785
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100786 unsigned int context_id;
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +0100787 enum gsm48_pdp_type_org pdp_type_org;
788 enum gsm48_pdp_type_nr pdp_type_nr;
789 struct osmo_sockaddr pdp_address[2];
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100790 char apn_str[GSM_APN_LENGTH];
791 uint8_t qos_subscribed[20];
792 size_t qos_subscribed_len;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200793};
794
Harald Welteb8b85a12016-06-17 00:06:42 +0200795struct sgsn_subscriber_pdp_data *
796vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
797{
798 struct sgsn_subscriber_pdp_data* pdata;
799
800 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
801
802 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
Harald Welte123261e2022-05-15 11:43:35 +0200803 vlr_stat_item_inc(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200804
805 return pdata;
806}
807
808static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
809{
810 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
811 int count = 0;
812
813 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
814 llist_del(&pdp->list);
Harald Welte123261e2022-05-15 11:43:35 +0200815 vlr_stat_item_dec(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200816 talloc_free(pdp);
817 count += 1;
818 }
819
820 return count;
821}
822
823static struct sgsn_subscriber_pdp_data *
824vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
825{
826 struct sgsn_subscriber_pdp_data *pdp;
827
828 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
829 if (pdp->context_id == context_id)
830 return pdp;
831 }
832
833 return NULL;
834}
835
836/***********************************************************************
837 * Actual Implementation
838 ***********************************************************************/
839
840static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100841 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200842{
843 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200844 LOGP(DVLR, LOGL_NOTICE,
845 "Unknown IMSI %s, discarding GSUP request "
846 "of type 0x%02x\n",
847 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100848 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200849 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
850 LOGP(DVLR, LOGL_NOTICE,
851 "Unknown IMSI %s, discarding GSUP error "
852 "of type 0x%02x, cause '%s' (%d)\n",
853 gsup_msg->imsi, gsup_msg->message_type,
854 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
855 gsup_msg->cause);
856 } else {
857 LOGP(DVLR, LOGL_NOTICE,
858 "Unknown IMSI %s, discarding GSUP response "
859 "of type 0x%02x\n",
860 gsup_msg->imsi, gsup_msg->message_type);
861 }
862
863 return -GMM_CAUSE_IMSI_UNKNOWN;
864}
865
866static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100867 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200868{
869 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
870 LOGGSUPP(LOGL_NOTICE, gsup_msg,
871 "Purge MS has failed with cause '%s' (%d)\n",
872 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
873 gsup_msg->cause);
874 return -gsup_msg->cause;
875 }
876 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
877 return 0;
878}
879
880/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100881int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200882{
883 struct osmo_gsup_message gsup_msg = {0};
884 int rc;
885
Harald Welte123261e2022-05-15 11:43:35 +0200886 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_UL_REQ);
887
Harald Welteb8b85a12016-06-17 00:06:42 +0200888 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200889 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 +0200890 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
891
892 return rc;
893}
894
895/* VLR internal call to request tuples from HLR */
896int vlr_subscr_req_sai(struct vlr_subscr *vsub,
897 const uint8_t *auts, const uint8_t *auts_rand)
898{
899 struct osmo_gsup_message gsup_msg = {0};
900
Harald Welte123261e2022-05-15 11:43:35 +0200901 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_SAI_REQ);
902
Harald Welteb8b85a12016-06-17 00:06:42 +0200903 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
904 gsup_msg.auts = auts;
905 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100906 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200907
908 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
909}
910
Oliver Smith7d053092018-12-14 17:37:38 +0100911/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
912int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
913{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100914 struct osmo_gsup_message gsup_msg = {
915 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200916 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100917 };
Oliver Smith7d053092018-12-14 17:37:38 +0100918 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
919 int len;
920
921 /* Encode IMEI */
922 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
923 if (len < 1) {
924 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
925 return -ENOSPC;
926 }
927 gsup_msg.imei_enc = imei_enc;
928 gsup_msg.imei_enc_len = len;
929
Harald Welte123261e2022-05-15 11:43:35 +0200930 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CHECK_IMEI_REQ);
931
Oliver Smith7d053092018-12-14 17:37:38 +0100932 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100933 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100934 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100935}
936
Harald Welteb8b85a12016-06-17 00:06:42 +0200937/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100938int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200939{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100940 struct osmo_gsup_message gsup_msg = {
941 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200942 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100943 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200944
Harald Welte123261e2022-05-15 11:43:35 +0200945 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_AUTH_FAIL_REP);
946
Max98f74672018-02-05 12:57:06 +0100947 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100948 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200949}
950
951/* Update the subscriber with GSUP-received auth tuples */
952void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
953 const struct osmo_gsup_message *gsup)
954{
955 unsigned int i;
956 unsigned int got_tuples;
957
958 if (gsup->num_auth_vectors) {
959 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
960 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100961 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200962 }
963
964 got_tuples = 0;
965 for (i = 0; i < gsup->num_auth_vectors; i++) {
966 size_t key_seq = i;
967
968 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
969 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100970 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200971 key_seq);
972 continue;
973 }
974 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
975 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100976 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200977 }
978
979 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
Harald Welte123261e2022-05-15 11:43:35 +0200980 vlr_rate_ctr_add(vsub->vlr, VLR_CTR_GSUP_RX_TUPLES, got_tuples);
Harald Welteb8b85a12016-06-17 00:06:42 +0200981
982 if (!got_tuples) {
983 /* FIXME what now? */
984 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
985 }
986
987 /* New tuples means last_tuple becomes invalid */
988 vsub->last_tuple = NULL;
989}
990
991/* Handle SendAuthInfo Result/Error from HLR */
992static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
993 const struct osmo_gsup_message *gsup)
994{
995 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
996 void *data = (void *) gsup;
997
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200998 if (!auth_fi) {
999 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
1000 osmo_gsup_message_type_name(gsup->message_type));
1001 return -1;
1002 }
1003
Harald Welteb8b85a12016-06-17 00:06:42 +02001004 switch (gsup->message_type) {
1005 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1006 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
1007 break;
1008 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1009 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
1010 break;
1011 default:
1012 return -1;
1013 }
1014
1015 return 0;
1016}
1017
Harald Welteb8b85a12016-06-17 00:06:42 +02001018static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
1019 const struct osmo_gsup_message *gsup_msg)
1020{
1021 unsigned idx;
1022 int rc;
1023
Neels Hofmeyr02dd2652019-11-13 07:05:11 +01001024 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001025 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
1026 gsup_msg->msisdn_enc,
1027 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001028 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
1029 vsub->imsi, vsub->msisdn);
1030 }
1031
1032 if (gsup_msg->hlr_enc) {
1033 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
1034 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
1035 gsup_msg->hlr_enc_len);
1036 vsub->hlr.len = 0;
1037 } else {
1038 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
1039 gsup_msg->hlr_enc_len);
1040 vsub->hlr.len = gsup_msg->hlr_enc_len;
1041 }
1042 }
1043
1044 if (gsup_msg->pdp_info_compl) {
1045 rc = vlr_subscr_pdp_data_clear(vsub);
1046 if (rc > 0)
1047 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
1048 }
1049
1050 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
1051 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
1052 size_t ctx_id = pdp_info->context_id;
1053 struct sgsn_subscriber_pdp_data *pdp_data;
1054
1055 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
1056 LOGVSUBP(LOGL_ERROR, vsub,
1057 "APN too long, context id = %zu, APN = %s\n",
1058 ctx_id, osmo_hexdump(pdp_info->apn_enc,
1059 pdp_info->apn_enc_len));
1060 continue;
1061 }
1062
1063 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
1064 LOGVSUBP(LOGL_ERROR, vsub,
1065 "QoS info too long (%zu)\n",
1066 pdp_info->qos_enc_len);
1067 continue;
1068 }
1069
1070 LOGVSUBP(LOGL_INFO, vsub,
1071 "Will set PDP info, context id = %zu, APN = %s\n",
1072 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
1073
1074 /* Set PDP info [ctx_id] */
1075 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
1076 if (!pdp_data) {
1077 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
1078 pdp_data->context_id = ctx_id;
1079 }
1080
1081 OSMO_ASSERT(pdp_data != NULL);
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +01001082 pdp_data->pdp_type_org = pdp_info->pdp_type_org;
1083 pdp_data->pdp_type_nr = pdp_info->pdp_type_nr;
1084 memcpy(&pdp_data->pdp_address[0], &pdp_info->pdp_address[0], sizeof(pdp_data->pdp_address[0]));
1085 memcpy(&pdp_data->pdp_address[1], &pdp_info->pdp_address[1], sizeof(pdp_data->pdp_address[1]));
Harald Welteb8b85a12016-06-17 00:06:42 +02001086 osmo_apn_to_str(pdp_data->apn_str,
1087 pdp_info->apn_enc, pdp_info->apn_enc_len);
1088 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
1089 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
1090 }
1091}
1092
1093
1094/* Handle InsertSubscrData Result from HLR */
1095static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
1096 const struct osmo_gsup_message *gsup)
1097{
1098 struct osmo_gsup_message gsup_reply = {0};
1099
Harald Welte123261e2022-05-15 11:43:35 +02001100 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_ISD_RES);
1101
Harald Welteb8b85a12016-06-17 00:06:42 +02001102 vlr_subscr_gsup_insert_data(vsub, gsup);
1103 vsub->vlr->ops.subscr_update(vsub);
1104
1105 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
1106 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
1107}
1108
1109/* Handle UpdateLocation Result from HLR */
1110static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
1111 const struct osmo_gsup_message *gsup)
1112{
Philipp Maier483cea82019-04-03 16:23:29 +02001113 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001114 bool sgs_lu_in_progress = false;
1115
1116 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1117 sgs_lu_in_progress = true;
1118
1119 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001120 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
1121 "without LU in progress\n");
1122 return -ENODEV;
1123 }
1124
1125 /* contrary to MAP, we allow piggy-backing subscriber data onto the
1126 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
1127 * nested INSERT SUBSCRIBER DATA transaction */
1128 vlr_subscr_gsup_insert_data(vsub, gsup);
1129
Harald Welte0df904d2018-12-03 11:00:04 +01001130 if (sgs_lu_in_progress) {
1131 sgs_lu_response.accepted = true;
1132 sgs_lu_response.vsub = vsub;
1133 vsub->sgs.response_cb(&sgs_lu_response);
1134 } else
1135 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001136
1137 return 0;
1138}
1139
1140/* Handle UpdateLocation Result from HLR */
1141static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
1142 const struct osmo_gsup_message *gsup)
1143{
Philipp Maier483cea82019-04-03 16:23:29 +02001144 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001145 bool sgs_lu_in_progress = false;
1146
1147 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1148 sgs_lu_in_progress = true;
1149
1150 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001151 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
1152 "without LU in progress\n");
1153 return -ENODEV;
1154 }
1155
1156 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
1157 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1158
Harald Welte0df904d2018-12-03 11:00:04 +01001159 if (sgs_lu_in_progress) {
1160 sgs_lu_response.accepted = false;
1161 sgs_lu_response.vsub = vsub;
1162 vsub->sgs.response_cb(&sgs_lu_response);
1163 } else
1164 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
1165 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001166 return 0;
1167}
1168
Alexander Couzens7312b152019-08-19 15:30:12 +02001169void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
1170 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +02001171{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001172 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1173 switch (gmm_cause) {
1174 case GMM_CAUSE_IMSI_UNKNOWN:
1175 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
1176 break;
1177 case GMM_CAUSE_ILLEGAL_MS:
1178 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
1179 break;
1180 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
1181 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
1182 break;
1183 case GMM_CAUSE_ILLEGAL_ME:
1184 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
1185 break;
1186 case GMM_CAUSE_GPRS_NOTALLOWED:
1187 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
1188 break;
1189 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
1190 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
1191 break;
1192 case GMM_CAUSE_MS_ID_NOT_DERIVED:
1193 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
1194 break;
1195 case GMM_CAUSE_IMPL_DETACHED:
1196 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
1197 break;
1198 case GMM_CAUSE_PLMN_NOTALLOWED:
1199 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
1200 break;
1201 case GMM_CAUSE_LA_NOTALLOWED:
1202 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
1203 break;
1204 case GMM_CAUSE_ROAMING_NOTALLOWED:
1205 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
1206 break;
1207 case GMM_CAUSE_NO_GPRS_PLMN:
1208 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
1209 break;
1210 case GMM_CAUSE_MSC_TEMP_NOTREACH:
1211 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
1212 break;
1213 case GMM_CAUSE_SYNC_FAIL:
1214 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
1215 break;
1216 case GMM_CAUSE_CONGESTION:
1217 gsm48_rej = GSM48_REJECT_CONGESTION;
1218 break;
1219 case GMM_CAUSE_SEM_INCORR_MSG:
1220 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1221 break;
1222 case GMM_CAUSE_INV_MAND_INFO:
1223 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1224 break;
1225 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1226 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1227 break;
1228 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1229 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1230 break;
1231 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1232 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1233 break;
1234 case GMM_CAUSE_COND_IE_ERR:
1235 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1236 break;
1237 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1238 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1239 break;
1240 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1241 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1242 break;
1243
1244 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1245 case GMM_CAUSE_MAC_FAIL:
1246 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1247 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1248 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1249 case GMM_CAUSE_NO_PDP_ACTIVATED:
1250 case GMM_CAUSE_NET_FAIL:
1251 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1252 break;
1253 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001254
1255 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001256}
1257
Harald Welteb8b85a12016-06-17 00:06:42 +02001258/* Handle LOCATION CANCEL request from HLR */
1259static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001260 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001261{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001262 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001263 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001264 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001265 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001266 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1267
Harald Welte123261e2022-05-15 11:43:35 +02001268 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CANCEL_RES);
1269
Harald Welteb8b85a12016-06-17 00:06:42 +02001270 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1271 is_update_procedure ?
1272 "update procedure" : "subscription withdraw");
1273
1274 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001275 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001276
Alexander Couzens7312b152019-08-19 15:30:12 +02001277 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001278 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001279
Harald Welte123261e2022-05-15 11:43:35 +02001280 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_CANCEL);
Harald Welte173bdf32022-05-15 12:12:54 +02001281 vlr_subscr_detach(vsub);
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001282
Max770fbd22018-01-24 12:48:33 +01001283 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001284}
1285
Oliver Smith7d053092018-12-14 17:37:38 +01001286/* Handle Check_IMEI_VLR result and error from HLR */
1287static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1288{
1289 if (!vsub->lu_fsm) {
1290 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1291 osmo_gsup_message_type_name(gsup->message_type));
1292 return -ENODEV;
1293 }
1294
Oliver Smithcbf2c932019-05-06 13:09:55 +02001295 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
Alexander Couzens25a92b82024-09-12 01:27:52 +02001296 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001297 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1298 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1299 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1300 else
1301 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1302 } else {
1303 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1304 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1305 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1306 }
1307
1308 return 0;
1309}
1310
Harald Welteb8b85a12016-06-17 00:06:42 +02001311/* Incoming handler for GSUP from HLR.
1312 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001313int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001314{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001315 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001316 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001317 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001318
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001319 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001320 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001321 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001322 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1323 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001324 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001325 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001326 default:
Harald Welte123261e2022-05-15 11:43:35 +02001327 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN_IMSI);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001328 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001329 }
1330 }
1331
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001332 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001333 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001334 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_RES);
1335 rc = vlr_subscr_handle_sai_res(vsub, gsup);
1336 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001337 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001338 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001339 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001340 break;
1341 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001342 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_ISD_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001343 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001344 break;
1345 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001346 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CANCEL_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001347 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001348 break;
1349 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001350 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001351 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001352 break;
1353 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001354 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001355 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001356 break;
1357 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001358 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_ERR);
1359 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001360 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001361 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_RES);
1362 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001363 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001364 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_DELETE_DATA_REQ);
1365 goto out_unimpl;
Oliver Smith7d053092018-12-14 17:37:38 +01001366 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001367 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_ERR);
1368 rc = vlr_subscr_handle_check_imei(vsub, gsup);
1369 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001370 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001371 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001372 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001373 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001374 default:
Harald Welte123261e2022-05-15 11:43:35 +02001375 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001376 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1377 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001378 break;
1379 }
1380
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001381 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001382 return rc;
Harald Welte123261e2022-05-15 11:43:35 +02001383
1384out_unimpl:
1385 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP msg_type=%d not yet implemented\n", gsup->message_type);
1386 vlr_subscr_put(vsub, __func__);
1387 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001388}
1389
1390/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001391int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001392{
Harald Welteb8b85a12016-06-17 00:06:42 +02001393 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001394 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001395 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001396 if (vsub->imsi[0]
1397 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001398 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001399 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001400 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001401 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001402 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001403 break;
1404 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001405 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001406 break;
1407 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001408 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001409 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001410 default:
1411 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001412 }
1413
1414 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001415 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001416 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001417 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1418 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001419 break;
1420 }
1421 }
1422
1423 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001424 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001425 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001426 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001427 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001428 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001429 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001430 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001431 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001432 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001433 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001434 }
1435
1436 return 0;
1437}
1438
1439/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1440int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1441{
1442 if (vsub->lu_fsm) {
1443 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1444 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1445 } else if (vsub->proc_arq_fsm) {
1446 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1447 PR_ARQ_E_TMSI_ACK, NULL);
1448 } else {
1449 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001450 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001451 return -EINVAL;
1452 }
1453}
1454
Maxdcc193d2017-12-27 19:34:15 +01001455bool vlr_subscr_expire(struct vlr_subscr *vsub)
1456{
1457 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001458 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001459 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001460 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001461
1462 return true;
1463 }
1464
1465 return false;
1466}
1467
Harald Welte173bdf32022-05-15 12:12:54 +02001468static int vlr_subscr_detach(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001469{
1470 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001471 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001472
1473 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001474 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001475
Harald Welte0df904d2018-12-03 11:00:04 +01001476 /* Inform the UE-SGs FSM that the subscriber has been detached */
1477 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1478
Maxdcc193d2017-12-27 19:34:15 +01001479 vlr_subscr_expire(vsub);
1480
Harald Welteb8b85a12016-06-17 00:06:42 +02001481 return 0;
1482}
1483
Harald Welte173bdf32022-05-15 12:12:54 +02001484/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
1485int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1486{
Harald Welte123261e2022-05-15 11:43:35 +02001487 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_REQ);
Harald Welte173bdf32022-05-15 12:12:54 +02001488 return vlr_subscr_detach(vsub);
1489}
1490
Harald Welteb8b85a12016-06-17 00:06:42 +02001491/* Tear down any running FSMs due to MSC connection timeout.
1492 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1493 * message before the entire connection is torn down.
1494 * \param[in] vsub subscriber to tear down
1495 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001496void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001497{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001498 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001499}
1500
1501struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1502{
1503 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1504 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001505
1506 /* Some of these are needed only on UTRAN, but in case the caller wants
1507 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001508 OSMO_ASSERT(ops->tx_auth_req);
1509 OSMO_ASSERT(ops->tx_auth_rej);
1510 OSMO_ASSERT(ops->tx_id_req);
1511 OSMO_ASSERT(ops->tx_lu_acc);
1512 OSMO_ASSERT(ops->tx_lu_rej);
1513 OSMO_ASSERT(ops->tx_cm_serv_acc);
1514 OSMO_ASSERT(ops->tx_cm_serv_rej);
1515 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001516 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001517 OSMO_ASSERT(ops->subscr_update);
1518 OSMO_ASSERT(ops->subscr_assoc);
1519
1520 INIT_LLIST_HEAD(&vlr->subscribers);
1521 INIT_LLIST_HEAD(&vlr->operations);
1522 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1523
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001524 /* defaults */
1525 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001526 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1527 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001528
Harald Welte123261e2022-05-15 11:43:35 +02001529 vlr->statg = osmo_stat_item_group_alloc(vlr, &vlr_statg_desc, 0);
1530 if (!vlr->statg)
1531 goto err_free;
1532
1533 vlr->ctrg = rate_ctr_group_alloc(vlr, &vlr_ctrg_desc, 0);
1534 if (!vlr->ctrg)
1535 goto err_statg;
1536
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001537 /* reset shared timer definitions */
1538 osmo_tdefs_reset(msc_tdefs_vlr);
1539
Harald Welteb8b85a12016-06-17 00:06:42 +02001540 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001541 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001542 /* osmo_lu_fsm.c */
1543 vlr_lu_fsm_init();
1544 /* vlr_access_request_fsm.c */
1545 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001546 /* vlr_sgs_fsm.c */
1547 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001548
1549 return vlr;
Harald Welte123261e2022-05-15 11:43:35 +02001550
1551err_statg:
1552 osmo_stat_item_group_free(vlr->statg);
1553err_free:
1554 talloc_free(vlr);
1555 return NULL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001556}
1557
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001558int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001559{
1560 OSMO_ASSERT(vlr);
1561
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001562 vlr->gcm = gcm;
1563 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1564 .func = vlr_gsup_rx,
1565 .data = vlr,
1566 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001567
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001568 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1569 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001570 return 0;
1571}
1572
1573/* MSC->VLR: Subscribre has disconnected */
1574int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1575{
1576 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1577 * interface */
1578 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1579 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1580 vsub->msc_conn_ref = NULL;
1581
1582 return 0;
1583}
1584
1585/* MSC->VLR: Receive Authentication Failure from Subscriber */
1586int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1587{
1588 struct vlr_auth_resp_par par = {0};
1589 par.auts = auts;
1590
1591 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1592 return 0;
1593}
1594
1595/* MSC->VLR: Receive Authentication Response from MS
1596 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1597int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1598 bool is_utran, const uint8_t *res, uint8_t res_len)
1599{
1600 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1601 struct vlr_auth_resp_par par;
1602
1603 par.is_r99 = is_r99;
1604 par.is_utran = is_utran;
1605 par.res = res;
1606 par.res_len = res_len;
1607 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1608
1609 return 0;
1610}
1611
1612/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001613void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001614{
1615 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001616 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001617 if (vsub->proc_arq_fsm
1618 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001619 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001620}
1621
1622/* Internal evaluation of requested ciphering mode.
1623 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1624 * \param[in] vlr VLR instance.
1625 * \param[in] fi Calling FSM instance, for logging.
1626 * \param[in] msc_conn_ref MSC conn to send to.
1627 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1628 * \returns 0 if no ciphering is needed or message was sent successfully,
1629 * or a negative value if ciph_mode is invalid or sending failed.
1630 */
1631int vlr_set_ciph_mode(struct vlr_instance *vlr,
1632 struct osmo_fsm_inst *fi,
1633 void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001634 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001635 bool retrieve_imeisv)
1636{
Harald Welte71c51df2017-12-23 18:51:48 +01001637 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1638 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001639}
1640
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001641/* Decide whether UMTS AKA should be used.
1642 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1643 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1644 * GERAN are R99 capable and UMTS AKA tokens are available.
1645 * \param[in] vec Auth tokens (received from the HLR).
1646 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1647 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1648 */
1649bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1650{
1651 if (!is_r99)
1652 return false;
1653 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1654 return false;
1655 return true;
1656}
1657
Harald Welteb8b85a12016-06-17 00:06:42 +02001658void log_set_filter_vlr_subscr(struct log_target *target,
1659 struct vlr_subscr *vlr_subscr)
1660{
1661 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001662 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001663
1664 /* free the old data */
1665 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001666 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001667 *fsub = NULL;
1668 }
1669
1670 if (vlr_subscr) {
1671 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001672 vlr_subscr_get(vlr_subscr, use);
1673 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001674 } else
1675 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1676}