blob: 887602c10a239af0aed1016b9399e003e5eaf2fe [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>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/vlr.h>
34#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020035
Harald Welteb8b85a12016-06-17 00:06:42 +020036#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020039#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020040
41#include "vlr_core.h"
42#include "vlr_auth_fsm.h"
43#include "vlr_lu_fsm.h"
44#include "vlr_access_req_fsm.h"
45
46#define SGSN_SUBSCR_MAX_RETRIES 3
47#define SGSN_SUBSCR_RETRY_INTERVAL 10
48
49/***********************************************************************
50 * Convenience functions
51 ***********************************************************************/
52
53const struct value_string vlr_ciph_names[] = {
54 OSMO_VALUE_STRING(VLR_CIPH_NONE),
55 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
56 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
57 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
58 { 0, NULL }
59};
60
61uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
62{
63 uint32_t tidx = 0xffffffff;
64
65 switch (timer) {
66 case 3270:
67 tidx = VLR_T_3270;
68 break;
69 case 3260:
70 tidx = VLR_T_3260;
71 break;
72 case 3250:
73 tidx = VLR_T_3250;
74 break;
75 }
76
77 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
78 return vlr->cfg.timer[tidx];
79}
80
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010081/* return static buffer with printable name of VLR subscriber */
Oliver Smith5598aae2019-01-08 11:47:21 +010082const char *vlr_subscr_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010083{
Neels Hofmeyr361e5712019-01-03 02:32:14 +010084 static char buf[128];
85 char imsi[23] = "";
86 char msisdn[25] = "";
87 char tmsi[23] = "";
88 char tmsi_new[23] = "";
89 bool present = false;
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010090 if (!vsub)
91 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +010092 if (vsub->imsi[0]) {
93 snprintf(imsi, sizeof(imsi), "IMSI-%s", vsub->imsi);
94 present = true;
95 }
96 if (vsub->msisdn[0]) {
97 snprintf(msisdn, sizeof(msisdn), "%sMSISDN-%s", present? ":" : "", vsub->msisdn);
98 present = true;
99 }
100 if (vsub->tmsi != GSM_RESERVED_TMSI) {
101 snprintf(tmsi, sizeof(tmsi), "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi);
102 present = true;
103 }
104 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
105 snprintf(tmsi_new, sizeof(tmsi_new), "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new);
106 present = true;
107 }
108 if (!present)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100109 return "unknown";
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100110
111 snprintf(buf, sizeof(buf), "%s%s%s%s", imsi, msisdn, tmsi, tmsi_new);
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100112 return buf;
113}
114
Oliver Smith5598aae2019-01-08 11:47:21 +0100115const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub)
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +0100116{
117 if (!vsub || !vsub->msisdn[0])
118 return vlr_subscr_name(vsub);
119 return vsub->msisdn;
120}
121
Harald Welteb8b85a12016-06-17 00:06:42 +0200122struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
123 const char *imsi,
124 const char *file, int line)
125{
126 struct vlr_subscr *vsub;
127
128 if (!imsi || !*imsi)
129 return NULL;
130
131 llist_for_each_entry(vsub, &vlr->subscribers, list) {
132 if (vlr_subscr_matches_imsi(vsub, imsi))
133 return _vlr_subscr_get(vsub, file, line);
134 }
135 return NULL;
136}
137
138struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
139 uint32_t tmsi,
140 const char *file, int line)
141{
142 struct vlr_subscr *vsub;
143
144 if (tmsi == GSM_RESERVED_TMSI)
145 return NULL;
146
147 llist_for_each_entry(vsub, &vlr->subscribers, list) {
148 if (vlr_subscr_matches_tmsi(vsub, tmsi))
149 return _vlr_subscr_get(vsub, file, line);
150 }
151 return NULL;
152}
153
154struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
155 const char *msisdn,
156 const char *file, int line)
157{
158 struct vlr_subscr *vsub;
159
160 if (!msisdn || !*msisdn)
161 return NULL;
162
163 llist_for_each_entry(vsub, &vlr->subscribers, list) {
164 if (vlr_subscr_matches_msisdn(vsub, msisdn))
165 return _vlr_subscr_get(vsub, file, line);
166 }
167 return NULL;
168}
169
170/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100171static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
172 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200173{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200174 struct msgb *msg = osmo_gsup_client_msgb_alloc();
Harald Welteb8b85a12016-06-17 00:06:42 +0200175
Max770fbd22018-01-24 12:48:33 +0100176 int rc = osmo_gsup_encode(msg, gsup_msg);
177 if (rc < 0) {
178 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
179 return rc;
180 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200181
182 if (!vlr->gsup_client) {
183 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
184 "send GSUP: %s\n", msgb_hexdump(msg));
185 msgb_free(msg);
186 return -ENOTSUP;
187 }
188
189 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
190 osmo_hexdump_nospc(msg->data, msg->len));
191
Harald Welte1ea6baf2018-07-31 19:40:52 +0200192 return osmo_gsup_client_send(vlr->gsup_client, msg);
Harald Welteb8b85a12016-06-17 00:06:42 +0200193}
194
195/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100196static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200197 struct osmo_gsup_message *gsup_msg)
198{
199 struct vlr_instance *vlr = vsub->vlr;
200
201 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100202 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200203
204 return vlr_tx_gsup_message(vlr, gsup_msg);
205}
206
207/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100208static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200209 struct osmo_gsup_message *gsup_orig,
210 enum gsm48_gmm_cause cause)
211{
212 struct osmo_gsup_message gsup_reply = {0};
213
Max98f74672018-02-05 12:57:06 +0100214 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200215 gsup_reply.cause = cause;
216 gsup_reply.message_type =
217 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
218
219 return vlr_tx_gsup_message(vlr, &gsup_reply);
220}
221
222struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
223{
224 if (!sub)
225 return NULL;
226 OSMO_ASSERT(sub->use_count < INT_MAX);
227 sub->use_count++;
228 LOGPSRC(DREF, LOGL_DEBUG, file, line,
229 "VLR subscr %s usage increases to: %d\n",
230 vlr_subscr_name(sub), sub->use_count);
231 return sub;
232}
233
234struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
235{
236 if (!sub)
237 return NULL;
238 sub->use_count--;
239 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
240 file, line,
241 "VLR subscr %s usage decreases to: %d\n",
242 vlr_subscr_name(sub), sub->use_count);
243 if (sub->use_count <= 0)
244 vlr_subscr_free(sub);
245 return NULL;
246}
247
248/* Allocate a new subscriber and insert it into list */
249static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
250{
251 struct vlr_subscr *vsub;
252 int i;
253
254 vsub = talloc_zero(vlr, struct vlr_subscr);
255 vsub->vlr = vlr;
256 vsub->tmsi = GSM_RESERVED_TMSI;
257 vsub->tmsi_new = GSM_RESERVED_TMSI;
258
259 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100260 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200261
262 INIT_LLIST_HEAD(&vsub->cs.requests);
263 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
264
265 llist_add_tail(&vsub->list, &vlr->subscribers);
266 return vsub;
267}
268
269struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
270{
271 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
272}
273
274/* Send a GSUP Purge MS request.
275 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
276 * this subscriber, not to the HLR? */
277int vlr_subscr_purge(struct vlr_subscr *vsub)
278{
279 struct osmo_gsup_message gsup_msg = {0};
280
281 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
282
283 /* provide HLR number in case we know it */
284 gsup_msg.hlr_enc_len = vsub->hlr.len;
285 gsup_msg.hlr_enc = vsub->hlr.buf;
286
287 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
288}
289
Neels Hofmeyr15809592018-04-06 02:57:51 +0200290void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
291 enum osmo_fsm_term_cause fsm_cause,
292 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200293{
294 if (!vsub)
295 return;
296
Neels Hofmeyr15809592018-04-06 02:57:51 +0200297 vlr_subscr_get(vsub);
298 if (vsub->lu_fsm)
299 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200300 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200301 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
302 vlr_subscr_put(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200303}
304
305/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
306void vlr_subscr_free(struct vlr_subscr *vsub)
307{
308 llist_del(&vsub->list);
309 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
310 talloc_free(vsub);
311}
312
313/* Generate a new TMSI and store in vsub->tmsi_new.
314 * Search all known subscribers to ensure that the TMSI is unique. */
315int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
316{
317 struct vlr_instance *vlr = vsub->vlr;
318 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100319 int tried, rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200320
321 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100322 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
323 if (rc < 0) {
324 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
325 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200326 }
327 /* throw the dice again, if the TSMI doesn't fit */
328 if (tmsi == GSM_RESERVED_TMSI)
329 continue;
330
331 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
332 if (vlr->cfg.is_ps) {
333 /* SGSN */
334 tmsi |= 0xC000000;
335 } else {
336 /* MSC */
337 if ((tmsi & 0xC0000000) == 0xC0000000)
338 tmsi &= ~0xC0000000;
339 }
340
341 /* If this TMSI is already in use, try another one. */
342 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
343 continue;
344
345 vsub->tmsi_new = tmsi;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100346 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200347 return 0;
348 }
349
350 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
351 " after %d tries\n", vlr_subscr_name(vsub), tried);
352 return -1;
353}
354
355/* Find subscriber by IMSI, or create new subscriber if not found.
356 * \param[in] vlr VLR instace.
357 * \param[in] imsi IMSI string.
358 * \param[out] created if non-NULL, returns whether a new entry was created. */
359struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
360 const char *imsi,
361 bool *created,
362 const char *file,
363 int line)
364{
365 struct vlr_subscr *vsub;
366 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
367 if (vsub) {
368 if (created)
369 *created = false;
370 return vsub;
371 }
372
373 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
374 if (!vsub)
375 return NULL;
376 vlr_subscr_set_imsi(vsub, imsi);
377 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
378 if (created)
379 *created = true;
380 return vsub;
381}
382
383/* Find subscriber by TMSI, or create new subscriber if not found.
384 * \param[in] vlr VLR instace.
385 * \param[in] tmsi TMSI.
386 * \param[out] created if non-NULL, returns whether a new entry was created. */
387struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
388 uint32_t tmsi,
389 bool *created,
390 const char *file,
391 int line)
392{
393 struct vlr_subscr *vsub;
394 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
395 if (vsub) {
396 if (created)
397 *created = false;
398 return vsub;
399 }
400
401 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
402 if (!vsub)
403 return NULL;
404 vsub->tmsi = tmsi;
405 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
406 if (created)
407 *created = true;
408 return vsub;
409}
410
411void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
412{
413 if (!vsub)
414 return;
Stefan Sperling9fbb6002018-06-25 17:31:59 +0200415
416 if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) {
417 LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n",
418 imsi, vsub->imsi);
419 /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */
420 }
421
Harald Welteb8b85a12016-06-17 00:06:42 +0200422 vsub->id = atoll(vsub->imsi);
423 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
424 vsub->imsi, vsub->id);
425}
426
427void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
428{
429 if (!vsub)
430 return;
Max98f74672018-02-05 12:57:06 +0100431 OSMO_STRLCPY_ARRAY(vsub->imei, imei);
Harald Welteb8b85a12016-06-17 00:06:42 +0200432 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
433 vsub->imsi, vsub->imei);
434}
435
436void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
437{
438 if (!vsub)
439 return;
Max98f74672018-02-05 12:57:06 +0100440 OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +0200441 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
442 vsub->imsi, vsub->imeisv);
443}
444
445/* Safely copy the given MSISDN string to vsub->msisdn */
446void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
447{
448 if (!vsub)
449 return;
Max98f74672018-02-05 12:57:06 +0100450 OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn);
Harald Welteb8b85a12016-06-17 00:06:42 +0200451 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
452 vsub->imsi, vsub->msisdn);
453}
454
455bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
456{
457 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
458}
459
460bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
461{
462 return vsub && tmsi != GSM_RESERVED_TMSI
463 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
464}
465
466bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
467{
468 return vsub && msisdn && vsub->msisdn[0]
469 && !strcmp(vsub->msisdn, msisdn);
470}
471
472bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
473{
474 return vsub && imei && vsub->imei[0]
475 && !strcmp(vsub->imei, imei);
476}
477
478/* Send updated subscriber information to HLR */
479int vlr_subscr_changed(struct vlr_subscr *vsub)
480{
481 /* FIXME */
482 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
483 return 0;
484}
485
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200486void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub)
487{
488 struct gsm_network *net = vsub->vlr->user_ctx; /* XXX move t3212 into struct vlr_instance? */
489 struct timespec now;
490
491 /* The T3212 timeout value field is coded as the binary representation of the timeout
492 * value for periodic updating in decihours. Mark the subscriber as inactive if it missed
493 * two consecutive location updates. Timeout is twice the t3212 value plus one minute. */
494 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) {
495 vsub->expire_lu = now.tv_sec + (net->t3212 * 60 * 6 * 2) + 60;
496 } else {
497 LOGP(DVLR, LOGL_ERROR,
498 "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub));
499 /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */
500 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
501 }
502}
503
504void vlr_subscr_expire_lu(void *data)
505{
506 struct vlr_instance *vlr = data;
507 struct vlr_subscr *vsub, *vsub_tmp;
508 struct timespec now;
509
510 if (llist_empty(&vlr->subscribers))
511 goto done;
512
513 if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
514 LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n");
515 goto done;
516 }
517
518 llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) {
519 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec)
520 continue;
521
522 LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub));
523 vlr_subscr_rx_imsi_detach(vsub);
524 }
525
526done:
527 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
528}
529
Harald Welteb8b85a12016-06-17 00:06:42 +0200530/***********************************************************************
531 * PDP context data
532 ***********************************************************************/
533
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200534#define GSM_APN_LENGTH 102
535
536/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
537/* see GSM 09.02, B.1, gprsSubscriptionData */
538struct sgsn_subscriber_pdp_data {
539 struct llist_head list;
540
541 unsigned int context_id;
542 uint16_t pdp_type;
543 char apn_str[GSM_APN_LENGTH];
544 uint8_t qos_subscribed[20];
545 size_t qos_subscribed_len;
546};
547
Harald Welteb8b85a12016-06-17 00:06:42 +0200548struct sgsn_subscriber_pdp_data *
549vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
550{
551 struct sgsn_subscriber_pdp_data* pdata;
552
553 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
554
555 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
556
557 return pdata;
558}
559
560static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
561{
562 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
563 int count = 0;
564
565 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
566 llist_del(&pdp->list);
567 talloc_free(pdp);
568 count += 1;
569 }
570
571 return count;
572}
573
574static struct sgsn_subscriber_pdp_data *
575vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
576{
577 struct sgsn_subscriber_pdp_data *pdp;
578
579 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
580 if (pdp->context_id == context_id)
581 return pdp;
582 }
583
584 return NULL;
585}
586
587/***********************************************************************
588 * Actual Implementation
589 ***********************************************************************/
590
591static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
592 struct osmo_gsup_message *gsup_msg)
593{
594 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Max770fbd22018-01-24 12:48:33 +0100595 int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN);
596 if (rc < 0)
597 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi);
598
Harald Welteb8b85a12016-06-17 00:06:42 +0200599 LOGP(DVLR, LOGL_NOTICE,
600 "Unknown IMSI %s, discarding GSUP request "
601 "of type 0x%02x\n",
602 gsup_msg->imsi, gsup_msg->message_type);
603 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
604 LOGP(DVLR, LOGL_NOTICE,
605 "Unknown IMSI %s, discarding GSUP error "
606 "of type 0x%02x, cause '%s' (%d)\n",
607 gsup_msg->imsi, gsup_msg->message_type,
608 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
609 gsup_msg->cause);
610 } else {
611 LOGP(DVLR, LOGL_NOTICE,
612 "Unknown IMSI %s, discarding GSUP response "
613 "of type 0x%02x\n",
614 gsup_msg->imsi, gsup_msg->message_type);
615 }
616
617 return -GMM_CAUSE_IMSI_UNKNOWN;
618}
619
620static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
621 struct osmo_gsup_message *gsup_msg)
622{
623 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
624 LOGGSUPP(LOGL_NOTICE, gsup_msg,
625 "Purge MS has failed with cause '%s' (%d)\n",
626 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
627 gsup_msg->cause);
628 return -gsup_msg->cause;
629 }
630 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
631 return 0;
632}
633
634/* VLR internal call to request UpdateLocation from HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +0100635int vlr_subscr_req_lu(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200636{
637 struct osmo_gsup_message gsup_msg = {0};
638 int rc;
639
640 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Neels Hofmeyrd0756b12018-09-28 02:41:39 +0200641 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 +0200642 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
643
644 return rc;
645}
646
647/* VLR internal call to request tuples from HLR */
648int vlr_subscr_req_sai(struct vlr_subscr *vsub,
649 const uint8_t *auts, const uint8_t *auts_rand)
650{
651 struct osmo_gsup_message gsup_msg = {0};
652
653 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
654 gsup_msg.auts = auts;
655 gsup_msg.rand = auts_rand;
656
657 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
658}
659
660/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100661int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200662{
663 struct osmo_gsup_message gsup_msg = {0};
664
665 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100666 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200667 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
668}
669
670/* Update the subscriber with GSUP-received auth tuples */
671void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
672 const struct osmo_gsup_message *gsup)
673{
674 unsigned int i;
675 unsigned int got_tuples;
676
677 if (gsup->num_auth_vectors) {
678 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
679 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100680 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200681 }
682
683 got_tuples = 0;
684 for (i = 0; i < gsup->num_auth_vectors; i++) {
685 size_t key_seq = i;
686
687 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
688 LOGVSUBP(LOGL_NOTICE, vsub,
689 "Skipping auth tuple wih invalid cksn %zu\n",
690 key_seq);
691 continue;
692 }
693 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
694 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100695 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200696 }
697
698 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
699
700 if (!got_tuples) {
701 /* FIXME what now? */
702 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
703 }
704
705 /* New tuples means last_tuple becomes invalid */
706 vsub->last_tuple = NULL;
707}
708
709/* Handle SendAuthInfo Result/Error from HLR */
710static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
711 const struct osmo_gsup_message *gsup)
712{
713 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
714 void *data = (void *) gsup;
715
716 switch (gsup->message_type) {
717 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
718 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
719 break;
720 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
721 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
722 break;
723 default:
724 return -1;
725 }
726
727 return 0;
728}
729
730static int decode_bcd_number_safe(char *output, int output_len,
731 const uint8_t *bcd_lv, int input_len,
732 int h_len)
733{
734 uint8_t len;
735 OSMO_ASSERT(output_len >= 1);
736 *output = '\0';
737 if (input_len < 1)
738 return -EIO;
739 len = bcd_lv[0];
740 if (input_len < len)
741 return -EIO;
742 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
743}
744
745static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
746 const struct osmo_gsup_message *gsup_msg)
747{
748 unsigned idx;
749 int rc;
750
Maxa263bb22017-12-27 13:23:44 +0100751 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200752 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
753 gsup_msg->msisdn_enc,
754 gsup_msg->msisdn_enc_len, 0);
755 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
756 vsub->imsi, vsub->msisdn);
757 }
758
759 if (gsup_msg->hlr_enc) {
760 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
761 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
762 gsup_msg->hlr_enc_len);
763 vsub->hlr.len = 0;
764 } else {
765 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
766 gsup_msg->hlr_enc_len);
767 vsub->hlr.len = gsup_msg->hlr_enc_len;
768 }
769 }
770
771 if (gsup_msg->pdp_info_compl) {
772 rc = vlr_subscr_pdp_data_clear(vsub);
773 if (rc > 0)
774 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
775 }
776
777 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
778 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
779 size_t ctx_id = pdp_info->context_id;
780 struct sgsn_subscriber_pdp_data *pdp_data;
781
782 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
783 LOGVSUBP(LOGL_ERROR, vsub,
784 "APN too long, context id = %zu, APN = %s\n",
785 ctx_id, osmo_hexdump(pdp_info->apn_enc,
786 pdp_info->apn_enc_len));
787 continue;
788 }
789
790 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
791 LOGVSUBP(LOGL_ERROR, vsub,
792 "QoS info too long (%zu)\n",
793 pdp_info->qos_enc_len);
794 continue;
795 }
796
797 LOGVSUBP(LOGL_INFO, vsub,
798 "Will set PDP info, context id = %zu, APN = %s\n",
799 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
800
801 /* Set PDP info [ctx_id] */
802 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
803 if (!pdp_data) {
804 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
805 pdp_data->context_id = ctx_id;
806 }
807
808 OSMO_ASSERT(pdp_data != NULL);
809 pdp_data->pdp_type = pdp_info->pdp_type;
810 osmo_apn_to_str(pdp_data->apn_str,
811 pdp_info->apn_enc, pdp_info->apn_enc_len);
812 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
813 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
814 }
815}
816
817
818/* Handle InsertSubscrData Result from HLR */
819static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
820 const struct osmo_gsup_message *gsup)
821{
822 struct osmo_gsup_message gsup_reply = {0};
823
824 vlr_subscr_gsup_insert_data(vsub, gsup);
825 vsub->vlr->ops.subscr_update(vsub);
826
827 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
828 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
829}
830
831/* Handle UpdateLocation Result from HLR */
832static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
833 const struct osmo_gsup_message *gsup)
834{
835 if (!vsub->lu_fsm) {
836 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
837 "without LU in progress\n");
838 return -ENODEV;
839 }
840
841 /* contrary to MAP, we allow piggy-backing subscriber data onto the
842 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
843 * nested INSERT SUBSCRIBER DATA transaction */
844 vlr_subscr_gsup_insert_data(vsub, gsup);
845
846 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
847
848 return 0;
849}
850
851/* Handle UpdateLocation Result from HLR */
852static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
853 const struct osmo_gsup_message *gsup)
854{
855 if (!vsub->lu_fsm) {
856 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
857 "without LU in progress\n");
858 return -ENODEV;
859 }
860
861 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
862 get_value_string(gsm48_gmm_cause_names, gsup->cause));
863
864 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
865 (void *)&gsup->cause);
866
867 return 0;
868}
869
Neels Hofmeyr15809592018-04-06 02:57:51 +0200870static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
871 enum osmo_fsm_term_cause *fsm_cause_p,
872 enum gsm48_reject_value *gsm48_rej_p)
873{
874 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
875 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
876 switch (gmm_cause) {
877 case GMM_CAUSE_IMSI_UNKNOWN:
878 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
879 break;
880 case GMM_CAUSE_ILLEGAL_MS:
881 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
882 break;
883 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
884 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
885 break;
886 case GMM_CAUSE_ILLEGAL_ME:
887 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
888 break;
889 case GMM_CAUSE_GPRS_NOTALLOWED:
890 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
891 break;
892 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
893 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
894 break;
895 case GMM_CAUSE_MS_ID_NOT_DERIVED:
896 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
897 break;
898 case GMM_CAUSE_IMPL_DETACHED:
899 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
900 break;
901 case GMM_CAUSE_PLMN_NOTALLOWED:
902 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
903 break;
904 case GMM_CAUSE_LA_NOTALLOWED:
905 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
906 break;
907 case GMM_CAUSE_ROAMING_NOTALLOWED:
908 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
909 break;
910 case GMM_CAUSE_NO_GPRS_PLMN:
911 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
912 break;
913 case GMM_CAUSE_MSC_TEMP_NOTREACH:
914 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
915 break;
916 case GMM_CAUSE_SYNC_FAIL:
917 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
918 break;
919 case GMM_CAUSE_CONGESTION:
920 gsm48_rej = GSM48_REJECT_CONGESTION;
921 break;
922 case GMM_CAUSE_SEM_INCORR_MSG:
923 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
924 break;
925 case GMM_CAUSE_INV_MAND_INFO:
926 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
927 break;
928 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
929 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
930 break;
931 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
932 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
933 break;
934 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
935 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
936 break;
937 case GMM_CAUSE_COND_IE_ERR:
938 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
939 break;
940 case GMM_CAUSE_MSG_INCOMP_P_STATE:
941 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
942 break;
943 case GMM_CAUSE_PROTO_ERR_UNSPEC:
944 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
945 break;
946
947 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
948 case GMM_CAUSE_MAC_FAIL:
949 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
950 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
951 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
952 case GMM_CAUSE_NO_PDP_ACTIVATED:
953 case GMM_CAUSE_NET_FAIL:
954 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
955 break;
956 }
957 switch (gmm_cause) {
958 /* refine any error causes here? */
959 default:
960 fsm_cause = OSMO_FSM_TERM_ERROR;
961 break;
962 }
963 if (fsm_cause_p)
964 *fsm_cause_p = fsm_cause;
965 if (gsm48_rej_p)
966 *gsm48_rej_p = gsm48_rej;
967}
968
Harald Welteb8b85a12016-06-17 00:06:42 +0200969/* Handle LOCATION CANCEL request from HLR */
970static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
971 struct osmo_gsup_message *gsup_msg)
972{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200973 enum gsm48_reject_value gsm48_rej;
974 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +0200975 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +0100976 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +0200977 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
978
979 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
980 is_update_procedure ?
981 "update procedure" : "subscription withdraw");
982
983 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +0100984 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +0200985
Neels Hofmeyr15809592018-04-06 02:57:51 +0200986 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
987 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +0200988
Stefan Sperlingad797ce2018-12-10 18:33:30 +0100989 vlr_subscr_rx_imsi_detach(vsub);
990
Max770fbd22018-01-24 12:48:33 +0100991 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200992}
993
994/* Incoming handler for GSUP from HLR.
995 * Keep this function non-static for direct invocation by unit tests. */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200996int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200997{
998 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
999 struct vlr_subscr *vsub;
1000 struct osmo_gsup_message gsup;
1001 int rc;
1002
1003 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
1004 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
1005
1006 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
1007 if (rc < 0) {
1008 LOGP(DVLR, LOGL_ERROR,
1009 "decoding GSUP message fails with error '%s' (%d)\n",
1010 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001011 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001012 }
1013
1014 if (!gsup.imsi[0]) {
1015 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +01001016 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
1017 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
1018 if (rc < 0)
1019 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
1020 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001021 rc = -GMM_CAUSE_INV_MAND_INFO;
1022 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001023 }
1024
1025 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
1026 if (!vsub) {
1027 switch (gsup.message_type) {
1028 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1029 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001030 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1031 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001032 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001033 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1034 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001035 }
1036 }
1037
1038 switch (gsup.message_type) {
1039 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1040 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1041 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1042 break;
1043 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1044 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1045 break;
1046 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1047 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1048 break;
1049 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1050 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1051 break;
1052 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1053 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1054 break;
1055 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1056 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1057 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1058 LOGVSUBP(LOGL_ERROR, vsub,
1059 "Rx GSUP msg_type=%d not yet implemented\n",
1060 gsup.message_type);
1061 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1062 break;
1063 default:
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001064 /* Forward message towards MSC */
1065 rc = vlr->ops.forward_gsup_msg(vsub, &gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001066 break;
1067 }
1068
1069 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001070
1071msgb_free_and_return:
1072 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001073 return rc;
1074}
1075
1076/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1077int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1078 const uint8_t *mi, size_t mi_len)
1079{
1080 char mi_string[GSM48_MI_SIZE];
1081 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1082
1083 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1084
1085 /* update the vlr_subscr with the given identity */
1086 switch (mi_type) {
1087 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001088 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1089 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1090 sizeof(vsub->imsi) - 1, mi_string);
1091 return -ENOSPC; /* ignore message; do not avance LU FSM */
1092 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001093 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1094 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1095 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001096 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001097 } else
1098 vlr_subscr_set_imsi(vsub, mi_string);
1099 break;
1100 case GSM_MI_TYPE_IMEI:
1101 vlr_subscr_set_imei(vsub, mi_string);
1102 break;
1103 case GSM_MI_TYPE_IMEISV:
1104 vlr_subscr_set_imeisv(vsub, mi_string);
1105 break;
1106 }
1107
1108 if (vsub->auth_fsm) {
1109 switch (mi_type) {
1110 case GSM_MI_TYPE_IMSI:
1111 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1112 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1113 break;
1114 }
1115 }
1116
1117 if (vsub->lu_fsm) {
1118 uint32_t event = 0;
1119 switch (mi_type) {
1120 case GSM_MI_TYPE_IMSI:
1121 event = VLR_ULA_E_ID_IMSI;
1122 break;
1123 case GSM_MI_TYPE_IMEI:
1124 event = VLR_ULA_E_ID_IMEI;
1125 break;
1126 case GSM_MI_TYPE_IMEISV:
1127 event = VLR_ULA_E_ID_IMEISV;
1128 break;
1129 default:
1130 OSMO_ASSERT(0);
1131 break;
1132 }
1133 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1134 } else {
1135 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1136 }
1137
1138 return 0;
1139}
1140
1141/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1142int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1143{
1144 if (vsub->lu_fsm) {
1145 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1146 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1147 } else if (vsub->proc_arq_fsm) {
1148 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1149 PR_ARQ_E_TMSI_ACK, NULL);
1150 } else {
1151 LOGVSUBP(LOGL_NOTICE, vsub,
1152 "gratuitous TMSI REALLOC COMPL");
1153 return -EINVAL;
1154 }
1155}
1156
Maxdcc193d2017-12-27 19:34:15 +01001157bool vlr_subscr_expire(struct vlr_subscr *vsub)
1158{
1159 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001160 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001161 vsub->lu_complete = false;
1162 vlr_subscr_put(vsub);
1163
1164 return true;
1165 }
1166
1167 return false;
1168}
1169
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001170/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001171int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1172{
1173 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001174 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001175
1176 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001177 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001178
Maxdcc193d2017-12-27 19:34:15 +01001179 vlr_subscr_expire(vsub);
1180
Harald Welteb8b85a12016-06-17 00:06:42 +02001181 return 0;
1182}
1183
1184/* Tear down any running FSMs due to MSC connection timeout.
1185 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1186 * message before the entire connection is torn down.
1187 * \param[in] vsub subscriber to tear down
1188 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001189void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001190{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001191 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001192}
1193
1194struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1195{
1196 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1197 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001198
1199 /* Some of these are needed only on UTRAN, but in case the caller wants
1200 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001201 OSMO_ASSERT(ops->tx_auth_req);
1202 OSMO_ASSERT(ops->tx_auth_rej);
1203 OSMO_ASSERT(ops->tx_id_req);
1204 OSMO_ASSERT(ops->tx_lu_acc);
1205 OSMO_ASSERT(ops->tx_lu_rej);
1206 OSMO_ASSERT(ops->tx_cm_serv_acc);
1207 OSMO_ASSERT(ops->tx_cm_serv_rej);
1208 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001209 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001210 OSMO_ASSERT(ops->subscr_update);
1211 OSMO_ASSERT(ops->subscr_assoc);
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001212 OSMO_ASSERT(ops->forward_gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001213
1214 INIT_LLIST_HEAD(&vlr->subscribers);
1215 INIT_LLIST_HEAD(&vlr->operations);
1216 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1217
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001218 /* defaults */
1219 vlr->cfg.assign_tmsi = true;
1220
Harald Welteb8b85a12016-06-17 00:06:42 +02001221 /* osmo_auth_fsm.c */
1222 osmo_fsm_register(&vlr_auth_fsm);
1223 /* osmo_lu_fsm.c */
1224 vlr_lu_fsm_init();
1225 /* vlr_access_request_fsm.c */
1226 vlr_parq_fsm_init();
1227
1228 return vlr;
1229}
1230
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001231int vlr_start(struct ipaccess_unit *ipa_dev, struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +02001232 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1233{
1234 OSMO_ASSERT(vlr);
1235
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001236 vlr->gsup_client = osmo_gsup_client_create2(vlr, ipa_dev,
1237 gsup_server_addr_str,
1238 gsup_server_port,
1239 &vlr_gsupc_read_cb, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001240 if (!vlr->gsup_client)
1241 return -ENOMEM;
1242 vlr->gsup_client->data = vlr;
1243
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001244 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1245 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001246 return 0;
1247}
1248
1249/* MSC->VLR: Subscribre has disconnected */
1250int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1251{
1252 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1253 * interface */
1254 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1255 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1256 vsub->msc_conn_ref = NULL;
1257
1258 return 0;
1259}
1260
1261/* MSC->VLR: Receive Authentication Failure from Subscriber */
1262int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1263{
1264 struct vlr_auth_resp_par par = {0};
1265 par.auts = auts;
1266
1267 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1268 return 0;
1269}
1270
1271/* MSC->VLR: Receive Authentication Response from MS
1272 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1273int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1274 bool is_utran, const uint8_t *res, uint8_t res_len)
1275{
1276 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1277 struct vlr_auth_resp_par par;
1278
1279 par.is_r99 = is_r99;
1280 par.is_utran = is_utran;
1281 par.res = res;
1282 par.res_len = res_len;
1283 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1284
1285 return 0;
1286}
1287
1288/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1289void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1290{
1291 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1292 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1293 if (vsub->proc_arq_fsm
1294 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1295 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1296 res);
1297}
1298
1299/* Internal evaluation of requested ciphering mode.
1300 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1301 * \param[in] vlr VLR instance.
1302 * \param[in] fi Calling FSM instance, for logging.
1303 * \param[in] msc_conn_ref MSC conn to send to.
1304 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1305 * \returns 0 if no ciphering is needed or message was sent successfully,
1306 * or a negative value if ciph_mode is invalid or sending failed.
1307 */
1308int vlr_set_ciph_mode(struct vlr_instance *vlr,
1309 struct osmo_fsm_inst *fi,
1310 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001311 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001312 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001313 bool retrieve_imeisv)
1314{
Harald Welte71c51df2017-12-23 18:51:48 +01001315 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001316 return 0;
1317
Harald Welte71c51df2017-12-23 18:51:48 +01001318 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1319 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001320}
1321
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001322/* Decide whether UMTS AKA should be used.
1323 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1324 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1325 * GERAN are R99 capable and UMTS AKA tokens are available.
1326 * \param[in] vec Auth tokens (received from the HLR).
1327 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1328 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1329 */
1330bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1331{
1332 if (!is_r99)
1333 return false;
1334 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1335 return false;
1336 return true;
1337}
1338
Harald Welteb8b85a12016-06-17 00:06:42 +02001339void log_set_filter_vlr_subscr(struct log_target *target,
1340 struct vlr_subscr *vlr_subscr)
1341{
1342 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1343
1344 /* free the old data */
1345 if (*fsub) {
1346 vlr_subscr_put(*fsub);
1347 *fsub = NULL;
1348 }
1349
1350 if (vlr_subscr) {
1351 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1352 *fsub = vlr_subscr_get(vlr_subscr);
1353 } else
1354 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1355}