blob: e2e02bfcec4b2156f976fe0e772f5031331ef470 [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 {
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100731 struct llist_head list;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200732
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100733 unsigned int context_id;
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +0100734 enum gsm48_pdp_type_org pdp_type_org;
735 enum gsm48_pdp_type_nr pdp_type_nr;
736 struct osmo_sockaddr pdp_address[2];
Pau Espin Pedrola4565832024-01-19 21:09:27 +0100737 char apn_str[GSM_APN_LENGTH];
738 uint8_t qos_subscribed[20];
739 size_t qos_subscribed_len;
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200740};
741
Harald Welteb8b85a12016-06-17 00:06:42 +0200742struct sgsn_subscriber_pdp_data *
743vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
744{
745 struct sgsn_subscriber_pdp_data* pdata;
746
747 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
748
749 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
Harald Welte123261e2022-05-15 11:43:35 +0200750 vlr_stat_item_inc(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200751
752 return pdata;
753}
754
755static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
756{
757 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
758 int count = 0;
759
760 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
761 llist_del(&pdp->list);
Harald Welte123261e2022-05-15 11:43:35 +0200762 vlr_stat_item_dec(vsub->vlr, VLR_STAT_PDP_COUNT);
Harald Welteb8b85a12016-06-17 00:06:42 +0200763 talloc_free(pdp);
764 count += 1;
765 }
766
767 return count;
768}
769
770static struct sgsn_subscriber_pdp_data *
771vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
772{
773 struct sgsn_subscriber_pdp_data *pdp;
774
775 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
776 if (pdp->context_id == context_id)
777 return pdp;
778 }
779
780 return NULL;
781}
782
783/***********************************************************************
784 * Actual Implementation
785 ***********************************************************************/
786
787static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100788 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200789{
790 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200791 LOGP(DVLR, LOGL_NOTICE,
792 "Unknown IMSI %s, discarding GSUP request "
793 "of type 0x%02x\n",
794 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100795 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200796 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
797 LOGP(DVLR, LOGL_NOTICE,
798 "Unknown IMSI %s, discarding GSUP error "
799 "of type 0x%02x, cause '%s' (%d)\n",
800 gsup_msg->imsi, gsup_msg->message_type,
801 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
802 gsup_msg->cause);
803 } else {
804 LOGP(DVLR, LOGL_NOTICE,
805 "Unknown IMSI %s, discarding GSUP response "
806 "of type 0x%02x\n",
807 gsup_msg->imsi, gsup_msg->message_type);
808 }
809
810 return -GMM_CAUSE_IMSI_UNKNOWN;
811}
812
813static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100814 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200815{
816 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
817 LOGGSUPP(LOGL_NOTICE, gsup_msg,
818 "Purge MS has failed with cause '%s' (%d)\n",
819 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
820 gsup_msg->cause);
821 return -gsup_msg->cause;
822 }
823 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
824 return 0;
825}
826
827/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100828int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200829{
830 struct osmo_gsup_message gsup_msg = {0};
831 int rc;
832
Harald Welte123261e2022-05-15 11:43:35 +0200833 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_UL_REQ);
834
Harald Welteb8b85a12016-06-17 00:06:42 +0200835 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200836 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 +0200837 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
838
839 return rc;
840}
841
842/* VLR internal call to request tuples from HLR */
843int vlr_subscr_req_sai(struct vlr_subscr *vsub,
844 const uint8_t *auts, const uint8_t *auts_rand)
845{
846 struct osmo_gsup_message gsup_msg = {0};
847
Harald Welte123261e2022-05-15 11:43:35 +0200848 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_SAI_REQ);
849
Harald Welteb8b85a12016-06-17 00:06:42 +0200850 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
851 gsup_msg.auts = auts;
852 gsup_msg.rand = auts_rand;
Neels Hofmeyr63b24642019-12-12 01:31:04 +0100853 gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Harald Welteb8b85a12016-06-17 00:06:42 +0200854
855 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
856}
857
Oliver Smith7d053092018-12-14 17:37:38 +0100858/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
859int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
860{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100861 struct osmo_gsup_message gsup_msg = {
862 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200863 .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100864 };
Oliver Smith7d053092018-12-14 17:37:38 +0100865 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
866 int len;
867
868 /* Encode IMEI */
869 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
870 if (len < 1) {
871 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
872 return -ENOSPC;
873 }
874 gsup_msg.imei_enc = imei_enc;
875 gsup_msg.imei_enc_len = len;
876
Harald Welte123261e2022-05-15 11:43:35 +0200877 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CHECK_IMEI_REQ);
878
Oliver Smith7d053092018-12-14 17:37:38 +0100879 /* Send CHECK_IMEI_REQUEST */
Oliver Smith7d053092018-12-14 17:37:38 +0100880 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100881 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100882}
883
Harald Welteb8b85a12016-06-17 00:06:42 +0200884/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100885int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200886{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100887 struct osmo_gsup_message gsup_msg = {
888 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
Vadim Yanitskiyed73ae12019-08-15 22:51:20 +0200889 .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100890 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200891
Harald Welte123261e2022-05-15 11:43:35 +0200892 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_AUTH_FAIL_REP);
893
Max98f74672018-02-05 12:57:06 +0100894 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100895 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200896}
897
898/* Update the subscriber with GSUP-received auth tuples */
899void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
900 const struct osmo_gsup_message *gsup)
901{
902 unsigned int i;
903 unsigned int got_tuples;
904
905 if (gsup->num_auth_vectors) {
906 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
907 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100908 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200909 }
910
911 got_tuples = 0;
912 for (i = 0; i < gsup->num_auth_vectors; i++) {
913 size_t key_seq = i;
914
915 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
916 LOGVSUBP(LOGL_NOTICE, vsub,
Martin Hauke3f07dac2019-11-14 17:49:08 +0100917 "Skipping auth tuple with invalid cksn %zu\n",
Harald Welteb8b85a12016-06-17 00:06:42 +0200918 key_seq);
919 continue;
920 }
921 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
922 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100923 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200924 }
925
926 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
Harald Welte123261e2022-05-15 11:43:35 +0200927 vlr_rate_ctr_add(vsub->vlr, VLR_CTR_GSUP_RX_TUPLES, got_tuples);
Harald Welteb8b85a12016-06-17 00:06:42 +0200928
929 if (!got_tuples) {
930 /* FIXME what now? */
931 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
932 }
933
934 /* New tuples means last_tuple becomes invalid */
935 vsub->last_tuple = NULL;
936}
937
938/* Handle SendAuthInfo Result/Error from HLR */
939static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
940 const struct osmo_gsup_message *gsup)
941{
942 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
943 void *data = (void *) gsup;
944
Neels Hofmeyr1bfe0e12019-09-16 18:07:54 +0200945 if (!auth_fi) {
946 LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n",
947 osmo_gsup_message_type_name(gsup->message_type));
948 return -1;
949 }
950
Harald Welteb8b85a12016-06-17 00:06:42 +0200951 switch (gsup->message_type) {
952 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
953 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
954 break;
955 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
956 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
957 break;
958 default:
959 return -1;
960 }
961
962 return 0;
963}
964
Harald Welteb8b85a12016-06-17 00:06:42 +0200965static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
966 const struct osmo_gsup_message *gsup_msg)
967{
968 unsigned idx;
969 int rc;
970
Neels Hofmeyr02dd2652019-11-13 07:05:11 +0100971 if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100972 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
973 gsup_msg->msisdn_enc,
974 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200975 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
976 vsub->imsi, vsub->msisdn);
977 }
978
979 if (gsup_msg->hlr_enc) {
980 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
981 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
982 gsup_msg->hlr_enc_len);
983 vsub->hlr.len = 0;
984 } else {
985 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
986 gsup_msg->hlr_enc_len);
987 vsub->hlr.len = gsup_msg->hlr_enc_len;
988 }
989 }
990
991 if (gsup_msg->pdp_info_compl) {
992 rc = vlr_subscr_pdp_data_clear(vsub);
993 if (rc > 0)
994 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
995 }
996
997 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
998 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
999 size_t ctx_id = pdp_info->context_id;
1000 struct sgsn_subscriber_pdp_data *pdp_data;
1001
1002 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
1003 LOGVSUBP(LOGL_ERROR, vsub,
1004 "APN too long, context id = %zu, APN = %s\n",
1005 ctx_id, osmo_hexdump(pdp_info->apn_enc,
1006 pdp_info->apn_enc_len));
1007 continue;
1008 }
1009
1010 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
1011 LOGVSUBP(LOGL_ERROR, vsub,
1012 "QoS info too long (%zu)\n",
1013 pdp_info->qos_enc_len);
1014 continue;
1015 }
1016
1017 LOGVSUBP(LOGL_INFO, vsub,
1018 "Will set PDP info, context id = %zu, APN = %s\n",
1019 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
1020
1021 /* Set PDP info [ctx_id] */
1022 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
1023 if (!pdp_data) {
1024 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
1025 pdp_data->context_id = ctx_id;
1026 }
1027
1028 OSMO_ASSERT(pdp_data != NULL);
Pau Espin Pedrol6f2749c2024-01-19 21:10:12 +01001029 pdp_data->pdp_type_org = pdp_info->pdp_type_org;
1030 pdp_data->pdp_type_nr = pdp_info->pdp_type_nr;
1031 memcpy(&pdp_data->pdp_address[0], &pdp_info->pdp_address[0], sizeof(pdp_data->pdp_address[0]));
1032 memcpy(&pdp_data->pdp_address[1], &pdp_info->pdp_address[1], sizeof(pdp_data->pdp_address[1]));
Harald Welteb8b85a12016-06-17 00:06:42 +02001033 osmo_apn_to_str(pdp_data->apn_str,
1034 pdp_info->apn_enc, pdp_info->apn_enc_len);
1035 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
1036 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
1037 }
1038}
1039
1040
1041/* Handle InsertSubscrData Result from HLR */
1042static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
1043 const struct osmo_gsup_message *gsup)
1044{
1045 struct osmo_gsup_message gsup_reply = {0};
1046
Harald Welte123261e2022-05-15 11:43:35 +02001047 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_ISD_RES);
1048
Harald Welteb8b85a12016-06-17 00:06:42 +02001049 vlr_subscr_gsup_insert_data(vsub, gsup);
1050 vsub->vlr->ops.subscr_update(vsub);
1051
1052 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
1053 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
1054}
1055
1056/* Handle UpdateLocation Result from HLR */
1057static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
1058 const struct osmo_gsup_message *gsup)
1059{
Philipp Maier483cea82019-04-03 16:23:29 +02001060 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001061 bool sgs_lu_in_progress = false;
1062
1063 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1064 sgs_lu_in_progress = true;
1065
1066 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001067 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
1068 "without LU in progress\n");
1069 return -ENODEV;
1070 }
1071
1072 /* contrary to MAP, we allow piggy-backing subscriber data onto the
1073 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
1074 * nested INSERT SUBSCRIBER DATA transaction */
1075 vlr_subscr_gsup_insert_data(vsub, gsup);
1076
Harald Welte0df904d2018-12-03 11:00:04 +01001077 if (sgs_lu_in_progress) {
1078 sgs_lu_response.accepted = true;
1079 sgs_lu_response.vsub = vsub;
1080 vsub->sgs.response_cb(&sgs_lu_response);
1081 } else
1082 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001083
1084 return 0;
1085}
1086
1087/* Handle UpdateLocation Result from HLR */
1088static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
1089 const struct osmo_gsup_message *gsup)
1090{
Philipp Maier483cea82019-04-03 16:23:29 +02001091 struct sgs_lu_response sgs_lu_response = {0};
Harald Welte0df904d2018-12-03 11:00:04 +01001092 bool sgs_lu_in_progress = false;
1093
1094 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
1095 sgs_lu_in_progress = true;
1096
1097 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001098 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
1099 "without LU in progress\n");
1100 return -ENODEV;
1101 }
1102
1103 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
1104 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1105
Harald Welte0df904d2018-12-03 11:00:04 +01001106 if (sgs_lu_in_progress) {
1107 sgs_lu_response.accepted = false;
1108 sgs_lu_response.vsub = vsub;
1109 vsub->sgs.response_cb(&sgs_lu_response);
1110 } else
1111 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
1112 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001113 return 0;
1114}
1115
Alexander Couzens7312b152019-08-19 15:30:12 +02001116void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause,
1117 enum gsm48_reject_value *gsm48_rej_p)
Neels Hofmeyr15809592018-04-06 02:57:51 +02001118{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001119 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1120 switch (gmm_cause) {
1121 case GMM_CAUSE_IMSI_UNKNOWN:
1122 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
1123 break;
1124 case GMM_CAUSE_ILLEGAL_MS:
1125 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
1126 break;
1127 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
1128 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
1129 break;
1130 case GMM_CAUSE_ILLEGAL_ME:
1131 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
1132 break;
1133 case GMM_CAUSE_GPRS_NOTALLOWED:
1134 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
1135 break;
1136 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
1137 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
1138 break;
1139 case GMM_CAUSE_MS_ID_NOT_DERIVED:
1140 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
1141 break;
1142 case GMM_CAUSE_IMPL_DETACHED:
1143 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
1144 break;
1145 case GMM_CAUSE_PLMN_NOTALLOWED:
1146 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
1147 break;
1148 case GMM_CAUSE_LA_NOTALLOWED:
1149 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
1150 break;
1151 case GMM_CAUSE_ROAMING_NOTALLOWED:
1152 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
1153 break;
1154 case GMM_CAUSE_NO_GPRS_PLMN:
1155 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
1156 break;
1157 case GMM_CAUSE_MSC_TEMP_NOTREACH:
1158 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
1159 break;
1160 case GMM_CAUSE_SYNC_FAIL:
1161 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
1162 break;
1163 case GMM_CAUSE_CONGESTION:
1164 gsm48_rej = GSM48_REJECT_CONGESTION;
1165 break;
1166 case GMM_CAUSE_SEM_INCORR_MSG:
1167 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
1168 break;
1169 case GMM_CAUSE_INV_MAND_INFO:
1170 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
1171 break;
1172 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
1173 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
1174 break;
1175 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
1176 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
1177 break;
1178 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1179 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1180 break;
1181 case GMM_CAUSE_COND_IE_ERR:
1182 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1183 break;
1184 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1185 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1186 break;
1187 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1188 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1189 break;
1190
1191 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1192 case GMM_CAUSE_MAC_FAIL:
1193 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1194 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1195 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1196 case GMM_CAUSE_NO_PDP_ACTIVATED:
1197 case GMM_CAUSE_NET_FAIL:
1198 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1199 break;
1200 }
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001201
1202 *gsm48_rej_p = gsm48_rej;
Neels Hofmeyr15809592018-04-06 02:57:51 +02001203}
1204
Harald Welteb8b85a12016-06-17 00:06:42 +02001205/* Handle LOCATION CANCEL request from HLR */
1206static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001207 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001208{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001209 enum gsm48_reject_value gsm48_rej;
Alexander Couzenseb1b03a2019-08-19 15:22:25 +02001210 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
Harald Welteb8b85a12016-06-17 00:06:42 +02001211 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001212 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001213 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1214
Harald Welte123261e2022-05-15 11:43:35 +02001215 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CANCEL_RES);
1216
Harald Welteb8b85a12016-06-17 00:06:42 +02001217 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1218 is_update_procedure ?
1219 "update procedure" : "subscription withdraw");
1220
1221 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001222 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001223
Alexander Couzens7312b152019-08-19 15:30:12 +02001224 vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001225 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001226
Harald Welte123261e2022-05-15 11:43:35 +02001227 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_CANCEL);
Harald Welte173bdf32022-05-15 12:12:54 +02001228 vlr_subscr_detach(vsub);
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001229
Max770fbd22018-01-24 12:48:33 +01001230 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001231}
1232
Oliver Smith7d053092018-12-14 17:37:38 +01001233/* Handle Check_IMEI_VLR result and error from HLR */
1234static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1235{
1236 if (!vsub->lu_fsm) {
1237 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1238 osmo_gsup_message_type_name(gsup->message_type));
1239 return -ENODEV;
1240 }
1241
Oliver Smithcbf2c932019-05-06 13:09:55 +02001242 /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch
1243 * it further to lu_compl_vlr_fsm (Check IMEI after LU). */
Oliver Smith7d053092018-12-14 17:37:38 +01001244 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1245 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1246 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1247 else
1248 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1249 } else {
1250 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1251 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1252 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1253 }
1254
1255 return 0;
1256}
1257
Harald Welteb8b85a12016-06-17 00:06:42 +02001258/* Incoming handler for GSUP from HLR.
1259 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001260int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001261{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001262 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001263 struct vlr_subscr *vsub;
Neels Hofmeyr27c8b032019-12-23 19:12:54 +01001264 int rc = 0;
Harald Welteb8b85a12016-06-17 00:06:42 +02001265
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001266 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001267 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001268 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001269 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1270 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001271 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001272 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001273 default:
Harald Welte123261e2022-05-15 11:43:35 +02001274 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN_IMSI);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001275 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001276 }
1277 }
1278
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001279 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001280 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001281 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_RES);
1282 rc = vlr_subscr_handle_sai_res(vsub, gsup);
1283 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001284 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001285 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001286 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001287 break;
1288 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001289 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_ISD_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001290 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001291 break;
1292 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001293 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CANCEL_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001294 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001295 break;
1296 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001297 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001298 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001299 break;
1300 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001301 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_ERR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001302 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001303 break;
1304 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001305 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_ERR);
1306 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001307 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001308 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_RES);
1309 goto out_unimpl;
Harald Welteb8b85a12016-06-17 00:06:42 +02001310 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Harald Welte123261e2022-05-15 11:43:35 +02001311 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_DELETE_DATA_REQ);
1312 goto out_unimpl;
Oliver Smith7d053092018-12-14 17:37:38 +01001313 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
Harald Welte123261e2022-05-15 11:43:35 +02001314 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_ERR);
1315 rc = vlr_subscr_handle_check_imei(vsub, gsup);
1316 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001317 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Harald Welte123261e2022-05-15 11:43:35 +02001318 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_RES);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001319 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001320 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001321 default:
Harald Welte123261e2022-05-15 11:43:35 +02001322 vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001323 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1324 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001325 break;
1326 }
1327
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001328 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001329 return rc;
Harald Welte123261e2022-05-15 11:43:35 +02001330
1331out_unimpl:
1332 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP msg_type=%d not yet implemented\n", gsup->message_type);
1333 vlr_subscr_put(vsub, __func__);
1334 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001335}
1336
1337/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001338int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi)
Harald Welteb8b85a12016-06-17 00:06:42 +02001339{
Harald Welteb8b85a12016-06-17 00:06:42 +02001340 /* update the vlr_subscr with the given identity */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001341 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001342 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001343 if (vsub->imsi[0]
1344 && !vlr_subscr_matches_imsi(vsub, mi->imsi)) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001345 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001346 " %s\n", mi->imsi);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001347 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001348 } else
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001349 vlr_subscr_set_imsi(vsub, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001350 break;
1351 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001352 vlr_subscr_set_imei(vsub, mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001353 break;
1354 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001355 vlr_subscr_set_imeisv(vsub, mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001356 break;
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001357 default:
1358 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001359 }
1360
1361 if (vsub->auth_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001362 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001363 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001364 return osmo_fsm_inst_dispatch(vsub->auth_fsm,
1365 VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001366 break;
1367 }
1368 }
1369
1370 if (vsub->lu_fsm) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001371 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001372 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001373 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001374 case GSM_MI_TYPE_IMEI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001375 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei);
Harald Welteb8b85a12016-06-17 00:06:42 +02001376 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001377 return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001378 default:
Neels Hofmeyra40adf72020-06-02 22:02:01 +02001379 return -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001380 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001381 }
1382
1383 return 0;
1384}
1385
1386/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1387int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1388{
1389 if (vsub->lu_fsm) {
1390 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1391 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1392 } else if (vsub->proc_arq_fsm) {
1393 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1394 PR_ARQ_E_TMSI_ACK, NULL);
1395 } else {
1396 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001397 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001398 return -EINVAL;
1399 }
1400}
1401
Maxdcc193d2017-12-27 19:34:15 +01001402bool vlr_subscr_expire(struct vlr_subscr *vsub)
1403{
1404 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001405 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001406 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001407 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001408
1409 return true;
1410 }
1411
1412 return false;
1413}
1414
Harald Welte173bdf32022-05-15 12:12:54 +02001415static int vlr_subscr_detach(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001416{
1417 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001418 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001419
1420 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001421 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001422
Harald Welte0df904d2018-12-03 11:00:04 +01001423 /* Inform the UE-SGs FSM that the subscriber has been detached */
1424 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1425
Maxdcc193d2017-12-27 19:34:15 +01001426 vlr_subscr_expire(vsub);
1427
Harald Welteb8b85a12016-06-17 00:06:42 +02001428 return 0;
1429}
1430
Harald Welte173bdf32022-05-15 12:12:54 +02001431/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
1432int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1433{
Harald Welte123261e2022-05-15 11:43:35 +02001434 vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_REQ);
Harald Welte173bdf32022-05-15 12:12:54 +02001435 return vlr_subscr_detach(vsub);
1436}
1437
Harald Welteb8b85a12016-06-17 00:06:42 +02001438/* Tear down any running FSMs due to MSC connection timeout.
1439 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1440 * message before the entire connection is torn down.
1441 * \param[in] vsub subscriber to tear down
1442 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001443void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001444{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001445 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001446}
1447
1448struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1449{
1450 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1451 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001452
1453 /* Some of these are needed only on UTRAN, but in case the caller wants
1454 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001455 OSMO_ASSERT(ops->tx_auth_req);
1456 OSMO_ASSERT(ops->tx_auth_rej);
1457 OSMO_ASSERT(ops->tx_id_req);
1458 OSMO_ASSERT(ops->tx_lu_acc);
1459 OSMO_ASSERT(ops->tx_lu_rej);
1460 OSMO_ASSERT(ops->tx_cm_serv_acc);
1461 OSMO_ASSERT(ops->tx_cm_serv_rej);
1462 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001463 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001464 OSMO_ASSERT(ops->subscr_update);
1465 OSMO_ASSERT(ops->subscr_assoc);
1466
1467 INIT_LLIST_HEAD(&vlr->subscribers);
1468 INIT_LLIST_HEAD(&vlr->operations);
1469 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1470
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001471 /* defaults */
1472 vlr->cfg.assign_tmsi = true;
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001473 vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT;
1474 vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr);
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001475
Harald Welte123261e2022-05-15 11:43:35 +02001476 vlr->statg = osmo_stat_item_group_alloc(vlr, &vlr_statg_desc, 0);
1477 if (!vlr->statg)
1478 goto err_free;
1479
1480 vlr->ctrg = rate_ctr_group_alloc(vlr, &vlr_ctrg_desc, 0);
1481 if (!vlr->ctrg)
1482 goto err_statg;
1483
Vadim Yanitskiyfc2b0192020-01-18 07:20:14 +07001484 /* reset shared timer definitions */
1485 osmo_tdefs_reset(msc_tdefs_vlr);
1486
Harald Welteb8b85a12016-06-17 00:06:42 +02001487 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001488 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001489 /* osmo_lu_fsm.c */
1490 vlr_lu_fsm_init();
1491 /* vlr_access_request_fsm.c */
1492 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001493 /* vlr_sgs_fsm.c */
1494 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001495
1496 return vlr;
Harald Welte123261e2022-05-15 11:43:35 +02001497
1498err_statg:
1499 osmo_stat_item_group_free(vlr->statg);
1500err_free:
1501 talloc_free(vlr);
1502 return NULL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001503}
1504
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001505int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001506{
1507 OSMO_ASSERT(vlr);
1508
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001509 vlr->gcm = gcm;
1510 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1511 .func = vlr_gsup_rx,
1512 .data = vlr,
1513 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001514
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001515 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1516 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001517 return 0;
1518}
1519
1520/* MSC->VLR: Subscribre has disconnected */
1521int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1522{
1523 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1524 * interface */
1525 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1526 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1527 vsub->msc_conn_ref = NULL;
1528
1529 return 0;
1530}
1531
1532/* MSC->VLR: Receive Authentication Failure from Subscriber */
1533int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1534{
1535 struct vlr_auth_resp_par par = {0};
1536 par.auts = auts;
1537
1538 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1539 return 0;
1540}
1541
1542/* MSC->VLR: Receive Authentication Response from MS
1543 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1544int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1545 bool is_utran, const uint8_t *res, uint8_t res_len)
1546{
1547 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1548 struct vlr_auth_resp_par par;
1549
1550 par.is_r99 = is_r99;
1551 par.is_utran = is_utran;
1552 par.res = res;
1553 par.res_len = res_len;
1554 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1555
1556 return 0;
1557}
1558
1559/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001560void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001561{
1562 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001563 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001564 if (vsub->proc_arq_fsm
1565 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001566 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001567}
1568
1569/* Internal evaluation of requested ciphering mode.
1570 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1571 * \param[in] vlr VLR instance.
1572 * \param[in] fi Calling FSM instance, for logging.
1573 * \param[in] msc_conn_ref MSC conn to send to.
1574 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1575 * \returns 0 if no ciphering is needed or message was sent successfully,
1576 * or a negative value if ciph_mode is invalid or sending failed.
1577 */
1578int vlr_set_ciph_mode(struct vlr_instance *vlr,
1579 struct osmo_fsm_inst *fi,
1580 void *msc_conn_ref,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001581 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001582 bool retrieve_imeisv)
1583{
Harald Welte71c51df2017-12-23 18:51:48 +01001584 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1585 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001586}
1587
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001588/* Decide whether UMTS AKA should be used.
1589 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1590 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1591 * GERAN are R99 capable and UMTS AKA tokens are available.
1592 * \param[in] vec Auth tokens (received from the HLR).
1593 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1594 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1595 */
1596bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1597{
1598 if (!is_r99)
1599 return false;
1600 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1601 return false;
1602 return true;
1603}
1604
Harald Welteb8b85a12016-06-17 00:06:42 +02001605void log_set_filter_vlr_subscr(struct log_target *target,
1606 struct vlr_subscr *vlr_subscr)
1607{
1608 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001609 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001610
1611 /* free the old data */
1612 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001613 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001614 *fsub = NULL;
1615 }
1616
1617 if (vlr_subscr) {
1618 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001619 vlr_subscr_get(vlr_subscr, use);
1620 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001621 } else
1622 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1623}