blob: 37d7ffdaf5df014423f4e88f471e162aa0970ae9 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) code base */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020025#include <osmocom/core/timer.h>
Vadim Yanitskiybaf71a72020-01-25 10:49:14 +070026#include <osmocom/core/tdef.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +020028#include <osmocom/gsm/gsm23236.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020029#include <osmocom/gsm/gsup.h>
30#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020031#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010032#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020034#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010035#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020036#include <osmocom/msc/vlr.h>
37#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038#include <osmocom/msc/gsup_client_mux.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020039
Harald Welteb8b85a12016-06-17 00:06:42 +020040#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020043#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020044
45#include "vlr_core.h"
46#include "vlr_auth_fsm.h"
47#include "vlr_lu_fsm.h"
48#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010049#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020050
51#define SGSN_SUBSCR_MAX_RETRIES 3
52#define SGSN_SUBSCR_RETRY_INTERVAL 10
53
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)) {
275 vlr_subscr_get_src(vsub, use, file, line);
276 return vsub;
277 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200278 }
279 return NULL;
280}
281
282struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
283 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100284 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200285 const char *file, int line)
286{
287 struct vlr_subscr *vsub;
288
289 if (tmsi == GSM_RESERVED_TMSI)
290 return NULL;
291
292 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100293 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
294 vlr_subscr_get_src(vsub, use, file, line);
295 return vsub;
296 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200297 }
298 return NULL;
299}
300
301struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
302 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100303 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200304 const char *file, int line)
305{
306 struct vlr_subscr *vsub;
307
308 if (!msisdn || !*msisdn)
309 return NULL;
310
311 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100312 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
313 vlr_subscr_get_src(vsub, use, file, line);
314 return vsub;
315 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200316 }
317 return NULL;
318}
319
Neels Hofmeyr5bdba0d2021-07-27 03:46:18 +0200320struct vlr_subscr *_vlr_subscr_find_by_mi(struct vlr_instance *vlr,
321 const struct osmo_mobile_identity *mi,
322 const char *use,
323 const char *file, int line)
324{
325 switch (mi->type) {
326 case GSM_MI_TYPE_IMSI:
327 return _vlr_subscr_find_by_imsi(vlr, mi->imsi, use, file, line);
328 case GSM_MI_TYPE_TMSI:
329 return _vlr_subscr_find_by_tmsi(vlr, mi->tmsi, use, file, line);
330 default:
331 return NULL;
332 }
333}
334
Harald Welteb8b85a12016-06-17 00:06:42 +0200335/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100336static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200337 struct osmo_gsup_message *gsup_msg)
338{
339 struct vlr_instance *vlr = vsub->vlr;
340
341 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100342 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200343
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100344 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200345
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100346 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200347}
348
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100349static 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 +0200350{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100351 struct vlr_subscr *vsub = e->use_count->talloc_object;
352 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100353 int32_t total;
354 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200355
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100356 if (!e->use)
357 return -EINVAL;
358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 total = osmo_use_count_total(&vsub->use_count);
360
361 if (total == 0
362 || (total == 1 && old_use_count == 0 && e->count == 1))
363 level = LOGL_INFO;
364 else
365 level = LOGL_DEBUG;
366
367 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100368 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
369 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
370
371 if (e->count < 0)
372 return -ERANGE;
373
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100374 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
375
376 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100377 vlr_subscr_free(vsub);
378 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200379}
380
381/* Allocate a new subscriber and insert it into list */
382static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
383{
384 struct vlr_subscr *vsub;
385 int i;
386
387 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100388 *vsub = (struct vlr_subscr){
389 .vlr = vlr,
390 .tmsi = GSM_RESERVED_TMSI,
391 .tmsi_new = GSM_RESERVED_TMSI,
392 .use_count = (struct osmo_use_count){
393 .talloc_object = vsub,
394 .use_cb = vlr_subscr_use_cb,
395 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100396 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100397 };
398 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 +0200399
400 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100401 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200402
403 INIT_LLIST_HEAD(&vsub->cs.requests);
404 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
405
Harald Welte0df904d2018-12-03 11:00:04 +0100406 /* Create an SGs FSM, which is needed to control CSFB,
407 * in cases where CSFB/SGs is not in use, this FSM will
408 * just do nothing. (see also: sgs_iface.c) */
409 vlr_sgs_fsm_create(vsub);
410
Harald Welteb8b85a12016-06-17 00:06:42 +0200411 llist_add_tail(&vsub->list, &vlr->subscribers);
Harald Welte123261e2022-05-15 11:43:35 +0200412 vlr_stat_item_inc(vlr, VLR_STAT_SUBSCRIBER_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200413 return vsub;
414}
415
Harald Welteb8b85a12016-06-17 00:06:42 +0200416/* Send a GSUP Purge MS request.
417 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
418 * this subscriber, not to the HLR? */
419int vlr_subscr_purge(struct vlr_subscr *vsub)
420{
421 struct osmo_gsup_message gsup_msg = {0};
422
Harald Welte123261e2022-05-15 11:43:35 +0200423 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_PURGE_MS_REQ);
424
Harald Welteb8b85a12016-06-17 00:06:42 +0200425 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
426
427 /* provide HLR number in case we know it */
428 gsup_msg.hlr_enc_len = vsub->hlr.len;
429 gsup_msg.hlr_enc = vsub->hlr.buf;
430
431 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
432}
433
Neels Hofmeyr15809592018-04-06 02:57:51 +0200434void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
435 enum osmo_fsm_term_cause fsm_cause,
436 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200437{
438 if (!vsub)
439 return;
440
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100441 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200442 if (vsub->lu_fsm)
443 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200444 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200445 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100446 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200447}
448
449/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
450void vlr_subscr_free(struct vlr_subscr *vsub)
451{
452 llist_del(&vsub->list);
Harald Welte123261e2022-05-15 11:43:35 +0200453 vlr_stat_item_dec(vsub->vlr, VLR_STAT_SUBSCRIBER_COUNT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100454 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
455 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100456
457 /* Make sure SGs timer Ts5 is removed */
458 osmo_timer_del(&vsub->sgs.Ts5);
459
460 /* Remove SGs FSM (see also: sgs_iface.c) */
461 vlr_sgs_fsm_remove(vsub);
462
Harald Welteb8b85a12016-06-17 00:06:42 +0200463 talloc_free(vsub);
464}
465
466/* Generate a new TMSI and store in vsub->tmsi_new.
467 * Search all known subscribers to ensure that the TMSI is unique. */
468int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
469{
470 struct vlr_instance *vlr = vsub->vlr;
471 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100472 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100473 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200474
475 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100476 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
477 if (rc < 0) {
478 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
479 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200480 }
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +0200481
482 if (!llist_empty(&vlr->cfg.nri_ranges->entries)) {
483 int16_t nri_v;
484 osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen);
485 osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen);
486 LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n",
487 osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi);
488 }
489
Harald Welteb8b85a12016-06-17 00:06:42 +0200490 /* throw the dice again, if the TSMI doesn't fit */
491 if (tmsi == GSM_RESERVED_TMSI)
492 continue;
493
494 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
495 if (vlr->cfg.is_ps) {
496 /* SGSN */
Eric Wild58abc672019-06-14 16:11:08 +0200497 tmsi |= GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200498 } else {
499 /* MSC */
Eric Wild58abc672019-06-14 16:11:08 +0200500 if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK)
501 tmsi &= ~GSM23003_TMSI_SGSN_MASK;
Harald Welteb8b85a12016-06-17 00:06:42 +0200502 }
503
504 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100505 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
506 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200507 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100508 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200509
510 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100511 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200512 return 0;
513 }
514
515 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
516 " after %d tries\n", vlr_subscr_name(vsub), tried);
517 return -1;
518}
519
520/* Find subscriber by IMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100521 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200522 * \param[in] imsi IMSI string.
523 * \param[out] created if non-NULL, returns whether a new entry was created. */
524struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
525 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100526 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200527 bool *created,
528 const char *file,
529 int line)
530{
531 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100532 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200533 if (vsub) {
534 if (created)
535 *created = false;
536 return vsub;
537 }
538
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100539 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200540 if (!vsub)
541 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100542 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200543 vlr_subscr_set_imsi(vsub, imsi);
544 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
545 if (created)
546 *created = true;
547 return vsub;
548}
549
550/* Find subscriber by TMSI, or create new subscriber if not found.
Martin Hauke3f07dac2019-11-14 17:49:08 +0100551 * \param[in] vlr VLR instance.
Harald Welteb8b85a12016-06-17 00:06:42 +0200552 * \param[in] tmsi TMSI.
553 * \param[out] created if non-NULL, returns whether a new entry was created. */
554struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
555 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100556 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200557 bool *created,
558 const char *file,
559 int line)
560{
561 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100562 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200563 if (vsub) {
564 if (created)
565 *created = false;
566 return vsub;
567 }
568
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100569 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200570 if (!vsub)
571 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100572 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200573 vsub->tmsi = tmsi;
574 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
575 if (created)
576 *created = true;
577 return vsub;
578}
579
580void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
581{
582 if (!vsub)
583 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200584
585 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
586 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
587 imsi, vsub->imsi);
588 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
589 }
590
Harald Welteb8b85a12016-06-17 00:06:42 +0200591 vsub->id = atoll(vsub->imsi);
592 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
593 vsub->imsi, vsub->id);
594}
595
596void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
597{
598 if (!vsub)
599 return;
Max98f74672018-02-05 12:57:06 +0100600 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200601 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
602 vsub->imsi, vsub->imei);
603}
604
605void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
606{
607 if (!vsub)
608 return;
Max98f74672018-02-05 12:57:06 +0100609 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200610 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
611 vsub->imsi, vsub->imeisv);
Oliver Smithb8077b02019-05-07 14:13:55 +0200612
613 /* Copy IMEISV to IMEI (additional SV digits get cut off) */
614 vlr_subscr_set_imei(vsub, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200615}
616
617/* Safely copy the given MSISDN string to vsub->msisdn */
618void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
619{
620 if (!vsub)
621 return;
Max98f74672018-02-05 12:57:06 +0100622 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200623 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
624 vsub->imsi, vsub->msisdn);
625}
626
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200627void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
628 const struct osmo_plmn_id *last_eutran_plmn)
629{
630 if (!vsub)
631 return;
632 if (last_eutran_plmn) {
633 vsub->sgs.last_eutran_plmn_present = true;
634 memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
635 } else {
636 vsub->sgs.last_eutran_plmn_present = false;
637 }
638 DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
639 vsub->sgs.last_eutran_plmn_present ?
640 osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
641 "(none)");
642}
643
Harald Welteb8b85a12016-06-17 00:06:42 +0200644bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
645{
646 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
647}
648
649bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
650{
651 return vsub && tmsi != GSM_RESERVED_TMSI
652 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
653}
654
655bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
656{
657 return vsub && msisdn && vsub->msisdn[0]
658 && !strcmp(vsub->msisdn, msisdn);
659}
660
661bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
662{
663 return vsub && imei && vsub->imei[0]
664 && !strcmp(vsub->imei, imei);
665}
666
667/* Send updated subscriber information to HLR */
668int vlr_subscr_changed(struct vlr_subscr *vsub)
669{
670 /* FIXME */
671 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
672 return 0;
673}
674
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200675void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
676{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200677 struct timespec now;
678
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700679 /* Mark the subscriber as inactive if it stopped to do periodical location updates. */
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200680 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700681 vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200682 } else {
683 LOGP(DVLR, LOGL_ERROR,
684 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
685 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
686 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
687 }
688}
689
690void vlr_subscr_expire_lu(void *data)
691{
692 struct vlr_instance *vlr = data;
693 struct vlr_subscr *vsub, *vsub_tmp;
694 struct timespec now;
695
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700696 /* Periodic location update might be disabled from the VTY,
697 * so we shall not expire subscribers until explicit IMSI Detach. */
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +0700698 if (!vlr_timer(vlr, 3212))
Vadim Yanitskiy718f32f2019-06-19 03:13:40 +0700699 goto done;
700
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200701 if (llist_empty(&vlr->subscribers))
702 goto done;
703
704 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
705 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
706 goto done;
707 }
708
709 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
710 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
711 continue;
712
713 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
Harald Welte123261e2022-05-15 11:43:35 +0200714 vlr_rate_ctr_inc(vlr, VLR_CTR_DETACH_BY_T3212);
Harald Welte173bdf32022-05-15 12:12:54 +0200715 vlr_subscr_detach(vsub);
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200716 }
717
718done:
719 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
720}
721
Harald Welteb8b85a12016-06-17 00:06:42 +0200722/***********************************************************************
723 * PDP context data
724 ***********************************************************************/
725
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200726#define GSM_APN_LENGTH 102
727
728/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
729/* see GSM 09.02, B.1, gprsSubscriptionData */
730struct sgsn_subscriber_pdp_data {
731 struct llist_head list;
732
733 unsigned int context_id;
734 uint16_t pdp_type;
735 char apn_str[GSM_APN_LENGTH];
736 uint8_t qos_subscribed[20];
737 size_t qos_subscribed_len;
738};
739
Harald Welteb8b85a12016-06-17 00:06:42 +0200740struct sgsn_subscriber_pdp_data *
741vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
742{
743 struct sgsn_subscriber_pdp_data* pdata;
744
745 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
746
747 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
Harald Welte123261e2022-05-15 11:43:35 +0200748 vlr_stat_item_inc(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200749
750 return pdata;
751}
752
753static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
754{
755 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
756 int count = 0;
757
758 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
759 llist_del(&pdp->list);
Harald Welte123261e2022-05-15 11:43:35 +0200760 vlr_stat_item_dec(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200761 talloc_free(pdp);
762 count += 1;
763 }
764
765 return count;
766}
767
768static struct sgsn_subscriber_pdp_data *
769vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
770{
771 struct sgsn_subscriber_pdp_data *pdp;
772
773 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
774 if (pdp->context_id == context_id)
775 return pdp;
776 }
777
778 return NULL;
779}
780
781/***********************************************************************
782 * Actual Implementation
783 ***********************************************************************/
784
785static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100786 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200787{
788 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200789 LOGP(DVLR, LOGL_NOTICE,
790 "Unknown IMSI %s, discarding GSUP request "
791 "of type 0x%02x\n",
792 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100793 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200794 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
795 LOGP(DVLR, LOGL_NOTICE,
796 "Unknown IMSI %s, discarding GSUP error "
797 "of type 0x%02x, cause '%s' (%d)\n",
798 gsup_msg->imsi, gsup_msg->message_type,
799 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
800 gsup_msg->cause);
801 } else {
802 LOGP(DVLR, LOGL_NOTICE,
803 "Unknown IMSI %s, discarding GSUP response "
804 "of type 0x%02x\n",
805 gsup_msg->imsi, gsup_msg->message_type);
806 }
807
808 return -GMM_CAUSE_IMSI_UNKNOWN;
809}
810
811static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100812 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200813{
814 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
815 LOGGSUPP(LOGL_NOTICE, gsup_msg,
816 "Purge MS has failed with cause '%s' (%d)\n",
817 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
818 gsup_msg->cause);
819 return -gsup_msg->cause;
820 }
821 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
822 return 0;
823}
824
825/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100826int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200827{
828 struct osmo_gsup_message gsup_msg = {0};
829 int rc;
830
Harald Welte123261e2022-05-15 11:43:35 +0200831 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_UL_REQ);
832
Harald Welteb8b85a12016-06-17 00:06:42 +0200833 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200834 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 +0200835 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
836
837 return rc;
838}
839
840/* VLR internal call to request tuples from HLR */
841int vlr_subscr_req_sai(struct vlr_subscr *vsub,
842 const uint8_t *auts, const uint8_t *auts_rand)
843{
844 struct osmo_gsup_message gsup_msg = {0};
845
Harald Welte123261e2022-05-15 11:43:35 +0200846 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_SAI_REQ);
847
Harald Welteb8b85a12016-06-17 00:06:42 +0200848 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
849 gsup_msg.auts = auts;
850 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100851 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200852
853 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
854}
855
Oliver Smith7d053092018-12-14 17:37:38 +0100856/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
857int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
858{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100859 struct osmo_gsup_message gsup_msg = {
860 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200861 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100862 };
Oliver Smith7d053092018-12-14 17:37:38 +0100863 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
864 int len;
865
866 /* Encode IMEI */
867 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
868 if (len < 1) {
869 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
870 return -ENOSPC;
871 }
872 gsup_msg.imei_enc = imei_enc;
873 gsup_msg.imei_enc_len = len;
874
Harald Welte123261e2022-05-15 11:43:35 +0200875 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CHECK_IMEI_REQ);
876
Oliver Smith7d053092018-12-14 17:37:38 +0100877 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100878 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100879 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100880}
881
Harald Welteb8b85a12016-06-17 00:06:42 +0200882/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100883int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200884{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100885 struct osmo_gsup_message gsup_msg = {
886 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200887 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200889
Harald Welte123261e2022-05-15 11:43:35 +0200890 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_AUTH_FAIL_REP);
891
Max98f74672018-02-05 12:57:06 +0100892 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100893 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200894}
895
896/* Update the subscriber with GSUP-received auth tuples */
897void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
898 const struct osmo_gsup_message *gsup)
899{
900 unsigned int i;
901 unsigned int got_tuples;
902
903 if (gsup->num_auth_vectors) {
904 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
905 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100906 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200907 }
908
909 got_tuples = 0;
910 for (i = 0; i < gsup->num_auth_vectors; i++) {
911 size_t key_seq = i;
912
913 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
914 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100915 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200916 key_seq);
917 continue;
918 }
919 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
920 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100921 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200922 }
923
924 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
Harald Welte123261e2022-05-15 11:43:35 +0200925 vlr_rate_ctr_add(vsub->vlr, VLR_CTR_GSUP_RX_TUPLES, got_tuples);
Harald Welteb8b85a12016-06-17 00:06:42 +0200926
927 if (!got_tuples) {
928 /* FIXME what now? */
929 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
930 }
931
932 /* New tuples means last_tuple becomes invalid */
933 vsub->last_tuple = NULL;
934}
935
936/* Handle SendAuthInfo Result/Error from HLR */
937static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
938 const struct osmo_gsup_message *gsup)
939{
940 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
941 void *data = (void *) gsup;
942
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200943 if (!auth_fi) {
944 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
945 osmo_gsup_message_type_name(gsup->message_type));
946 return -1;
947 }
948
Harald Welteb8b85a12016-06-17 00:06:42 +0200949 switch (gsup->message_type) {
950 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
951 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
952 break;
953 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
954 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
955 break;
956 default:
957 return -1;
958 }
959
960 return 0;
961}
962
Harald Welteb8b85a12016-06-17 00:06:42 +0200963static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
964 const struct osmo_gsup_message *gsup_msg)
965{
966 unsigned idx;
967 int rc;
968
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100969 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100970 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
971 gsup_msg->msisdn_enc,
972 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200973 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
974 vsub->imsi, vsub->msisdn);
975 }
976
977 if (gsup_msg->hlr_enc) {
978 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
979 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
980 gsup_msg->hlr_enc_len);
981 vsub->hlr.len = 0;
982 } else {
983 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
984 gsup_msg->hlr_enc_len);
985 vsub->hlr.len = gsup_msg->hlr_enc_len;
986 }
987 }
988
989 if (gsup_msg->pdp_info_compl) {
990 rc = vlr_subscr_pdp_data_clear(vsub);
991 if (rc > 0)
992 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
993 }
994
995 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
996 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
997 size_t ctx_id = pdp_info->context_id;
998 struct sgsn_subscriber_pdp_data *pdp_data;
999
1000 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
1001 LOGVSUBP(LOGL_ERROR, vsub,
1002 "APN too long, context id = %zu, APN = %s\n",
1003 ctx_id, osmo_hexdump(pdp_info->apn_enc,
1004 pdp_info->apn_enc_len));
1005 continue;
1006 }
1007
1008 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
1009 LOGVSUBP(LOGL_ERROR, vsub,
1010 "QoS info too long (%zu)\n",
1011 pdp_info->qos_enc_len);
1012 continue;
1013 }
1014
1015 LOGVSUBP(LOGL_INFO, vsub,
1016 "Will set PDP info, context id = %zu, APN = %s\n",
1017 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
1018
1019 /* Set PDP info [ctx_id] */
1020 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
1021 if (!pdp_data) {
1022 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
1023 pdp_data->context_id = ctx_id;
1024 }
1025
1026 OSMO_ASSERT(pdp_data != NULL);
1027 pdp_data->pdp_type = pdp_info->pdp_type;
1028 osmo_apn_to_str(pdp_data->apn_str,
1029 pdp_info->apn_enc, pdp_info->apn_enc_len);
1030 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
1031 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
1032 }
1033}
1034
1035
1036/* Handle InsertSubscrData Result from HLR */
1037static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
1038 const struct osmo_gsup_message *gsup)
1039{
1040 struct osmo_gsup_message gsup_reply = {0};
1041
Harald Welte123261e2022-05-15 11:43:35 +02001042 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_ISD_RES);
1043
Harald Welteb8b85a12016-06-17 00:06:42 +02001044 vlr_subscr_gsup_insert_data(vsub, gsup);
1045 vsub->vlr->ops.subscr_update(vsub);
1046
1047 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
1048 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
1049}
1050
1051/* Handle UpdateLocation Result from HLR */
1052static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
1053 const struct osmo_gsup_message *gsup)
1054{
Philipp Maier483cea82019-04-03 16:23:29 +02001055 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001056 bool sgs_lu_in_progress = false;
1057
1058 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1059 sgs_lu_in_progress = true;
1060
1061 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001062 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
1063 "without LU in progress\n");
1064 return -ENODEV;
1065 }
1066
1067 /* contrary to MAP, we allow piggy-backing subscriber data onto the
1068 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
1069 * nested INSERT SUBSCRIBER DATA transaction */
1070 vlr_subscr_gsup_insert_data(vsub, gsup);
1071
Harald Welte0df904d2018-12-03 11:00:04 +01001072 if (sgs_lu_in_progress) {
1073 sgs_lu_response.accepted = true;
1074 sgs_lu_response.vsub = vsub;
1075 vsub->sgs.response_cb(&sgs_lu_response);
1076 } else
1077 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001078
1079 return 0;
1080}
1081
1082/* Handle UpdateLocation Result from HLR */
1083static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
1084 const struct osmo_gsup_message *gsup)
1085{
Philipp Maier483cea82019-04-03 16:23:29 +02001086 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001087 bool sgs_lu_in_progress = false;
1088
1089 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1090 sgs_lu_in_progress = true;
1091
1092 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001093 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
1094 "without LU in progress\n");
1095 return -ENODEV;
1096 }
1097
1098 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
1099 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1100
Harald Welte0df904d2018-12-03 11:00:04 +01001101 if (sgs_lu_in_progress) {
1102 sgs_lu_response.accepted = false;
1103 sgs_lu_response.vsub = vsub;
1104 vsub->sgs.response_cb(&sgs_lu_response);
1105 } else
1106 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
1107 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001108 return 0;
1109}
1110
Alexander Couzens7312b152019-08-19 15:30:12 +02001111void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
1112 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +02001113{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001114 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1115 switch (gmm_cause) {
1116 case GMM_CAUSE_IMSI_UNKNOWN:
1117 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
1118 break;
1119 case GMM_CAUSE_ILLEGAL_MS:
1120 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
1121 break;
1122 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
1123 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
1124 break;
1125 case GMM_CAUSE_ILLEGAL_ME:
1126 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
1127 break;
1128 case GMM_CAUSE_GPRS_NOTALLOWED:
1129 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
1130 break;
1131 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
1132 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
1133 break;
1134 case GMM_CAUSE_MS_ID_NOT_DERIVED:
1135 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
1136 break;
1137 case GMM_CAUSE_IMPL_DETACHED:
1138 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
1139 break;
1140 case GMM_CAUSE_PLMN_NOTALLOWED:
1141 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
1142 break;
1143 case GMM_CAUSE_LA_NOTALLOWED:
1144 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
1145 break;
1146 case GMM_CAUSE_ROAMING_NOTALLOWED:
1147 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
1148 break;
1149 case GMM_CAUSE_NO_GPRS_PLMN:
1150 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
1151 break;
1152 case GMM_CAUSE_MSC_TEMP_NOTREACH:
1153 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
1154 break;
1155 case GMM_CAUSE_SYNC_FAIL:
1156 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
1157 break;
1158 case GMM_CAUSE_CONGESTION:
1159 gsm48_rej = GSM48_REJECT_CONGESTION;
1160 break;
1161 case GMM_CAUSE_SEM_INCORR_MSG:
1162 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1163 break;
1164 case GMM_CAUSE_INV_MAND_INFO:
1165 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1166 break;
1167 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1168 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1169 break;
1170 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1171 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1172 break;
1173 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1174 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1175 break;
1176 case GMM_CAUSE_COND_IE_ERR:
1177 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1178 break;
1179 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1180 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1181 break;
1182 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1183 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1184 break;
1185
1186 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1187 case GMM_CAUSE_MAC_FAIL:
1188 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1189 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1190 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1191 case GMM_CAUSE_NO_PDP_ACTIVATED:
1192 case GMM_CAUSE_NET_FAIL:
1193 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1194 break;
1195 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001196
1197 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001198}
1199
Harald Welteb8b85a12016-06-17 00:06:42 +02001200/* Handle LOCATION CANCEL request from HLR */
1201static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001202 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001203{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001204 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001205 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001206 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001207 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001208 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1209
Harald Welte123261e2022-05-15 11:43:35 +02001210 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CANCEL_RES);
1211
Harald Welteb8b85a12016-06-17 00:06:42 +02001212 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1213 is_update_procedure ?
1214 "update procedure" : "subscription withdraw");
1215
1216 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001217 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001218
Alexander Couzens7312b152019-08-19 15:30:12 +02001219 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001220 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001221
Harald Welte123261e2022-05-15 11:43:35 +02001222 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_CANCEL);
Harald Welte173bdf32022-05-15 12:12:54 +02001223 vlr_subscr_detach(vsub);
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001224
Max770fbd22018-01-24 12:48:33 +01001225 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001226}
1227
Oliver Smith7d053092018-12-14 17:37:38 +01001228/* Handle Check_IMEI_VLR result and error from HLR */
1229static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1230{
1231 if (!vsub->lu_fsm) {
1232 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1233 osmo_gsup_message_type_name(gsup->message_type));
1234 return -ENODEV;
1235 }
1236
Oliver Smithcbf2c932019-05-06 13:09:55 +02001237 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1238 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001239 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1240 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1241 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1242 else
1243 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1244 } else {
1245 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1246 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1247 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1248 }
1249
1250 return 0;
1251}
1252
Harald Welteb8b85a12016-06-17 00:06:42 +02001253/* Incoming handler for GSUP from HLR.
1254 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001255int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001256{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001257 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001258 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001259 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001260
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001261 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001262 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001263 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001264 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1265 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001266 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001267 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001268 default:
Harald Welte123261e2022-05-15 11:43:35 +02001269 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN_IMSI);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001270 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001271 }
1272 }
1273
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001274 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001275 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001276 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_RES);
1277 rc = vlr_subscr_handle_sai_res(vsub, gsup);
1278 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001279 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001280 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001281 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001282 break;
1283 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001284 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_ISD_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001285 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001286 break;
1287 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001288 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CANCEL_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001289 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001290 break;
1291 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001292 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001293 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001294 break;
1295 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001296 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001297 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001298 break;
1299 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001300 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_ERR);
1301 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001302 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001303 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_RES);
1304 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001305 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001306 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_DELETE_DATA_REQ);
1307 goto out_unimpl;
Oliver Smith7d053092018-12-14 17:37:38 +01001308 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001309 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_ERR);
1310 rc = vlr_subscr_handle_check_imei(vsub, gsup);
1311 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001312 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001313 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001314 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001315 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001316 default:
Harald Welte123261e2022-05-15 11:43:35 +02001317 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001318 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1319 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001320 break;
1321 }
1322
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001323 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001324 return rc;
Harald Welte123261e2022-05-15 11:43:35 +02001325
1326out_unimpl:
1327 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP msg_type=%d not yet implemented\n", gsup->message_type);
1328 vlr_subscr_put(vsub, __func__);
1329 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001330}
1331
1332/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001333int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001334{
Harald Welteb8b85a12016-06-17 00:06:42 +02001335 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001336 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001337 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001338 if (vsub->imsi[0]
1339 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001340 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001341 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001342 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001343 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001344 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001345 break;
1346 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001347 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001348 break;
1349 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001350 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001351 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001352 default:
1353 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001354 }
1355
1356 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001357 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001358 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001359 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1360 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001361 break;
1362 }
1363 }
1364
1365 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001366 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001367 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001368 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001369 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001370 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001371 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001372 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001373 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001374 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001375 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001376 }
1377
1378 return 0;
1379}
1380
1381/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1382int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1383{
1384 if (vsub->lu_fsm) {
1385 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1386 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1387 } else if (vsub->proc_arq_fsm) {
1388 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1389 PR_ARQ_E_TMSI_ACK, NULL);
1390 } else {
1391 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001392 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001393 return -EINVAL;
1394 }
1395}
1396
Maxdcc193d2017-12-27 19:34:15 +01001397bool vlr_subscr_expire(struct vlr_subscr *vsub)
1398{
1399 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001400 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001401 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001402 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001403
1404 return true;
1405 }
1406
1407 return false;
1408}
1409
Harald Welte173bdf32022-05-15 12:12:54 +02001410static int vlr_subscr_detach(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001411{
1412 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001413 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001414
1415 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001416 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001417
Harald Welte0df904d2018-12-03 11:00:04 +01001418 /* Inform the UE-SGs FSM that the subscriber has been detached */
1419 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1420
Maxdcc193d2017-12-27 19:34:15 +01001421 vlr_subscr_expire(vsub);
1422
Harald Welteb8b85a12016-06-17 00:06:42 +02001423 return 0;
1424}
1425
Harald Welte173bdf32022-05-15 12:12:54 +02001426/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
1427int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1428{
Harald Welte123261e2022-05-15 11:43:35 +02001429 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_REQ);
Harald Welte173bdf32022-05-15 12:12:54 +02001430 return vlr_subscr_detach(vsub);
1431}
1432
Harald Welteb8b85a12016-06-17 00:06:42 +02001433/* Tear down any running FSMs due to MSC connection timeout.
1434 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1435 * message before the entire connection is torn down.
1436 * \param[in] vsub subscriber to tear down
1437 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001438void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001439{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001440 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001441}
1442
1443struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1444{
1445 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1446 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001447
1448 /* Some of these are needed only on UTRAN, but in case the caller wants
1449 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001450 OSMO_ASSERT(ops->tx_auth_req);
1451 OSMO_ASSERT(ops->tx_auth_rej);
1452 OSMO_ASSERT(ops->tx_id_req);
1453 OSMO_ASSERT(ops->tx_lu_acc);
1454 OSMO_ASSERT(ops->tx_lu_rej);
1455 OSMO_ASSERT(ops->tx_cm_serv_acc);
1456 OSMO_ASSERT(ops->tx_cm_serv_rej);
1457 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001458 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001459 OSMO_ASSERT(ops->subscr_update);
1460 OSMO_ASSERT(ops->subscr_assoc);
1461
1462 INIT_LLIST_HEAD(&vlr->subscribers);
1463 INIT_LLIST_HEAD(&vlr->operations);
1464 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1465
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001466 /* defaults */
1467 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001468 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1469 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001470
Harald Welte123261e2022-05-15 11:43:35 +02001471 vlr->statg = osmo_stat_item_group_alloc(vlr, &vlr_statg_desc, 0);
1472 if (!vlr->statg)
1473 goto err_free;
1474
1475 vlr->ctrg = rate_ctr_group_alloc(vlr, &vlr_ctrg_desc, 0);
1476 if (!vlr->ctrg)
1477 goto err_statg;
1478
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001479 /* reset shared timer definitions */
1480 osmo_tdefs_reset(msc_tdefs_vlr);
1481
Harald Welteb8b85a12016-06-17 00:06:42 +02001482 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001483 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001484 /* osmo_lu_fsm.c */
1485 vlr_lu_fsm_init();
1486 /* vlr_access_request_fsm.c */
1487 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001488 /* vlr_sgs_fsm.c */
1489 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001490
1491 return vlr;
Harald Welte123261e2022-05-15 11:43:35 +02001492
1493err_statg:
1494 osmo_stat_item_group_free(vlr->statg);
1495err_free:
1496 talloc_free(vlr);
1497 return NULL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001498}
1499
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001500int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001501{
1502 OSMO_ASSERT(vlr);
1503
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001504 vlr->gcm = gcm;
1505 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1506 .func = vlr_gsup_rx,
1507 .data = vlr,
1508 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001509
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001510 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1511 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001512 return 0;
1513}
1514
1515/* MSC->VLR: Subscribre has disconnected */
1516int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1517{
1518 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1519 * interface */
1520 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1521 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1522 vsub->msc_conn_ref = NULL;
1523
1524 return 0;
1525}
1526
1527/* MSC->VLR: Receive Authentication Failure from Subscriber */
1528int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1529{
1530 struct vlr_auth_resp_par par = {0};
1531 par.auts = auts;
1532
1533 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1534 return 0;
1535}
1536
1537/* MSC->VLR: Receive Authentication Response from MS
1538 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1539int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1540 bool is_utran, const uint8_t *res, uint8_t res_len)
1541{
1542 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1543 struct vlr_auth_resp_par par;
1544
1545 par.is_r99 = is_r99;
1546 par.is_utran = is_utran;
1547 par.res = res;
1548 par.res_len = res_len;
1549 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1550
1551 return 0;
1552}
1553
1554/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001555void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001556{
1557 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001558 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001559 if (vsub->proc_arq_fsm
1560 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001561 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001562}
1563
1564/* Internal evaluation of requested ciphering mode.
1565 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1566 * \param[in] vlr VLR instance.
1567 * \param[in] fi Calling FSM instance, for logging.
1568 * \param[in] msc_conn_ref MSC conn to send to.
1569 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1570 * \returns 0 if no ciphering is needed or message was sent successfully,
1571 * or a negative value if ciph_mode is invalid or sending failed.
1572 */
1573int vlr_set_ciph_mode(struct vlr_instance *vlr,
1574 struct osmo_fsm_inst *fi,
1575 void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001576 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001577 bool retrieve_imeisv)
1578{
Harald Welte71c51df2017-12-23 18:51:48 +01001579 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1580 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001581}
1582
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001583/* Decide whether UMTS AKA should be used.
1584 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1585 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1586 * GERAN are R99 capable and UMTS AKA tokens are available.
1587 * \param[in] vec Auth tokens (received from the HLR).
1588 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1589 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1590 */
1591bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1592{
1593 if (!is_r99)
1594 return false;
1595 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1596 return false;
1597 return true;
1598}
1599
Harald Welteb8b85a12016-06-17 00:06:42 +02001600void log_set_filter_vlr_subscr(struct log_target *target,
1601 struct vlr_subscr *vlr_subscr)
1602{
1603 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001604 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001605
1606 /* free the old data */
1607 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001608 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001609 *fsub = NULL;
1610 }
1611
1612 if (vlr_subscr) {
1613 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001614 vlr_subscr_get(vlr_subscr, use);
1615 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001616 } else
1617 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1618}