blob: f73eeb4f85c4ec67457e7babef597504cc9a9751 [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>
25#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
26#include <osmocom/gsm/gsup.h>
27#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020028#include <osmocom/gsm/gsm48.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020029#include <osmocom/msc/gsm_subscriber.h>
30#include <osmocom/msc/gsup_client.h>
31#include <osmocom/msc/vlr.h>
32#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020033
Harald Welteb8b85a12016-06-17 00:06:42 +020034#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020037#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020038
39#include "vlr_core.h"
40#include "vlr_auth_fsm.h"
41#include "vlr_lu_fsm.h"
42#include "vlr_access_req_fsm.h"
43
44#define SGSN_SUBSCR_MAX_RETRIES 3
45#define SGSN_SUBSCR_RETRY_INTERVAL 10
46
47/***********************************************************************
48 * Convenience functions
49 ***********************************************************************/
50
51const struct value_string vlr_ciph_names[] = {
52 OSMO_VALUE_STRING(VLR_CIPH_NONE),
53 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
54 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
55 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
56 { 0, NULL }
57};
58
59uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
60{
61 uint32_t tidx = 0xffffffff;
62
63 switch (timer) {
64 case 3270:
65 tidx = VLR_T_3270;
66 break;
67 case 3260:
68 tidx = VLR_T_3260;
69 break;
70 case 3250:
71 tidx = VLR_T_3250;
72 break;
73 }
74
75 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
76 return vlr->cfg.timer[tidx];
77}
78
79struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
80 const char *imsi,
81 const char *file, int line)
82{
83 struct vlr_subscr *vsub;
84
85 if (!imsi || !*imsi)
86 return NULL;
87
88 llist_for_each_entry(vsub, &vlr->subscribers, list) {
89 if (vlr_subscr_matches_imsi(vsub, imsi))
90 return _vlr_subscr_get(vsub, file, line);
91 }
92 return NULL;
93}
94
95struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
96 uint32_t tmsi,
97 const char *file, int line)
98{
99 struct vlr_subscr *vsub;
100
101 if (tmsi == GSM_RESERVED_TMSI)
102 return NULL;
103
104 llist_for_each_entry(vsub, &vlr->subscribers, list) {
105 if (vlr_subscr_matches_tmsi(vsub, tmsi))
106 return _vlr_subscr_get(vsub, file, line);
107 }
108 return NULL;
109}
110
111struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
112 const char *msisdn,
113 const char *file, int line)
114{
115 struct vlr_subscr *vsub;
116
117 if (!msisdn || !*msisdn)
118 return NULL;
119
120 llist_for_each_entry(vsub, &vlr->subscribers, list) {
121 if (vlr_subscr_matches_msisdn(vsub, msisdn))
122 return _vlr_subscr_get(vsub, file, line);
123 }
124 return NULL;
125}
126
127/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100128static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
129 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200130{
131 struct msgb *msg = gsup_client_msgb_alloc();
132
Max770fbd22018-01-24 12:48:33 +0100133 int rc = osmo_gsup_encode(msg, gsup_msg);
134 if (rc < 0) {
135 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
136 return rc;
137 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200138
139 if (!vlr->gsup_client) {
140 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
141 "send GSUP: %s\n", msgb_hexdump(msg));
142 msgb_free(msg);
143 return -ENOTSUP;
144 }
145
146 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
147 osmo_hexdump_nospc(msg->data, msg->len));
148
149 return gsup_client_send(vlr->gsup_client, msg);
150}
151
152/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100153static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200154 struct osmo_gsup_message *gsup_msg)
155{
156 struct vlr_instance *vlr = vsub->vlr;
157
158 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100159 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200160
161 return vlr_tx_gsup_message(vlr, gsup_msg);
162}
163
164/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100165static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200166 struct osmo_gsup_message *gsup_orig,
167 enum gsm48_gmm_cause cause)
168{
169 struct osmo_gsup_message gsup_reply = {0};
170
Max98f74672018-02-05 12:57:06 +0100171 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200172 gsup_reply.cause = cause;
173 gsup_reply.message_type =
174 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
175
176 return vlr_tx_gsup_message(vlr, &gsup_reply);
177}
178
179struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
180{
181 if (!sub)
182 return NULL;
183 OSMO_ASSERT(sub->use_count < INT_MAX);
184 sub->use_count++;
185 LOGPSRC(DREF, LOGL_DEBUG, file, line,
186 "VLR subscr %s usage increases to: %d\n",
187 vlr_subscr_name(sub), sub->use_count);
188 return sub;
189}
190
191struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
192{
193 if (!sub)
194 return NULL;
195 sub->use_count--;
196 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
197 file, line,
198 "VLR subscr %s usage decreases to: %d\n",
199 vlr_subscr_name(sub), sub->use_count);
200 if (sub->use_count <= 0)
201 vlr_subscr_free(sub);
202 return NULL;
203}
204
205/* Allocate a new subscriber and insert it into list */
206static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
207{
208 struct vlr_subscr *vsub;
209 int i;
210
211 vsub = talloc_zero(vlr, struct vlr_subscr);
212 vsub->vlr = vlr;
213 vsub->tmsi = GSM_RESERVED_TMSI;
214 vsub->tmsi_new = GSM_RESERVED_TMSI;
215
216 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
217 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
218
219 INIT_LLIST_HEAD(&vsub->cs.requests);
220 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
221
222 llist_add_tail(&vsub->list, &vlr->subscribers);
223 return vsub;
224}
225
226struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
227{
228 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
229}
230
231/* Send a GSUP Purge MS request.
232 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
233 * this subscriber, not to the HLR? */
234int vlr_subscr_purge(struct vlr_subscr *vsub)
235{
236 struct osmo_gsup_message gsup_msg = {0};
237
238 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
239
240 /* provide HLR number in case we know it */
241 gsup_msg.hlr_enc_len = vsub->hlr.len;
242 gsup_msg.hlr_enc = vsub->hlr.buf;
243
244 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
245}
246
247void vlr_subscr_cancel(struct vlr_subscr *vsub, enum gsm48_gmm_cause cause)
248{
249 if (!vsub)
250 return;
251
252 if (vsub->lu_fsm) {
253 if (vsub->lu_fsm->state == VLR_ULA_S_WAIT_HLR_UPD)
254 osmo_fsm_inst_dispatch(vsub->lu_fsm,
255 VLR_ULA_E_HLR_LU_RES,
256 (void*)&cause);
257 else
258 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_ERROR,
259 0);
260 }
261
262 if (vsub->proc_arq_fsm)
263 osmo_fsm_inst_term(vsub->proc_arq_fsm, OSMO_FSM_TERM_ERROR, 0);
264}
265
266/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
267void vlr_subscr_free(struct vlr_subscr *vsub)
268{
269 llist_del(&vsub->list);
270 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
271 talloc_free(vsub);
272}
273
274/* Generate a new TMSI and store in vsub->tmsi_new.
275 * Search all known subscribers to ensure that the TMSI is unique. */
276int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
277{
278 struct vlr_instance *vlr = vsub->vlr;
279 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100280 int tried, rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200281
282 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100283 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
284 if (rc < 0) {
285 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
286 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200287 }
288 /* throw the dice again, if the TSMI doesn't fit */
289 if (tmsi == GSM_RESERVED_TMSI)
290 continue;
291
292 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
293 if (vlr->cfg.is_ps) {
294 /* SGSN */
295 tmsi |= 0xC000000;
296 } else {
297 /* MSC */
298 if ((tmsi & 0xC0000000) == 0xC0000000)
299 tmsi &= ~0xC0000000;
300 }
301
302 /* If this TMSI is already in use, try another one. */
303 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
304 continue;
305
306 vsub->tmsi_new = tmsi;
307 return 0;
308 }
309
310 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
311 " after %d tries\n", vlr_subscr_name(vsub), tried);
312 return -1;
313}
314
315/* Find subscriber by IMSI, or create new subscriber if not found.
316 * \param[in] vlr VLR instace.
317 * \param[in] imsi IMSI string.
318 * \param[out] created if non-NULL, returns whether a new entry was created. */
319struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
320 const char *imsi,
321 bool *created,
322 const char *file,
323 int line)
324{
325 struct vlr_subscr *vsub;
326 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
327 if (vsub) {
328 if (created)
329 *created = false;
330 return vsub;
331 }
332
333 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
334 if (!vsub)
335 return NULL;
336 vlr_subscr_set_imsi(vsub, imsi);
337 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
338 if (created)
339 *created = true;
340 return vsub;
341}
342
343/* Find subscriber by TMSI, or create new subscriber if not found.
344 * \param[in] vlr VLR instace.
345 * \param[in] tmsi TMSI.
346 * \param[out] created if non-NULL, returns whether a new entry was created. */
347struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
348 uint32_t tmsi,
349 bool *created,
350 const char *file,
351 int line)
352{
353 struct vlr_subscr *vsub;
354 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
355 if (vsub) {
356 if (created)
357 *created = false;
358 return vsub;
359 }
360
361 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
362 if (!vsub)
363 return NULL;
364 vsub->tmsi = tmsi;
365 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
366 if (created)
367 *created = true;
368 return vsub;
369}
370
371void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
372{
373 if (!vsub)
374 return;
Max98f74672018-02-05 12:57:06 +0100375 OSMO_STRLCPY_ARRAY(vsub->imsi, imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200376 vsub->id = atoll(vsub->imsi);
377 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
378 vsub->imsi, vsub->id);
379}
380
381void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
382{
383 if (!vsub)
384 return;
Max98f74672018-02-05 12:57:06 +0100385 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200386 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
387 vsub->imsi, vsub->imei);
388}
389
390void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
391{
392 if (!vsub)
393 return;
Max98f74672018-02-05 12:57:06 +0100394 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200395 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
396 vsub->imsi, vsub->imeisv);
397}
398
399/* Safely copy the given MSISDN string to vsub->msisdn */
400void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
401{
402 if (!vsub)
403 return;
Max98f74672018-02-05 12:57:06 +0100404 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200405 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
406 vsub->imsi, vsub->msisdn);
407}
408
409bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
410{
411 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
412}
413
414bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
415{
416 return vsub && tmsi != GSM_RESERVED_TMSI
417 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
418}
419
420bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
421{
422 return vsub && msisdn && vsub->msisdn[0]
423 && !strcmp(vsub->msisdn, msisdn);
424}
425
426bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
427{
428 return vsub && imei && vsub->imei[0]
429 && !strcmp(vsub->imei, imei);
430}
431
432/* Send updated subscriber information to HLR */
433int vlr_subscr_changed(struct vlr_subscr *vsub)
434{
435 /* FIXME */
436 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
437 return 0;
438}
439
440/***********************************************************************
441 * PDP context data
442 ***********************************************************************/
443
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200444#define GSM_APN_LENGTH 102
445
446/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
447/* see GSM 09.02, B.1, gprsSubscriptionData */
448struct sgsn_subscriber_pdp_data {
449 struct llist_head list;
450
451 unsigned int context_id;
452 uint16_t pdp_type;
453 char apn_str[GSM_APN_LENGTH];
454 uint8_t qos_subscribed[20];
455 size_t qos_subscribed_len;
456};
457
Harald Welteb8b85a12016-06-17 00:06:42 +0200458struct sgsn_subscriber_pdp_data *
459vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
460{
461 struct sgsn_subscriber_pdp_data* pdata;
462
463 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
464
465 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
466
467 return pdata;
468}
469
470static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
471{
472 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
473 int count = 0;
474
475 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
476 llist_del(&pdp->list);
477 talloc_free(pdp);
478 count += 1;
479 }
480
481 return count;
482}
483
484static struct sgsn_subscriber_pdp_data *
485vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
486{
487 struct sgsn_subscriber_pdp_data *pdp;
488
489 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
490 if (pdp->context_id == context_id)
491 return pdp;
492 }
493
494 return NULL;
495}
496
497/***********************************************************************
498 * Actual Implementation
499 ***********************************************************************/
500
501static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
502 struct osmo_gsup_message *gsup_msg)
503{
504 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Max770fbd22018-01-24 12:48:33 +0100505 int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
506 if (rc < 0)
507 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi);
508
Harald Welteb8b85a12016-06-17 00:06:42 +0200509 LOGP(DVLR, LOGL_NOTICE,
510 "Unknown IMSI %s, discarding GSUP request "
511 "of type 0x%02x\n",
512 gsup_msg->imsi, gsup_msg->message_type);
513 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
514 LOGP(DVLR, LOGL_NOTICE,
515 "Unknown IMSI %s, discarding GSUP error "
516 "of type 0x%02x, cause '%s' (%d)\n",
517 gsup_msg->imsi, gsup_msg->message_type,
518 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
519 gsup_msg->cause);
520 } else {
521 LOGP(DVLR, LOGL_NOTICE,
522 "Unknown IMSI %s, discarding GSUP response "
523 "of type 0x%02x\n",
524 gsup_msg->imsi, gsup_msg->message_type);
525 }
526
527 return -GMM_CAUSE_IMSI_UNKNOWN;
528}
529
530static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
531 struct osmo_gsup_message *gsup_msg)
532{
533 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
534 LOGGSUPP(LOGL_NOTICE, gsup_msg,
535 "Purge MS has failed with cause '%s' (%d)\n",
536 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
537 gsup_msg->cause);
538 return -gsup_msg->cause;
539 }
540 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
541 return 0;
542}
543
544/* VLR internal call to request UpdateLocation from HLR */
545int vlr_subscr_req_lu(struct vlr_subscr *vsub, bool is_ps)
546{
547 struct osmo_gsup_message gsup_msg = {0};
548 int rc;
549
550 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
551 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
552
553 return rc;
554}
555
556/* VLR internal call to request tuples from HLR */
557int vlr_subscr_req_sai(struct vlr_subscr *vsub,
558 const uint8_t *auts, const uint8_t *auts_rand)
559{
560 struct osmo_gsup_message gsup_msg = {0};
561
562 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
563 gsup_msg.auts = auts;
564 gsup_msg.rand = auts_rand;
565
566 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
567}
568
569/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100570int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200571{
572 struct osmo_gsup_message gsup_msg = {0};
573
574 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100575 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200576 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
577}
578
579/* Update the subscriber with GSUP-received auth tuples */
580void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
581 const struct osmo_gsup_message *gsup)
582{
583 unsigned int i;
584 unsigned int got_tuples;
585
586 if (gsup->num_auth_vectors) {
587 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
588 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
589 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
590 }
591
592 got_tuples = 0;
593 for (i = 0; i < gsup->num_auth_vectors; i++) {
594 size_t key_seq = i;
595
596 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
597 LOGVSUBP(LOGL_NOTICE, vsub,
598 "Skipping auth tuple wih invalid cksn %zu\n",
599 key_seq);
600 continue;
601 }
602 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
603 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100604 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200605 }
606
607 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
608
609 if (!got_tuples) {
610 /* FIXME what now? */
611 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
612 }
613
614 /* New tuples means last_tuple becomes invalid */
615 vsub->last_tuple = NULL;
616}
617
618/* Handle SendAuthInfo Result/Error from HLR */
619static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
620 const struct osmo_gsup_message *gsup)
621{
622 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
623 void *data = (void *) gsup;
624
625 switch (gsup->message_type) {
626 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
627 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
628 break;
629 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
630 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
631 break;
632 default:
633 return -1;
634 }
635
636 return 0;
637}
638
639static int decode_bcd_number_safe(char *output, int output_len,
640 const uint8_t *bcd_lv, int input_len,
641 int h_len)
642{
643 uint8_t len;
644 OSMO_ASSERT(output_len >= 1);
645 *output = '\0';
646 if (input_len < 1)
647 return -EIO;
648 len = bcd_lv[0];
649 if (input_len < len)
650 return -EIO;
651 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
652}
653
654static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
655 const struct osmo_gsup_message *gsup_msg)
656{
657 unsigned idx;
658 int rc;
659
Maxa263bb22017-12-27 13:23:44 +0100660 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200661 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
662 gsup_msg->msisdn_enc,
663 gsup_msg->msisdn_enc_len, 0);
664 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
665 vsub->imsi, vsub->msisdn);
666 }
667
668 if (gsup_msg->hlr_enc) {
669 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
670 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
671 gsup_msg->hlr_enc_len);
672 vsub->hlr.len = 0;
673 } else {
674 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
675 gsup_msg->hlr_enc_len);
676 vsub->hlr.len = gsup_msg->hlr_enc_len;
677 }
678 }
679
680 if (gsup_msg->pdp_info_compl) {
681 rc = vlr_subscr_pdp_data_clear(vsub);
682 if (rc > 0)
683 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
684 }
685
686 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
687 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
688 size_t ctx_id = pdp_info->context_id;
689 struct sgsn_subscriber_pdp_data *pdp_data;
690
691 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
692 LOGVSUBP(LOGL_ERROR, vsub,
693 "APN too long, context id = %zu, APN = %s\n",
694 ctx_id, osmo_hexdump(pdp_info->apn_enc,
695 pdp_info->apn_enc_len));
696 continue;
697 }
698
699 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
700 LOGVSUBP(LOGL_ERROR, vsub,
701 "QoS info too long (%zu)\n",
702 pdp_info->qos_enc_len);
703 continue;
704 }
705
706 LOGVSUBP(LOGL_INFO, vsub,
707 "Will set PDP info, context id = %zu, APN = %s\n",
708 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
709
710 /* Set PDP info [ctx_id] */
711 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
712 if (!pdp_data) {
713 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
714 pdp_data->context_id = ctx_id;
715 }
716
717 OSMO_ASSERT(pdp_data != NULL);
718 pdp_data->pdp_type = pdp_info->pdp_type;
719 osmo_apn_to_str(pdp_data->apn_str,
720 pdp_info->apn_enc, pdp_info->apn_enc_len);
721 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
722 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
723 }
724}
725
726
727/* Handle InsertSubscrData Result from HLR */
728static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
729 const struct osmo_gsup_message *gsup)
730{
731 struct osmo_gsup_message gsup_reply = {0};
732
733 vlr_subscr_gsup_insert_data(vsub, gsup);
734 vsub->vlr->ops.subscr_update(vsub);
735
736 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
737 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
738}
739
740/* Handle UpdateLocation Result from HLR */
741static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
742 const struct osmo_gsup_message *gsup)
743{
744 if (!vsub->lu_fsm) {
745 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
746 "without LU in progress\n");
747 return -ENODEV;
748 }
749
750 /* contrary to MAP, we allow piggy-backing subscriber data onto the
751 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
752 * nested INSERT SUBSCRIBER DATA transaction */
753 vlr_subscr_gsup_insert_data(vsub, gsup);
754
755 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
756
757 return 0;
758}
759
760/* Handle UpdateLocation Result from HLR */
761static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
762 const struct osmo_gsup_message *gsup)
763{
764 if (!vsub->lu_fsm) {
765 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
766 "without LU in progress\n");
767 return -ENODEV;
768 }
769
770 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
771 get_value_string(gsm48_gmm_cause_names, gsup->cause));
772
773 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
774 (void *)&gsup->cause);
775
776 return 0;
777}
778
779/* Handle LOCATION CANCEL request from HLR */
780static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
781 struct osmo_gsup_message *gsup_msg)
782{
783 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +0100784 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +0200785 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
786
787 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
788 is_update_procedure ?
789 "update procedure" : "subscription withdraw");
790
791 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +0100792 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +0200793
794 vlr_subscr_cancel(vsub, gsup_msg->cause);
795
Max770fbd22018-01-24 12:48:33 +0100796 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200797}
798
799/* Incoming handler for GSUP from HLR.
800 * Keep this function non-static for direct invocation by unit tests. */
801int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
802{
803 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
804 struct vlr_subscr *vsub;
805 struct osmo_gsup_message gsup;
806 int rc;
807
808 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
809 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
810
811 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
812 if (rc < 0) {
813 LOGP(DVLR, LOGL_ERROR,
814 "decoding GSUP message fails with error '%s' (%d)\n",
815 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100816 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200817 }
818
819 if (!gsup.imsi[0]) {
820 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +0100821 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
822 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
823 if (rc < 0)
824 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
825 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100826 rc = -GMM_CAUSE_INV_MAND_INFO;
827 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200828 }
829
830 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
831 if (!vsub) {
832 switch (gsup.message_type) {
833 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
834 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100835 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
836 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200837 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100838 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
839 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200840 }
841 }
842
843 switch (gsup.message_type) {
844 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
845 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
846 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
847 break;
848 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
849 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
850 break;
851 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
852 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
853 break;
854 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
855 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
856 break;
857 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
858 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
859 break;
860 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
861 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
862 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
863 LOGVSUBP(LOGL_ERROR, vsub,
864 "Rx GSUP msg_type=%d not yet implemented\n",
865 gsup.message_type);
866 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
867 break;
868 default:
869 LOGVSUBP(LOGL_ERROR, vsub,
870 "Rx GSUP msg_type=%d not valid at VLR/SGSN side\n",
871 gsup.message_type);
872 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
873 break;
874 }
875
876 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100877
878msgb_free_and_return:
879 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200880 return rc;
881}
882
883/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
884int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
885 const uint8_t *mi, size_t mi_len)
886{
887 char mi_string[GSM48_MI_SIZE];
888 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
889
890 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
891
892 /* update the vlr_subscr with the given identity */
893 switch (mi_type) {
894 case GSM_MI_TYPE_IMSI:
895 if (vsub->imsi[0]
896 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
897 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
898 " %s\n", mi_string);
899 } else
900 vlr_subscr_set_imsi(vsub, mi_string);
901 break;
902 case GSM_MI_TYPE_IMEI:
903 vlr_subscr_set_imei(vsub, mi_string);
904 break;
905 case GSM_MI_TYPE_IMEISV:
906 vlr_subscr_set_imeisv(vsub, mi_string);
907 break;
908 }
909
910 if (vsub->auth_fsm) {
911 switch (mi_type) {
912 case GSM_MI_TYPE_IMSI:
913 osmo_fsm_inst_dispatch(vsub->auth_fsm,
914 VLR_AUTH_E_MS_ID_IMSI, mi_string);
915 break;
916 }
917 }
918
919 if (vsub->lu_fsm) {
920 uint32_t event = 0;
921 switch (mi_type) {
922 case GSM_MI_TYPE_IMSI:
923 event = VLR_ULA_E_ID_IMSI;
924 break;
925 case GSM_MI_TYPE_IMEI:
926 event = VLR_ULA_E_ID_IMEI;
927 break;
928 case GSM_MI_TYPE_IMEISV:
929 event = VLR_ULA_E_ID_IMEISV;
930 break;
931 default:
932 OSMO_ASSERT(0);
933 break;
934 }
935 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
936 } else {
937 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
938 }
939
940 return 0;
941}
942
943/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
944int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
945{
946 if (vsub->lu_fsm) {
947 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
948 VLR_ULA_E_NEW_TMSI_ACK, NULL);
949 } else if (vsub->proc_arq_fsm) {
950 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
951 PR_ARQ_E_TMSI_ACK, NULL);
952 } else {
953 LOGVSUBP(LOGL_NOTICE, vsub,
954 "gratuitous TMSI REALLOC COMPL");
955 return -EINVAL;
956 }
957}
958
Maxdcc193d2017-12-27 19:34:15 +0100959bool vlr_subscr_expire(struct vlr_subscr *vsub)
960{
961 if (vsub->lu_complete) {
962 vsub->lu_complete = false;
963 vlr_subscr_put(vsub);
964
965 return true;
966 }
967
968 return false;
969}
970
Harald Welteb8b85a12016-06-17 00:06:42 +0200971int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
972{
973 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
974 vlr_subscr_cancel(vsub, GMM_CAUSE_IMPL_DETACHED);
975
976 vsub->imsi_detached_flag = true;
Maxdcc193d2017-12-27 19:34:15 +0100977
978 /* balancing the get from vlr_lu_compl_fsm_success() */
979 vlr_subscr_expire(vsub);
980
Harald Welteb8b85a12016-06-17 00:06:42 +0200981 return 0;
982}
983
984/* Tear down any running FSMs due to MSC connection timeout.
985 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
986 * message before the entire connection is torn down.
987 * \param[in] vsub subscriber to tear down
988 */
989void vlr_subscr_conn_timeout(struct vlr_subscr *vsub)
990{
991 if (!vsub)
992 return;
993
Neels Hofmeyr3bae8362017-11-18 23:26:24 +0100994 vlr_subscr_get(vsub);
995 if (vsub->lu_fsm)
996 vlr_loc_update_conn_timeout(vsub->lu_fsm);
997 if (vsub->proc_arq_fsm)
998 vlr_parq_conn_timeout(vsub->proc_arq_fsm);
999 vlr_subscr_put(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +02001000}
1001
1002struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1003{
1004 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1005 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001006
1007 /* Some of these are needed only on UTRAN, but in case the caller wants
1008 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001009 OSMO_ASSERT(ops->tx_auth_req);
1010 OSMO_ASSERT(ops->tx_auth_rej);
1011 OSMO_ASSERT(ops->tx_id_req);
1012 OSMO_ASSERT(ops->tx_lu_acc);
1013 OSMO_ASSERT(ops->tx_lu_rej);
1014 OSMO_ASSERT(ops->tx_cm_serv_acc);
1015 OSMO_ASSERT(ops->tx_cm_serv_rej);
1016 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001017 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001018 OSMO_ASSERT(ops->subscr_update);
1019 OSMO_ASSERT(ops->subscr_assoc);
1020
1021 INIT_LLIST_HEAD(&vlr->subscribers);
1022 INIT_LLIST_HEAD(&vlr->operations);
1023 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1024
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001025 /* defaults */
1026 vlr->cfg.assign_tmsi = true;
1027
Harald Welteb8b85a12016-06-17 00:06:42 +02001028 /* osmo_auth_fsm.c */
1029 osmo_fsm_register(&vlr_auth_fsm);
1030 /* osmo_lu_fsm.c */
1031 vlr_lu_fsm_init();
1032 /* vlr_access_request_fsm.c */
1033 vlr_parq_fsm_init();
1034
1035 return vlr;
1036}
1037
1038int vlr_start(const char *gsup_unit_name, struct vlr_instance *vlr,
1039 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1040{
1041 OSMO_ASSERT(vlr);
1042
1043 vlr->gsup_client = gsup_client_create(gsup_unit_name,
1044 gsup_server_addr_str,
1045 gsup_server_port,
1046 &vlr_gsupc_read_cb, NULL);
1047 if (!vlr->gsup_client)
1048 return -ENOMEM;
1049 vlr->gsup_client->data = vlr;
1050
1051 return 0;
1052}
1053
1054/* MSC->VLR: Subscribre has disconnected */
1055int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1056{
1057 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1058 * interface */
1059 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1060 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1061 vsub->msc_conn_ref = NULL;
1062
1063 return 0;
1064}
1065
1066/* MSC->VLR: Receive Authentication Failure from Subscriber */
1067int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1068{
1069 struct vlr_auth_resp_par par = {0};
1070 par.auts = auts;
1071
1072 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1073 return 0;
1074}
1075
1076/* MSC->VLR: Receive Authentication Response from MS
1077 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1078int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1079 bool is_utran, const uint8_t *res, uint8_t res_len)
1080{
1081 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1082 struct vlr_auth_resp_par par;
1083
1084 par.is_r99 = is_r99;
1085 par.is_utran = is_utran;
1086 par.res = res;
1087 par.res_len = res_len;
1088 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1089
1090 return 0;
1091}
1092
1093/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1094void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1095{
1096 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1097 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1098 if (vsub->proc_arq_fsm
1099 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1100 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1101 res);
1102}
1103
1104/* Internal evaluation of requested ciphering mode.
1105 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1106 * \param[in] vlr VLR instance.
1107 * \param[in] fi Calling FSM instance, for logging.
1108 * \param[in] msc_conn_ref MSC conn to send to.
1109 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1110 * \returns 0 if no ciphering is needed or message was sent successfully,
1111 * or a negative value if ciph_mode is invalid or sending failed.
1112 */
1113int vlr_set_ciph_mode(struct vlr_instance *vlr,
1114 struct osmo_fsm_inst *fi,
1115 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001116 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001117 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001118 bool retrieve_imeisv)
1119{
Harald Welte71c51df2017-12-23 18:51:48 +01001120 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001121 return 0;
1122
Harald Welte71c51df2017-12-23 18:51:48 +01001123 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1124 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001125}
1126
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001127/* Decide whether UMTS AKA should be used.
1128 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1129 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1130 * GERAN are R99 capable and UMTS AKA tokens are available.
1131 * \param[in] vec Auth tokens (received from the HLR).
1132 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1133 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1134 */
1135bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1136{
1137 if (!is_r99)
1138 return false;
1139 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1140 return false;
1141 return true;
1142}
1143
Harald Welteb8b85a12016-06-17 00:06:42 +02001144void log_set_filter_vlr_subscr(struct log_target *target,
1145 struct vlr_subscr *vlr_subscr)
1146{
1147 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1148
1149 /* free the old data */
1150 if (*fsub) {
1151 vlr_subscr_put(*fsub);
1152 *fsub = NULL;
1153 }
1154
1155 if (vlr_subscr) {
1156 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1157 *fsub = vlr_subscr_get(vlr_subscr);
1158 } else
1159 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1160}