blob: cb5794f305c50fd87c04d8c6e6fcd12d0f2209bb [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) code base */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020025#include <osmocom/core/timer.h>
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070026#include <osmocom/core/tdef.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +020028#include <osmocom/gsm/gsm23236.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020029#include <osmocom/gsm/gsup.h>
30#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020031#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010032#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020034#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010035#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020036#include <osmocom/msc/vlr.h>
37#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038#include <osmocom/msc/gsup_client_mux.h>
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>
43#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020044#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020045
46#include "vlr_core.h"
47#include "vlr_auth_fsm.h"
48#include "vlr_lu_fsm.h"
49#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010050#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020051
52#define SGSN_SUBSCR_MAX_RETRIES 3
53#define SGSN_SUBSCR_RETRY_INTERVAL 10
54
Harald Welte123261e2022-05-15 11:43:35 +020055enum vlr_stat_item_idx {
56 VLR_STAT_SUBSCRIBER_COUNT,
57 VLR_STAT_PDP_COUNT,
58};
59
60static const struct osmo_stat_item_desc vlr_stat_item_desc[] = {
61 [VLR_STAT_SUBSCRIBER_COUNT] = { "subscribers",
62 "Number of subscribers present in VLR" },
63 [VLR_STAT_PDP_COUNT] = { "pdp",
64 "Number of PDP records present in VLR" },
65};
66
67static const struct osmo_stat_item_group_desc vlr_statg_desc = {
68 "vlr",
69 "visitor location register",
70 OSMO_STATS_CLASS_GLOBAL,
71 ARRAY_SIZE(vlr_stat_item_desc),
72 vlr_stat_item_desc,
73};
74
75enum vlr_rate_ctr_idx {
76 VLR_CTR_GSUP_RX_UNKNOWN_IMSI,
77 VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR,
78 VLR_CTR_GSUP_RX_TUPLES,
79 VLR_CTR_GSUP_RX_UL_RES,
80 VLR_CTR_GSUP_RX_UL_ERR,
81 VLR_CTR_GSUP_RX_SAI_RES,
82 VLR_CTR_GSUP_RX_SAI_ERR,
83 VLR_CTR_GSUP_RX_ISD_REQ,
84 VLR_CTR_GSUP_RX_CANCEL_REQ,
85 VLR_CTR_GSUP_RX_CHECK_IMEI_RES,
86 VLR_CTR_GSUP_RX_CHECK_IMEI_ERR,
87 VLR_CTR_GSUP_RX_PURGE_MS_RES,
88 VLR_CTR_GSUP_RX_PURGE_MS_ERR,
89 VLR_CTR_GSUP_RX_DELETE_DATA_REQ,
90 VLR_CTR_GSUP_RX_UNKNOWN,
91
92 VLR_CTR_GSUP_TX_UL_REQ,
93 VLR_CTR_GSUP_TX_ISD_RES,
94 VLR_CTR_GSUP_TX_SAI_REQ,
95 VLR_CTR_GSUP_TX_PURGE_MS_REQ,
96 VLR_CTR_GSUP_TX_CHECK_IMEI_REQ,
97 VLR_CTR_GSUP_TX_AUTH_FAIL_REP,
98 VLR_CTR_GSUP_TX_CANCEL_RES,
99
100 VLR_CTR_DETACH_BY_REQ,
101 VLR_CTR_DETACH_BY_CANCEL,
102 VLR_CTR_DETACH_BY_T3212,
103};
104
105static const struct rate_ctr_desc vlr_ctr_desc[] = {
106 [VLR_CTR_GSUP_RX_UNKNOWN_IMSI] = { "gsup:rx:unknown_imsi",
107 "Received GSUP messages for unknown IMSI" },
108 [VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR] = { "gsup:rx:purge_no_subscr",
109 "Received GSUP purge for unknown subscriber" },
110 [VLR_CTR_GSUP_RX_TUPLES] = { "gsup:rx:auth_tuples",
111 "Received GSUP authentication tuples" },
112 [VLR_CTR_GSUP_RX_UL_RES] = { "gsup:rx:upd_loc:res",
113 "Received GSUP Update Location Result messages" },
114 [VLR_CTR_GSUP_RX_UL_ERR] = { "gsup:rx:upd_loc:err",
115 "Received GSUP Update Location Error messages" },
116 [VLR_CTR_GSUP_RX_SAI_RES] = { "gsup:rx:send_auth_info:res",
117 "Received GSUP Send Auth Info Result messages" },
118 [VLR_CTR_GSUP_RX_SAI_ERR] = { "gsup:rx:send_auth_info:err",
119 "Received GSUP Send Auth Info Error messages" },
120 [VLR_CTR_GSUP_RX_ISD_REQ] = { "gsup:rx:ins_sub_data:req",
121 "Received GSUP Insert Subscriber Data Request messages" },
122 [VLR_CTR_GSUP_RX_CANCEL_REQ] = { "gsup:rx:cancel:req",
123 "Received GSUP Cancel Subscriber messages" },
124 [VLR_CTR_GSUP_RX_CHECK_IMEI_RES] = { "gsup:rx:check_imei:res",
125 "Received GSUP Check IMEI Result messages" },
126 [VLR_CTR_GSUP_RX_CHECK_IMEI_ERR] = { "gsup:rx:check_imei:err",
127 "Received GSUP Check IMEI Error messages" },
128 [VLR_CTR_GSUP_RX_PURGE_MS_RES] = { "gsup:rx:purge_ms:res",
129 "Received GSUP Purge MS Result messages" },
130 [VLR_CTR_GSUP_RX_PURGE_MS_ERR] = { "gsup:rx:purge_ms:err",
131 "Received GSUP Purge MS Error messages" },
132 [VLR_CTR_GSUP_RX_DELETE_DATA_REQ] = { "gsup:rx:del_sub_data:req",
133 "Received GSUP Delete Subscriber Data Request messages" },
134 [VLR_CTR_GSUP_RX_UNKNOWN] = { "gsup:rx:unknown_msgtype",
135 "Received GSUP message of unknown type" },
136
137 [VLR_CTR_GSUP_TX_UL_REQ] = { "gsup:tx:upd_loc:req",
138 "Transmitted GSUP Update Location Request messages" },
139 [VLR_CTR_GSUP_TX_ISD_RES] = { "gsup:tx:ins_sub_data:res",
140 "Transmitted GSUP Insert Subscriber Data Result messages" },
141 [VLR_CTR_GSUP_TX_SAI_REQ] = { "gsup:tx:send_auth_info:res",
142 "Transmitted GSUP Send Auth Info Request messages" },
143 [VLR_CTR_GSUP_TX_PURGE_MS_REQ] = { "gsup:tx:purge_ms:req",
144 "Transmitted GSUP Purge MS Request messages" },
145 [VLR_CTR_GSUP_TX_CHECK_IMEI_REQ] = { "gsup:tx:check_imei:req",
146 "Transmitted GSUP Check IMEI Request messages" },
147 [VLR_CTR_GSUP_TX_AUTH_FAIL_REP] = { "gsup:tx:auth_fail:rep",
148 "Transmitted GSUP Auth Fail Report messages" },
149 [VLR_CTR_GSUP_TX_CANCEL_RES] = { "gsup:tx:cancel:res",
150 "Transmitted GSUP Cancel Result messages" },
151
152 [VLR_CTR_DETACH_BY_REQ] = { "detach:imsi_det_req",
153 "VLR Subscriber Detach by IMSI DETACH REQ" },
154 [VLR_CTR_DETACH_BY_CANCEL] = { "detach:gsup_cancel_req",
155 "VLR Subscriber Detach by GSUP CANCEL REQ" },
156 [VLR_CTR_DETACH_BY_T3212] = { "detach:t3212_timeout",
157 "VLR Subscriber Detach by T3212 timeout" },
158};
159
160static const struct rate_ctr_group_desc vlr_ctrg_desc = {
161 "vlr",
162 "visitor location register",
163 OSMO_STATS_CLASS_GLOBAL,
164 ARRAY_SIZE(vlr_ctr_desc),
165 vlr_ctr_desc,
166};
167
168
169#define vlr_rate_ctr_inc(vlr, idx) \
170 rate_ctr_inc(rate_ctr_group_get_ctr((vlr)->ctrg, idx))
171#define vlr_rate_ctr_add(vlr, idx, val) \
172 rate_ctr_add(rate_ctr_group_get_ctr((vlr)->ctrg, idx), val)
173
174#define vlr_stat_item_inc(vlr, idx) \
175 osmo_stat_item_inc(osmo_stat_item_group_get_item((vlr)->statg, idx), 1)
176#define vlr_stat_item_dec(vlr, idx) \
177 osmo_stat_item_dec(osmo_stat_item_group_get_item((vlr)->statg, idx), 1)
178#define vlr_stat_item_set(vlr, idx, val) \
179 osmo_stat_item_set(osmo_stat_item_group_get_item((vlr)->statg, idx), val)
180
181
Harald Welteb8b85a12016-06-17 00:06:42 +0200182/***********************************************************************
183 * Convenience functions
184 ***********************************************************************/
185
Harald Welte173bdf32022-05-15 12:12:54 +0200186static int vlr_subscr_detach(struct vlr_subscr *vsub);
187
Harald Welteb8b85a12016-06-17 00:06:42 +0200188const struct value_string vlr_ciph_names[] = {
189 OSMO_VALUE_STRING(VLR_CIPH_NONE),
190 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
191 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
192 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
193 { 0, NULL }
194};
195
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700196/* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */
197struct osmo_tdef msc_tdefs_vlr[] = {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700198 { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" },
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700199 { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" },
200 { .T = 3260, .default_val = 12, .desc = "Authentication procedure" },
201 { .T = 3270, .default_val = 12, .desc = "Identification procedure" },
202 { /* terminator */ }
203};
204
205/* This is just a wrapper around the osmo_tdef API.
206 * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */
Harald Welteb8b85a12016-06-17 00:06:42 +0200207uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
208{
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700209 /* NOTE: since we usually do not need more than one instance of the VLR,
210 * and since libosmocore's osmo_tdef API does not (yet) support dynamic
211 * configuration, we always use the global instance of msc_tdefs_vlr. */
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +0700212 return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200213}
214
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100215/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +0100216const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100217{
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100218 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100219 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100220 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100221 if (!vsub)
222 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100223 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100224 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100225 present = true;
226 }
227 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100228 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100229 present = true;
230 }
231 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100232 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100233 present = true;
234 }
235 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100236 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100237 present = true;
238 }
239 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100240 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100241
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100242 return buf;
243}
244
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100245const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
246{
247 /* cast away the const so we can shorten the string within the static buffer */
248 char *name = (char*)vlr_subscr_name(vsub);
249 size_t len = strlen(name);
250 if (maxlen < 2)
251 return "-";
252 if (len > maxlen)
253 strcpy(name + maxlen - 2, "..");
254 return name;
255}
256
Oliver Smith5598aae2019-01-08 11:47:21 +0100257const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100258{
259 if (!vsub || !vsub->msisdn[0])
260 return vlr_subscr_name(vsub);
261 return vsub->msisdn;
262}
263
Harald Welteb8b85a12016-06-17 00:06:42 +0200264struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
265 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100266 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200267 const char *file, int line)
268{
269 struct vlr_subscr *vsub;
270
271 if (!imsi || !*imsi)
272 return NULL;
273
274 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100275 if (vlr_subscr_matches_imsi(vsub, imsi)) {
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100276 if (use)
277 vlr_subscr_get_src(vsub, use, file, line);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100278 return vsub;
279 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200280 }
281 return NULL;
282}
283
284struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
285 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100286 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200287 const char *file, int line)
288{
289 struct vlr_subscr *vsub;
290
291 if (tmsi == GSM_RESERVED_TMSI)
292 return NULL;
293
294 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100295 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
296 vlr_subscr_get_src(vsub, use, file, line);
297 return vsub;
298 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200299 }
300 return NULL;
301}
302
303struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
304 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100305 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200306 const char *file, int line)
307{
308 struct vlr_subscr *vsub;
309
310 if (!msisdn || !*msisdn)
311 return NULL;
312
313 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100314 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
315 vlr_subscr_get_src(vsub, use, file, line);
316 return vsub;
317 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200318 }
319 return NULL;
320}
321
Neels Hofmeyr5bdba0d2021-07-27 03:46:18 +0200322struct vlr_subscr *_vlr_subscr_find_by_mi(struct vlr_instance *vlr,
323 const struct osmo_mobile_identity *mi,
324 const char *use,
325 const char *file, int line)
326{
327 switch (mi->type) {
328 case GSM_MI_TYPE_IMSI:
329 return _vlr_subscr_find_by_imsi(vlr, mi->imsi, use, file, line);
330 case GSM_MI_TYPE_TMSI:
331 return _vlr_subscr_find_by_tmsi(vlr, mi->tmsi, use, file, line);
332 default:
333 return NULL;
334 }
335}
336
Harald Welteb8b85a12016-06-17 00:06:42 +0200337/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100338static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200339 struct osmo_gsup_message *gsup_msg)
340{
341 struct vlr_instance *vlr = vsub->vlr;
342
343 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100344 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200345
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100346 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200347
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100348 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200349}
350
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100351static 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 +0200352{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100353 struct vlr_subscr *vsub = e->use_count->talloc_object;
354 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100355 int32_t total;
356 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200357
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100358 if (!e->use)
359 return -EINVAL;
360
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100361 total = osmo_use_count_total(&vsub->use_count);
362
363 if (total == 0
364 || (total == 1 && old_use_count == 0 && e->count == 1))
365 level = LOGL_INFO;
366 else
367 level = LOGL_DEBUG;
368
369 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100370 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
371 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
372
373 if (e->count < 0)
374 return -ERANGE;
375
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
377
378 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100379 vlr_subscr_free(vsub);
380 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200381}
382
383/* Allocate a new subscriber and insert it into list */
384static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
385{
386 struct vlr_subscr *vsub;
387 int i;
388
389 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100390 *vsub = (struct vlr_subscr){
391 .vlr = vlr,
392 .tmsi = GSM_RESERVED_TMSI,
393 .tmsi_new = GSM_RESERVED_TMSI,
394 .use_count = (struct osmo_use_count){
395 .talloc_object = vsub,
396 .use_cb = vlr_subscr_use_cb,
397 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100398 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100399 };
400 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 +0200401
402 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100403 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200404
405 INIT_LLIST_HEAD(&vsub->cs.requests);
406 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
407
Harald Welte0df904d2018-12-03 11:00:04 +0100408 /* Create an SGs FSM, which is needed to control CSFB,
409 * in cases where CSFB/SGs is not in use, this FSM will
410 * just do nothing. (see also: sgs_iface.c) */
411 vlr_sgs_fsm_create(vsub);
412
Harald Welteb8b85a12016-06-17 00:06:42 +0200413 llist_add_tail(&vsub->list, &vlr->subscribers);
Harald Welte123261e2022-05-15 11:43:35 +0200414 vlr_stat_item_inc(vlr, VLR_STAT_SUBSCRIBER_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200415 return vsub;
416}
417
Harald Welteb8b85a12016-06-17 00:06:42 +0200418/* Send a GSUP Purge MS request.
419 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
420 * this subscriber, not to the HLR? */
421int vlr_subscr_purge(struct vlr_subscr *vsub)
422{
423 struct osmo_gsup_message gsup_msg = {0};
424
Harald Welte123261e2022-05-15 11:43:35 +0200425 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_PURGE_MS_REQ);
426
Harald Welteb8b85a12016-06-17 00:06:42 +0200427 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
428
429 /* provide HLR number in case we know it */
430 gsup_msg.hlr_enc_len = vsub->hlr.len;
431 gsup_msg.hlr_enc = vsub->hlr.buf;
432
433 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
434}
435
Neels Hofmeyr15809592018-04-06 02:57:51 +0200436void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
437 enum osmo_fsm_term_cause fsm_cause,
438 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200439{
440 if (!vsub)
441 return;
442
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100443 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200444 if (vsub->lu_fsm)
445 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200446 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200447 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100448 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200449}
450
451/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
452void vlr_subscr_free(struct vlr_subscr *vsub)
453{
454 llist_del(&vsub->list);
Harald Welte123261e2022-05-15 11:43:35 +0200455 vlr_stat_item_dec(vsub->vlr, VLR_STAT_SUBSCRIBER_COUNT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100456 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
457 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100458
459 /* Make sure SGs timer Ts5 is removed */
460 osmo_timer_del(&vsub->sgs.Ts5);
461
462 /* Remove SGs FSM (see also: sgs_iface.c) */
463 vlr_sgs_fsm_remove(vsub);
464
Harald Welteb8b85a12016-06-17 00:06:42 +0200465 talloc_free(vsub);
466}
467
468/* Generate a new TMSI and store in vsub->tmsi_new.
469 * Search all known subscribers to ensure that the TMSI is unique. */
470int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
471{
472 struct vlr_instance *vlr = vsub->vlr;
473 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100474 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100475 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200476
477 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100478 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
479 if (rc < 0) {
480 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
481 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200482 }
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +0200483
484 if (!llist_empty(&vlr->cfg.nri_ranges->entries)) {
485 int16_t nri_v;
486 osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen);
487 osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen);
488 LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n",
489 osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi);
490 }
491
Harald Welteb8b85a12016-06-17 00:06:42 +0200492 /* throw the dice again, if the TSMI doesn't fit */
493 if (tmsi == GSM_RESERVED_TMSI)
494 continue;
495
496 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
497 if (vlr->cfg.is_ps) {
498 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200499 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200500 } else {
501 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200502 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
503 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200504 }
505
506 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100507 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
508 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200509 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100510 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200511
512 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100513 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200514 return 0;
515 }
516
517 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
518 " after %d tries\n", vlr_subscr_name(vsub), tried);
519 return -1;
520}
521
522/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100523 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200524 * \param[in] imsi IMSI string.
525 * \param[out] created if non-NULL, returns whether a new entry was created. */
526struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
527 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100528 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200529 bool *created,
530 const char *file,
531 int line)
532{
533 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100534 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200535 if (vsub) {
536 if (created)
537 *created = false;
538 return vsub;
539 }
540
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100541 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200542 if (!vsub)
543 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100544 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200545 vlr_subscr_set_imsi(vsub, imsi);
546 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
547 if (created)
548 *created = true;
549 return vsub;
550}
551
552/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100553 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200554 * \param[in] tmsi TMSI.
555 * \param[out] created if non-NULL, returns whether a new entry was created. */
556struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
557 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100558 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200559 bool *created,
560 const char *file,
561 int line)
562{
563 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100564 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200565 if (vsub) {
566 if (created)
567 *created = false;
568 return vsub;
569 }
570
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100571 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200572 if (!vsub)
573 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100574 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200575 vsub->tmsi = tmsi;
576 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
577 if (created)
578 *created = true;
579 return vsub;
580}
581
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100582static void dedup_vsub(struct vlr_subscr *exists, struct vlr_subscr *vsub)
583{
584 struct vlr_instance *vlr = exists->vlr;
585 int i;
586 int j;
587 LOGP(DVLR, LOGL_NOTICE,
588 "There is an existing subscriber for IMSI %s used by %s, replacing with new VLR subscr: %s used by %s\n",
589 exists->imsi, osmo_use_count_to_str_c(OTC_SELECT, &exists->use_count),
590 vlr_subscr_name(vsub),
591 osmo_use_count_to_str_c(OTC_SELECT, &vsub->use_count));
592
593 /* Take over some state from the previous vsub */
594 paging_request_join_vsub(vsub, exists);
595 if (!vsub->msisdn[0])
596 OSMO_STRLCPY_ARRAY(vsub->msisdn, exists->msisdn);
597 if (!vsub->name[0])
598 OSMO_STRLCPY_ARRAY(vsub->name, exists->name);
599 /* Copy valid auth tuples we may already have, to reduce the need to ask for new ones from the HLR */
600 for (i = 0; i < ARRAY_SIZE(exists->auth_tuples); i++) {
601 if (exists->auth_tuples[i].key_seq == VLR_KEY_SEQ_INVAL)
602 continue;
603 for (j = 0; j < ARRAY_SIZE(vsub->auth_tuples); j++) {
604 if (vsub->auth_tuples[j].key_seq != VLR_KEY_SEQ_INVAL)
605 continue;
606 vsub->auth_tuples[j] = exists->auth_tuples[i];
607 }
608 }
609
610 if (exists->msc_conn_ref)
611 LOGVSUBP(LOGL_ERROR, vsub,
612 "There is an existing VLR entry for this same subscriber with an active connection."
613 " That should not be possible. Discarding old subscriber entry %s.\n",
614 exists->imsi);
615
616 if (vlr->ops.subscr_inval)
617 vlr->ops.subscr_inval(exists->msc_conn_ref, exists);
618 vlr_subscr_free(exists);
619}
620
Harald Welteb8b85a12016-06-17 00:06:42 +0200621void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
622{
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100623 struct vlr_subscr *exists;
Harald Welteb8b85a12016-06-17 00:06:42 +0200624 if (!vsub)
625 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200626
Neels Hofmeyr2fd69e12024-03-26 00:50:04 +0100627 /* If the same IMSI is already set, nothing changes. */
628 if (!strcmp(vsub->imsi, imsi))
629 return;
630
631 /* We've just learned about this new IMSI, our primary key in the VLR. make sure to invalidate any prior VLR
632 * entries for this IMSI. */
633 exists = vlr_subscr_find_by_imsi(vsub->vlr, imsi, NULL);
634
635 if (exists)
636 dedup_vsub(exists, vsub);
637
638 /* Set the IMSI on the new subscriber, here. */
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200639 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
640 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
641 imsi, vsub->imsi);
642 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
643 }
644
Harald Welteb8b85a12016-06-17 00:06:42 +0200645 vsub->id = atoll(vsub->imsi);
646 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
647 vsub->imsi, vsub->id);
648}
649
650void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
651{
652 if (!vsub)
653 return;
Max98f74672018-02-05 12:57:06 +0100654 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200655 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
656 vsub->imsi, vsub->imei);
657}
658
659void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
660{
661 if (!vsub)
662 return;
Max98f74672018-02-05 12:57:06 +0100663 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200664 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
665 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200666
667 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
668 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200669}
670
671/* Safely copy the given MSISDN string to vsub->msisdn */
672void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
673{
674 if (!vsub)
675 return;
Max98f74672018-02-05 12:57:06 +0100676 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200677 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
678 vsub->imsi, vsub->msisdn);
679}
680
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200681void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
682 const struct osmo_plmn_id *last_eutran_plmn)
683{
684 if (!vsub)
685 return;
686 if (last_eutran_plmn) {
687 vsub->sgs.last_eutran_plmn_present = true;
688 memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
689 } else {
690 vsub->sgs.last_eutran_plmn_present = false;
691 }
692 DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
693 vsub->sgs.last_eutran_plmn_present ?
694 osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
695 "(none)");
696}
697
Harald Welteb8b85a12016-06-17 00:06:42 +0200698bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
699{
700 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
701}
702
703bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
704{
705 return vsub && tmsi != GSM_RESERVED_TMSI
706 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
707}
708
709bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
710{
711 return vsub && msisdn && vsub->msisdn[0]
712 && !strcmp(vsub->msisdn, msisdn);
713}
714
715bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
716{
717 return vsub && imei && vsub->imei[0]
718 && !strcmp(vsub->imei, imei);
719}
720
721/* Send updated subscriber information to HLR */
722int vlr_subscr_changed(struct vlr_subscr *vsub)
723{
724 /* FIXME */
725 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
726 return 0;
727}
728
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200729void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
730{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200731 struct timespec now;
732
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700733 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200734 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700735 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200736 } else {
737 LOGP(DVLR, LOGL_ERROR,
738 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
739 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
740 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
741 }
742}
743
744void vlr_subscr_expire_lu(void *data)
745{
746 struct vlr_instance *vlr = data;
747 struct vlr_subscr *vsub, *vsub_tmp;
748 struct timespec now;
749
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700750 /* Periodic location update might be disabled from the VTY,
751 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700752 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700753 goto done;
754
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200755 if (llist_empty(&vlr->subscribers))
756 goto done;
757
758 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
759 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
760 goto done;
761 }
762
763 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
764 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
765 continue;
766
767 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
Harald Welte123261e2022-05-15 11:43:35 +0200768 vlr_rate_ctr_inc(vlr, VLR_CTR_DETACH_BY_T3212);
Harald Welte173bdf32022-05-15 12:12:54 +0200769 vlr_subscr_detach(vsub);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200770 }
771
772done:
773 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
774}
775
Harald Welteb8b85a12016-06-17 00:06:42 +0200776/***********************************************************************
777 * PDP context data
778 ***********************************************************************/
779
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200780#define GSM_APN_LENGTH 102
781
782/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
783/* see GSM 09.02, B.1, gprsSubscriptionData */
784struct sgsn_subscriber_pdp_data {
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100785 struct llist_head list;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200786
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100787 unsigned int context_id;
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +0100788 enum gsm48_pdp_type_org pdp_type_org;
789 enum gsm48_pdp_type_nr pdp_type_nr;
790 struct osmo_sockaddr pdp_address[2];
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100791 char apn_str[GSM_APN_LENGTH];
792 uint8_t qos_subscribed[20];
793 size_t qos_subscribed_len;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200794};
795
Harald Welteb8b85a12016-06-17 00:06:42 +0200796struct sgsn_subscriber_pdp_data *
797vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
798{
799 struct sgsn_subscriber_pdp_data* pdata;
800
801 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
802
803 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
Harald Welte123261e2022-05-15 11:43:35 +0200804 vlr_stat_item_inc(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200805
806 return pdata;
807}
808
809static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
810{
811 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
812 int count = 0;
813
814 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
815 llist_del(&pdp->list);
Harald Welte123261e2022-05-15 11:43:35 +0200816 vlr_stat_item_dec(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200817 talloc_free(pdp);
818 count += 1;
819 }
820
821 return count;
822}
823
824static struct sgsn_subscriber_pdp_data *
825vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
826{
827 struct sgsn_subscriber_pdp_data *pdp;
828
829 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
830 if (pdp->context_id == context_id)
831 return pdp;
832 }
833
834 return NULL;
835}
836
837/***********************************************************************
838 * Actual Implementation
839 ***********************************************************************/
840
841static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100842 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200843{
844 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200845 LOGP(DVLR, LOGL_NOTICE,
846 "Unknown IMSI %s, discarding GSUP request "
847 "of type 0x%02x\n",
848 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100849 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200850 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
851 LOGP(DVLR, LOGL_NOTICE,
852 "Unknown IMSI %s, discarding GSUP error "
853 "of type 0x%02x, cause '%s' (%d)\n",
854 gsup_msg->imsi, gsup_msg->message_type,
855 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
856 gsup_msg->cause);
857 } else {
858 LOGP(DVLR, LOGL_NOTICE,
859 "Unknown IMSI %s, discarding GSUP response "
860 "of type 0x%02x\n",
861 gsup_msg->imsi, gsup_msg->message_type);
862 }
863
864 return -GMM_CAUSE_IMSI_UNKNOWN;
865}
866
867static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100868 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200869{
870 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
871 LOGGSUPP(LOGL_NOTICE, gsup_msg,
872 "Purge MS has failed with cause '%s' (%d)\n",
873 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
874 gsup_msg->cause);
875 return -gsup_msg->cause;
876 }
877 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
878 return 0;
879}
880
881/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100882int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200883{
884 struct osmo_gsup_message gsup_msg = {0};
885 int rc;
886
Harald Welte123261e2022-05-15 11:43:35 +0200887 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_UL_REQ);
888
Harald Welteb8b85a12016-06-17 00:06:42 +0200889 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200890 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 +0200891 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
892
893 return rc;
894}
895
896/* VLR internal call to request tuples from HLR */
897int vlr_subscr_req_sai(struct vlr_subscr *vsub,
898 const uint8_t *auts, const uint8_t *auts_rand)
899{
900 struct osmo_gsup_message gsup_msg = {0};
901
Harald Welte123261e2022-05-15 11:43:35 +0200902 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_SAI_REQ);
903
Harald Welteb8b85a12016-06-17 00:06:42 +0200904 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
905 gsup_msg.auts = auts;
906 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100907 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200908
909 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
910}
911
Oliver Smith7d053092018-12-14 17:37:38 +0100912/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
913int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
914{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100915 struct osmo_gsup_message gsup_msg = {
916 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200917 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100918 };
Oliver Smith7d053092018-12-14 17:37:38 +0100919 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
920 int len;
921
922 /* Encode IMEI */
923 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
924 if (len < 1) {
925 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
926 return -ENOSPC;
927 }
928 gsup_msg.imei_enc = imei_enc;
929 gsup_msg.imei_enc_len = len;
930
Harald Welte123261e2022-05-15 11:43:35 +0200931 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CHECK_IMEI_REQ);
932
Oliver Smith7d053092018-12-14 17:37:38 +0100933 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100934 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100935 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100936}
937
Harald Welteb8b85a12016-06-17 00:06:42 +0200938/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100939int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200940{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100941 struct osmo_gsup_message gsup_msg = {
942 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200943 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100944 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200945
Harald Welte123261e2022-05-15 11:43:35 +0200946 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_AUTH_FAIL_REP);
947
Max98f74672018-02-05 12:57:06 +0100948 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100949 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200950}
951
952/* Update the subscriber with GSUP-received auth tuples */
953void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
954 const struct osmo_gsup_message *gsup)
955{
956 unsigned int i;
957 unsigned int got_tuples;
958
959 if (gsup->num_auth_vectors) {
960 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
961 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100962 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200963 }
964
965 got_tuples = 0;
966 for (i = 0; i < gsup->num_auth_vectors; i++) {
967 size_t key_seq = i;
968
969 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
970 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100971 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200972 key_seq);
973 continue;
974 }
975 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
976 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100977 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200978 }
979
980 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
Harald Welte123261e2022-05-15 11:43:35 +0200981 vlr_rate_ctr_add(vsub->vlr, VLR_CTR_GSUP_RX_TUPLES, got_tuples);
Harald Welteb8b85a12016-06-17 00:06:42 +0200982
983 if (!got_tuples) {
984 /* FIXME what now? */
985 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
986 }
987
988 /* New tuples means last_tuple becomes invalid */
989 vsub->last_tuple = NULL;
990}
991
992/* Handle SendAuthInfo Result/Error from HLR */
993static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
994 const struct osmo_gsup_message *gsup)
995{
996 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
997 void *data = (void *) gsup;
998
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200999 if (!auth_fi) {
1000 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
1001 osmo_gsup_message_type_name(gsup->message_type));
1002 return -1;
1003 }
1004
Harald Welteb8b85a12016-06-17 00:06:42 +02001005 switch (gsup->message_type) {
1006 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1007 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
1008 break;
1009 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1010 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
1011 break;
1012 default:
1013 return -1;
1014 }
1015
1016 return 0;
1017}
1018
Harald Welteb8b85a12016-06-17 00:06:42 +02001019static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
1020 const struct osmo_gsup_message *gsup_msg)
1021{
1022 unsigned idx;
1023 int rc;
1024
Neels Hofmeyr02dd2652019-11-13 07:05:11 +01001025 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001026 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
1027 gsup_msg->msisdn_enc,
1028 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001029 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
1030 vsub->imsi, vsub->msisdn);
1031 }
1032
1033 if (gsup_msg->hlr_enc) {
1034 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
1035 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
1036 gsup_msg->hlr_enc_len);
1037 vsub->hlr.len = 0;
1038 } else {
1039 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
1040 gsup_msg->hlr_enc_len);
1041 vsub->hlr.len = gsup_msg->hlr_enc_len;
1042 }
1043 }
1044
1045 if (gsup_msg->pdp_info_compl) {
1046 rc = vlr_subscr_pdp_data_clear(vsub);
1047 if (rc > 0)
1048 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
1049 }
1050
1051 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
1052 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
1053 size_t ctx_id = pdp_info->context_id;
1054 struct sgsn_subscriber_pdp_data *pdp_data;
1055
1056 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
1057 LOGVSUBP(LOGL_ERROR, vsub,
1058 "APN too long, context id = %zu, APN = %s\n",
1059 ctx_id, osmo_hexdump(pdp_info->apn_enc,
1060 pdp_info->apn_enc_len));
1061 continue;
1062 }
1063
1064 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
1065 LOGVSUBP(LOGL_ERROR, vsub,
1066 "QoS info too long (%zu)\n",
1067 pdp_info->qos_enc_len);
1068 continue;
1069 }
1070
1071 LOGVSUBP(LOGL_INFO, vsub,
1072 "Will set PDP info, context id = %zu, APN = %s\n",
1073 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
1074
1075 /* Set PDP info [ctx_id] */
1076 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
1077 if (!pdp_data) {
1078 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
1079 pdp_data->context_id = ctx_id;
1080 }
1081
1082 OSMO_ASSERT(pdp_data != NULL);
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +01001083 pdp_data->pdp_type_org = pdp_info->pdp_type_org;
1084 pdp_data->pdp_type_nr = pdp_info->pdp_type_nr;
1085 memcpy(&pdp_data->pdp_address[0], &pdp_info->pdp_address[0], sizeof(pdp_data->pdp_address[0]));
1086 memcpy(&pdp_data->pdp_address[1], &pdp_info->pdp_address[1], sizeof(pdp_data->pdp_address[1]));
Harald Welteb8b85a12016-06-17 00:06:42 +02001087 osmo_apn_to_str(pdp_data->apn_str,
1088 pdp_info->apn_enc, pdp_info->apn_enc_len);
1089 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
1090 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
1091 }
1092}
1093
1094
1095/* Handle InsertSubscrData Result from HLR */
1096static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
1097 const struct osmo_gsup_message *gsup)
1098{
1099 struct osmo_gsup_message gsup_reply = {0};
1100
Harald Welte123261e2022-05-15 11:43:35 +02001101 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_ISD_RES);
1102
Harald Welteb8b85a12016-06-17 00:06:42 +02001103 vlr_subscr_gsup_insert_data(vsub, gsup);
1104 vsub->vlr->ops.subscr_update(vsub);
1105
1106 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
1107 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
1108}
1109
1110/* Handle UpdateLocation Result from HLR */
1111static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
1112 const struct osmo_gsup_message *gsup)
1113{
Philipp Maier483cea82019-04-03 16:23:29 +02001114 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001115 bool sgs_lu_in_progress = false;
1116
1117 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1118 sgs_lu_in_progress = true;
1119
1120 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001121 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
1122 "without LU in progress\n");
1123 return -ENODEV;
1124 }
1125
1126 /* contrary to MAP, we allow piggy-backing subscriber data onto the
1127 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
1128 * nested INSERT SUBSCRIBER DATA transaction */
1129 vlr_subscr_gsup_insert_data(vsub, gsup);
1130
Harald Welte0df904d2018-12-03 11:00:04 +01001131 if (sgs_lu_in_progress) {
1132 sgs_lu_response.accepted = true;
1133 sgs_lu_response.vsub = vsub;
1134 vsub->sgs.response_cb(&sgs_lu_response);
1135 } else
1136 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001137
1138 return 0;
1139}
1140
1141/* Handle UpdateLocation Result from HLR */
1142static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
1143 const struct osmo_gsup_message *gsup)
1144{
Philipp Maier483cea82019-04-03 16:23:29 +02001145 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001146 bool sgs_lu_in_progress = false;
1147
1148 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1149 sgs_lu_in_progress = true;
1150
1151 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001152 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
1153 "without LU in progress\n");
1154 return -ENODEV;
1155 }
1156
1157 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
1158 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1159
Harald Welte0df904d2018-12-03 11:00:04 +01001160 if (sgs_lu_in_progress) {
1161 sgs_lu_response.accepted = false;
1162 sgs_lu_response.vsub = vsub;
1163 vsub->sgs.response_cb(&sgs_lu_response);
1164 } else
1165 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
1166 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001167 return 0;
1168}
1169
Alexander Couzens7312b152019-08-19 15:30:12 +02001170void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
1171 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +02001172{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001173 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1174 switch (gmm_cause) {
1175 case GMM_CAUSE_IMSI_UNKNOWN:
1176 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
1177 break;
1178 case GMM_CAUSE_ILLEGAL_MS:
1179 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
1180 break;
1181 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
1182 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
1183 break;
1184 case GMM_CAUSE_ILLEGAL_ME:
1185 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
1186 break;
1187 case GMM_CAUSE_GPRS_NOTALLOWED:
1188 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
1189 break;
1190 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
1191 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
1192 break;
1193 case GMM_CAUSE_MS_ID_NOT_DERIVED:
1194 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
1195 break;
1196 case GMM_CAUSE_IMPL_DETACHED:
1197 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
1198 break;
1199 case GMM_CAUSE_PLMN_NOTALLOWED:
1200 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
1201 break;
1202 case GMM_CAUSE_LA_NOTALLOWED:
1203 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
1204 break;
1205 case GMM_CAUSE_ROAMING_NOTALLOWED:
1206 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
1207 break;
1208 case GMM_CAUSE_NO_GPRS_PLMN:
1209 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
1210 break;
1211 case GMM_CAUSE_MSC_TEMP_NOTREACH:
1212 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
1213 break;
1214 case GMM_CAUSE_SYNC_FAIL:
1215 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
1216 break;
1217 case GMM_CAUSE_CONGESTION:
1218 gsm48_rej = GSM48_REJECT_CONGESTION;
1219 break;
1220 case GMM_CAUSE_SEM_INCORR_MSG:
1221 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1222 break;
1223 case GMM_CAUSE_INV_MAND_INFO:
1224 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1225 break;
1226 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1227 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1228 break;
1229 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1230 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1231 break;
1232 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1233 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1234 break;
1235 case GMM_CAUSE_COND_IE_ERR:
1236 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1237 break;
1238 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1239 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1240 break;
1241 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1242 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1243 break;
1244
1245 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1246 case GMM_CAUSE_MAC_FAIL:
1247 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1248 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1249 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1250 case GMM_CAUSE_NO_PDP_ACTIVATED:
1251 case GMM_CAUSE_NET_FAIL:
1252 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1253 break;
1254 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001255
1256 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001257}
1258
Harald Welteb8b85a12016-06-17 00:06:42 +02001259/* Handle LOCATION CANCEL request from HLR */
1260static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001261 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001262{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001263 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001264 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001265 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001266 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001267 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1268
Harald Welte123261e2022-05-15 11:43:35 +02001269 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CANCEL_RES);
1270
Harald Welteb8b85a12016-06-17 00:06:42 +02001271 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1272 is_update_procedure ?
1273 "update procedure" : "subscription withdraw");
1274
1275 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001276 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001277
Alexander Couzens7312b152019-08-19 15:30:12 +02001278 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001279 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001280
Harald Welte123261e2022-05-15 11:43:35 +02001281 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_CANCEL);
Harald Welte173bdf32022-05-15 12:12:54 +02001282 vlr_subscr_detach(vsub);
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001283
Max770fbd22018-01-24 12:48:33 +01001284 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001285}
1286
Oliver Smith7d053092018-12-14 17:37:38 +01001287/* Handle Check_IMEI_VLR result and error from HLR */
1288static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1289{
1290 if (!vsub->lu_fsm) {
1291 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1292 osmo_gsup_message_type_name(gsup->message_type));
1293 return -ENODEV;
1294 }
1295
Oliver Smithcbf2c932019-05-06 13:09:55 +02001296 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1297 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001298 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1299 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1300 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1301 else
1302 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1303 } else {
1304 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1305 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1306 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1307 }
1308
1309 return 0;
1310}
1311
Harald Welteb8b85a12016-06-17 00:06:42 +02001312/* Incoming handler for GSUP from HLR.
1313 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001314int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001315{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001316 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001317 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001318 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001319
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001320 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001321 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001322 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001323 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1324 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001325 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001326 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001327 default:
Harald Welte123261e2022-05-15 11:43:35 +02001328 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN_IMSI);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001329 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001330 }
1331 }
1332
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001334 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001335 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_RES);
1336 rc = vlr_subscr_handle_sai_res(vsub, gsup);
1337 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001338 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001339 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001340 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001341 break;
1342 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001343 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_ISD_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001344 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001345 break;
1346 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001347 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CANCEL_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001348 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001349 break;
1350 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001351 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001352 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001353 break;
1354 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001355 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001356 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001357 break;
1358 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001359 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_ERR);
1360 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001361 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001362 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_RES);
1363 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001364 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001365 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_DELETE_DATA_REQ);
1366 goto out_unimpl;
Oliver Smith7d053092018-12-14 17:37:38 +01001367 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001368 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_ERR);
1369 rc = vlr_subscr_handle_check_imei(vsub, gsup);
1370 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001371 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001372 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001373 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001374 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001375 default:
Harald Welte123261e2022-05-15 11:43:35 +02001376 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001377 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1378 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001379 break;
1380 }
1381
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001382 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001383 return rc;
Harald Welte123261e2022-05-15 11:43:35 +02001384
1385out_unimpl:
1386 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP msg_type=%d not yet implemented\n", gsup->message_type);
1387 vlr_subscr_put(vsub, __func__);
1388 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001389}
1390
1391/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001392int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001393{
Harald Welteb8b85a12016-06-17 00:06:42 +02001394 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001395 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001396 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001397 if (vsub->imsi[0]
1398 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001399 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001400 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001401 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001402 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001403 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001404 break;
1405 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001406 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001407 break;
1408 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001409 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001410 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001411 default:
1412 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001413 }
1414
1415 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001416 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001417 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001418 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1419 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001420 break;
1421 }
1422 }
1423
1424 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001425 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001426 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001427 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001428 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001429 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001430 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001431 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001432 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001433 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001434 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001435 }
1436
1437 return 0;
1438}
1439
1440/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1441int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1442{
1443 if (vsub->lu_fsm) {
1444 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1445 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1446 } else if (vsub->proc_arq_fsm) {
1447 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1448 PR_ARQ_E_TMSI_ACK, NULL);
1449 } else {
1450 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001451 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001452 return -EINVAL;
1453 }
1454}
1455
Maxdcc193d2017-12-27 19:34:15 +01001456bool vlr_subscr_expire(struct vlr_subscr *vsub)
1457{
1458 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001459 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001460 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001461 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001462
1463 return true;
1464 }
1465
1466 return false;
1467}
1468
Harald Welte173bdf32022-05-15 12:12:54 +02001469static int vlr_subscr_detach(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001470{
1471 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001472 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001473
1474 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001475 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001476
Harald Welte0df904d2018-12-03 11:00:04 +01001477 /* Inform the UE-SGs FSM that the subscriber has been detached */
1478 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1479
Maxdcc193d2017-12-27 19:34:15 +01001480 vlr_subscr_expire(vsub);
1481
Harald Welteb8b85a12016-06-17 00:06:42 +02001482 return 0;
1483}
1484
Harald Welte173bdf32022-05-15 12:12:54 +02001485/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
1486int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1487{
Harald Welte123261e2022-05-15 11:43:35 +02001488 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_REQ);
Harald Welte173bdf32022-05-15 12:12:54 +02001489 return vlr_subscr_detach(vsub);
1490}
1491
Harald Welteb8b85a12016-06-17 00:06:42 +02001492/* Tear down any running FSMs due to MSC connection timeout.
1493 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1494 * message before the entire connection is torn down.
1495 * \param[in] vsub subscriber to tear down
1496 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001497void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001498{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001499 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001500}
1501
1502struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1503{
1504 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1505 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001506
1507 /* Some of these are needed only on UTRAN, but in case the caller wants
1508 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001509 OSMO_ASSERT(ops->tx_auth_req);
1510 OSMO_ASSERT(ops->tx_auth_rej);
1511 OSMO_ASSERT(ops->tx_id_req);
1512 OSMO_ASSERT(ops->tx_lu_acc);
1513 OSMO_ASSERT(ops->tx_lu_rej);
1514 OSMO_ASSERT(ops->tx_cm_serv_acc);
1515 OSMO_ASSERT(ops->tx_cm_serv_rej);
1516 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001517 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001518 OSMO_ASSERT(ops->subscr_update);
1519 OSMO_ASSERT(ops->subscr_assoc);
1520
1521 INIT_LLIST_HEAD(&vlr->subscribers);
1522 INIT_LLIST_HEAD(&vlr->operations);
1523 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1524
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001525 /* defaults */
1526 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001527 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1528 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001529
Harald Welte123261e2022-05-15 11:43:35 +02001530 vlr->statg = osmo_stat_item_group_alloc(vlr, &vlr_statg_desc, 0);
1531 if (!vlr->statg)
1532 goto err_free;
1533
1534 vlr->ctrg = rate_ctr_group_alloc(vlr, &vlr_ctrg_desc, 0);
1535 if (!vlr->ctrg)
1536 goto err_statg;
1537
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001538 /* reset shared timer definitions */
1539 osmo_tdefs_reset(msc_tdefs_vlr);
1540
Harald Welteb8b85a12016-06-17 00:06:42 +02001541 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001542 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001543 /* osmo_lu_fsm.c */
1544 vlr_lu_fsm_init();
1545 /* vlr_access_request_fsm.c */
1546 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001547 /* vlr_sgs_fsm.c */
1548 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001549
1550 return vlr;
Harald Welte123261e2022-05-15 11:43:35 +02001551
1552err_statg:
1553 osmo_stat_item_group_free(vlr->statg);
1554err_free:
1555 talloc_free(vlr);
1556 return NULL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001557}
1558
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001559int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001560{
1561 OSMO_ASSERT(vlr);
1562
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001563 vlr->gcm = gcm;
1564 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1565 .func = vlr_gsup_rx,
1566 .data = vlr,
1567 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001568
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001569 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1570 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001571 return 0;
1572}
1573
1574/* MSC->VLR: Subscribre has disconnected */
1575int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1576{
1577 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1578 * interface */
1579 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1580 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1581 vsub->msc_conn_ref = NULL;
1582
1583 return 0;
1584}
1585
1586/* MSC->VLR: Receive Authentication Failure from Subscriber */
1587int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1588{
1589 struct vlr_auth_resp_par par = {0};
1590 par.auts = auts;
1591
1592 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1593 return 0;
1594}
1595
1596/* MSC->VLR: Receive Authentication Response from MS
1597 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1598int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1599 bool is_utran, const uint8_t *res, uint8_t res_len)
1600{
1601 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1602 struct vlr_auth_resp_par par;
1603
1604 par.is_r99 = is_r99;
1605 par.is_utran = is_utran;
1606 par.res = res;
1607 par.res_len = res_len;
1608 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1609
1610 return 0;
1611}
1612
1613/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001614void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001615{
1616 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001617 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001618 if (vsub->proc_arq_fsm
1619 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001620 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001621}
1622
1623/* Internal evaluation of requested ciphering mode.
1624 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1625 * \param[in] vlr VLR instance.
1626 * \param[in] fi Calling FSM instance, for logging.
1627 * \param[in] msc_conn_ref MSC conn to send to.
1628 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1629 * \returns 0 if no ciphering is needed or message was sent successfully,
1630 * or a negative value if ciph_mode is invalid or sending failed.
1631 */
1632int vlr_set_ciph_mode(struct vlr_instance *vlr,
1633 struct osmo_fsm_inst *fi,
1634 void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001635 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001636 bool retrieve_imeisv)
1637{
Harald Welte71c51df2017-12-23 18:51:48 +01001638 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1639 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001640}
1641
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001642/* Decide whether UMTS AKA should be used.
1643 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1644 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1645 * GERAN are R99 capable and UMTS AKA tokens are available.
1646 * \param[in] vec Auth tokens (received from the HLR).
1647 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1648 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1649 */
1650bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1651{
1652 if (!is_r99)
1653 return false;
1654 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1655 return false;
1656 return true;
1657}
1658
Harald Welteb8b85a12016-06-17 00:06:42 +02001659void log_set_filter_vlr_subscr(struct log_target *target,
1660 struct vlr_subscr *vlr_subscr)
1661{
1662 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001663 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001664
1665 /* free the old data */
1666 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001667 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001668 *fsub = NULL;
1669 }
1670
1671 if (vlr_subscr) {
1672 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001673 vlr_subscr_get(vlr_subscr, use);
1674 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001675 } else
1676 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1677}