blob: cd4209d66324041f13ff5f1e498bc36a2d30d135 [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,
122 const char *file, int line)
123{
124 struct vlr_subscr *vsub;
125
126 if (!imsi || !*imsi)
127 return NULL;
128
129 llist_for_each_entry(vsub, &vlr->subscribers, list) {
130 if (vlr_subscr_matches_imsi(vsub, imsi))
131 return _vlr_subscr_get(vsub, file, line);
132 }
133 return NULL;
134}
135
136struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
137 uint32_t tmsi,
138 const char *file, int line)
139{
140 struct vlr_subscr *vsub;
141
142 if (tmsi == GSM_RESERVED_TMSI)
143 return NULL;
144
145 llist_for_each_entry(vsub, &vlr->subscribers, list) {
146 if (vlr_subscr_matches_tmsi(vsub, tmsi))
147 return _vlr_subscr_get(vsub, file, line);
148 }
149 return NULL;
150}
151
152struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
153 const char *msisdn,
154 const char *file, int line)
155{
156 struct vlr_subscr *vsub;
157
158 if (!msisdn || !*msisdn)
159 return NULL;
160
161 llist_for_each_entry(vsub, &vlr->subscribers, list) {
162 if (vlr_subscr_matches_msisdn(vsub, msisdn))
163 return _vlr_subscr_get(vsub, file, line);
164 }
165 return NULL;
166}
167
168/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100169static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
170 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200171{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200172 struct msgb *msg = osmo_gsup_client_msgb_alloc();
Harald Welteb8b85a12016-06-17 00:06:42 +0200173
Max770fbd22018-01-24 12:48:33 +0100174 int rc = osmo_gsup_encode(msg, gsup_msg);
175 if (rc < 0) {
176 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
177 return rc;
178 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200179
180 if (!vlr->gsup_client) {
181 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
182 "send GSUP: %s\n", msgb_hexdump(msg));
183 msgb_free(msg);
184 return -ENOTSUP;
185 }
186
187 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
188 osmo_hexdump_nospc(msg->data, msg->len));
189
Harald Welte1ea6baf2018-07-31 19:40:52 +0200190 return osmo_gsup_client_send(vlr->gsup_client, msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200191}
192
193/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100194static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200195 struct osmo_gsup_message *gsup_msg)
196{
197 struct vlr_instance *vlr = vsub->vlr;
198
199 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100200 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200201
202 return vlr_tx_gsup_message(vlr, gsup_msg);
203}
204
205/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100206static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200207 struct osmo_gsup_message *gsup_orig,
208 enum gsm48_gmm_cause cause)
209{
210 struct osmo_gsup_message gsup_reply = {0};
211
Max98f74672018-02-05 12:57:06 +0100212 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200213 gsup_reply.cause = cause;
214 gsup_reply.message_type =
215 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
216
217 return vlr_tx_gsup_message(vlr, &gsup_reply);
218}
219
220struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
221{
222 if (!sub)
223 return NULL;
224 OSMO_ASSERT(sub->use_count < INT_MAX);
225 sub->use_count++;
226 LOGPSRC(DREF, LOGL_DEBUG, file, line,
227 "VLR subscr %s usage increases to: %d\n",
228 vlr_subscr_name(sub), sub->use_count);
229 return sub;
230}
231
232struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
233{
234 if (!sub)
235 return NULL;
236 sub->use_count--;
237 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
238 file, line,
239 "VLR subscr %s usage decreases to: %d\n",
240 vlr_subscr_name(sub), sub->use_count);
241 if (sub->use_count <= 0)
242 vlr_subscr_free(sub);
243 return NULL;
244}
245
246/* Allocate a new subscriber and insert it into list */
247static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
248{
249 struct vlr_subscr *vsub;
250 int i;
251
252 vsub = talloc_zero(vlr, struct vlr_subscr);
253 vsub->vlr = vlr;
254 vsub->tmsi = GSM_RESERVED_TMSI;
255 vsub->tmsi_new = GSM_RESERVED_TMSI;
256
257 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100258 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200259
260 INIT_LLIST_HEAD(&vsub->cs.requests);
261 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
262
Harald Welte0df904d2018-12-03 11:00:04 +0100263 /* Create an SGs FSM, which is needed to control CSFB,
264 * in cases where CSFB/SGs is not in use, this FSM will
265 * just do nothing. (see also: sgs_iface.c) */
266 vlr_sgs_fsm_create(vsub);
267
Harald Welteb8b85a12016-06-17 00:06:42 +0200268 llist_add_tail(&vsub->list, &vlr->subscribers);
269 return vsub;
270}
271
272struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
273{
274 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
275}
276
277/* Send a GSUP Purge MS request.
278 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
279 * this subscriber, not to the HLR? */
280int vlr_subscr_purge(struct vlr_subscr *vsub)
281{
282 struct osmo_gsup_message gsup_msg = {0};
283
284 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
285
286 /* provide HLR number in case we know it */
287 gsup_msg.hlr_enc_len = vsub->hlr.len;
288 gsup_msg.hlr_enc = vsub->hlr.buf;
289
290 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
291}
292
Neels Hofmeyr15809592018-04-06 02:57:51 +0200293void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
294 enum osmo_fsm_term_cause fsm_cause,
295 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200296{
297 if (!vsub)
298 return;
299
Neels Hofmeyr15809592018-04-06 02:57:51 +0200300 vlr_subscr_get(vsub);
301 if (vsub->lu_fsm)
302 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200303 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200304 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
305 vlr_subscr_put(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200306}
307
308/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
309void vlr_subscr_free(struct vlr_subscr *vsub)
310{
311 llist_del(&vsub->list);
312 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
Harald Welte0df904d2018-12-03 11:00:04 +0100313
314 /* Make sure SGs timer Ts5 is removed */
315 osmo_timer_del(&vsub->sgs.Ts5);
316
317 /* Remove SGs FSM (see also: sgs_iface.c) */
318 vlr_sgs_fsm_remove(vsub);
319
Harald Welteb8b85a12016-06-17 00:06:42 +0200320 talloc_free(vsub);
321}
322
323/* Generate a new TMSI and store in vsub->tmsi_new.
324 * Search all known subscribers to ensure that the TMSI is unique. */
325int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
326{
327 struct vlr_instance *vlr = vsub->vlr;
328 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100329 int tried, rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200330
331 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100332 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
333 if (rc < 0) {
334 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
335 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200336 }
337 /* throw the dice again, if the TSMI doesn't fit */
338 if (tmsi == GSM_RESERVED_TMSI)
339 continue;
340
341 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
342 if (vlr->cfg.is_ps) {
343 /* SGSN */
344 tmsi |= 0xC000000;
345 } else {
346 /* MSC */
347 if ((tmsi & 0xC0000000) == 0xC0000000)
348 tmsi &= ~0xC0000000;
349 }
350
351 /* If this TMSI is already in use, try another one. */
352 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
353 continue;
354
355 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100356 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200357 return 0;
358 }
359
360 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
361 " after %d tries\n", vlr_subscr_name(vsub), tried);
362 return -1;
363}
364
365/* Find subscriber by IMSI, or create new subscriber if not found.
366 * \param[in] vlr VLR instace.
367 * \param[in] imsi IMSI string.
368 * \param[out] created if non-NULL, returns whether a new entry was created. */
369struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
370 const char *imsi,
371 bool *created,
372 const char *file,
373 int line)
374{
375 struct vlr_subscr *vsub;
376 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
377 if (vsub) {
378 if (created)
379 *created = false;
380 return vsub;
381 }
382
383 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
384 if (!vsub)
385 return NULL;
386 vlr_subscr_set_imsi(vsub, imsi);
387 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
388 if (created)
389 *created = true;
390 return vsub;
391}
392
393/* Find subscriber by TMSI, or create new subscriber if not found.
394 * \param[in] vlr VLR instace.
395 * \param[in] tmsi TMSI.
396 * \param[out] created if non-NULL, returns whether a new entry was created. */
397struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
398 uint32_t tmsi,
399 bool *created,
400 const char *file,
401 int line)
402{
403 struct vlr_subscr *vsub;
404 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
405 if (vsub) {
406 if (created)
407 *created = false;
408 return vsub;
409 }
410
411 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
412 if (!vsub)
413 return NULL;
414 vsub->tmsi = tmsi;
415 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
416 if (created)
417 *created = true;
418 return vsub;
419}
420
421void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
422{
423 if (!vsub)
424 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200425
426 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
427 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
428 imsi, vsub->imsi);
429 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
430 }
431
Harald Welteb8b85a12016-06-17 00:06:42 +0200432 vsub->id = atoll(vsub->imsi);
433 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
434 vsub->imsi, vsub->id);
435}
436
437void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
438{
439 if (!vsub)
440 return;
Max98f74672018-02-05 12:57:06 +0100441 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200442 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
443 vsub->imsi, vsub->imei);
444}
445
446void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
447{
448 if (!vsub)
449 return;
Max98f74672018-02-05 12:57:06 +0100450 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200451 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
452 vsub->imsi, vsub->imeisv);
453}
454
455/* Safely copy the given MSISDN string to vsub->msisdn */
456void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
457{
458 if (!vsub)
459 return;
Max98f74672018-02-05 12:57:06 +0100460 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200461 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
462 vsub->imsi, vsub->msisdn);
463}
464
465bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
466{
467 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
468}
469
470bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
471{
472 return vsub && tmsi != GSM_RESERVED_TMSI
473 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
474}
475
476bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
477{
478 return vsub && msisdn && vsub->msisdn[0]
479 && !strcmp(vsub->msisdn, msisdn);
480}
481
482bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
483{
484 return vsub && imei && vsub->imei[0]
485 && !strcmp(vsub->imei, imei);
486}
487
488/* Send updated subscriber information to HLR */
489int vlr_subscr_changed(struct vlr_subscr *vsub)
490{
491 /* FIXME */
492 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
493 return 0;
494}
495
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200496void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
497{
498 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
499 struct timespec now;
500
501 /* The T3212 timeout value field is coded as the binary representation of the timeout
502 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
503 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
504 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
505 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
506 } else {
507 LOGP(DVLR, LOGL_ERROR,
508 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
509 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
510 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
511 }
512}
513
514void vlr_subscr_expire_lu(void *data)
515{
516 struct vlr_instance *vlr = data;
517 struct vlr_subscr *vsub, *vsub_tmp;
518 struct timespec now;
519
520 if (llist_empty(&vlr->subscribers))
521 goto done;
522
523 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
524 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
525 goto done;
526 }
527
528 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
529 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
530 continue;
531
532 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
533 vlr_subscr_rx_imsi_detach(vsub);
534 }
535
536done:
537 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
538}
539
Harald Welteb8b85a12016-06-17 00:06:42 +0200540/***********************************************************************
541 * PDP context data
542 ***********************************************************************/
543
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200544#define GSM_APN_LENGTH 102
545
546/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
547/* see GSM 09.02, B.1, gprsSubscriptionData */
548struct sgsn_subscriber_pdp_data {
549 struct llist_head list;
550
551 unsigned int context_id;
552 uint16_t pdp_type;
553 char apn_str[GSM_APN_LENGTH];
554 uint8_t qos_subscribed[20];
555 size_t qos_subscribed_len;
556};
557
Harald Welteb8b85a12016-06-17 00:06:42 +0200558struct sgsn_subscriber_pdp_data *
559vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
560{
561 struct sgsn_subscriber_pdp_data* pdata;
562
563 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
564
565 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
566
567 return pdata;
568}
569
570static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
571{
572 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
573 int count = 0;
574
575 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
576 llist_del(&pdp->list);
577 talloc_free(pdp);
578 count += 1;
579 }
580
581 return count;
582}
583
584static struct sgsn_subscriber_pdp_data *
585vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
586{
587 struct sgsn_subscriber_pdp_data *pdp;
588
589 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
590 if (pdp->context_id == context_id)
591 return pdp;
592 }
593
594 return NULL;
595}
596
597/***********************************************************************
598 * Actual Implementation
599 ***********************************************************************/
600
601static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
602 struct osmo_gsup_message *gsup_msg)
603{
604 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Max770fbd22018-01-24 12:48:33 +0100605 int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
606 if (rc < 0)
607 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi);
608
Harald Welteb8b85a12016-06-17 00:06:42 +0200609 LOGP(DVLR, LOGL_NOTICE,
610 "Unknown IMSI %s, discarding GSUP request "
611 "of type 0x%02x\n",
612 gsup_msg->imsi, gsup_msg->message_type);
613 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
614 LOGP(DVLR, LOGL_NOTICE,
615 "Unknown IMSI %s, discarding GSUP error "
616 "of type 0x%02x, cause '%s' (%d)\n",
617 gsup_msg->imsi, gsup_msg->message_type,
618 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
619 gsup_msg->cause);
620 } else {
621 LOGP(DVLR, LOGL_NOTICE,
622 "Unknown IMSI %s, discarding GSUP response "
623 "of type 0x%02x\n",
624 gsup_msg->imsi, gsup_msg->message_type);
625 }
626
627 return -GMM_CAUSE_IMSI_UNKNOWN;
628}
629
630static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
631 struct osmo_gsup_message *gsup_msg)
632{
633 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
634 LOGGSUPP(LOGL_NOTICE, gsup_msg,
635 "Purge MS has failed with cause '%s' (%d)\n",
636 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
637 gsup_msg->cause);
638 return -gsup_msg->cause;
639 }
640 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
641 return 0;
642}
643
644/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100645int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200646{
647 struct osmo_gsup_message gsup_msg = {0};
648 int rc;
649
650 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200651 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 +0200652 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
653
654 return rc;
655}
656
657/* VLR internal call to request tuples from HLR */
658int vlr_subscr_req_sai(struct vlr_subscr *vsub,
659 const uint8_t *auts, const uint8_t *auts_rand)
660{
661 struct osmo_gsup_message gsup_msg = {0};
662
663 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
664 gsup_msg.auts = auts;
665 gsup_msg.rand = auts_rand;
666
667 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
668}
669
Oliver Smith7d053092018-12-14 17:37:38 +0100670/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
671int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
672{
673 struct osmo_gsup_message gsup_msg = {0};
674 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);
689 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
690}
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{
695 struct osmo_gsup_message gsup_msg = {0};
696
697 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100698 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200699 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
700}
701
702/* Update the subscriber with GSUP-received auth tuples */
703void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
704 const struct osmo_gsup_message *gsup)
705{
706 unsigned int i;
707 unsigned int got_tuples;
708
709 if (gsup->num_auth_vectors) {
710 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
711 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100712 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200713 }
714
715 got_tuples = 0;
716 for (i = 0; i < gsup->num_auth_vectors; i++) {
717 size_t key_seq = i;
718
719 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
720 LOGVSUBP(LOGL_NOTICE, vsub,
721 "Skipping auth tuple wih invalid cksn %zu\n",
722 key_seq);
723 continue;
724 }
725 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
726 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100727 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200728 }
729
730 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
731
732 if (!got_tuples) {
733 /* FIXME what now? */
734 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
735 }
736
737 /* New tuples means last_tuple becomes invalid */
738 vsub->last_tuple = NULL;
739}
740
741/* Handle SendAuthInfo Result/Error from HLR */
742static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
743 const struct osmo_gsup_message *gsup)
744{
745 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
746 void *data = (void *) gsup;
747
748 switch (gsup->message_type) {
749 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
750 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
751 break;
752 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
753 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
754 break;
755 default:
756 return -1;
757 }
758
759 return 0;
760}
761
762static int decode_bcd_number_safe(char *output, int output_len,
763 const uint8_t *bcd_lv, int input_len,
764 int h_len)
765{
766 uint8_t len;
767 OSMO_ASSERT(output_len >= 1);
768 *output = '\0';
769 if (input_len < 1)
770 return -EIO;
771 len = bcd_lv[0];
772 if (input_len < len)
773 return -EIO;
774 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
775}
776
777static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
778 const struct osmo_gsup_message *gsup_msg)
779{
780 unsigned idx;
781 int rc;
782
Maxa263bb22017-12-27 13:23:44 +0100783 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200784 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
785 gsup_msg->msisdn_enc,
786 gsup_msg->msisdn_enc_len, 0);
787 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
788 vsub->imsi, vsub->msisdn);
789 }
790
791 if (gsup_msg->hlr_enc) {
792 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
793 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
794 gsup_msg->hlr_enc_len);
795 vsub->hlr.len = 0;
796 } else {
797 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
798 gsup_msg->hlr_enc_len);
799 vsub->hlr.len = gsup_msg->hlr_enc_len;
800 }
801 }
802
803 if (gsup_msg->pdp_info_compl) {
804 rc = vlr_subscr_pdp_data_clear(vsub);
805 if (rc > 0)
806 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
807 }
808
809 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
810 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
811 size_t ctx_id = pdp_info->context_id;
812 struct sgsn_subscriber_pdp_data *pdp_data;
813
814 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
815 LOGVSUBP(LOGL_ERROR, vsub,
816 "APN too long, context id = %zu, APN = %s\n",
817 ctx_id, osmo_hexdump(pdp_info->apn_enc,
818 pdp_info->apn_enc_len));
819 continue;
820 }
821
822 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
823 LOGVSUBP(LOGL_ERROR, vsub,
824 "QoS info too long (%zu)\n",
825 pdp_info->qos_enc_len);
826 continue;
827 }
828
829 LOGVSUBP(LOGL_INFO, vsub,
830 "Will set PDP info, context id = %zu, APN = %s\n",
831 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
832
833 /* Set PDP info [ctx_id] */
834 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
835 if (!pdp_data) {
836 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
837 pdp_data->context_id = ctx_id;
838 }
839
840 OSMO_ASSERT(pdp_data != NULL);
841 pdp_data->pdp_type = pdp_info->pdp_type;
842 osmo_apn_to_str(pdp_data->apn_str,
843 pdp_info->apn_enc, pdp_info->apn_enc_len);
844 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
845 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
846 }
847}
848
849
850/* Handle InsertSubscrData Result from HLR */
851static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
852 const struct osmo_gsup_message *gsup)
853{
854 struct osmo_gsup_message gsup_reply = {0};
855
856 vlr_subscr_gsup_insert_data(vsub, gsup);
857 vsub->vlr->ops.subscr_update(vsub);
858
859 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
860 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
861}
862
863/* Handle UpdateLocation Result from HLR */
864static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
865 const struct osmo_gsup_message *gsup)
866{
Harald Welte0df904d2018-12-03 11:00:04 +0100867 struct sgs_lu_response sgs_lu_response;
868 bool sgs_lu_in_progress = false;
869
870 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
871 sgs_lu_in_progress = true;
872
873 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200874 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
875 "without LU in progress\n");
876 return -ENODEV;
877 }
878
879 /* contrary to MAP, we allow piggy-backing subscriber data onto the
880 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
881 * nested INSERT SUBSCRIBER DATA transaction */
882 vlr_subscr_gsup_insert_data(vsub, gsup);
883
Harald Welte0df904d2018-12-03 11:00:04 +0100884 if (sgs_lu_in_progress) {
885 sgs_lu_response.accepted = true;
886 sgs_lu_response.vsub = vsub;
887 vsub->sgs.response_cb(&sgs_lu_response);
888 } else
889 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200890
891 return 0;
892}
893
894/* Handle UpdateLocation Result from HLR */
895static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
896 const struct osmo_gsup_message *gsup)
897{
Harald Welte0df904d2018-12-03 11:00:04 +0100898 struct sgs_lu_response sgs_lu_response;
899 bool sgs_lu_in_progress = false;
900
901 if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES)
902 sgs_lu_in_progress = true;
903
904 if (!vsub->lu_fsm && !sgs_lu_in_progress) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200905 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
906 "without LU in progress\n");
907 return -ENODEV;
908 }
909
910 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
911 get_value_string(gsm48_gmm_cause_names, gsup->cause));
912
Harald Welte0df904d2018-12-03 11:00:04 +0100913 if (sgs_lu_in_progress) {
914 sgs_lu_response.accepted = false;
915 sgs_lu_response.vsub = vsub;
916 vsub->sgs.response_cb(&sgs_lu_response);
917 } else
918 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
919 (void *)&gsup->cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200920 return 0;
921}
922
Neels Hofmeyr15809592018-04-06 02:57:51 +0200923static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
924 enum osmo_fsm_term_cause *fsm_cause_p,
925 enum gsm48_reject_value *gsm48_rej_p)
926{
927 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
928 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
929 switch (gmm_cause) {
930 case GMM_CAUSE_IMSI_UNKNOWN:
931 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
932 break;
933 case GMM_CAUSE_ILLEGAL_MS:
934 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
935 break;
936 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
937 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
938 break;
939 case GMM_CAUSE_ILLEGAL_ME:
940 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
941 break;
942 case GMM_CAUSE_GPRS_NOTALLOWED:
943 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
944 break;
945 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
946 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
947 break;
948 case GMM_CAUSE_MS_ID_NOT_DERIVED:
949 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
950 break;
951 case GMM_CAUSE_IMPL_DETACHED:
952 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
953 break;
954 case GMM_CAUSE_PLMN_NOTALLOWED:
955 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
956 break;
957 case GMM_CAUSE_LA_NOTALLOWED:
958 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
959 break;
960 case GMM_CAUSE_ROAMING_NOTALLOWED:
961 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
962 break;
963 case GMM_CAUSE_NO_GPRS_PLMN:
964 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
965 break;
966 case GMM_CAUSE_MSC_TEMP_NOTREACH:
967 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
968 break;
969 case GMM_CAUSE_SYNC_FAIL:
970 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
971 break;
972 case GMM_CAUSE_CONGESTION:
973 gsm48_rej = GSM48_REJECT_CONGESTION;
974 break;
975 case GMM_CAUSE_SEM_INCORR_MSG:
976 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
977 break;
978 case GMM_CAUSE_INV_MAND_INFO:
979 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
980 break;
981 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
982 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
983 break;
984 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
985 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
986 break;
987 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
988 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
989 break;
990 case GMM_CAUSE_COND_IE_ERR:
991 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
992 break;
993 case GMM_CAUSE_MSG_INCOMP_P_STATE:
994 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
995 break;
996 case GMM_CAUSE_PROTO_ERR_UNSPEC:
997 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
998 break;
999
1000 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
1001 case GMM_CAUSE_MAC_FAIL:
1002 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
1003 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
1004 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
1005 case GMM_CAUSE_NO_PDP_ACTIVATED:
1006 case GMM_CAUSE_NET_FAIL:
1007 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
1008 break;
1009 }
1010 switch (gmm_cause) {
1011 /* refine any error causes here? */
1012 default:
1013 fsm_cause = OSMO_FSM_TERM_ERROR;
1014 break;
1015 }
1016 if (fsm_cause_p)
1017 *fsm_cause_p = fsm_cause;
1018 if (gsm48_rej_p)
1019 *gsm48_rej_p = gsm48_rej;
1020}
1021
Harald Welteb8b85a12016-06-17 00:06:42 +02001022/* Handle LOCATION CANCEL request from HLR */
1023static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
1024 struct osmo_gsup_message *gsup_msg)
1025{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001026 enum gsm48_reject_value gsm48_rej;
1027 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +02001028 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +01001029 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +02001030 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1031
1032 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1033 is_update_procedure ?
1034 "update procedure" : "subscription withdraw");
1035
1036 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001037 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001038
Neels Hofmeyr15809592018-04-06 02:57:51 +02001039 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
1040 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001041
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001042 vlr_subscr_rx_imsi_detach(vsub);
1043
Max770fbd22018-01-24 12:48:33 +01001044 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001045}
1046
Oliver Smith7d053092018-12-14 17:37:38 +01001047/* Handle Check_IMEI_VLR result and error from HLR */
1048static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1049{
1050 if (!vsub->lu_fsm) {
1051 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1052 osmo_gsup_message_type_name(gsup->message_type));
1053 return -ENODEV;
1054 }
1055
1056 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1057 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1058 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1059 else
1060 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1061 } else {
1062 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1063 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1064 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1065 }
1066
1067 return 0;
1068}
1069
Harald Welteb8b85a12016-06-17 00:06:42 +02001070/* Incoming handler for GSUP from HLR.
1071 * Keep this function non-static for direct invocation by unit tests. */
Harald Welte1ea6baf2018-07-31 19:40:52 +02001072int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001073{
1074 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
1075 struct vlr_subscr *vsub;
1076 struct osmo_gsup_message gsup;
1077 int rc;
1078
1079 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
1080 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
1081
1082 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
1083 if (rc < 0) {
1084 LOGP(DVLR, LOGL_ERROR,
1085 "decoding GSUP message fails with error '%s' (%d)\n",
1086 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001087 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001088 }
1089
1090 if (!gsup.imsi[0]) {
1091 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +01001092 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
1093 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
1094 if (rc < 0)
1095 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
1096 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001097 rc = -GMM_CAUSE_INV_MAND_INFO;
1098 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001099 }
1100
1101 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
1102 if (!vsub) {
1103 switch (gsup.message_type) {
1104 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1105 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001106 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1107 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001108 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001109 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1110 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001111 }
1112 }
1113
1114 switch (gsup.message_type) {
1115 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1116 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1117 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1118 break;
1119 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1120 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1121 break;
1122 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1123 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1124 break;
1125 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1126 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1127 break;
1128 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1129 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1130 break;
1131 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1132 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1133 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1134 LOGVSUBP(LOGL_ERROR, vsub,
1135 "Rx GSUP msg_type=%d not yet implemented\n",
1136 gsup.message_type);
1137 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1138 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001139 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1140 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
1141 rc = vlr_subscr_handle_check_imei(vsub, &gsup);
1142 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001143 default:
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001144 /* Forward message towards MSC */
1145 rc = vlr->ops.forward_gsup_msg(vsub, &gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001146 break;
1147 }
1148
1149 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001150
1151msgb_free_and_return:
1152 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001153 return rc;
1154}
1155
1156/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1157int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1158 const uint8_t *mi, size_t mi_len)
1159{
1160 char mi_string[GSM48_MI_SIZE];
1161 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1162
1163 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1164
1165 /* update the vlr_subscr with the given identity */
1166 switch (mi_type) {
1167 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001168 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1169 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1170 sizeof(vsub->imsi) - 1, mi_string);
1171 return -ENOSPC; /* ignore message; do not avance LU FSM */
1172 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001173 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1174 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1175 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001176 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001177 } else
1178 vlr_subscr_set_imsi(vsub, mi_string);
1179 break;
1180 case GSM_MI_TYPE_IMEI:
1181 vlr_subscr_set_imei(vsub, mi_string);
1182 break;
1183 case GSM_MI_TYPE_IMEISV:
1184 vlr_subscr_set_imeisv(vsub, mi_string);
1185 break;
1186 }
1187
1188 if (vsub->auth_fsm) {
1189 switch (mi_type) {
1190 case GSM_MI_TYPE_IMSI:
1191 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1192 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1193 break;
1194 }
1195 }
1196
1197 if (vsub->lu_fsm) {
1198 uint32_t event = 0;
1199 switch (mi_type) {
1200 case GSM_MI_TYPE_IMSI:
1201 event = VLR_ULA_E_ID_IMSI;
1202 break;
1203 case GSM_MI_TYPE_IMEI:
1204 event = VLR_ULA_E_ID_IMEI;
1205 break;
1206 case GSM_MI_TYPE_IMEISV:
1207 event = VLR_ULA_E_ID_IMEISV;
1208 break;
1209 default:
1210 OSMO_ASSERT(0);
1211 break;
1212 }
1213 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1214 } else {
1215 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1216 }
1217
1218 return 0;
1219}
1220
1221/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1222int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1223{
1224 if (vsub->lu_fsm) {
1225 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1226 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1227 } else if (vsub->proc_arq_fsm) {
1228 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1229 PR_ARQ_E_TMSI_ACK, NULL);
1230 } else {
1231 LOGVSUBP(LOGL_NOTICE, vsub,
1232 "gratuitous TMSI REALLOC COMPL");
1233 return -EINVAL;
1234 }
1235}
1236
Maxdcc193d2017-12-27 19:34:15 +01001237bool vlr_subscr_expire(struct vlr_subscr *vsub)
1238{
1239 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001240 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001241 vsub->lu_complete = false;
1242 vlr_subscr_put(vsub);
1243
1244 return true;
1245 }
1246
1247 return false;
1248}
1249
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001250/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001251int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1252{
1253 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001254 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001255
1256 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001257 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001258
Harald Welte0df904d2018-12-03 11:00:04 +01001259 /* Inform the UE-SGs FSM that the subscriber has been detached */
1260 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL);
1261
Maxdcc193d2017-12-27 19:34:15 +01001262 vlr_subscr_expire(vsub);
1263
Harald Welteb8b85a12016-06-17 00:06:42 +02001264 return 0;
1265}
1266
1267/* Tear down any running FSMs due to MSC connection timeout.
1268 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1269 * message before the entire connection is torn down.
1270 * \param[in] vsub subscriber to tear down
1271 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001272void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001273{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001274 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001275}
1276
1277struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1278{
1279 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1280 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001281
1282 /* Some of these are needed only on UTRAN, but in case the caller wants
1283 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001284 OSMO_ASSERT(ops->tx_auth_req);
1285 OSMO_ASSERT(ops->tx_auth_rej);
1286 OSMO_ASSERT(ops->tx_id_req);
1287 OSMO_ASSERT(ops->tx_lu_acc);
1288 OSMO_ASSERT(ops->tx_lu_rej);
1289 OSMO_ASSERT(ops->tx_cm_serv_acc);
1290 OSMO_ASSERT(ops->tx_cm_serv_rej);
1291 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001292 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001293 OSMO_ASSERT(ops->subscr_update);
1294 OSMO_ASSERT(ops->subscr_assoc);
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001295 OSMO_ASSERT(ops->forward_gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001296
1297 INIT_LLIST_HEAD(&vlr->subscribers);
1298 INIT_LLIST_HEAD(&vlr->operations);
1299 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1300
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001301 /* defaults */
1302 vlr->cfg.assign_tmsi = true;
1303
Harald Welteb8b85a12016-06-17 00:06:42 +02001304 /* osmo_auth_fsm.c */
1305 osmo_fsm_register(&vlr_auth_fsm);
1306 /* osmo_lu_fsm.c */
1307 vlr_lu_fsm_init();
1308 /* vlr_access_request_fsm.c */
1309 vlr_parq_fsm_init();
Harald Welte0df904d2018-12-03 11:00:04 +01001310 /* vlr_sgs_fsm.c */
1311 vlr_sgs_fsm_init();
Harald Welteb8b85a12016-06-17 00:06:42 +02001312
1313 return vlr;
1314}
1315
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001316int vlr_start(struct ipaccess_unit *ipa_dev, struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +02001317 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1318{
1319 OSMO_ASSERT(vlr);
1320
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001321 vlr->gsup_client = osmo_gsup_client_create2(vlr, ipa_dev,
1322 gsup_server_addr_str,
1323 gsup_server_port,
1324 &vlr_gsupc_read_cb, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001325 if (!vlr->gsup_client)
1326 return -ENOMEM;
1327 vlr->gsup_client->data = vlr;
1328
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001329 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1330 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001331 return 0;
1332}
1333
1334/* MSC->VLR: Subscribre has disconnected */
1335int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1336{
1337 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1338 * interface */
1339 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1340 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1341 vsub->msc_conn_ref = NULL;
1342
1343 return 0;
1344}
1345
1346/* MSC->VLR: Receive Authentication Failure from Subscriber */
1347int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1348{
1349 struct vlr_auth_resp_par par = {0};
1350 par.auts = auts;
1351
1352 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1353 return 0;
1354}
1355
1356/* MSC->VLR: Receive Authentication Response from MS
1357 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1358int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1359 bool is_utran, const uint8_t *res, uint8_t res_len)
1360{
1361 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1362 struct vlr_auth_resp_par par;
1363
1364 par.is_r99 = is_r99;
1365 par.is_utran = is_utran;
1366 par.res = res;
1367 par.res_len = res_len;
1368 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1369
1370 return 0;
1371}
1372
1373/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1374void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1375{
1376 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1377 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1378 if (vsub->proc_arq_fsm
1379 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1380 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1381 res);
1382}
1383
1384/* Internal evaluation of requested ciphering mode.
1385 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1386 * \param[in] vlr VLR instance.
1387 * \param[in] fi Calling FSM instance, for logging.
1388 * \param[in] msc_conn_ref MSC conn to send to.
1389 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1390 * \returns 0 if no ciphering is needed or message was sent successfully,
1391 * or a negative value if ciph_mode is invalid or sending failed.
1392 */
1393int vlr_set_ciph_mode(struct vlr_instance *vlr,
1394 struct osmo_fsm_inst *fi,
1395 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001396 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001397 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001398 bool retrieve_imeisv)
1399{
Harald Welte71c51df2017-12-23 18:51:48 +01001400 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001401 return 0;
1402
Harald Welte71c51df2017-12-23 18:51:48 +01001403 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1404 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001405}
1406
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001407/* Decide whether UMTS AKA should be used.
1408 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1409 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1410 * GERAN are R99 capable and UMTS AKA tokens are available.
1411 * \param[in] vec Auth tokens (received from the HLR).
1412 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1413 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1414 */
1415bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1416{
1417 if (!is_r99)
1418 return false;
1419 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1420 return false;
1421 return true;
1422}
1423
Harald Welteb8b85a12016-06-17 00:06:42 +02001424void log_set_filter_vlr_subscr(struct log_target *target,
1425 struct vlr_subscr *vlr_subscr)
1426{
1427 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1428
1429 /* free the old data */
1430 if (*fsub) {
1431 vlr_subscr_put(*fsub);
1432 *fsub = NULL;
1433 }
1434
1435 if (vlr_subscr) {
1436 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1437 *fsub = vlr_subscr_get(vlr_subscr);
1438 } else
1439 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1440}