blob: 451c5217d87e80f952a0576ff11b5103d6719f82 [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
Oliver Smith7d053092018-12-14 17:37:38 +0100660/* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */
661int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub)
662{
663 struct osmo_gsup_message gsup_msg = {0};
664 uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */
665 int len;
666
667 /* Encode IMEI */
668 len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei);
669 if (len < 1) {
670 LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei);
671 return -ENOSPC;
672 }
673 gsup_msg.imei_enc = imei_enc;
674 gsup_msg.imei_enc_len = len;
675
676 /* Send CHECK_IMEI_REQUEST */
677 gsup_msg.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST;
678 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
679 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
680}
681
Harald Welteb8b85a12016-06-17 00:06:42 +0200682/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100683int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200684{
685 struct osmo_gsup_message gsup_msg = {0};
686
687 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100688 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200689 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
690}
691
692/* Update the subscriber with GSUP-received auth tuples */
693void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
694 const struct osmo_gsup_message *gsup)
695{
696 unsigned int i;
697 unsigned int got_tuples;
698
699 if (gsup->num_auth_vectors) {
700 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
701 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100702 vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200703 }
704
705 got_tuples = 0;
706 for (i = 0; i < gsup->num_auth_vectors; i++) {
707 size_t key_seq = i;
708
709 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
710 LOGVSUBP(LOGL_NOTICE, vsub,
711 "Skipping auth tuple wih invalid cksn %zu\n",
712 key_seq);
713 continue;
714 }
715 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
716 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100717 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200718 }
719
720 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
721
722 if (!got_tuples) {
723 /* FIXME what now? */
724 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
725 }
726
727 /* New tuples means last_tuple becomes invalid */
728 vsub->last_tuple = NULL;
729}
730
731/* Handle SendAuthInfo Result/Error from HLR */
732static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
733 const struct osmo_gsup_message *gsup)
734{
735 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
736 void *data = (void *) gsup;
737
738 switch (gsup->message_type) {
739 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
740 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
741 break;
742 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
743 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
744 break;
745 default:
746 return -1;
747 }
748
749 return 0;
750}
751
752static int decode_bcd_number_safe(char *output, int output_len,
753 const uint8_t *bcd_lv, int input_len,
754 int h_len)
755{
756 uint8_t len;
757 OSMO_ASSERT(output_len >= 1);
758 *output = '\0';
759 if (input_len < 1)
760 return -EIO;
761 len = bcd_lv[0];
762 if (input_len < len)
763 return -EIO;
764 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
765}
766
767static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
768 const struct osmo_gsup_message *gsup_msg)
769{
770 unsigned idx;
771 int rc;
772
Maxa263bb22017-12-27 13:23:44 +0100773 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200774 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
775 gsup_msg->msisdn_enc,
776 gsup_msg->msisdn_enc_len, 0);
777 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
778 vsub->imsi, vsub->msisdn);
779 }
780
781 if (gsup_msg->hlr_enc) {
782 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
783 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
784 gsup_msg->hlr_enc_len);
785 vsub->hlr.len = 0;
786 } else {
787 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
788 gsup_msg->hlr_enc_len);
789 vsub->hlr.len = gsup_msg->hlr_enc_len;
790 }
791 }
792
793 if (gsup_msg->pdp_info_compl) {
794 rc = vlr_subscr_pdp_data_clear(vsub);
795 if (rc > 0)
796 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
797 }
798
799 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
800 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
801 size_t ctx_id = pdp_info->context_id;
802 struct sgsn_subscriber_pdp_data *pdp_data;
803
804 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
805 LOGVSUBP(LOGL_ERROR, vsub,
806 "APN too long, context id = %zu, APN = %s\n",
807 ctx_id, osmo_hexdump(pdp_info->apn_enc,
808 pdp_info->apn_enc_len));
809 continue;
810 }
811
812 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
813 LOGVSUBP(LOGL_ERROR, vsub,
814 "QoS info too long (%zu)\n",
815 pdp_info->qos_enc_len);
816 continue;
817 }
818
819 LOGVSUBP(LOGL_INFO, vsub,
820 "Will set PDP info, context id = %zu, APN = %s\n",
821 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
822
823 /* Set PDP info [ctx_id] */
824 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
825 if (!pdp_data) {
826 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
827 pdp_data->context_id = ctx_id;
828 }
829
830 OSMO_ASSERT(pdp_data != NULL);
831 pdp_data->pdp_type = pdp_info->pdp_type;
832 osmo_apn_to_str(pdp_data->apn_str,
833 pdp_info->apn_enc, pdp_info->apn_enc_len);
834 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
835 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
836 }
837}
838
839
840/* Handle InsertSubscrData Result from HLR */
841static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
842 const struct osmo_gsup_message *gsup)
843{
844 struct osmo_gsup_message gsup_reply = {0};
845
846 vlr_subscr_gsup_insert_data(vsub, gsup);
847 vsub->vlr->ops.subscr_update(vsub);
848
849 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
850 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
851}
852
853/* Handle UpdateLocation Result from HLR */
854static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
855 const struct osmo_gsup_message *gsup)
856{
857 if (!vsub->lu_fsm) {
858 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
859 "without LU in progress\n");
860 return -ENODEV;
861 }
862
863 /* contrary to MAP, we allow piggy-backing subscriber data onto the
864 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
865 * nested INSERT SUBSCRIBER DATA transaction */
866 vlr_subscr_gsup_insert_data(vsub, gsup);
867
868 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
869
870 return 0;
871}
872
873/* Handle UpdateLocation Result from HLR */
874static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
875 const struct osmo_gsup_message *gsup)
876{
877 if (!vsub->lu_fsm) {
878 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
879 "without LU in progress\n");
880 return -ENODEV;
881 }
882
883 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
884 get_value_string(gsm48_gmm_cause_names, gsup->cause));
885
886 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
887 (void *)&gsup->cause);
888
889 return 0;
890}
891
Neels Hofmeyr15809592018-04-06 02:57:51 +0200892static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
893 enum osmo_fsm_term_cause *fsm_cause_p,
894 enum gsm48_reject_value *gsm48_rej_p)
895{
896 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
897 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
898 switch (gmm_cause) {
899 case GMM_CAUSE_IMSI_UNKNOWN:
900 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
901 break;
902 case GMM_CAUSE_ILLEGAL_MS:
903 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
904 break;
905 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
906 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
907 break;
908 case GMM_CAUSE_ILLEGAL_ME:
909 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
910 break;
911 case GMM_CAUSE_GPRS_NOTALLOWED:
912 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
913 break;
914 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
915 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
916 break;
917 case GMM_CAUSE_MS_ID_NOT_DERIVED:
918 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
919 break;
920 case GMM_CAUSE_IMPL_DETACHED:
921 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
922 break;
923 case GMM_CAUSE_PLMN_NOTALLOWED:
924 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
925 break;
926 case GMM_CAUSE_LA_NOTALLOWED:
927 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
928 break;
929 case GMM_CAUSE_ROAMING_NOTALLOWED:
930 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
931 break;
932 case GMM_CAUSE_NO_GPRS_PLMN:
933 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
934 break;
935 case GMM_CAUSE_MSC_TEMP_NOTREACH:
936 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
937 break;
938 case GMM_CAUSE_SYNC_FAIL:
939 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
940 break;
941 case GMM_CAUSE_CONGESTION:
942 gsm48_rej = GSM48_REJECT_CONGESTION;
943 break;
944 case GMM_CAUSE_SEM_INCORR_MSG:
945 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
946 break;
947 case GMM_CAUSE_INV_MAND_INFO:
948 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
949 break;
950 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
951 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
952 break;
953 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
954 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
955 break;
956 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
957 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
958 break;
959 case GMM_CAUSE_COND_IE_ERR:
960 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
961 break;
962 case GMM_CAUSE_MSG_INCOMP_P_STATE:
963 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
964 break;
965 case GMM_CAUSE_PROTO_ERR_UNSPEC:
966 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
967 break;
968
969 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
970 case GMM_CAUSE_MAC_FAIL:
971 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
972 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
973 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
974 case GMM_CAUSE_NO_PDP_ACTIVATED:
975 case GMM_CAUSE_NET_FAIL:
976 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
977 break;
978 }
979 switch (gmm_cause) {
980 /* refine any error causes here? */
981 default:
982 fsm_cause = OSMO_FSM_TERM_ERROR;
983 break;
984 }
985 if (fsm_cause_p)
986 *fsm_cause_p = fsm_cause;
987 if (gsm48_rej_p)
988 *gsm48_rej_p = gsm48_rej;
989}
990
Harald Welteb8b85a12016-06-17 00:06:42 +0200991/* Handle LOCATION CANCEL request from HLR */
992static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
993 struct osmo_gsup_message *gsup_msg)
994{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200995 enum gsm48_reject_value gsm48_rej;
996 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +0200997 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +0100998 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +0200999 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
1000
1001 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
1002 is_update_procedure ?
1003 "update procedure" : "subscription withdraw");
1004
1005 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +01001006 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +02001007
Neels Hofmeyr15809592018-04-06 02:57:51 +02001008 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
1009 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +02001010
Stefan Sperlingad797ce2018-12-10 18:33:30 +01001011 vlr_subscr_rx_imsi_detach(vsub);
1012
Max770fbd22018-01-24 12:48:33 +01001013 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +02001014}
1015
Oliver Smith7d053092018-12-14 17:37:38 +01001016/* Handle Check_IMEI_VLR result and error from HLR */
1017static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup)
1018{
1019 if (!vsub->lu_fsm) {
1020 LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n",
1021 osmo_gsup_message_type_name(gsup->message_type));
1022 return -ENODEV;
1023 }
1024
1025 if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) {
1026 if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK)
1027 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL);
1028 else
1029 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1030 } else {
1031 LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n",
1032 get_value_string(gsm48_gmm_cause_names, gsup->cause));
1033 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL);
1034 }
1035
1036 return 0;
1037}
1038
Harald Welteb8b85a12016-06-17 00:06:42 +02001039/* Incoming handler for GSUP from HLR.
1040 * Keep this function non-static for direct invocation by unit tests. */
Harald Welte1ea6baf2018-07-31 19:40:52 +02001041int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteb8b85a12016-06-17 00:06:42 +02001042{
1043 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
1044 struct vlr_subscr *vsub;
1045 struct osmo_gsup_message gsup;
1046 int rc;
1047
1048 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
1049 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
1050
1051 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
1052 if (rc < 0) {
1053 LOGP(DVLR, LOGL_ERROR,
1054 "decoding GSUP message fails with error '%s' (%d)\n",
1055 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001056 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001057 }
1058
1059 if (!gsup.imsi[0]) {
1060 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +01001061 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
1062 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
1063 if (rc < 0)
1064 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
1065 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001066 rc = -GMM_CAUSE_INV_MAND_INFO;
1067 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001068 }
1069
1070 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
1071 if (!vsub) {
1072 switch (gsup.message_type) {
1073 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1074 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001075 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1076 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001077 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001078 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1079 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001080 }
1081 }
1082
1083 switch (gsup.message_type) {
1084 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1085 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1086 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1087 break;
1088 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1089 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1090 break;
1091 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1092 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1093 break;
1094 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1095 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1096 break;
1097 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1098 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1099 break;
1100 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1101 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1102 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1103 LOGVSUBP(LOGL_ERROR, vsub,
1104 "Rx GSUP msg_type=%d not yet implemented\n",
1105 gsup.message_type);
1106 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1107 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001108 case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR:
1109 case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT:
1110 rc = vlr_subscr_handle_check_imei(vsub, &gsup);
1111 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001112 default:
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001113 /* Forward message towards MSC */
1114 rc = vlr->ops.forward_gsup_msg(vsub, &gsup);
Harald Welteb8b85a12016-06-17 00:06:42 +02001115 break;
1116 }
1117
1118 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001119
1120msgb_free_and_return:
1121 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001122 return rc;
1123}
1124
1125/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1126int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1127 const uint8_t *mi, size_t mi_len)
1128{
1129 char mi_string[GSM48_MI_SIZE];
1130 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1131
1132 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1133
1134 /* update the vlr_subscr with the given identity */
1135 switch (mi_type) {
1136 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001137 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1138 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1139 sizeof(vsub->imsi) - 1, mi_string);
1140 return -ENOSPC; /* ignore message; do not avance LU FSM */
1141 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001142 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1143 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1144 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001145 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001146 } else
1147 vlr_subscr_set_imsi(vsub, mi_string);
1148 break;
1149 case GSM_MI_TYPE_IMEI:
1150 vlr_subscr_set_imei(vsub, mi_string);
1151 break;
1152 case GSM_MI_TYPE_IMEISV:
1153 vlr_subscr_set_imeisv(vsub, mi_string);
1154 break;
1155 }
1156
1157 if (vsub->auth_fsm) {
1158 switch (mi_type) {
1159 case GSM_MI_TYPE_IMSI:
1160 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1161 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1162 break;
1163 }
1164 }
1165
1166 if (vsub->lu_fsm) {
1167 uint32_t event = 0;
1168 switch (mi_type) {
1169 case GSM_MI_TYPE_IMSI:
1170 event = VLR_ULA_E_ID_IMSI;
1171 break;
1172 case GSM_MI_TYPE_IMEI:
1173 event = VLR_ULA_E_ID_IMEI;
1174 break;
1175 case GSM_MI_TYPE_IMEISV:
1176 event = VLR_ULA_E_ID_IMEISV;
1177 break;
1178 default:
1179 OSMO_ASSERT(0);
1180 break;
1181 }
1182 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1183 } else {
1184 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1185 }
1186
1187 return 0;
1188}
1189
1190/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1191int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1192{
1193 if (vsub->lu_fsm) {
1194 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1195 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1196 } else if (vsub->proc_arq_fsm) {
1197 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1198 PR_ARQ_E_TMSI_ACK, NULL);
1199 } else {
1200 LOGVSUBP(LOGL_NOTICE, vsub,
1201 "gratuitous TMSI REALLOC COMPL");
1202 return -EINVAL;
1203 }
1204}
1205
Maxdcc193d2017-12-27 19:34:15 +01001206bool vlr_subscr_expire(struct vlr_subscr *vsub)
1207{
1208 if (vsub->lu_complete) {
Neels Hofmeyr5c8b1442018-12-11 12:43:10 +01001209 /* balancing the get from vlr_lu_compl_fsm_success() */
Maxdcc193d2017-12-27 19:34:15 +01001210 vsub->lu_complete = false;
1211 vlr_subscr_put(vsub);
1212
1213 return true;
1214 }
1215
1216 return false;
1217}
1218
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001219/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001220int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1221{
1222 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001223 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001224
1225 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001226 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001227
Maxdcc193d2017-12-27 19:34:15 +01001228 vlr_subscr_expire(vsub);
1229
Harald Welteb8b85a12016-06-17 00:06:42 +02001230 return 0;
1231}
1232
1233/* Tear down any running FSMs due to MSC connection timeout.
1234 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1235 * message before the entire connection is torn down.
1236 * \param[in] vsub subscriber to tear down
1237 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001238void vlr_ran_conn_timeout(struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +02001239{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001240 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001241}
1242
1243struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1244{
1245 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1246 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001247
1248 /* Some of these are needed only on UTRAN, but in case the caller wants
1249 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001250 OSMO_ASSERT(ops->tx_auth_req);
1251 OSMO_ASSERT(ops->tx_auth_rej);
1252 OSMO_ASSERT(ops->tx_id_req);
1253 OSMO_ASSERT(ops->tx_lu_acc);
1254 OSMO_ASSERT(ops->tx_lu_rej);
1255 OSMO_ASSERT(ops->tx_cm_serv_acc);
1256 OSMO_ASSERT(ops->tx_cm_serv_rej);
1257 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001258 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001259 OSMO_ASSERT(ops->subscr_update);
1260 OSMO_ASSERT(ops->subscr_assoc);
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001261 OSMO_ASSERT(ops->forward_gsup_msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001262
1263 INIT_LLIST_HEAD(&vlr->subscribers);
1264 INIT_LLIST_HEAD(&vlr->operations);
1265 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
1266
Neels Hofmeyr0b8dec72017-10-29 01:57:35 +02001267 /* defaults */
1268 vlr->cfg.assign_tmsi = true;
1269
Harald Welteb8b85a12016-06-17 00:06:42 +02001270 /* osmo_auth_fsm.c */
1271 osmo_fsm_register(&vlr_auth_fsm);
1272 /* osmo_lu_fsm.c */
1273 vlr_lu_fsm_init();
1274 /* vlr_access_request_fsm.c */
1275 vlr_parq_fsm_init();
1276
1277 return vlr;
1278}
1279
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001280int vlr_start(struct ipaccess_unit *ipa_dev, struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +02001281 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1282{
1283 OSMO_ASSERT(vlr);
1284
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001285 vlr->gsup_client = osmo_gsup_client_create2(vlr, ipa_dev,
1286 gsup_server_addr_str,
1287 gsup_server_port,
1288 &vlr_gsupc_read_cb, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +02001289 if (!vlr->gsup_client)
1290 return -ENOMEM;
1291 vlr->gsup_client->data = vlr;
1292
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001293 osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr);
1294 osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001295 return 0;
1296}
1297
1298/* MSC->VLR: Subscribre has disconnected */
1299int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1300{
1301 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1302 * interface */
1303 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1304 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1305 vsub->msc_conn_ref = NULL;
1306
1307 return 0;
1308}
1309
1310/* MSC->VLR: Receive Authentication Failure from Subscriber */
1311int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1312{
1313 struct vlr_auth_resp_par par = {0};
1314 par.auts = auts;
1315
1316 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1317 return 0;
1318}
1319
1320/* MSC->VLR: Receive Authentication Response from MS
1321 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1322int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1323 bool is_utran, const uint8_t *res, uint8_t res_len)
1324{
1325 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1326 struct vlr_auth_resp_par par;
1327
1328 par.is_r99 = is_r99;
1329 par.is_utran = is_utran;
1330 par.res = res;
1331 par.res_len = res_len;
1332 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1333
1334 return 0;
1335}
1336
1337/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1338void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1339{
1340 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1341 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1342 if (vsub->proc_arq_fsm
1343 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1344 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1345 res);
1346}
1347
1348/* Internal evaluation of requested ciphering mode.
1349 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1350 * \param[in] vlr VLR instance.
1351 * \param[in] fi Calling FSM instance, for logging.
1352 * \param[in] msc_conn_ref MSC conn to send to.
1353 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1354 * \returns 0 if no ciphering is needed or message was sent successfully,
1355 * or a negative value if ciph_mode is invalid or sending failed.
1356 */
1357int vlr_set_ciph_mode(struct vlr_instance *vlr,
1358 struct osmo_fsm_inst *fi,
1359 void *msc_conn_ref,
Harald Welte71c51df2017-12-23 18:51:48 +01001360 bool ciph_required,
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +01001361 bool umts_aka,
Harald Welteb8b85a12016-06-17 00:06:42 +02001362 bool retrieve_imeisv)
1363{
Harald Welte71c51df2017-12-23 18:51:48 +01001364 if (!ciph_required)
Harald Welteb8b85a12016-06-17 00:06:42 +02001365 return 0;
1366
Harald Welte71c51df2017-12-23 18:51:48 +01001367 LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n");
1368 return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
Harald Welteb8b85a12016-06-17 00:06:42 +02001369}
1370
Neels Hofmeyre3d72d72017-12-18 02:06:44 +01001371/* Decide whether UMTS AKA should be used.
1372 * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA
1373 * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and
1374 * GERAN are R99 capable and UMTS AKA tokens are available.
1375 * \param[in] vec Auth tokens (received from the HLR).
1376 * \param[in] is_r99 True when BTS and GERAN are R99 capable.
1377 * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA.
1378 */
1379bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99)
1380{
1381 if (!is_r99)
1382 return false;
1383 if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS))
1384 return false;
1385 return true;
1386}
1387
Harald Welteb8b85a12016-06-17 00:06:42 +02001388void log_set_filter_vlr_subscr(struct log_target *target,
1389 struct vlr_subscr *vlr_subscr)
1390{
1391 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1392
1393 /* free the old data */
1394 if (*fsub) {
1395 vlr_subscr_put(*fsub);
1396 *fsub = NULL;
1397 }
1398
1399 if (vlr_subscr) {
1400 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1401 *fsub = vlr_subscr_get(vlr_subscr);
1402 } else
1403 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1404}