blob: 50824698908a800bf228ac521fd4efae10755ebc [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>
Harald Welteb8b85a12016-06-17 00:06:42 +020026#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
27#include <osmocom/gsm/gsup.h>
28#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020029#include <osmocom/gsm/gsm48.h>
Stefan Sperlingafa030d2018-12-06 12:06:59 +010030#include <osmocom/gsm/ipa.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020031#include <osmocom/msc/gsm_subscriber.h>
Harald Welte1ea6baf2018-07-31 19:40:52 +020032#include <osmocom/gsupclient/gsup_client.h>
Harald Welte0df904d2018-12-03 11:00:04 +010033#include <osmocom/msc/vlr_sgs.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020034#include <osmocom/msc/vlr.h>
35#include <osmocom/msc/debug.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010036#include <osmocom/msc/gsup_client_mux.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020037
Harald Welteb8b85a12016-06-17 00:06:42 +020038#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020041#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020042
43#include "vlr_core.h"
44#include "vlr_auth_fsm.h"
45#include "vlr_lu_fsm.h"
46#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010047#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020048
49#define SGSN_SUBSCR_MAX_RETRIES 3
50#define SGSN_SUBSCR_RETRY_INTERVAL 10
51
52/***********************************************************************
53 * Convenience functions
54 ***********************************************************************/
55
56const struct value_string vlr_ciph_names[] = {
57 OSMO_VALUE_STRING(VLR_CIPH_NONE),
58 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
59 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
60 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
61 { 0, NULL }
62};
63
64uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
65{
66 uint32_t tidx = 0xffffffff;
67
68 switch (timer) {
69 case 3270:
70 tidx = VLR_T_3270;
71 break;
72 case 3260:
73 tidx = VLR_T_3260;
74 break;
75 case 3250:
76 tidx = VLR_T_3250;
77 break;
78 }
79
80 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
81 return vlr->cfg.timer[tidx];
82}
83
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010084/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010085const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010086{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010087 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010088 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010089 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010090 if (!vsub)
91 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010092 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010093 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010094 present = true;
95 }
96 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010097 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010098 present = true;
99 }
100 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100101 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100102 present = true;
103 }
104 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100105 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100106 present = true;
107 }
108 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100109 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100110
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100111 return buf;
112}
113
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100114const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen)
115{
116 /* cast away the const so we can shorten the string within the static buffer */
117 char *name = (char*)vlr_subscr_name(vsub);
118 size_t len = strlen(name);
119 if (maxlen < 2)
120 return "-";
121 if (len > maxlen)
122 strcpy(name + maxlen - 2, "..");
123 return name;
124}
125
Oliver Smith5598aae2019-01-08 11:47:21 +0100126const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100127{
128 if (!vsub || !vsub->msisdn[0])
129 return vlr_subscr_name(vsub);
130 return vsub->msisdn;
131}
132
Harald Welteb8b85a12016-06-17 00:06:42 +0200133struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
134 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100135 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200136 const char *file, int line)
137{
138 struct vlr_subscr *vsub;
139
140 if (!imsi || !*imsi)
141 return NULL;
142
143 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100144 if (vlr_subscr_matches_imsi(vsub, imsi)) {
145 vlr_subscr_get_src(vsub, use, file, line);
146 return vsub;
147 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200148 }
149 return NULL;
150}
151
152struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
153 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100154 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200155 const char *file, int line)
156{
157 struct vlr_subscr *vsub;
158
159 if (tmsi == GSM_RESERVED_TMSI)
160 return NULL;
161
162 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100163 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
164 vlr_subscr_get_src(vsub, use, file, line);
165 return vsub;
166 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200167 }
168 return NULL;
169}
170
171struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
172 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100173 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200174 const char *file, int line)
175{
176 struct vlr_subscr *vsub;
177
178 if (!msisdn || !*msisdn)
179 return NULL;
180
181 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100182 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
183 vlr_subscr_get_src(vsub, use, file, line);
184 return vsub;
185 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200186 }
187 return NULL;
188}
189
Harald Welteb8b85a12016-06-17 00:06:42 +0200190/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100191static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200192 struct osmo_gsup_message *gsup_msg)
193{
194 struct vlr_instance *vlr = vsub->vlr;
195
196 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100197 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200198
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100199 gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200200
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100201 return gsup_client_mux_tx(vlr->gcm, gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200202}
203
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100204static 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 +0200205{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100206 struct vlr_subscr *vsub = e->use_count->talloc_object;
207 char buf[128];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100208 int32_t total;
209 int level;
Harald Welteb8b85a12016-06-17 00:06:42 +0200210
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100211 if (!e->use)
212 return -EINVAL;
213
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100214 total = osmo_use_count_total(&vsub->use_count);
215
216 if (total == 0
217 || (total == 1 && old_use_count == 0 && e->count == 1))
218 level = LOGL_INFO;
219 else
220 level = LOGL_DEBUG;
221
222 LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n",
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100223 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
224 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
225
226 if (e->count < 0)
227 return -ERANGE;
228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100229 vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total);
230
231 if (total <= 0)
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100232 vlr_subscr_free(vsub);
233 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200234}
235
236/* Allocate a new subscriber and insert it into list */
237static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
238{
239 struct vlr_subscr *vsub;
240 int i;
241
242 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100243 *vsub = (struct vlr_subscr){
244 .vlr = vlr,
245 .tmsi = GSM_RESERVED_TMSI,
246 .tmsi_new = GSM_RESERVED_TMSI,
247 .use_count = (struct osmo_use_count){
248 .talloc_object = vsub,
249 .use_cb = vlr_subscr_use_cb,
250 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100251 .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100252 };
253 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 +0200254
255 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100256 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200257
258 INIT_LLIST_HEAD(&vsub->cs.requests);
259 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
260
Harald Welte0df904d2018-12-03 11:00:04 +0100261 /* Create an SGs FSM, which is needed to control CSFB,
262 * in cases where CSFB/SGs is not in use, this FSM will
263 * just do nothing. (see also: sgs_iface.c) */
264 vlr_sgs_fsm_create(vsub);
265
Harald Welteb8b85a12016-06-17 00:06:42 +0200266 llist_add_tail(&vsub->list, &vlr->subscribers);
267 return vsub;
268}
269
Harald Welteb8b85a12016-06-17 00:06:42 +0200270/* Send a GSUP Purge MS request.
271 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
272 * this subscriber, not to the HLR? */
273int vlr_subscr_purge(struct vlr_subscr *vsub)
274{
275 struct osmo_gsup_message gsup_msg = {0};
276
277 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
278
279 /* provide HLR number in case we know it */
280 gsup_msg.hlr_enc_len = vsub->hlr.len;
281 gsup_msg.hlr_enc = vsub->hlr.buf;
282
283 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
284}
285
Neels Hofmeyr15809592018-04-06 02:57:51 +0200286void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
287 enum osmo_fsm_term_cause fsm_cause,
288 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200289{
290 if (!vsub)
291 return;
292
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100293 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200294 if (vsub->lu_fsm)
295 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200296 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200297 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100298 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200299}
300
301/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
302void vlr_subscr_free(struct vlr_subscr *vsub)
303{
304 llist_del(&vsub->list);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub),
306 vsub->max_total_use_count);
Harald Welte0df904d2018-12-03 11:00:04 +0100307
308 /* Make sure SGs timer Ts5 is removed */
309 osmo_timer_del(&vsub->sgs.Ts5);
310
311 /* Remove SGs FSM (see also: sgs_iface.c) */
312 vlr_sgs_fsm_remove(vsub);
313
Harald Welteb8b85a12016-06-17 00:06:42 +0200314 talloc_free(vsub);
315}
316
317/* Generate a new TMSI and store in vsub->tmsi_new.
318 * Search all known subscribers to ensure that the TMSI is unique. */
319int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
320{
321 struct vlr_instance *vlr = vsub->vlr;
322 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100323 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100324 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200325
326 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100327 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
328 if (rc < 0) {
329 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
330 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200331 }
332 /* throw the dice again, if the TSMI doesn't fit */
333 if (tmsi == GSM_RESERVED_TMSI)
334 continue;
335
336 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
337 if (vlr->cfg.is_ps) {
338 /* SGSN */
339 tmsi |= 0xC000000;
340 } else {
341 /* MSC */
342 if ((tmsi & 0xC0000000) == 0xC0000000)
343 tmsi &= ~0xC0000000;
344 }
345
346 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100347 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
348 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200349 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100350 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200351
352 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100353 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200354 return 0;
355 }
356
357 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
358 " after %d tries\n", vlr_subscr_name(vsub), tried);
359 return -1;
360}
361
362/* Find subscriber by IMSI, or create new subscriber if not found.
363 * \param[in] vlr VLR instace.
364 * \param[in] imsi IMSI string.
365 * \param[out] created if non-NULL, returns whether a new entry was created. */
366struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
367 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100368 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200369 bool *created,
370 const char *file,
371 int line)
372{
373 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100374 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200375 if (vsub) {
376 if (created)
377 *created = false;
378 return vsub;
379 }
380
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100381 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200382 if (!vsub)
383 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100384 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200385 vlr_subscr_set_imsi(vsub, imsi);
386 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
387 if (created)
388 *created = true;
389 return vsub;
390}
391
392/* Find subscriber by TMSI, or create new subscriber if not found.
393 * \param[in] vlr VLR instace.
394 * \param[in] tmsi TMSI.
395 * \param[out] created if non-NULL, returns whether a new entry was created. */
396struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
397 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100398 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200399 bool *created,
400 const char *file,
401 int line)
402{
403 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100404 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200405 if (vsub) {
406 if (created)
407 *created = false;
408 return vsub;
409 }
410
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100411 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200412 if (!vsub)
413 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100414 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200415 vsub->tmsi = tmsi;
416 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
417 if (created)
418 *created = true;
419 return vsub;
420}
421
422void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
423{
424 if (!vsub)
425 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200426
427 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
428 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
429 imsi, vsub->imsi);
430 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
431 }
432
Harald Welteb8b85a12016-06-17 00:06:42 +0200433 vsub->id = atoll(vsub->imsi);
434 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
435 vsub->imsi, vsub->id);
436}
437
438void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
439{
440 if (!vsub)
441 return;
Max98f74672018-02-05 12:57:06 +0100442 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200443 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
444 vsub->imsi, vsub->imei);
445}
446
447void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
448{
449 if (!vsub)
450 return;
Max98f74672018-02-05 12:57:06 +0100451 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200452 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
453 vsub->imsi, vsub->imeisv);
454}
455
456/* Safely copy the given MSISDN string to vsub->msisdn */
457void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
458{
459 if (!vsub)
460 return;
Max98f74672018-02-05 12:57:06 +0100461 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200462 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
463 vsub->imsi, vsub->msisdn);
464}
465
466bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
467{
468 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
469}
470
471bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
472{
473 return vsub && tmsi != GSM_RESERVED_TMSI
474 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
475}
476
477bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
478{
479 return vsub && msisdn && vsub->msisdn[0]
480 && !strcmp(vsub->msisdn, msisdn);
481}
482
483bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
484{
485 return vsub && imei && vsub->imei[0]
486 && !strcmp(vsub->imei, imei);
487}
488
489/* Send updated subscriber information to HLR */
490int vlr_subscr_changed(struct vlr_subscr *vsub)
491{
492 /* FIXME */
493 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
494 return 0;
495}
496
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200497void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
498{
499 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
500 struct timespec now;
501
502 /* The T3212 timeout value field is coded as the binary representation of the timeout
503 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
504 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
505 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
506 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
507 } else {
508 LOGP(DVLR, LOGL_ERROR,
509 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
510 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
511 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
512 }
513}
514
515void vlr_subscr_expire_lu(void *data)
516{
517 struct vlr_instance *vlr = data;
518 struct vlr_subscr *vsub, *vsub_tmp;
519 struct timespec now;
520
521 if (llist_empty(&vlr->subscribers))
522 goto done;
523
524 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
525 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
526 goto done;
527 }
528
529 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
530 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
531 continue;
532
533 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
534 vlr_subscr_rx_imsi_detach(vsub);
535 }
536
537done:
538 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
539}
540
Harald Welteb8b85a12016-06-17 00:06:42 +0200541/***********************************************************************
542 * PDP context data
543 ***********************************************************************/
544
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200545#define GSM_APN_LENGTH 102
546
547/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
548/* see GSM 09.02, B.1, gprsSubscriptionData */
549struct sgsn_subscriber_pdp_data {
550 struct llist_head list;
551
552 unsigned int context_id;
553 uint16_t pdp_type;
554 char apn_str[GSM_APN_LENGTH];
555 uint8_t qos_subscribed[20];
556 size_t qos_subscribed_len;
557};
558
Harald Welteb8b85a12016-06-17 00:06:42 +0200559struct sgsn_subscriber_pdp_data *
560vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
561{
562 struct sgsn_subscriber_pdp_data* pdata;
563
564 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
565
566 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
567
568 return pdata;
569}
570
571static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
572{
573 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
574 int count = 0;
575
576 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
577 llist_del(&pdp->list);
578 talloc_free(pdp);
579 count += 1;
580 }
581
582 return count;
583}
584
585static struct sgsn_subscriber_pdp_data *
586vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
587{
588 struct sgsn_subscriber_pdp_data *pdp;
589
590 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
591 if (pdp->context_id == context_id)
592 return pdp;
593 }
594
595 return NULL;
596}
597
598/***********************************************************************
599 * Actual Implementation
600 ***********************************************************************/
601
602static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100603 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200604{
605 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200606 LOGP(DVLR, LOGL_NOTICE,
607 "Unknown IMSI %s, discarding GSUP request "
608 "of type 0x%02x\n",
609 gsup_msg->imsi, gsup_msg->message_type);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100610 gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
Harald Welteb8b85a12016-06-17 00:06:42 +0200611 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
612 LOGP(DVLR, LOGL_NOTICE,
613 "Unknown IMSI %s, discarding GSUP error "
614 "of type 0x%02x, cause '%s' (%d)\n",
615 gsup_msg->imsi, gsup_msg->message_type,
616 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
617 gsup_msg->cause);
618 } else {
619 LOGP(DVLR, LOGL_NOTICE,
620 "Unknown IMSI %s, discarding GSUP response "
621 "of type 0x%02x\n",
622 gsup_msg->imsi, gsup_msg->message_type);
623 }
624
625 return -GMM_CAUSE_IMSI_UNKNOWN;
626}
627
628static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100629 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200630{
631 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
632 LOGGSUPP(LOGL_NOTICE, gsup_msg,
633 "Purge MS has failed with cause '%s' (%d)\n",
634 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
635 gsup_msg->cause);
636 return -gsup_msg->cause;
637 }
638 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
639 return 0;
640}
641
642/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100643int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200644{
645 struct osmo_gsup_message gsup_msg = {0};
646 int rc;
647
648 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200649 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 +0200650 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
651
652 return rc;
653}
654
655/* VLR internal call to request tuples from HLR */
656int vlr_subscr_req_sai(struct vlr_subscr *vsub,
657 const uint8_t *auts, const uint8_t *auts_rand)
658{
659 struct osmo_gsup_message gsup_msg = {0};
660
661 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
662 gsup_msg.auts = auts;
663 gsup_msg.rand = auts_rand;
664
665 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
666}
667
Oliver Smith7d053092018-12-14 17:37:38 +0100668/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
669int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
670{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100671 struct osmo_gsup_message gsup_msg = {
672 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
673 };
Oliver Smith7d053092018-12-14 17:37:38 +0100674 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
675 int len;
676
677 /* Encode IMEI */
678 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
679 if (len < 1) {
680 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
681 return -ENOSPC;
682 }
683 gsup_msg.imei_enc = imei_enc;
684 gsup_msg.imei_enc_len = len;
685
686 /* Send CHECK_IMEI_REQUEST */
687 gsup_msg.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST;
688 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100689 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Oliver Smith7d053092018-12-14 17:37:38 +0100690}
691
Harald Welteb8b85a12016-06-17 00:06:42 +0200692/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100693int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200694{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100695 struct osmo_gsup_message gsup_msg = {
696 .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT,
697 };
Harald Welteb8b85a12016-06-17 00:06:42 +0200698
699 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100700 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100701 return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200702}
703
704/* Update the subscriber with GSUP-received auth tuples */
705void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
706 const struct osmo_gsup_message *gsup)
707{
708 unsigned int i;
709 unsigned int got_tuples;
710
711 if (gsup->num_auth_vectors) {
712 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
713 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100714 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200715 }
716
717 got_tuples = 0;
718 for (i = 0; i < gsup->num_auth_vectors; i++) {
719 size_t key_seq = i;
720
721 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
722 LOGVSUBP(LOGL_NOTICE, vsub,
723 "Skipping auth tuple wih invalid cksn %zu\n",
724 key_seq);
725 continue;
726 }
727 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
728 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100729 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200730 }
731
732 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
733
734 if (!got_tuples) {
735 /* FIXME what now? */
736 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
737 }
738
739 /* New tuples means last_tuple becomes invalid */
740 vsub->last_tuple = NULL;
741}
742
743/* Handle SendAuthInfo Result/Error from HLR */
744static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
745 const struct osmo_gsup_message *gsup)
746{
747 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
748 void *data = (void *) gsup;
749
750 switch (gsup->message_type) {
751 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
752 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
753 break;
754 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
755 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
756 break;
757 default:
758 return -1;
759 }
760
761 return 0;
762}
763
Harald Welteb8b85a12016-06-17 00:06:42 +0200764static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
765 const struct osmo_gsup_message *gsup_msg)
766{
767 unsigned idx;
768 int rc;
769
Maxa263bb22017-12-27 13:23:44 +0100770 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100771 gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn),
772 gsup_msg->msisdn_enc,
773 gsup_msg->msisdn_enc_len, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200774 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
775 vsub->imsi, vsub->msisdn);
776 }
777
778 if (gsup_msg->hlr_enc) {
779 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
780 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
781 gsup_msg->hlr_enc_len);
782 vsub->hlr.len = 0;
783 } else {
784 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
785 gsup_msg->hlr_enc_len);
786 vsub->hlr.len = gsup_msg->hlr_enc_len;
787 }
788 }
789
790 if (gsup_msg->pdp_info_compl) {
791 rc = vlr_subscr_pdp_data_clear(vsub);
792 if (rc > 0)
793 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
794 }
795
796 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
797 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
798 size_t ctx_id = pdp_info->context_id;
799 struct sgsn_subscriber_pdp_data *pdp_data;
800
801 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
802 LOGVSUBP(LOGL_ERROR, vsub,
803 "APN too long, context id = %zu, APN = %s\n",
804 ctx_id, osmo_hexdump(pdp_info->apn_enc,
805 pdp_info->apn_enc_len));
806 continue;
807 }
808
809 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
810 LOGVSUBP(LOGL_ERROR, vsub,
811 "QoS info too long (%zu)\n",
812 pdp_info->qos_enc_len);
813 continue;
814 }
815
816 LOGVSUBP(LOGL_INFO, vsub,
817 "Will set PDP info, context id = %zu, APN = %s\n",
818 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
819
820 /* Set PDP info [ctx_id] */
821 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
822 if (!pdp_data) {
823 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
824 pdp_data->context_id = ctx_id;
825 }
826
827 OSMO_ASSERT(pdp_data != NULL);
828 pdp_data->pdp_type = pdp_info->pdp_type;
829 osmo_apn_to_str(pdp_data->apn_str,
830 pdp_info->apn_enc, pdp_info->apn_enc_len);
831 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
832 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
833 }
834}
835
836
837/* Handle InsertSubscrData Result from HLR */
838static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
839 const struct osmo_gsup_message *gsup)
840{
841 struct osmo_gsup_message gsup_reply = {0};
842
843 vlr_subscr_gsup_insert_data(vsub, gsup);
844 vsub->vlr->ops.subscr_update(vsub);
845
846 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
847 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
848}
849
850/* Handle UpdateLocation Result from HLR */
851static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
852 const struct osmo_gsup_message *gsup)
853{
Harald Welte0df904d2018-12-03 11:00:04 +0100854 struct sgs_lu_response sgs_lu_response;
855 bool sgs_lu_in_progress = false;
856
857 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
858 sgs_lu_in_progress = true;
859
860 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200861 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
862 "without LU in progress\n");
863 return -ENODEV;
864 }
865
866 /* contrary to MAP, we allow piggy-backing subscriber data onto the
867 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
868 * nested INSERT SUBSCRIBER DATA transaction */
869 vlr_subscr_gsup_insert_data(vsub, gsup);
870
Harald Welte0df904d2018-12-03 11:00:04 +0100871 if (sgs_lu_in_progress) {
872 sgs_lu_response.accepted = true;
873 sgs_lu_response.vsub = vsub;
874 vsub->sgs.response_cb(&sgs_lu_response);
875 } else
876 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200877
878 return 0;
879}
880
881/* Handle UpdateLocation Result from HLR */
882static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
883 const struct osmo_gsup_message *gsup)
884{
Harald Welte0df904d2018-12-03 11:00:04 +0100885 struct sgs_lu_response sgs_lu_response;
886 bool sgs_lu_in_progress = false;
887
888 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
889 sgs_lu_in_progress = true;
890
891 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200892 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
893 "without LU in progress\n");
894 return -ENODEV;
895 }
896
897 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
898 get_value_string(gsm48_gmm_cause_names, gsup->cause));
899
Harald Welte0df904d2018-12-03 11:00:04 +0100900 if (sgs_lu_in_progress) {
901 sgs_lu_response.accepted = false;
902 sgs_lu_response.vsub = vsub;
903 vsub->sgs.response_cb(&sgs_lu_response);
904 } else
905 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
906 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200907 return 0;
908}
909
Neels Hofmeyr15809592018-04-06 02:57:51 +0200910static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
911 enum osmo_fsm_term_cause *fsm_cause_p,
912 enum gsm48_reject_value *gsm48_rej_p)
913{
914 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
915 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
916 switch (gmm_cause) {
917 case GMM_CAUSE_IMSI_UNKNOWN:
918 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
919 break;
920 case GMM_CAUSE_ILLEGAL_MS:
921 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
922 break;
923 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
924 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
925 break;
926 case GMM_CAUSE_ILLEGAL_ME:
927 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
928 break;
929 case GMM_CAUSE_GPRS_NOTALLOWED:
930 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
931 break;
932 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
933 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
934 break;
935 case GMM_CAUSE_MS_ID_NOT_DERIVED:
936 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
937 break;
938 case GMM_CAUSE_IMPL_DETACHED:
939 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
940 break;
941 case GMM_CAUSE_PLMN_NOTALLOWED:
942 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
943 break;
944 case GMM_CAUSE_LA_NOTALLOWED:
945 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
946 break;
947 case GMM_CAUSE_ROAMING_NOTALLOWED:
948 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
949 break;
950 case GMM_CAUSE_NO_GPRS_PLMN:
951 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
952 break;
953 case GMM_CAUSE_MSC_TEMP_NOTREACH:
954 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
955 break;
956 case GMM_CAUSE_SYNC_FAIL:
957 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
958 break;
959 case GMM_CAUSE_CONGESTION:
960 gsm48_rej = GSM48_REJECT_CONGESTION;
961 break;
962 case GMM_CAUSE_SEM_INCORR_MSG:
963 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
964 break;
965 case GMM_CAUSE_INV_MAND_INFO:
966 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
967 break;
968 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
969 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
970 break;
971 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
972 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
973 break;
974 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
975 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
976 break;
977 case GMM_CAUSE_COND_IE_ERR:
978 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
979 break;
980 case GMM_CAUSE_MSG_INCOMP_P_STATE:
981 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
982 break;
983 case GMM_CAUSE_PROTO_ERR_UNSPEC:
984 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
985 break;
986
987 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
988 case GMM_CAUSE_MAC_FAIL:
989 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
990 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
991 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
992 case GMM_CAUSE_NO_PDP_ACTIVATED:
993 case GMM_CAUSE_NET_FAIL:
994 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
995 break;
996 }
997 switch (gmm_cause) {
998 /* refine any error causes here? */
999 default:
1000 fsm_cause = OSMO_FSM_TERM_ERROR;
1001 break;
1002 }
1003 if (fsm_cause_p)
1004 *fsm_cause_p = fsm_cause;
1005 if (gsm48_rej_p)
1006 *gsm48_rej_p = gsm48_rej;
1007}
1008
Harald Welteb8b85a12016-06-17 00:06:42 +02001009/* Handle LOCATION CANCEL request from HLR */
1010static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001011 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001012{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001013 enum gsm48_reject_value gsm48_rej;
1014 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +02001015 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001016 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001017 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1018
1019 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1020 is_update_procedure ?
1021 "update procedure" : "subscription withdraw");
1022
1023 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001024 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001025
Neels Hofmeyr15809592018-04-06 02:57:51 +02001026 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
1027 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001028
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001029 vlr_subscr_rx_imsi_detach(vsub);
1030
Max770fbd22018-01-24 12:48:33 +01001031 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001032}
1033
Oliver Smith7d053092018-12-14 17:37:38 +01001034/* Handle Check_IMEI_VLR result and error from HLR */
1035static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1036{
1037 if (!vsub->lu_fsm) {
1038 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1039 osmo_gsup_message_type_name(gsup->message_type));
1040 return -ENODEV;
1041 }
1042
1043 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1044 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1045 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1046 else
1047 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1048 } else {
1049 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1050 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1051 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1052 }
1053
1054 return 0;
1055}
1056
Harald Welteb8b85a12016-06-17 00:06:42 +02001057/* Incoming handler for GSUP from HLR.
1058 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001059int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup)
Harald Welteb8b85a12016-06-17 00:06:42 +02001060{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001061 struct vlr_instance *vlr = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001062 struct vlr_subscr *vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +02001063 int rc;
1064
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001065 vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001066 if (!vsub) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001067 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001068 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1069 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001070 return vlr_rx_gsup_purge_no_subscr(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001071 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001072 return vlr_rx_gsup_unknown_imsi(vlr, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001073 }
1074 }
1075
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 switch (gsup->message_type) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001077 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1078 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001079 rc = vlr_subscr_handle_sai_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001080 break;
1081 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001082 rc = vlr_subscr_handle_isd_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001083 break;
1084 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001085 rc = vlr_subscr_handle_cancel_req(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001086 break;
1087 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001088 rc = vlr_subscr_handle_lu_res(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001089 break;
1090 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001091 rc = vlr_subscr_handle_lu_err(vsub, gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001092 break;
1093 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1094 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1095 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1096 LOGVSUBP(LOGL_ERROR, vsub,
1097 "Rx GSUP msg_type=%d not yet implemented\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001098 gsup->message_type);
Harald Welteb8b85a12016-06-17 00:06:42 +02001099 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1100 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001101 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1102 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001103 rc = vlr_subscr_handle_check_imei(vsub, gsup);
Oliver Smith7d053092018-12-14 17:37:38 +01001104 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001105 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001106 LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type);
1107 rc = -EINVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +02001108 break;
1109 }
1110
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001111 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001112 return rc;
1113}
1114
1115/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1116int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1117 const uint8_t *mi, size_t mi_len)
1118{
1119 char mi_string[GSM48_MI_SIZE];
1120 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1121
1122 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1123
1124 /* update the vlr_subscr with the given identity */
1125 switch (mi_type) {
1126 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001127 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1128 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1129 sizeof(vsub->imsi) - 1, mi_string);
1130 return -ENOSPC; /* ignore message; do not avance LU FSM */
1131 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001132 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1133 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1134 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001135 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001136 } else
1137 vlr_subscr_set_imsi(vsub, mi_string);
1138 break;
1139 case GSM_MI_TYPE_IMEI:
1140 vlr_subscr_set_imei(vsub, mi_string);
1141 break;
1142 case GSM_MI_TYPE_IMEISV:
1143 vlr_subscr_set_imeisv(vsub, mi_string);
1144 break;
1145 }
1146
1147 if (vsub->auth_fsm) {
1148 switch (mi_type) {
1149 case GSM_MI_TYPE_IMSI:
1150 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1151 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1152 break;
1153 }
1154 }
1155
1156 if (vsub->lu_fsm) {
1157 uint32_t event = 0;
1158 switch (mi_type) {
1159 case GSM_MI_TYPE_IMSI:
1160 event = VLR_ULA_E_ID_IMSI;
1161 break;
1162 case GSM_MI_TYPE_IMEI:
1163 event = VLR_ULA_E_ID_IMEI;
1164 break;
1165 case GSM_MI_TYPE_IMEISV:
1166 event = VLR_ULA_E_ID_IMEISV;
1167 break;
1168 default:
1169 OSMO_ASSERT(0);
1170 break;
1171 }
1172 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1173 } else {
1174 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1175 }
1176
1177 return 0;
1178}
1179
1180/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1181int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1182{
1183 if (vsub->lu_fsm) {
1184 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1185 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1186 } else if (vsub->proc_arq_fsm) {
1187 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1188 PR_ARQ_E_TMSI_ACK, NULL);
1189 } else {
1190 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001191 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001192 return -EINVAL;
1193 }
1194}
1195
Maxdcc193d2017-12-27 19:34:15 +01001196bool vlr_subscr_expire(struct vlr_subscr *vsub)
1197{
1198 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001199 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001200 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001201 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001202
1203 return true;
1204 }
1205
1206 return false;
1207}
1208
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001209/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001210int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1211{
1212 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001213 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001214
1215 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001216 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001217
Harald Welte0df904d2018-12-03 11:00:04 +01001218 /* Inform the UE-SGs FSM that the subscriber has been detached */
1219 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1220
Maxdcc193d2017-12-27 19:34:15 +01001221 vlr_subscr_expire(vsub);
1222
Harald Welteb8b85a12016-06-17 00:06:42 +02001223 return 0;
1224}
1225
1226/* Tear down any running FSMs due to MSC connection timeout.
1227 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1228 * message before the entire connection is torn down.
1229 * \param[in] vsub subscriber to tear down
1230 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001231void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001232{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001233 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001234}
1235
1236struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1237{
1238 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1239 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001240
1241 /* Some of these are needed only on UTRAN, but in case the caller wants
1242 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001243 OSMO_ASSERT(ops->tx_auth_req);
1244 OSMO_ASSERT(ops->tx_auth_rej);
1245 OSMO_ASSERT(ops->tx_id_req);
1246 OSMO_ASSERT(ops->tx_lu_acc);
1247 OSMO_ASSERT(ops->tx_lu_rej);
1248 OSMO_ASSERT(ops->tx_cm_serv_acc);
1249 OSMO_ASSERT(ops->tx_cm_serv_rej);
1250 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001251 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001252 OSMO_ASSERT(ops->subscr_update);
1253 OSMO_ASSERT(ops->subscr_assoc);
1254
1255 INIT_LLIST_HEAD(&vlr->subscribers);
1256 INIT_LLIST_HEAD(&vlr->operations);
1257 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1258
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001259 /* defaults */
1260 vlr->cfg.assign_tmsi = true;
1261
Harald Welteb8b85a12016-06-17 00:06:42 +02001262 /* osmo_auth_fsm.c */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001263 OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001264 /* osmo_lu_fsm.c */
1265 vlr_lu_fsm_init();
1266 /* vlr_access_request_fsm.c */
1267 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001268 /* vlr_sgs_fsm.c */
1269 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001270
1271 return vlr;
1272}
1273
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001274int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm)
Harald Welteb8b85a12016-06-17 00:06:42 +02001275{
1276 OSMO_ASSERT(vlr);
1277
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001278 vlr->gcm = gcm;
1279 gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){
1280 .func = vlr_gsup_rx,
1281 .data = vlr,
1282 };
Harald Welteb8b85a12016-06-17 00:06:42 +02001283
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001284 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1285 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001286 return 0;
1287}
1288
1289/* MSC->VLR: Subscribre has disconnected */
1290int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1291{
1292 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1293 * interface */
1294 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1295 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1296 vsub->msc_conn_ref = NULL;
1297
1298 return 0;
1299}
1300
1301/* MSC->VLR: Receive Authentication Failure from Subscriber */
1302int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1303{
1304 struct vlr_auth_resp_par par = {0};
1305 par.auts = auts;
1306
1307 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1308 return 0;
1309}
1310
1311/* MSC->VLR: Receive Authentication Response from MS
1312 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1313int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1314 bool is_utran, const uint8_t *res, uint8_t res_len)
1315{
1316 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1317 struct vlr_auth_resp_par par;
1318
1319 par.is_r99 = is_r99;
1320 par.is_utran = is_utran;
1321 par.res = res;
1322 par.res_len = res_len;
1323 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1324
1325 return 0;
1326}
1327
1328/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001329void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result)
Harald Welteb8b85a12016-06-17 00:06:42 +02001330{
1331 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001332 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001333 if (vsub->proc_arq_fsm
1334 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001335 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001336}
1337
1338/* Internal evaluation of requested ciphering mode.
1339 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1340 * \param[in] vlr VLR instance.
1341 * \param[in] fi Calling FSM instance, for logging.
1342 * \param[in] msc_conn_ref MSC conn to send to.
1343 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1344 * \returns 0 if no ciphering is needed or message was sent successfully,
1345 * or a negative value if ciph_mode is invalid or sending failed.
1346 */
1347int vlr_set_ciph_mode(struct vlr_instance *vlr,
1348 struct osmo_fsm_inst *fi,
1349 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001350 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001351 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001352 bool retrieve_imeisv)
1353{
Harald Welte71c51df2017-12-23 18:51:48 +01001354 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001355 return 0;
1356
Harald Welte71c51df2017-12-23 18:51:48 +01001357 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1358 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001359}
1360
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001361/* Decide whether UMTS AKA should be used.
1362 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1363 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1364 * GERAN are R99 capable and UMTS AKA tokens are available.
1365 * \param[in] vec Auth tokens (received from the HLR).
1366 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1367 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1368 */
1369bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1370{
1371 if (!is_r99)
1372 return false;
1373 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1374 return false;
1375 return true;
1376}
1377
Harald Welteb8b85a12016-06-17 00:06:42 +02001378void log_set_filter_vlr_subscr(struct log_target *target,
1379 struct vlr_subscr *vlr_subscr)
1380{
1381 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001382 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001383
1384 /* free the old data */
1385 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001386 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001387 *fsub = NULL;
1388 }
1389
1390 if (vlr_subscr) {
1391 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001392 vlr_subscr_get(vlr_subscr, use);
1393 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001394 } else
1395 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1396}