blob: c6d8805d664e7ac5d311e526c834d13886b451fb [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>
Harald Welteb8b85a12016-06-17 00:06:42 +020036
Harald Welteb8b85a12016-06-17 00:06:42 +020037#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020040#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020041
42#include "vlr_core.h"
43#include "vlr_auth_fsm.h"
44#include "vlr_lu_fsm.h"
45#include "vlr_access_req_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010046#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020047
48#define SGSN_SUBSCR_MAX_RETRIES 3
49#define SGSN_SUBSCR_RETRY_INTERVAL 10
50
51/***********************************************************************
52 * Convenience functions
53 ***********************************************************************/
54
55const struct value_string vlr_ciph_names[] = {
56 OSMO_VALUE_STRING(VLR_CIPH_NONE),
57 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
58 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
59 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
60 { 0, NULL }
61};
62
63uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
64{
65 uint32_t tidx = 0xffffffff;
66
67 switch (timer) {
68 case 3270:
69 tidx = VLR_T_3270;
70 break;
71 case 3260:
72 tidx = VLR_T_3260;
73 break;
74 case 3250:
75 tidx = VLR_T_3250;
76 break;
77 }
78
79 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
80 return vlr->cfg.timer[tidx];
81}
82
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010083/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010084const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010085{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010086 static char buf[128];
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010087 struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) };
Neels Hofmeyr361e5712019-01-03 02:32:14 +010088 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010089 if (!vsub)
90 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010091 if (vsub->imsi[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010092 OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010093 present = true;
94 }
95 if (vsub->msisdn[0]) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +010096 OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
Neels Hofmeyr361e5712019-01-03 02:32:14 +010097 present = true;
98 }
99 if (vsub->tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100100 OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100101 present = true;
102 }
103 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr83e311f2019-03-19 16:39:10 +0100104 OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100105 present = true;
106 }
107 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100108 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100109
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100110 return buf;
111}
112
Oliver Smith5598aae2019-01-08 11:47:21 +0100113const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100114{
115 if (!vsub || !vsub->msisdn[0])
116 return vlr_subscr_name(vsub);
117 return vsub->msisdn;
118}
119
Harald Welteb8b85a12016-06-17 00:06:42 +0200120struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
121 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100122 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200123 const char *file, int line)
124{
125 struct vlr_subscr *vsub;
126
127 if (!imsi || !*imsi)
128 return NULL;
129
130 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100131 if (vlr_subscr_matches_imsi(vsub, imsi)) {
132 vlr_subscr_get_src(vsub, use, file, line);
133 return vsub;
134 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200135 }
136 return NULL;
137}
138
139struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
140 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100141 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200142 const char *file, int line)
143{
144 struct vlr_subscr *vsub;
145
146 if (tmsi == GSM_RESERVED_TMSI)
147 return NULL;
148
149 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100150 if (vlr_subscr_matches_tmsi(vsub, tmsi)) {
151 vlr_subscr_get_src(vsub, use, file, line);
152 return vsub;
153 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200154 }
155 return NULL;
156}
157
158struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
159 const char *msisdn,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100160 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200161 const char *file, int line)
162{
163 struct vlr_subscr *vsub;
164
165 if (!msisdn || !*msisdn)
166 return NULL;
167
168 llist_for_each_entry(vsub, &vlr->subscribers, list) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100169 if (vlr_subscr_matches_msisdn(vsub, msisdn)) {
170 vlr_subscr_get_src(vsub, use, file, line);
171 return vsub;
172 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200173 }
174 return NULL;
175}
176
177/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100178static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
179 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200180{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200181 struct msgb *msg = osmo_gsup_client_msgb_alloc();
Harald Welteb8b85a12016-06-17 00:06:42 +0200182
Max770fbd22018-01-24 12:48:33 +0100183 int rc = osmo_gsup_encode(msg, gsup_msg);
184 if (rc < 0) {
185 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
186 return rc;
187 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200188
189 if (!vlr->gsup_client) {
190 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
191 "send GSUP: %s\n", msgb_hexdump(msg));
192 msgb_free(msg);
193 return -ENOTSUP;
194 }
195
196 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
197 osmo_hexdump_nospc(msg->data, msg->len));
198
Harald Welte1ea6baf2018-07-31 19:40:52 +0200199 return osmo_gsup_client_send(vlr->gsup_client, msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200200}
201
202/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100203static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200204 struct osmo_gsup_message *gsup_msg)
205{
206 struct vlr_instance *vlr = vsub->vlr;
207
208 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100209 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200210
211 return vlr_tx_gsup_message(vlr, gsup_msg);
212}
213
214/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100215static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200216 struct osmo_gsup_message *gsup_orig,
217 enum gsm48_gmm_cause cause)
218{
219 struct osmo_gsup_message gsup_reply = {0};
220
Max98f74672018-02-05 12:57:06 +0100221 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200222 gsup_reply.cause = cause;
223 gsup_reply.message_type =
224 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
225
226 return vlr_tx_gsup_message(vlr, &gsup_reply);
227}
228
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100229static 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 +0200230{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100231 struct vlr_subscr *vsub = e->use_count->talloc_object;
232 char buf[128];
Harald Welteb8b85a12016-06-17 00:06:42 +0200233
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100234 if (!e->use)
235 return -EINVAL;
236
237 LOGPSRC(DREF, LOGL_DEBUG, file, line, "VLR subscr %s %s %s: now used by %s\n",
238 vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use,
239 osmo_use_count_name_buf(buf, sizeof(buf), e->use_count));
240
241 if (e->count < 0)
242 return -ERANGE;
243
244 if (osmo_use_count_total(e->use_count) <= 0)
245 vlr_subscr_free(vsub);
246 return 0;
Harald Welteb8b85a12016-06-17 00:06:42 +0200247}
248
249/* Allocate a new subscriber and insert it into list */
250static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
251{
252 struct vlr_subscr *vsub;
253 int i;
254
255 vsub = talloc_zero(vlr, struct vlr_subscr);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100256 *vsub = (struct vlr_subscr){
257 .vlr = vlr,
258 .tmsi = GSM_RESERVED_TMSI,
259 .tmsi_new = GSM_RESERVED_TMSI,
260 .use_count = (struct osmo_use_count){
261 .talloc_object = vsub,
262 .use_cb = vlr_subscr_use_cb,
263 },
264 };
265 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 +0200266
267 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100268 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200269
270 INIT_LLIST_HEAD(&vsub->cs.requests);
271 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
272
Harald Welte0df904d2018-12-03 11:00:04 +0100273 /* Create an SGs FSM, which is needed to control CSFB,
274 * in cases where CSFB/SGs is not in use, this FSM will
275 * just do nothing. (see also: sgs_iface.c) */
276 vlr_sgs_fsm_create(vsub);
277
Harald Welteb8b85a12016-06-17 00:06:42 +0200278 llist_add_tail(&vsub->list, &vlr->subscribers);
279 return vsub;
280}
281
Harald Welteb8b85a12016-06-17 00:06:42 +0200282/* Send a GSUP Purge MS request.
283 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
284 * this subscriber, not to the HLR? */
285int vlr_subscr_purge(struct vlr_subscr *vsub)
286{
287 struct osmo_gsup_message gsup_msg = {0};
288
289 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
290
291 /* provide HLR number in case we know it */
292 gsup_msg.hlr_enc_len = vsub->hlr.len;
293 gsup_msg.hlr_enc = vsub->hlr.buf;
294
295 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
296}
297
Neels Hofmeyr15809592018-04-06 02:57:51 +0200298void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
299 enum osmo_fsm_term_cause fsm_cause,
300 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200301{
302 if (!vsub)
303 return;
304
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100305 vlr_subscr_get(vsub, __func__);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200306 if (vsub->lu_fsm)
307 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200308 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200309 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100310 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200311}
312
313/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
314void vlr_subscr_free(struct vlr_subscr *vsub)
315{
316 llist_del(&vsub->list);
317 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
Harald Welte0df904d2018-12-03 11:00:04 +0100318
319 /* Make sure SGs timer Ts5 is removed */
320 osmo_timer_del(&vsub->sgs.Ts5);
321
322 /* Remove SGs FSM (see also: sgs_iface.c) */
323 vlr_sgs_fsm_remove(vsub);
324
Harald Welteb8b85a12016-06-17 00:06:42 +0200325 talloc_free(vsub);
326}
327
328/* Generate a new TMSI and store in vsub->tmsi_new.
329 * Search all known subscribers to ensure that the TMSI is unique. */
330int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
331{
332 struct vlr_instance *vlr = vsub->vlr;
333 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100334 int tried, rc;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100335 struct vlr_subscr *other_vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200336
337 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100338 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
339 if (rc < 0) {
340 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
341 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200342 }
343 /* throw the dice again, if the TSMI doesn't fit */
344 if (tmsi == GSM_RESERVED_TMSI)
345 continue;
346
347 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
348 if (vlr->cfg.is_ps) {
349 /* SGSN */
350 tmsi |= 0xC000000;
351 } else {
352 /* MSC */
353 if ((tmsi & 0xC0000000) == 0xC0000000)
354 tmsi &= ~0xC0000000;
355 }
356
357 /* If this TMSI is already in use, try another one. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100358 if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) {
359 vlr_subscr_put(other_vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200360 continue;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100361 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200362
363 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100364 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200365 return 0;
366 }
367
368 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
369 " after %d tries\n", vlr_subscr_name(vsub), tried);
370 return -1;
371}
372
373/* Find subscriber by IMSI, or create new subscriber if not found.
374 * \param[in] vlr VLR instace.
375 * \param[in] imsi IMSI string.
376 * \param[out] created if non-NULL, returns whether a new entry was created. */
377struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
378 const char *imsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100379 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200380 bool *created,
381 const char *file,
382 int line)
383{
384 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100385 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200386 if (vsub) {
387 if (created)
388 *created = false;
389 return vsub;
390 }
391
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100392 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200393 if (!vsub)
394 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100395 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200396 vlr_subscr_set_imsi(vsub, imsi);
397 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
398 if (created)
399 *created = true;
400 return vsub;
401}
402
403/* Find subscriber by TMSI, or create new subscriber if not found.
404 * \param[in] vlr VLR instace.
405 * \param[in] tmsi TMSI.
406 * \param[out] created if non-NULL, returns whether a new entry was created. */
407struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
408 uint32_t tmsi,
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100409 const char *use,
Harald Welteb8b85a12016-06-17 00:06:42 +0200410 bool *created,
411 const char *file,
412 int line)
413{
414 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100415 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200416 if (vsub) {
417 if (created)
418 *created = false;
419 return vsub;
420 }
421
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100422 vsub = _vlr_subscr_alloc(vlr);
Harald Welteb8b85a12016-06-17 00:06:42 +0200423 if (!vsub)
424 return NULL;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100425 vlr_subscr_get_src(vsub, use, file, line);
Harald Welteb8b85a12016-06-17 00:06:42 +0200426 vsub->tmsi = tmsi;
427 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
428 if (created)
429 *created = true;
430 return vsub;
431}
432
433void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
434{
435 if (!vsub)
436 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200437
438 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
439 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
440 imsi, vsub->imsi);
441 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
442 }
443
Harald Welteb8b85a12016-06-17 00:06:42 +0200444 vsub->id = atoll(vsub->imsi);
445 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
446 vsub->imsi, vsub->id);
447}
448
449void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
450{
451 if (!vsub)
452 return;
Max98f74672018-02-05 12:57:06 +0100453 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200454 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
455 vsub->imsi, vsub->imei);
456}
457
458void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
459{
460 if (!vsub)
461 return;
Max98f74672018-02-05 12:57:06 +0100462 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200463 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
464 vsub->imsi, vsub->imeisv);
465}
466
467/* Safely copy the given MSISDN string to vsub->msisdn */
468void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
469{
470 if (!vsub)
471 return;
Max98f74672018-02-05 12:57:06 +0100472 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200473 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
474 vsub->imsi, vsub->msisdn);
475}
476
477bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
478{
479 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
480}
481
482bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
483{
484 return vsub && tmsi != GSM_RESERVED_TMSI
485 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
486}
487
488bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
489{
490 return vsub && msisdn && vsub->msisdn[0]
491 && !strcmp(vsub->msisdn, msisdn);
492}
493
494bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
495{
496 return vsub && imei && vsub->imei[0]
497 && !strcmp(vsub->imei, imei);
498}
499
500/* Send updated subscriber information to HLR */
501int vlr_subscr_changed(struct vlr_subscr *vsub)
502{
503 /* FIXME */
504 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
505 return 0;
506}
507
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200508void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
509{
510 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
511 struct timespec now;
512
513 /* The T3212 timeout value field is coded as the binary representation of the timeout
514 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
515 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
516 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
517 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
518 } else {
519 LOGP(DVLR, LOGL_ERROR,
520 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
521 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
522 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
523 }
524}
525
526void vlr_subscr_expire_lu(void *data)
527{
528 struct vlr_instance *vlr = data;
529 struct vlr_subscr *vsub, *vsub_tmp;
530 struct timespec now;
531
532 if (llist_empty(&vlr->subscribers))
533 goto done;
534
535 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
536 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
537 goto done;
538 }
539
540 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
541 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
542 continue;
543
544 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
545 vlr_subscr_rx_imsi_detach(vsub);
546 }
547
548done:
549 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
550}
551
Harald Welteb8b85a12016-06-17 00:06:42 +0200552/***********************************************************************
553 * PDP context data
554 ***********************************************************************/
555
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200556#define GSM_APN_LENGTH 102
557
558/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
559/* see GSM 09.02, B.1, gprsSubscriptionData */
560struct sgsn_subscriber_pdp_data {
561 struct llist_head list;
562
563 unsigned int context_id;
564 uint16_t pdp_type;
565 char apn_str[GSM_APN_LENGTH];
566 uint8_t qos_subscribed[20];
567 size_t qos_subscribed_len;
568};
569
Harald Welteb8b85a12016-06-17 00:06:42 +0200570struct sgsn_subscriber_pdp_data *
571vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
572{
573 struct sgsn_subscriber_pdp_data* pdata;
574
575 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
576
577 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
578
579 return pdata;
580}
581
582static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
583{
584 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
585 int count = 0;
586
587 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
588 llist_del(&pdp->list);
589 talloc_free(pdp);
590 count += 1;
591 }
592
593 return count;
594}
595
596static struct sgsn_subscriber_pdp_data *
597vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
598{
599 struct sgsn_subscriber_pdp_data *pdp;
600
601 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
602 if (pdp->context_id == context_id)
603 return pdp;
604 }
605
606 return NULL;
607}
608
609/***********************************************************************
610 * Actual Implementation
611 ***********************************************************************/
612
613static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
614 struct osmo_gsup_message *gsup_msg)
615{
616 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Max770fbd22018-01-24 12:48:33 +0100617 int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
618 if (rc < 0)
619 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi);
620
Harald Welteb8b85a12016-06-17 00:06:42 +0200621 LOGP(DVLR, LOGL_NOTICE,
622 "Unknown IMSI %s, discarding GSUP request "
623 "of type 0x%02x\n",
624 gsup_msg->imsi, gsup_msg->message_type);
625 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
626 LOGP(DVLR, LOGL_NOTICE,
627 "Unknown IMSI %s, discarding GSUP error "
628 "of type 0x%02x, cause '%s' (%d)\n",
629 gsup_msg->imsi, gsup_msg->message_type,
630 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
631 gsup_msg->cause);
632 } else {
633 LOGP(DVLR, LOGL_NOTICE,
634 "Unknown IMSI %s, discarding GSUP response "
635 "of type 0x%02x\n",
636 gsup_msg->imsi, gsup_msg->message_type);
637 }
638
639 return -GMM_CAUSE_IMSI_UNKNOWN;
640}
641
642static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
643 struct osmo_gsup_message *gsup_msg)
644{
645 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
646 LOGGSUPP(LOGL_NOTICE, gsup_msg,
647 "Purge MS has failed with cause '%s' (%d)\n",
648 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
649 gsup_msg->cause);
650 return -gsup_msg->cause;
651 }
652 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
653 return 0;
654}
655
656/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100657int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200658{
659 struct osmo_gsup_message gsup_msg = {0};
660 int rc;
661
662 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200663 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 +0200664 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
665
666 return rc;
667}
668
669/* VLR internal call to request tuples from HLR */
670int vlr_subscr_req_sai(struct vlr_subscr *vsub,
671 const uint8_t *auts, const uint8_t *auts_rand)
672{
673 struct osmo_gsup_message gsup_msg = {0};
674
675 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
676 gsup_msg.auts = auts;
677 gsup_msg.rand = auts_rand;
678
679 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
680}
681
Oliver Smith7d053092018-12-14 17:37:38 +0100682/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
683int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
684{
685 struct osmo_gsup_message gsup_msg = {0};
686 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
687 int len;
688
689 /* Encode IMEI */
690 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
691 if (len < 1) {
692 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
693 return -ENOSPC;
694 }
695 gsup_msg.imei_enc = imei_enc;
696 gsup_msg.imei_enc_len = len;
697
698 /* Send CHECK_IMEI_REQUEST */
699 gsup_msg.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST;
700 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
701 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
702}
703
Harald Welteb8b85a12016-06-17 00:06:42 +0200704/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100705int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200706{
707 struct osmo_gsup_message gsup_msg = {0};
708
709 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100710 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200711 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
712}
713
714/* Update the subscriber with GSUP-received auth tuples */
715void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
716 const struct osmo_gsup_message *gsup)
717{
718 unsigned int i;
719 unsigned int got_tuples;
720
721 if (gsup->num_auth_vectors) {
722 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
723 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100724 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200725 }
726
727 got_tuples = 0;
728 for (i = 0; i < gsup->num_auth_vectors; i++) {
729 size_t key_seq = i;
730
731 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
732 LOGVSUBP(LOGL_NOTICE, vsub,
733 "Skipping auth tuple wih invalid cksn %zu\n",
734 key_seq);
735 continue;
736 }
737 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
738 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100739 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200740 }
741
742 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
743
744 if (!got_tuples) {
745 /* FIXME what now? */
746 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
747 }
748
749 /* New tuples means last_tuple becomes invalid */
750 vsub->last_tuple = NULL;
751}
752
753/* Handle SendAuthInfo Result/Error from HLR */
754static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
755 const struct osmo_gsup_message *gsup)
756{
757 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
758 void *data = (void *) gsup;
759
760 switch (gsup->message_type) {
761 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
762 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
763 break;
764 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
765 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
766 break;
767 default:
768 return -1;
769 }
770
771 return 0;
772}
773
774static int decode_bcd_number_safe(char *output, int output_len,
775 const uint8_t *bcd_lv, int input_len,
776 int h_len)
777{
778 uint8_t len;
779 OSMO_ASSERT(output_len >= 1);
780 *output = '\0';
781 if (input_len < 1)
782 return -EIO;
783 len = bcd_lv[0];
784 if (input_len < len)
785 return -EIO;
786 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
787}
788
789static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
790 const struct osmo_gsup_message *gsup_msg)
791{
792 unsigned idx;
793 int rc;
794
Maxa263bb22017-12-27 13:23:44 +0100795 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200796 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
797 gsup_msg->msisdn_enc,
798 gsup_msg->msisdn_enc_len, 0);
799 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
800 vsub->imsi, vsub->msisdn);
801 }
802
803 if (gsup_msg->hlr_enc) {
804 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
805 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
806 gsup_msg->hlr_enc_len);
807 vsub->hlr.len = 0;
808 } else {
809 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
810 gsup_msg->hlr_enc_len);
811 vsub->hlr.len = gsup_msg->hlr_enc_len;
812 }
813 }
814
815 if (gsup_msg->pdp_info_compl) {
816 rc = vlr_subscr_pdp_data_clear(vsub);
817 if (rc > 0)
818 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
819 }
820
821 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
822 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
823 size_t ctx_id = pdp_info->context_id;
824 struct sgsn_subscriber_pdp_data *pdp_data;
825
826 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
827 LOGVSUBP(LOGL_ERROR, vsub,
828 "APN too long, context id = %zu, APN = %s\n",
829 ctx_id, osmo_hexdump(pdp_info->apn_enc,
830 pdp_info->apn_enc_len));
831 continue;
832 }
833
834 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
835 LOGVSUBP(LOGL_ERROR, vsub,
836 "QoS info too long (%zu)\n",
837 pdp_info->qos_enc_len);
838 continue;
839 }
840
841 LOGVSUBP(LOGL_INFO, vsub,
842 "Will set PDP info, context id = %zu, APN = %s\n",
843 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
844
845 /* Set PDP info [ctx_id] */
846 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
847 if (!pdp_data) {
848 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
849 pdp_data->context_id = ctx_id;
850 }
851
852 OSMO_ASSERT(pdp_data != NULL);
853 pdp_data->pdp_type = pdp_info->pdp_type;
854 osmo_apn_to_str(pdp_data->apn_str,
855 pdp_info->apn_enc, pdp_info->apn_enc_len);
856 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
857 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
858 }
859}
860
861
862/* Handle InsertSubscrData Result from HLR */
863static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
864 const struct osmo_gsup_message *gsup)
865{
866 struct osmo_gsup_message gsup_reply = {0};
867
868 vlr_subscr_gsup_insert_data(vsub, gsup);
869 vsub->vlr->ops.subscr_update(vsub);
870
871 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
872 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
873}
874
875/* Handle UpdateLocation Result from HLR */
876static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
877 const struct osmo_gsup_message *gsup)
878{
Harald Welte0df904d2018-12-03 11:00:04 +0100879 struct sgs_lu_response sgs_lu_response;
880 bool sgs_lu_in_progress = false;
881
882 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
883 sgs_lu_in_progress = true;
884
885 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200886 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
887 "without LU in progress\n");
888 return -ENODEV;
889 }
890
891 /* contrary to MAP, we allow piggy-backing subscriber data onto the
892 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
893 * nested INSERT SUBSCRIBER DATA transaction */
894 vlr_subscr_gsup_insert_data(vsub, gsup);
895
Harald Welte0df904d2018-12-03 11:00:04 +0100896 if (sgs_lu_in_progress) {
897 sgs_lu_response.accepted = true;
898 sgs_lu_response.vsub = vsub;
899 vsub->sgs.response_cb(&sgs_lu_response);
900 } else
901 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200902
903 return 0;
904}
905
906/* Handle UpdateLocation Result from HLR */
907static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
908 const struct osmo_gsup_message *gsup)
909{
Harald Welte0df904d2018-12-03 11:00:04 +0100910 struct sgs_lu_response sgs_lu_response;
911 bool sgs_lu_in_progress = false;
912
913 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
914 sgs_lu_in_progress = true;
915
916 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200917 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
918 "without LU in progress\n");
919 return -ENODEV;
920 }
921
922 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
923 get_value_string(gsm48_gmm_cause_names, gsup->cause));
924
Harald Welte0df904d2018-12-03 11:00:04 +0100925 if (sgs_lu_in_progress) {
926 sgs_lu_response.accepted = false;
927 sgs_lu_response.vsub = vsub;
928 vsub->sgs.response_cb(&sgs_lu_response);
929 } else
930 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
931 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200932 return 0;
933}
934
Neels Hofmeyr15809592018-04-06 02:57:51 +0200935static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
936 enum osmo_fsm_term_cause *fsm_cause_p,
937 enum gsm48_reject_value *gsm48_rej_p)
938{
939 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
940 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
941 switch (gmm_cause) {
942 case GMM_CAUSE_IMSI_UNKNOWN:
943 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
944 break;
945 case GMM_CAUSE_ILLEGAL_MS:
946 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
947 break;
948 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
949 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
950 break;
951 case GMM_CAUSE_ILLEGAL_ME:
952 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
953 break;
954 case GMM_CAUSE_GPRS_NOTALLOWED:
955 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
956 break;
957 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
958 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
959 break;
960 case GMM_CAUSE_MS_ID_NOT_DERIVED:
961 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
962 break;
963 case GMM_CAUSE_IMPL_DETACHED:
964 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
965 break;
966 case GMM_CAUSE_PLMN_NOTALLOWED:
967 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
968 break;
969 case GMM_CAUSE_LA_NOTALLOWED:
970 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
971 break;
972 case GMM_CAUSE_ROAMING_NOTALLOWED:
973 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
974 break;
975 case GMM_CAUSE_NO_GPRS_PLMN:
976 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
977 break;
978 case GMM_CAUSE_MSC_TEMP_NOTREACH:
979 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
980 break;
981 case GMM_CAUSE_SYNC_FAIL:
982 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
983 break;
984 case GMM_CAUSE_CONGESTION:
985 gsm48_rej = GSM48_REJECT_CONGESTION;
986 break;
987 case GMM_CAUSE_SEM_INCORR_MSG:
988 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
989 break;
990 case GMM_CAUSE_INV_MAND_INFO:
991 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
992 break;
993 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
994 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
995 break;
996 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
997 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
998 break;
999 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
1000 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
1001 break;
1002 case GMM_CAUSE_COND_IE_ERR:
1003 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
1004 break;
1005 case GMM_CAUSE_MSG_INCOMP_P_STATE:
1006 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
1007 break;
1008 case GMM_CAUSE_PROTO_ERR_UNSPEC:
1009 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
1010 break;
1011
1012 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1013 case GMM_CAUSE_MAC_FAIL:
1014 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1015 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1016 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1017 case GMM_CAUSE_NO_PDP_ACTIVATED:
1018 case GMM_CAUSE_NET_FAIL:
1019 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1020 break;
1021 }
1022 switch (gmm_cause) {
1023 /* refine any error causes here? */
1024 default:
1025 fsm_cause = OSMO_FSM_TERM_ERROR;
1026 break;
1027 }
1028 if (fsm_cause_p)
1029 *fsm_cause_p = fsm_cause;
1030 if (gsm48_rej_p)
1031 *gsm48_rej_p = gsm48_rej;
1032}
1033
Harald Welteb8b85a12016-06-17 00:06:42 +02001034/* Handle LOCATION CANCEL request from HLR */
1035static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
1036 struct osmo_gsup_message *gsup_msg)
1037{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001038 enum gsm48_reject_value gsm48_rej;
1039 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +02001040 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001041 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001042 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1043
1044 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1045 is_update_procedure ?
1046 "update procedure" : "subscription withdraw");
1047
1048 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001049 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001050
Neels Hofmeyr15809592018-04-06 02:57:51 +02001051 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
1052 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001053
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001054 vlr_subscr_rx_imsi_detach(vsub);
1055
Max770fbd22018-01-24 12:48:33 +01001056 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001057}
1058
Oliver Smith7d053092018-12-14 17:37:38 +01001059/* Handle Check_IMEI_VLR result and error from HLR */
1060static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1061{
1062 if (!vsub->lu_fsm) {
1063 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1064 osmo_gsup_message_type_name(gsup->message_type));
1065 return -ENODEV;
1066 }
1067
1068 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1069 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1070 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1071 else
1072 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1073 } else {
1074 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1075 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1076 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1077 }
1078
1079 return 0;
1080}
1081
Harald Welteb8b85a12016-06-17 00:06:42 +02001082/* Incoming handler for GSUP from HLR.
1083 * Keep this function non-static for direct invocation by unit tests. */
Harald Welte1ea6baf2018-07-31 19:40:52 +02001084int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001085{
1086 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
1087 struct vlr_subscr *vsub;
1088 struct osmo_gsup_message gsup;
1089 int rc;
1090
1091 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
1092 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
1093
1094 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
1095 if (rc < 0) {
1096 LOGP(DVLR, LOGL_ERROR,
1097 "decoding GSUP message fails with error '%s' (%d)\n",
1098 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001099 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001100 }
1101
1102 if (!gsup.imsi[0]) {
1103 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +01001104 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
1105 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
1106 if (rc < 0)
1107 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
1108 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001109 rc = -GMM_CAUSE_INV_MAND_INFO;
1110 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001111 }
1112
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001113 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +02001114 if (!vsub) {
1115 switch (gsup.message_type) {
1116 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1117 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001118 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1119 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001120 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001121 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1122 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001123 }
1124 }
1125
1126 switch (gsup.message_type) {
1127 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1128 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1129 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1130 break;
1131 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1132 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1133 break;
1134 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1135 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1136 break;
1137 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1138 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1139 break;
1140 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1141 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1142 break;
1143 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1144 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1145 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1146 LOGVSUBP(LOGL_ERROR, vsub,
1147 "Rx GSUP msg_type=%d not yet implemented\n",
1148 gsup.message_type);
1149 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1150 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001151 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1152 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
1153 rc = vlr_subscr_handle_check_imei(vsub, &gsup);
1154 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001155 default:
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001156 /* Forward message towards MSC */
1157 rc = vlr->ops.forward_gsup_msg(vsub, &gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001158 break;
1159 }
1160
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001161 vlr_subscr_put(vsub, __func__);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001162
1163msgb_free_and_return:
1164 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001165 return rc;
1166}
1167
1168/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1169int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1170 const uint8_t *mi, size_t mi_len)
1171{
1172 char mi_string[GSM48_MI_SIZE];
1173 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1174
1175 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1176
1177 /* update the vlr_subscr with the given identity */
1178 switch (mi_type) {
1179 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001180 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1181 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1182 sizeof(vsub->imsi) - 1, mi_string);
1183 return -ENOSPC; /* ignore message; do not avance LU FSM */
1184 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001185 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1186 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1187 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001188 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001189 } else
1190 vlr_subscr_set_imsi(vsub, mi_string);
1191 break;
1192 case GSM_MI_TYPE_IMEI:
1193 vlr_subscr_set_imei(vsub, mi_string);
1194 break;
1195 case GSM_MI_TYPE_IMEISV:
1196 vlr_subscr_set_imeisv(vsub, mi_string);
1197 break;
1198 }
1199
1200 if (vsub->auth_fsm) {
1201 switch (mi_type) {
1202 case GSM_MI_TYPE_IMSI:
1203 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1204 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1205 break;
1206 }
1207 }
1208
1209 if (vsub->lu_fsm) {
1210 uint32_t event = 0;
1211 switch (mi_type) {
1212 case GSM_MI_TYPE_IMSI:
1213 event = VLR_ULA_E_ID_IMSI;
1214 break;
1215 case GSM_MI_TYPE_IMEI:
1216 event = VLR_ULA_E_ID_IMEI;
1217 break;
1218 case GSM_MI_TYPE_IMEISV:
1219 event = VLR_ULA_E_ID_IMEISV;
1220 break;
1221 default:
1222 OSMO_ASSERT(0);
1223 break;
1224 }
1225 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1226 } else {
1227 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1228 }
1229
1230 return 0;
1231}
1232
1233/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1234int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1235{
1236 if (vsub->lu_fsm) {
1237 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1238 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1239 } else if (vsub->proc_arq_fsm) {
1240 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1241 PR_ARQ_E_TMSI_ACK, NULL);
1242 } else {
1243 LOGVSUBP(LOGL_NOTICE, vsub,
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +02001244 "gratuitous TMSI REALLOC COMPL\n");
Harald Welteb8b85a12016-06-17 00:06:42 +02001245 return -EINVAL;
1246 }
1247}
1248
Maxdcc193d2017-12-27 19:34:15 +01001249bool vlr_subscr_expire(struct vlr_subscr *vsub)
1250{
1251 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001252 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001253 vsub->lu_complete = false;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001254 vlr_subscr_put(vsub, VSUB_USE_ATTACHED);
Maxdcc193d2017-12-27 19:34:15 +01001255
1256 return true;
1257 }
1258
1259 return false;
1260}
1261
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001262/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001263int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1264{
1265 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001266 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001267
1268 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001269 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001270
Harald Welte0df904d2018-12-03 11:00:04 +01001271 /* Inform the UE-SGs FSM that the subscriber has been detached */
1272 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1273
Maxdcc193d2017-12-27 19:34:15 +01001274 vlr_subscr_expire(vsub);
1275
Harald Welteb8b85a12016-06-17 00:06:42 +02001276 return 0;
1277}
1278
1279/* Tear down any running FSMs due to MSC connection timeout.
1280 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1281 * message before the entire connection is torn down.
1282 * \param[in] vsub subscriber to tear down
1283 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001284void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001285{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001286 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001287}
1288
1289struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1290{
1291 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1292 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001293
1294 /* Some of these are needed only on UTRAN, but in case the caller wants
1295 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001296 OSMO_ASSERT(ops->tx_auth_req);
1297 OSMO_ASSERT(ops->tx_auth_rej);
1298 OSMO_ASSERT(ops->tx_id_req);
1299 OSMO_ASSERT(ops->tx_lu_acc);
1300 OSMO_ASSERT(ops->tx_lu_rej);
1301 OSMO_ASSERT(ops->tx_cm_serv_acc);
1302 OSMO_ASSERT(ops->tx_cm_serv_rej);
1303 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001304 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001305 OSMO_ASSERT(ops->subscr_update);
1306 OSMO_ASSERT(ops->subscr_assoc);
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001307 OSMO_ASSERT(ops->forward_gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001308
1309 INIT_LLIST_HEAD(&vlr->subscribers);
1310 INIT_LLIST_HEAD(&vlr->operations);
1311 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1312
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001313 /* defaults */
1314 vlr->cfg.assign_tmsi = true;
1315
Harald Welteb8b85a12016-06-17 00:06:42 +02001316 /* osmo_auth_fsm.c */
1317 osmo_fsm_register(&vlr_auth_fsm);
1318 /* osmo_lu_fsm.c */
1319 vlr_lu_fsm_init();
1320 /* vlr_access_request_fsm.c */
1321 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001322 /* vlr_sgs_fsm.c */
1323 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001324
1325 return vlr;
1326}
1327
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001328int vlr_start(struct ipaccess_unit *ipa_dev, struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +02001329 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1330{
1331 OSMO_ASSERT(vlr);
1332
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001333 vlr->gsup_client = osmo_gsup_client_create2(vlr, ipa_dev,
1334 gsup_server_addr_str,
1335 gsup_server_port,
1336 &vlr_gsupc_read_cb, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001337 if (!vlr->gsup_client)
1338 return -ENOMEM;
1339 vlr->gsup_client->data = vlr;
1340
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001341 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1342 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001343 return 0;
1344}
1345
1346/* MSC->VLR: Subscribre has disconnected */
1347int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1348{
1349 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1350 * interface */
1351 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1352 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1353 vsub->msc_conn_ref = NULL;
1354
1355 return 0;
1356}
1357
1358/* MSC->VLR: Receive Authentication Failure from Subscriber */
1359int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1360{
1361 struct vlr_auth_resp_par par = {0};
1362 par.auts = auts;
1363
1364 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1365 return 0;
1366}
1367
1368/* MSC->VLR: Receive Authentication Response from MS
1369 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1370int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1371 bool is_utran, const uint8_t *res, uint8_t res_len)
1372{
1373 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1374 struct vlr_auth_resp_par par;
1375
1376 par.is_r99 = is_r99;
1377 par.is_utran = is_utran;
1378 par.res = res;
1379 par.res_len = res_len;
1380 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1381
1382 return 0;
1383}
1384
1385/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1386void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1387{
1388 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1389 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1390 if (vsub->proc_arq_fsm
1391 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1392 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1393 res);
1394}
1395
1396/* Internal evaluation of requested ciphering mode.
1397 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1398 * \param[in] vlr VLR instance.
1399 * \param[in] fi Calling FSM instance, for logging.
1400 * \param[in] msc_conn_ref MSC conn to send to.
1401 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1402 * \returns 0 if no ciphering is needed or message was sent successfully,
1403 * or a negative value if ciph_mode is invalid or sending failed.
1404 */
1405int vlr_set_ciph_mode(struct vlr_instance *vlr,
1406 struct osmo_fsm_inst *fi,
1407 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001408 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001409 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001410 bool retrieve_imeisv)
1411{
Harald Welte71c51df2017-12-23 18:51:48 +01001412 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001413 return 0;
1414
Harald Welte71c51df2017-12-23 18:51:48 +01001415 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1416 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001417}
1418
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001419/* Decide whether UMTS AKA should be used.
1420 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1421 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1422 * GERAN are R99 capable and UMTS AKA tokens are available.
1423 * \param[in] vec Auth tokens (received from the HLR).
1424 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1425 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1426 */
1427bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1428{
1429 if (!is_r99)
1430 return false;
1431 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1432 return false;
1433 return true;
1434}
1435
Harald Welteb8b85a12016-06-17 00:06:42 +02001436void log_set_filter_vlr_subscr(struct log_target *target,
1437 struct vlr_subscr *vlr_subscr)
1438{
1439 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001440 const char *use = "logfilter";
Harald Welteb8b85a12016-06-17 00:06:42 +02001441
1442 /* free the old data */
1443 if (*fsub) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001444 vlr_subscr_put(*fsub, use);
Harald Welteb8b85a12016-06-17 00:06:42 +02001445 *fsub = NULL;
1446 }
1447
1448 if (vlr_subscr) {
1449 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001450 vlr_subscr_get(vlr_subscr, use);
1451 *fsub = vlr_subscr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001452 } else
1453 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1454}