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