blob: 29098b15b5b543c2fb643c969db16c85f6e57af2 [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>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/gsm_subscriber.h>
31#include <osmocom/msc/gsup_client.h>
32#include <osmocom/msc/vlr.h>
33#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020034
Harald Welteb8b85a12016-06-17 00:06:42 +020035#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020038#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020039
40#include "vlr_core.h"
41#include "vlr_auth_fsm.h"
42#include "vlr_lu_fsm.h"
43#include "vlr_access_req_fsm.h"
44
45#define SGSN_SUBSCR_MAX_RETRIES 3
46#define SGSN_SUBSCR_RETRY_INTERVAL 10
47
48/***********************************************************************
49 * Convenience functions
50 ***********************************************************************/
51
52const struct value_string vlr_ciph_names[] = {
53 OSMO_VALUE_STRING(VLR_CIPH_NONE),
54 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
55 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
56 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
57 { 0, NULL }
58};
59
60uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
61{
62 uint32_t tidx = 0xffffffff;
63
64 switch (timer) {
65 case 3270:
66 tidx = VLR_T_3270;
67 break;
68 case 3260:
69 tidx = VLR_T_3260;
70 break;
71 case 3250:
72 tidx = VLR_T_3250;
73 break;
74 }
75
76 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
77 return vlr->cfg.timer[tidx];
78}
79
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010080/* return static buffer with printable name of VLR subscriber */
81const char *vlr_subscr_name(struct vlr_subscr *vsub)
82{
83 static char buf[32];
84 if (!vsub)
85 return "unknown";
86 if (vsub->msisdn[0])
87 snprintf(buf, sizeof(buf), "MSISDN:%s", vsub->msisdn);
88 else if (vsub->imsi[0])
89 snprintf(buf, sizeof(buf), "IMSI:%s", vsub->imsi);
90 else if (vsub->tmsi != GSM_RESERVED_TMSI)
91 snprintf(buf, sizeof(buf), "TMSI:0x%08x", vsub->tmsi);
92 else if (vsub->tmsi_new != GSM_RESERVED_TMSI)
93 snprintf(buf, sizeof(buf), "TMSI(new):0x%08x", vsub->tmsi_new);
94 else
95 return "unknown";
96 buf[sizeof(buf)-1] = '\0';
97 return buf;
98}
99
100const char *vlr_subscr_msisdn_or_name(struct vlr_subscr *vsub)
101{
102 if (!vsub || !vsub->msisdn[0])
103 return vlr_subscr_name(vsub);
104 return vsub->msisdn;
105}
106
Harald Welteb8b85a12016-06-17 00:06:42 +0200107struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
108 const char *imsi,
109 const char *file, int line)
110{
111 struct vlr_subscr *vsub;
112
113 if (!imsi || !*imsi)
114 return NULL;
115
116 llist_for_each_entry(vsub, &vlr->subscribers, list) {
117 if (vlr_subscr_matches_imsi(vsub, imsi))
118 return _vlr_subscr_get(vsub, file, line);
119 }
120 return NULL;
121}
122
123struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
124 uint32_t tmsi,
125 const char *file, int line)
126{
127 struct vlr_subscr *vsub;
128
129 if (tmsi == GSM_RESERVED_TMSI)
130 return NULL;
131
132 llist_for_each_entry(vsub, &vlr->subscribers, list) {
133 if (vlr_subscr_matches_tmsi(vsub, tmsi))
134 return _vlr_subscr_get(vsub, file, line);
135 }
136 return NULL;
137}
138
139struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
140 const char *msisdn,
141 const char *file, int line)
142{
143 struct vlr_subscr *vsub;
144
145 if (!msisdn || !*msisdn)
146 return NULL;
147
148 llist_for_each_entry(vsub, &vlr->subscribers, list) {
149 if (vlr_subscr_matches_msisdn(vsub, msisdn))
150 return _vlr_subscr_get(vsub, file, line);
151 }
152 return NULL;
153}
154
155/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100156static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
157 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200158{
159 struct msgb *msg = gsup_client_msgb_alloc();
160
Max770fbd22018-01-24 12:48:33 +0100161 int rc = osmo_gsup_encode(msg, gsup_msg);
162 if (rc < 0) {
163 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
164 return rc;
165 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200166
167 if (!vlr->gsup_client) {
168 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
169 "send GSUP: %s\n", msgb_hexdump(msg));
170 msgb_free(msg);
171 return -ENOTSUP;
172 }
173
174 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
175 osmo_hexdump_nospc(msg->data, msg->len));
176
177 return gsup_client_send(vlr->gsup_client, msg);
178}
179
180/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100181static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200182 struct osmo_gsup_message *gsup_msg)
183{
184 struct vlr_instance *vlr = vsub->vlr;
185
186 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100187 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200188
189 return vlr_tx_gsup_message(vlr, gsup_msg);
190}
191
192/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100193static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200194 struct osmo_gsup_message *gsup_orig,
195 enum gsm48_gmm_cause cause)
196{
197 struct osmo_gsup_message gsup_reply = {0};
198
Max98f74672018-02-05 12:57:06 +0100199 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200200 gsup_reply.cause = cause;
201 gsup_reply.message_type =
202 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
203
204 return vlr_tx_gsup_message(vlr, &gsup_reply);
205}
206
207struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
208{
209 if (!sub)
210 return NULL;
211 OSMO_ASSERT(sub->use_count < INT_MAX);
212 sub->use_count++;
213 LOGPSRC(DREF, LOGL_DEBUG, file, line,
214 "VLR subscr %s usage increases to: %d\n",
215 vlr_subscr_name(sub), sub->use_count);
216 return sub;
217}
218
219struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
220{
221 if (!sub)
222 return NULL;
223 sub->use_count--;
224 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
225 file, line,
226 "VLR subscr %s usage decreases to: %d\n",
227 vlr_subscr_name(sub), sub->use_count);
228 if (sub->use_count <= 0)
229 vlr_subscr_free(sub);
230 return NULL;
231}
232
233/* Allocate a new subscriber and insert it into list */
234static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
235{
236 struct vlr_subscr *vsub;
237 int i;
238
239 vsub = talloc_zero(vlr, struct vlr_subscr);
240 vsub->vlr = vlr;
241 vsub->tmsi = GSM_RESERVED_TMSI;
242 vsub->tmsi_new = GSM_RESERVED_TMSI;
243
244 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
245 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
246
247 INIT_LLIST_HEAD(&vsub->cs.requests);
248 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
249
250 llist_add_tail(&vsub->list, &vlr->subscribers);
251 return vsub;
252}
253
254struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
255{
256 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
257}
258
259/* Send a GSUP Purge MS request.
260 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
261 * this subscriber, not to the HLR? */
262int vlr_subscr_purge(struct vlr_subscr *vsub)
263{
264 struct osmo_gsup_message gsup_msg = {0};
265
266 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
267
268 /* provide HLR number in case we know it */
269 gsup_msg.hlr_enc_len = vsub->hlr.len;
270 gsup_msg.hlr_enc = vsub->hlr.buf;
271
272 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
273}
274
Neels Hofmeyr15809592018-04-06 02:57:51 +0200275void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
276 enum osmo_fsm_term_cause fsm_cause,
277 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200278{
279 if (!vsub)
280 return;
281
Neels Hofmeyr15809592018-04-06 02:57:51 +0200282 vlr_subscr_get(vsub);
283 if (vsub->lu_fsm)
284 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200285 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200286 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
287 vlr_subscr_put(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200288}
289
290/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
291void vlr_subscr_free(struct vlr_subscr *vsub)
292{
293 llist_del(&vsub->list);
294 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
295 talloc_free(vsub);
296}
297
298/* Generate a new TMSI and store in vsub->tmsi_new.
299 * Search all known subscribers to ensure that the TMSI is unique. */
300int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
301{
302 struct vlr_instance *vlr = vsub->vlr;
303 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100304 int tried, rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200305
306 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100307 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
308 if (rc < 0) {
309 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
310 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200311 }
312 /* throw the dice again, if the TSMI doesn't fit */
313 if (tmsi == GSM_RESERVED_TMSI)
314 continue;
315
316 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
317 if (vlr->cfg.is_ps) {
318 /* SGSN */
319 tmsi |= 0xC000000;
320 } else {
321 /* MSC */
322 if ((tmsi & 0xC0000000) == 0xC0000000)
323 tmsi &= ~0xC0000000;
324 }
325
326 /* If this TMSI is already in use, try another one. */
327 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
328 continue;
329
330 vsub->tmsi_new = tmsi;
331 return 0;
332 }
333
334 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
335 " after %d tries\n", vlr_subscr_name(vsub), tried);
336 return -1;
337}
338
339/* Find subscriber by IMSI, or create new subscriber if not found.
340 * \param[in] vlr VLR instace.
341 * \param[in] imsi IMSI string.
342 * \param[out] created if non-NULL, returns whether a new entry was created. */
343struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
344 const char *imsi,
345 bool *created,
346 const char *file,
347 int line)
348{
349 struct vlr_subscr *vsub;
350 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
351 if (vsub) {
352 if (created)
353 *created = false;
354 return vsub;
355 }
356
357 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
358 if (!vsub)
359 return NULL;
360 vlr_subscr_set_imsi(vsub, imsi);
361 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
362 if (created)
363 *created = true;
364 return vsub;
365}
366
367/* Find subscriber by TMSI, or create new subscriber if not found.
368 * \param[in] vlr VLR instace.
369 * \param[in] tmsi TMSI.
370 * \param[out] created if non-NULL, returns whether a new entry was created. */
371struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
372 uint32_t tmsi,
373 bool *created,
374 const char *file,
375 int line)
376{
377 struct vlr_subscr *vsub;
378 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
379 if (vsub) {
380 if (created)
381 *created = false;
382 return vsub;
383 }
384
385 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
386 if (!vsub)
387 return NULL;
388 vsub->tmsi = tmsi;
389 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
390 if (created)
391 *created = true;
392 return vsub;
393}
394
395void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
396{
397 if (!vsub)
398 return;
Max98f74672018-02-05 12:57:06 +0100399 OSMO_STRLCPY_ARRAY(vsub->imsi, imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200400 vsub->id = atoll(vsub->imsi);
401 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
402 vsub->imsi, vsub->id);
403}
404
405void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
406{
407 if (!vsub)
408 return;
Max98f74672018-02-05 12:57:06 +0100409 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200410 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
411 vsub->imsi, vsub->imei);
412}
413
414void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
415{
416 if (!vsub)
417 return;
Max98f74672018-02-05 12:57:06 +0100418 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200419 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
420 vsub->imsi, vsub->imeisv);
421}
422
423/* Safely copy the given MSISDN string to vsub->msisdn */
424void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
425{
426 if (!vsub)
427 return;
Max98f74672018-02-05 12:57:06 +0100428 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200429 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
430 vsub->imsi, vsub->msisdn);
431}
432
433bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
434{
435 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
436}
437
438bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
439{
440 return vsub && tmsi != GSM_RESERVED_TMSI
441 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
442}
443
444bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
445{
446 return vsub && msisdn && vsub->msisdn[0]
447 && !strcmp(vsub->msisdn, msisdn);
448}
449
450bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
451{
452 return vsub && imei && vsub->imei[0]
453 && !strcmp(vsub->imei, imei);
454}
455
456/* Send updated subscriber information to HLR */
457int vlr_subscr_changed(struct vlr_subscr *vsub)
458{
459 /* FIXME */
460 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
461 return 0;
462}
463
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200464void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
465{
466 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
467 struct timespec now;
468
469 /* The T3212 timeout value field is coded as the binary representation of the timeout
470 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
471 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
472 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
473 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
474 } else {
475 LOGP(DVLR, LOGL_ERROR,
476 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
477 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
478 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
479 }
480}
481
482void vlr_subscr_expire_lu(void *data)
483{
484 struct vlr_instance *vlr = data;
485 struct vlr_subscr *vsub, *vsub_tmp;
486 struct timespec now;
487
488 if (llist_empty(&vlr->subscribers))
489 goto done;
490
491 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
492 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
493 goto done;
494 }
495
496 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
497 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
498 continue;
499
500 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
501 vlr_subscr_rx_imsi_detach(vsub);
502 }
503
504done:
505 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
506}
507
Harald Welteb8b85a12016-06-17 00:06:42 +0200508/***********************************************************************
509 * PDP context data
510 ***********************************************************************/
511
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200512#define GSM_APN_LENGTH 102
513
514/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
515/* see GSM 09.02, B.1, gprsSubscriptionData */
516struct sgsn_subscriber_pdp_data {
517 struct llist_head list;
518
519 unsigned int context_id;
520 uint16_t pdp_type;
521 char apn_str[GSM_APN_LENGTH];
522 uint8_t qos_subscribed[20];
523 size_t qos_subscribed_len;
524};
525
Harald Welteb8b85a12016-06-17 00:06:42 +0200526struct sgsn_subscriber_pdp_data *
527vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
528{
529 struct sgsn_subscriber_pdp_data* pdata;
530
531 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
532
533 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
534
535 return pdata;
536}
537
538static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
539{
540 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
541 int count = 0;
542
543 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
544 llist_del(&pdp->list);
545 talloc_free(pdp);
546 count += 1;
547 }
548
549 return count;
550}
551
552static struct sgsn_subscriber_pdp_data *
553vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
554{
555 struct sgsn_subscriber_pdp_data *pdp;
556
557 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
558 if (pdp->context_id == context_id)
559 return pdp;
560 }
561
562 return NULL;
563}
564
565/***********************************************************************
566 * Actual Implementation
567 ***********************************************************************/
568
569static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
570 struct osmo_gsup_message *gsup_msg)
571{
572 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Max770fbd22018-01-24 12:48:33 +0100573 int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
574 if (rc < 0)
575 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi);
576
Harald Welteb8b85a12016-06-17 00:06:42 +0200577 LOGP(DVLR, LOGL_NOTICE,
578 "Unknown IMSI %s, discarding GSUP request "
579 "of type 0x%02x\n",
580 gsup_msg->imsi, gsup_msg->message_type);
581 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
582 LOGP(DVLR, LOGL_NOTICE,
583 "Unknown IMSI %s, discarding GSUP error "
584 "of type 0x%02x, cause '%s' (%d)\n",
585 gsup_msg->imsi, gsup_msg->message_type,
586 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
587 gsup_msg->cause);
588 } else {
589 LOGP(DVLR, LOGL_NOTICE,
590 "Unknown IMSI %s, discarding GSUP response "
591 "of type 0x%02x\n",
592 gsup_msg->imsi, gsup_msg->message_type);
593 }
594
595 return -GMM_CAUSE_IMSI_UNKNOWN;
596}
597
598static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
599 struct osmo_gsup_message *gsup_msg)
600{
601 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
602 LOGGSUPP(LOGL_NOTICE, gsup_msg,
603 "Purge MS has failed with cause '%s' (%d)\n",
604 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
605 gsup_msg->cause);
606 return -gsup_msg->cause;
607 }
608 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
609 return 0;
610}
611
612/* VLR internal call to request UpdateLocation from HLR */
613int vlr_subscr_req_lu(struct vlr_subscr *vsub, bool is_ps)
614{
615 struct osmo_gsup_message gsup_msg = {0};
616 int rc;
617
618 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
619 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
620
621 return rc;
622}
623
624/* VLR internal call to request tuples from HLR */
625int vlr_subscr_req_sai(struct vlr_subscr *vsub,
626 const uint8_t *auts, const uint8_t *auts_rand)
627{
628 struct osmo_gsup_message gsup_msg = {0};
629
630 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
631 gsup_msg.auts = auts;
632 gsup_msg.rand = auts_rand;
633
634 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
635}
636
637/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100638int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200639{
640 struct osmo_gsup_message gsup_msg = {0};
641
642 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100643 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200644 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
645}
646
647/* Update the subscriber with GSUP-received auth tuples */
648void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
649 const struct osmo_gsup_message *gsup)
650{
651 unsigned int i;
652 unsigned int got_tuples;
653
654 if (gsup->num_auth_vectors) {
655 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
656 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
657 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
658 }
659
660 got_tuples = 0;
661 for (i = 0; i < gsup->num_auth_vectors; i++) {
662 size_t key_seq = i;
663
664 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
665 LOGVSUBP(LOGL_NOTICE, vsub,
666 "Skipping auth tuple wih invalid cksn %zu\n",
667 key_seq);
668 continue;
669 }
670 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
671 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100672 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200673 }
674
675 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
676
677 if (!got_tuples) {
678 /* FIXME what now? */
679 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
680 }
681
682 /* New tuples means last_tuple becomes invalid */
683 vsub->last_tuple = NULL;
684}
685
686/* Handle SendAuthInfo Result/Error from HLR */
687static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
688 const struct osmo_gsup_message *gsup)
689{
690 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
691 void *data = (void *) gsup;
692
693 switch (gsup->message_type) {
694 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
695 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
696 break;
697 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
698 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
699 break;
700 default:
701 return -1;
702 }
703
704 return 0;
705}
706
707static int decode_bcd_number_safe(char *output, int output_len,
708 const uint8_t *bcd_lv, int input_len,
709 int h_len)
710{
711 uint8_t len;
712 OSMO_ASSERT(output_len >= 1);
713 *output = '\0';
714 if (input_len < 1)
715 return -EIO;
716 len = bcd_lv[0];
717 if (input_len < len)
718 return -EIO;
719 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
720}
721
722static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
723 const struct osmo_gsup_message *gsup_msg)
724{
725 unsigned idx;
726 int rc;
727
Maxa263bb22017-12-27 13:23:44 +0100728 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200729 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
730 gsup_msg->msisdn_enc,
731 gsup_msg->msisdn_enc_len, 0);
732 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
733 vsub->imsi, vsub->msisdn);
734 }
735
736 if (gsup_msg->hlr_enc) {
737 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
738 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
739 gsup_msg->hlr_enc_len);
740 vsub->hlr.len = 0;
741 } else {
742 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
743 gsup_msg->hlr_enc_len);
744 vsub->hlr.len = gsup_msg->hlr_enc_len;
745 }
746 }
747
748 if (gsup_msg->pdp_info_compl) {
749 rc = vlr_subscr_pdp_data_clear(vsub);
750 if (rc > 0)
751 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
752 }
753
754 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
755 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
756 size_t ctx_id = pdp_info->context_id;
757 struct sgsn_subscriber_pdp_data *pdp_data;
758
759 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
760 LOGVSUBP(LOGL_ERROR, vsub,
761 "APN too long, context id = %zu, APN = %s\n",
762 ctx_id, osmo_hexdump(pdp_info->apn_enc,
763 pdp_info->apn_enc_len));
764 continue;
765 }
766
767 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
768 LOGVSUBP(LOGL_ERROR, vsub,
769 "QoS info too long (%zu)\n",
770 pdp_info->qos_enc_len);
771 continue;
772 }
773
774 LOGVSUBP(LOGL_INFO, vsub,
775 "Will set PDP info, context id = %zu, APN = %s\n",
776 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
777
778 /* Set PDP info [ctx_id] */
779 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
780 if (!pdp_data) {
781 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
782 pdp_data->context_id = ctx_id;
783 }
784
785 OSMO_ASSERT(pdp_data != NULL);
786 pdp_data->pdp_type = pdp_info->pdp_type;
787 osmo_apn_to_str(pdp_data->apn_str,
788 pdp_info->apn_enc, pdp_info->apn_enc_len);
789 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
790 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
791 }
792}
793
794
795/* Handle InsertSubscrData Result from HLR */
796static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
797 const struct osmo_gsup_message *gsup)
798{
799 struct osmo_gsup_message gsup_reply = {0};
800
801 vlr_subscr_gsup_insert_data(vsub, gsup);
802 vsub->vlr->ops.subscr_update(vsub);
803
804 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
805 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
806}
807
808/* Handle UpdateLocation Result from HLR */
809static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
810 const struct osmo_gsup_message *gsup)
811{
812 if (!vsub->lu_fsm) {
813 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
814 "without LU in progress\n");
815 return -ENODEV;
816 }
817
818 /* contrary to MAP, we allow piggy-backing subscriber data onto the
819 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
820 * nested INSERT SUBSCRIBER DATA transaction */
821 vlr_subscr_gsup_insert_data(vsub, gsup);
822
823 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
824
825 return 0;
826}
827
828/* Handle UpdateLocation Result from HLR */
829static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
830 const struct osmo_gsup_message *gsup)
831{
832 if (!vsub->lu_fsm) {
833 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
834 "without LU in progress\n");
835 return -ENODEV;
836 }
837
838 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
839 get_value_string(gsm48_gmm_cause_names, gsup->cause));
840
841 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
842 (void *)&gsup->cause);
843
844 return 0;
845}
846
Neels Hofmeyr15809592018-04-06 02:57:51 +0200847static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
848 enum osmo_fsm_term_cause *fsm_cause_p,
849 enum gsm48_reject_value *gsm48_rej_p)
850{
851 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
852 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
853 switch (gmm_cause) {
854 case GMM_CAUSE_IMSI_UNKNOWN:
855 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
856 break;
857 case GMM_CAUSE_ILLEGAL_MS:
858 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
859 break;
860 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
861 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
862 break;
863 case GMM_CAUSE_ILLEGAL_ME:
864 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
865 break;
866 case GMM_CAUSE_GPRS_NOTALLOWED:
867 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
868 break;
869 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
870 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
871 break;
872 case GMM_CAUSE_MS_ID_NOT_DERIVED:
873 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
874 break;
875 case GMM_CAUSE_IMPL_DETACHED:
876 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
877 break;
878 case GMM_CAUSE_PLMN_NOTALLOWED:
879 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
880 break;
881 case GMM_CAUSE_LA_NOTALLOWED:
882 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
883 break;
884 case GMM_CAUSE_ROAMING_NOTALLOWED:
885 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
886 break;
887 case GMM_CAUSE_NO_GPRS_PLMN:
888 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
889 break;
890 case GMM_CAUSE_MSC_TEMP_NOTREACH:
891 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
892 break;
893 case GMM_CAUSE_SYNC_FAIL:
894 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
895 break;
896 case GMM_CAUSE_CONGESTION:
897 gsm48_rej = GSM48_REJECT_CONGESTION;
898 break;
899 case GMM_CAUSE_SEM_INCORR_MSG:
900 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
901 break;
902 case GMM_CAUSE_INV_MAND_INFO:
903 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
904 break;
905 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
906 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
907 break;
908 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
909 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
910 break;
911 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
912 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
913 break;
914 case GMM_CAUSE_COND_IE_ERR:
915 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
916 break;
917 case GMM_CAUSE_MSG_INCOMP_P_STATE:
918 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
919 break;
920 case GMM_CAUSE_PROTO_ERR_UNSPEC:
921 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
922 break;
923
924 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
925 case GMM_CAUSE_MAC_FAIL:
926 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
927 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
928 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
929 case GMM_CAUSE_NO_PDP_ACTIVATED:
930 case GMM_CAUSE_NET_FAIL:
931 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
932 break;
933 }
934 switch (gmm_cause) {
935 /* refine any error causes here? */
936 default:
937 fsm_cause = OSMO_FSM_TERM_ERROR;
938 break;
939 }
940 if (fsm_cause_p)
941 *fsm_cause_p = fsm_cause;
942 if (gsm48_rej_p)
943 *gsm48_rej_p = gsm48_rej;
944}
945
Harald Welteb8b85a12016-06-17 00:06:42 +0200946/* Handle LOCATION CANCEL request from HLR */
947static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
948 struct osmo_gsup_message *gsup_msg)
949{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200950 enum gsm48_reject_value gsm48_rej;
951 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +0200952 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +0100953 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +0200954 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
955
956 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
957 is_update_procedure ?
958 "update procedure" : "subscription withdraw");
959
960 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +0100961 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +0200962
Neels Hofmeyr15809592018-04-06 02:57:51 +0200963 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
964 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +0200965
Max770fbd22018-01-24 12:48:33 +0100966 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200967}
968
969/* Incoming handler for GSUP from HLR.
970 * Keep this function non-static for direct invocation by unit tests. */
971int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
972{
973 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
974 struct vlr_subscr *vsub;
975 struct osmo_gsup_message gsup;
976 int rc;
977
978 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
979 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
980
981 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
982 if (rc < 0) {
983 LOGP(DVLR, LOGL_ERROR,
984 "decoding GSUP message fails with error '%s' (%d)\n",
985 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100986 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200987 }
988
989 if (!gsup.imsi[0]) {
990 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +0100991 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
992 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
993 if (rc < 0)
994 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
995 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100996 rc = -GMM_CAUSE_INV_MAND_INFO;
997 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200998 }
999
1000 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
1001 if (!vsub) {
1002 switch (gsup.message_type) {
1003 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1004 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001005 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1006 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001007 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001008 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1009 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001010 }
1011 }
1012
1013 switch (gsup.message_type) {
1014 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1015 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1016 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1017 break;
1018 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1019 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1020 break;
1021 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1022 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1023 break;
1024 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1025 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1026 break;
1027 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1028 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1029 break;
1030 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1031 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1032 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1033 LOGVSUBP(LOGL_ERROR, vsub,
1034 "Rx GSUP msg_type=%d not yet implemented\n",
1035 gsup.message_type);
1036 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1037 break;
1038 default:
1039 LOGVSUBP(LOGL_ERROR, vsub,
1040 "Rx GSUP msg_type=%d not valid at VLR/SGSN side\n",
1041 gsup.message_type);
1042 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1043 break;
1044 }
1045
1046 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001047
1048msgb_free_and_return:
1049 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001050 return rc;
1051}
1052
1053/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1054int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1055 const uint8_t *mi, size_t mi_len)
1056{
1057 char mi_string[GSM48_MI_SIZE];
1058 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1059
1060 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1061
1062 /* update the vlr_subscr with the given identity */
1063 switch (mi_type) {
1064 case GSM_MI_TYPE_IMSI:
1065 if (vsub->imsi[0]
1066 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1067 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1068 " %s\n", mi_string);
1069 } else
1070 vlr_subscr_set_imsi(vsub, mi_string);
1071 break;
1072 case GSM_MI_TYPE_IMEI:
1073 vlr_subscr_set_imei(vsub, mi_string);
1074 break;
1075 case GSM_MI_TYPE_IMEISV:
1076 vlr_subscr_set_imeisv(vsub, mi_string);
1077 break;
1078 }
1079
1080 if (vsub->auth_fsm) {
1081 switch (mi_type) {
1082 case GSM_MI_TYPE_IMSI:
1083 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1084 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1085 break;
1086 }
1087 }
1088
1089 if (vsub->lu_fsm) {
1090 uint32_t event = 0;
1091 switch (mi_type) {
1092 case GSM_MI_TYPE_IMSI:
1093 event = VLR_ULA_E_ID_IMSI;
1094 break;
1095 case GSM_MI_TYPE_IMEI:
1096 event = VLR_ULA_E_ID_IMEI;
1097 break;
1098 case GSM_MI_TYPE_IMEISV:
1099 event = VLR_ULA_E_ID_IMEISV;
1100 break;
1101 default:
1102 OSMO_ASSERT(0);
1103 break;
1104 }
1105 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1106 } else {
1107 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1108 }
1109
1110 return 0;
1111}
1112
1113/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1114int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1115{
1116 if (vsub->lu_fsm) {
1117 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1118 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1119 } else if (vsub->proc_arq_fsm) {
1120 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1121 PR_ARQ_E_TMSI_ACK, NULL);
1122 } else {
1123 LOGVSUBP(LOGL_NOTICE, vsub,
1124 "gratuitous TMSI REALLOC COMPL");
1125 return -EINVAL;
1126 }
1127}
1128
Maxdcc193d2017-12-27 19:34:15 +01001129bool vlr_subscr_expire(struct vlr_subscr *vsub)
1130{
1131 if (vsub->lu_complete) {
1132 vsub->lu_complete = false;
1133 vlr_subscr_put(vsub);
1134
1135 return true;
1136 }
1137
1138 return false;
1139}
1140
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001141/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001142int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1143{
1144 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001145 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001146
1147 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001148 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001149
1150 /* balancing the get from vlr_lu_compl_fsm_success() */
1151 vlr_subscr_expire(vsub);
1152
Harald Welteb8b85a12016-06-17 00:06:42 +02001153 return 0;
1154}
1155
1156/* Tear down any running FSMs due to MSC connection timeout.
1157 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1158 * message before the entire connection is torn down.
1159 * \param[in] vsub subscriber to tear down
1160 */
1161void vlr_subscr_conn_timeout(struct vlr_subscr *vsub)
1162{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001163 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001164}
1165
1166struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1167{
1168 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1169 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001170
1171 /* Some of these are needed only on UTRAN, but in case the caller wants
1172 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001173 OSMO_ASSERT(ops->tx_auth_req);
1174 OSMO_ASSERT(ops->tx_auth_rej);
1175 OSMO_ASSERT(ops->tx_id_req);
1176 OSMO_ASSERT(ops->tx_lu_acc);
1177 OSMO_ASSERT(ops->tx_lu_rej);
1178 OSMO_ASSERT(ops->tx_cm_serv_acc);
1179 OSMO_ASSERT(ops->tx_cm_serv_rej);
1180 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001181 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001182 OSMO_ASSERT(ops->subscr_update);
1183 OSMO_ASSERT(ops->subscr_assoc);
1184
1185 INIT_LLIST_HEAD(&vlr->subscribers);
1186 INIT_LLIST_HEAD(&vlr->operations);
1187 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1188
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001189 /* defaults */
1190 vlr->cfg.assign_tmsi = true;
1191
Harald Welteb8b85a12016-06-17 00:06:42 +02001192 /* osmo_auth_fsm.c */
1193 osmo_fsm_register(&vlr_auth_fsm);
1194 /* osmo_lu_fsm.c */
1195 vlr_lu_fsm_init();
1196 /* vlr_access_request_fsm.c */
1197 vlr_parq_fsm_init();
1198
1199 return vlr;
1200}
1201
1202int vlr_start(const char *gsup_unit_name, struct vlr_instance *vlr,
1203 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1204{
1205 OSMO_ASSERT(vlr);
1206
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001207 vlr->gsup_client = gsup_client_create(vlr, gsup_unit_name,
Harald Welteb8b85a12016-06-17 00:06:42 +02001208 gsup_server_addr_str,
1209 gsup_server_port,
1210 &vlr_gsupc_read_cb, NULL);
1211 if (!vlr->gsup_client)
1212 return -ENOMEM;
1213 vlr->gsup_client->data = vlr;
1214
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001215 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1216 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001217 return 0;
1218}
1219
1220/* MSC->VLR: Subscribre has disconnected */
1221int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1222{
1223 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1224 * interface */
1225 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1226 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1227 vsub->msc_conn_ref = NULL;
1228
1229 return 0;
1230}
1231
1232/* MSC->VLR: Receive Authentication Failure from Subscriber */
1233int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1234{
1235 struct vlr_auth_resp_par par = {0};
1236 par.auts = auts;
1237
1238 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1239 return 0;
1240}
1241
1242/* MSC->VLR: Receive Authentication Response from MS
1243 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1244int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1245 bool is_utran, const uint8_t *res, uint8_t res_len)
1246{
1247 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1248 struct vlr_auth_resp_par par;
1249
1250 par.is_r99 = is_r99;
1251 par.is_utran = is_utran;
1252 par.res = res;
1253 par.res_len = res_len;
1254 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1255
1256 return 0;
1257}
1258
1259/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1260void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1261{
1262 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1263 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1264 if (vsub->proc_arq_fsm
1265 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1266 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1267 res);
1268}
1269
1270/* Internal evaluation of requested ciphering mode.
1271 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1272 * \param[in] vlr VLR instance.
1273 * \param[in] fi Calling FSM instance, for logging.
1274 * \param[in] msc_conn_ref MSC conn to send to.
1275 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1276 * \returns 0 if no ciphering is needed or message was sent successfully,
1277 * or a negative value if ciph_mode is invalid or sending failed.
1278 */
1279int vlr_set_ciph_mode(struct vlr_instance *vlr,
1280 struct osmo_fsm_inst *fi,
1281 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001282 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001283 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001284 bool retrieve_imeisv)
1285{
Harald Welte71c51df2017-12-23 18:51:48 +01001286 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001287 return 0;
1288
Harald Welte71c51df2017-12-23 18:51:48 +01001289 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1290 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001291}
1292
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001293/* Decide whether UMTS AKA should be used.
1294 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1295 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1296 * GERAN are R99 capable and UMTS AKA tokens are available.
1297 * \param[in] vec Auth tokens (received from the HLR).
1298 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1299 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1300 */
1301bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1302{
1303 if (!is_r99)
1304 return false;
1305 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1306 return false;
1307 return true;
1308}
1309
Harald Welteb8b85a12016-06-17 00:06:42 +02001310void log_set_filter_vlr_subscr(struct log_target *target,
1311 struct vlr_subscr *vlr_subscr)
1312{
1313 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1314
1315 /* free the old data */
1316 if (*fsub) {
1317 vlr_subscr_put(*fsub);
1318 *fsub = NULL;
1319 }
1320
1321 if (vlr_subscr) {
1322 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1323 *fsub = vlr_subscr_get(vlr_subscr);
1324 } else
1325 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1326}