blob: cff2e1204e175d163d745a238d505ebb0c3cb9e1 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) code base */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <osmocom/core/linuxlist.h>
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
Stefan Sperlingdefc3c82018-05-15 14:48:04 +020025#include <osmocom/core/timer.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020026#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
27#include <osmocom/gsm/gsup.h>
28#include <osmocom/gsm/apn.h>
Max43b01b02017-09-15 11:22:30 +020029#include <osmocom/gsm/gsm48.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/gsm_subscriber.h>
31#include <osmocom/msc/gsup_client.h>
32#include <osmocom/msc/vlr.h>
33#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020034
Harald Welteb8b85a12016-06-17 00:06:42 +020035#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <limits.h>
Max43b01b02017-09-15 11:22:30 +020038#include <errno.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020039
40#include "vlr_core.h"
41#include "vlr_auth_fsm.h"
42#include "vlr_lu_fsm.h"
43#include "vlr_access_req_fsm.h"
44
45#define SGSN_SUBSCR_MAX_RETRIES 3
46#define SGSN_SUBSCR_RETRY_INTERVAL 10
47
48/***********************************************************************
49 * Convenience functions
50 ***********************************************************************/
51
52const struct value_string vlr_ciph_names[] = {
53 OSMO_VALUE_STRING(VLR_CIPH_NONE),
54 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
55 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
56 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
57 { 0, NULL }
58};
59
60uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
61{
62 uint32_t tidx = 0xffffffff;
63
64 switch (timer) {
65 case 3270:
66 tidx = VLR_T_3270;
67 break;
68 case 3260:
69 tidx = VLR_T_3260;
70 break;
71 case 3250:
72 tidx = VLR_T_3250;
73 break;
74 }
75
76 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
77 return vlr->cfg.timer[tidx];
78}
79
Neels Hofmeyr7a2f58e2018-03-22 16:03:49 +010080/* return static buffer with printable name of VLR subscriber */
81const char *vlr_subscr_name(struct vlr_subscr *vsub)
82{
83 static char buf[32];
84 if (!vsub)
85 return "unknown";
86 if (vsub->msisdn[0])
87 snprintf(buf, sizeof(buf), "MSISDN:%s", vsub->msisdn);
88 else if (vsub->imsi[0])
89 snprintf(buf, sizeof(buf), "IMSI:%s", vsub->imsi);
90 else if (vsub->tmsi != GSM_RESERVED_TMSI)
91 snprintf(buf, sizeof(buf), "TMSI:0x%08x", vsub->tmsi);
92 else if (vsub->tmsi_new != GSM_RESERVED_TMSI)
93 snprintf(buf, sizeof(buf), "TMSI(new):0x%08x", vsub->tmsi_new);
94 else
95 return "unknown";
96 buf[sizeof(buf)-1] = '\0';
97 return buf;
98}
99
100const char *vlr_subscr_msisdn_or_name(struct vlr_subscr *vsub)
101{
102 if (!vsub || !vsub->msisdn[0])
103 return vlr_subscr_name(vsub);
104 return vsub->msisdn;
105}
106
Harald Welteb8b85a12016-06-17 00:06:42 +0200107struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
108 const char *imsi,
109 const char *file, int line)
110{
111 struct vlr_subscr *vsub;
112
113 if (!imsi || !*imsi)
114 return NULL;
115
116 llist_for_each_entry(vsub, &vlr->subscribers, list) {
117 if (vlr_subscr_matches_imsi(vsub, imsi))
118 return _vlr_subscr_get(vsub, file, line);
119 }
120 return NULL;
121}
122
123struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
124 uint32_t tmsi,
125 const char *file, int line)
126{
127 struct vlr_subscr *vsub;
128
129 if (tmsi == GSM_RESERVED_TMSI)
130 return NULL;
131
132 llist_for_each_entry(vsub, &vlr->subscribers, list) {
133 if (vlr_subscr_matches_tmsi(vsub, tmsi))
134 return _vlr_subscr_get(vsub, file, line);
135 }
136 return NULL;
137}
138
139struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
140 const char *msisdn,
141 const char *file, int line)
142{
143 struct vlr_subscr *vsub;
144
145 if (!msisdn || !*msisdn)
146 return NULL;
147
148 llist_for_each_entry(vsub, &vlr->subscribers, list) {
149 if (vlr_subscr_matches_msisdn(vsub, msisdn))
150 return _vlr_subscr_get(vsub, file, line);
151 }
152 return NULL;
153}
154
155/* Transmit GSUP message to HLR */
Max923a2392018-01-24 13:55:03 +0100156static int vlr_tx_gsup_message(const struct vlr_instance *vlr,
157 const struct osmo_gsup_message *gsup_msg)
Harald Welteb8b85a12016-06-17 00:06:42 +0200158{
159 struct msgb *msg = gsup_client_msgb_alloc();
160
Max770fbd22018-01-24 12:48:33 +0100161 int rc = osmo_gsup_encode(msg, gsup_msg);
162 if (rc < 0) {
163 LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc));
164 return rc;
165 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200166
167 if (!vlr->gsup_client) {
168 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
169 "send GSUP: %s\n", msgb_hexdump(msg));
170 msgb_free(msg);
171 return -ENOTSUP;
172 }
173
174 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
175 osmo_hexdump_nospc(msg->data, msg->len));
176
177 return gsup_client_send(vlr->gsup_client, msg);
178}
179
180/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
Max923a2392018-01-24 13:55:03 +0100181static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200182 struct osmo_gsup_message *gsup_msg)
183{
184 struct vlr_instance *vlr = vsub->vlr;
185
186 if (strlen(gsup_msg->imsi) == 0)
Max98f74672018-02-05 12:57:06 +0100187 OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200188
189 return vlr_tx_gsup_message(vlr, gsup_msg);
190}
191
192/* Transmit GSUP error in response to original message */
Max923a2392018-01-24 13:55:03 +0100193static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr,
Harald Welteb8b85a12016-06-17 00:06:42 +0200194 struct osmo_gsup_message *gsup_orig,
195 enum gsm48_gmm_cause cause)
196{
197 struct osmo_gsup_message gsup_reply = {0};
198
Max98f74672018-02-05 12:57:06 +0100199 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200200 gsup_reply.cause = cause;
201 gsup_reply.message_type =
202 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
203
204 return vlr_tx_gsup_message(vlr, &gsup_reply);
205}
206
207struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
208{
209 if (!sub)
210 return NULL;
211 OSMO_ASSERT(sub->use_count < INT_MAX);
212 sub->use_count++;
213 LOGPSRC(DREF, LOGL_DEBUG, file, line,
214 "VLR subscr %s usage increases to: %d\n",
215 vlr_subscr_name(sub), sub->use_count);
216 return sub;
217}
218
219struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
220{
221 if (!sub)
222 return NULL;
223 sub->use_count--;
224 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
225 file, line,
226 "VLR subscr %s usage decreases to: %d\n",
227 vlr_subscr_name(sub), sub->use_count);
228 if (sub->use_count <= 0)
229 vlr_subscr_free(sub);
230 return NULL;
231}
232
233/* Allocate a new subscriber and insert it into list */
234static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
235{
236 struct vlr_subscr *vsub;
237 int i;
238
239 vsub = talloc_zero(vlr, struct vlr_subscr);
240 vsub->vlr = vlr;
241 vsub->tmsi = GSM_RESERVED_TMSI;
242 vsub->tmsi_new = GSM_RESERVED_TMSI;
243
244 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
245 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
246
247 INIT_LLIST_HEAD(&vsub->cs.requests);
248 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
249
250 llist_add_tail(&vsub->list, &vlr->subscribers);
251 return vsub;
252}
253
254struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
255{
256 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
257}
258
259/* Send a GSUP Purge MS request.
260 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
261 * this subscriber, not to the HLR? */
262int vlr_subscr_purge(struct vlr_subscr *vsub)
263{
264 struct osmo_gsup_message gsup_msg = {0};
265
266 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
267
268 /* provide HLR number in case we know it */
269 gsup_msg.hlr_enc_len = vsub->hlr.len;
270 gsup_msg.hlr_enc = vsub->hlr.buf;
271
272 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
273}
274
Neels Hofmeyr15809592018-04-06 02:57:51 +0200275void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub,
276 enum osmo_fsm_term_cause fsm_cause,
277 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200278{
279 if (!vsub)
280 return;
281
Neels Hofmeyr15809592018-04-06 02:57:51 +0200282 vlr_subscr_get(vsub);
283 if (vsub->lu_fsm)
284 vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200285 if (vsub->proc_arq_fsm)
Neels Hofmeyr15809592018-04-06 02:57:51 +0200286 vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause);
287 vlr_subscr_put(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200288}
289
290/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
291void vlr_subscr_free(struct vlr_subscr *vsub)
292{
293 llist_del(&vsub->list);
294 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
295 talloc_free(vsub);
296}
297
298/* Generate a new TMSI and store in vsub->tmsi_new.
299 * Search all known subscribers to ensure that the TMSI is unique. */
300int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
301{
302 struct vlr_instance *vlr = vsub->vlr;
303 uint32_t tmsi;
Max753c15d2017-12-21 14:50:44 +0100304 int tried, rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200305
306 for (tried = 0; tried < 100; tried++) {
Max753c15d2017-12-21 14:50:44 +0100307 rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi));
308 if (rc < 0) {
309 LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc));
310 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200311 }
312 /* throw the dice again, if the TSMI doesn't fit */
313 if (tmsi == GSM_RESERVED_TMSI)
314 continue;
315
316 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
317 if (vlr->cfg.is_ps) {
318 /* SGSN */
319 tmsi |= 0xC000000;
320 } else {
321 /* MSC */
322 if ((tmsi & 0xC0000000) == 0xC0000000)
323 tmsi &= ~0xC0000000;
324 }
325
326 /* If this TMSI is already in use, try another one. */
327 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
328 continue;
329
330 vsub->tmsi_new = tmsi;
331 return 0;
332 }
333
334 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
335 " after %d tries\n", vlr_subscr_name(vsub), tried);
336 return -1;
337}
338
339/* Find subscriber by IMSI, or create new subscriber if not found.
340 * \param[in] vlr VLR instace.
341 * \param[in] imsi IMSI string.
342 * \param[out] created if non-NULL, returns whether a new entry was created. */
343struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
344 const char *imsi,
345 bool *created,
346 const char *file,
347 int line)
348{
349 struct vlr_subscr *vsub;
350 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
351 if (vsub) {
352 if (created)
353 *created = false;
354 return vsub;
355 }
356
357 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
358 if (!vsub)
359 return NULL;
360 vlr_subscr_set_imsi(vsub, imsi);
361 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
362 if (created)
363 *created = true;
364 return vsub;
365}
366
367/* Find subscriber by TMSI, or create new subscriber if not found.
368 * \param[in] vlr VLR instace.
369 * \param[in] tmsi TMSI.
370 * \param[out] created if non-NULL, returns whether a new entry was created. */
371struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
372 uint32_t tmsi,
373 bool *created,
374 const char *file,
375 int line)
376{
377 struct vlr_subscr *vsub;
378 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
379 if (vsub) {
380 if (created)
381 *created = false;
382 return vsub;
383 }
384
385 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
386 if (!vsub)
387 return NULL;
388 vsub->tmsi = tmsi;
389 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
390 if (created)
391 *created = true;
392 return vsub;
393}
394
395void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
396{
397 if (!vsub)
398 return;
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 */
619int vlr_subscr_req_lu(struct vlr_subscr *vsub, bool is_ps)
620{
621 struct osmo_gsup_message gsup_msg = {0};
622 int rc;
623
624 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
625 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
626
627 return rc;
628}
629
630/* VLR internal call to request tuples from HLR */
631int vlr_subscr_req_sai(struct vlr_subscr *vsub,
632 const uint8_t *auts, const uint8_t *auts_rand)
633{
634 struct osmo_gsup_message gsup_msg = {0};
635
636 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
637 gsup_msg.auts = auts;
638 gsup_msg.rand = auts_rand;
639
640 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
641}
642
643/* Tell HLR that authentication failure occurred */
Max923a2392018-01-24 13:55:03 +0100644int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +0200645{
646 struct osmo_gsup_message gsup_msg = {0};
647
648 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
Max98f74672018-02-05 12:57:06 +0100649 OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200650 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
651}
652
653/* Update the subscriber with GSUP-received auth tuples */
654void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
655 const struct osmo_gsup_message *gsup)
656{
657 unsigned int i;
658 unsigned int got_tuples;
659
660 if (gsup->num_auth_vectors) {
661 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
662 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
663 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
664 }
665
666 got_tuples = 0;
667 for (i = 0; i < gsup->num_auth_vectors; i++) {
668 size_t key_seq = i;
669
670 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
671 LOGVSUBP(LOGL_NOTICE, vsub,
672 "Skipping auth tuple wih invalid cksn %zu\n",
673 key_seq);
674 continue;
675 }
676 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
677 vsub->auth_tuples[i].key_seq = key_seq;
Max5e2e9bd2018-02-06 19:31:08 +0100678 got_tuples++;
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 }
680
681 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
682
683 if (!got_tuples) {
684 /* FIXME what now? */
685 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
686 }
687
688 /* New tuples means last_tuple becomes invalid */
689 vsub->last_tuple = NULL;
690}
691
692/* Handle SendAuthInfo Result/Error from HLR */
693static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
694 const struct osmo_gsup_message *gsup)
695{
696 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
697 void *data = (void *) gsup;
698
699 switch (gsup->message_type) {
700 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
701 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
702 break;
703 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
704 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
705 break;
706 default:
707 return -1;
708 }
709
710 return 0;
711}
712
713static int decode_bcd_number_safe(char *output, int output_len,
714 const uint8_t *bcd_lv, int input_len,
715 int h_len)
716{
717 uint8_t len;
718 OSMO_ASSERT(output_len >= 1);
719 *output = '\0';
720 if (input_len < 1)
721 return -EIO;
722 len = bcd_lv[0];
723 if (input_len < len)
724 return -EIO;
725 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
726}
727
728static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
729 const struct osmo_gsup_message *gsup_msg)
730{
731 unsigned idx;
732 int rc;
733
Maxa263bb22017-12-27 13:23:44 +0100734 if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()?
Harald Welteb8b85a12016-06-17 00:06:42 +0200735 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
736 gsup_msg->msisdn_enc,
737 gsup_msg->msisdn_enc_len, 0);
738 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
739 vsub->imsi, vsub->msisdn);
740 }
741
742 if (gsup_msg->hlr_enc) {
743 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
744 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
745 gsup_msg->hlr_enc_len);
746 vsub->hlr.len = 0;
747 } else {
748 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
749 gsup_msg->hlr_enc_len);
750 vsub->hlr.len = gsup_msg->hlr_enc_len;
751 }
752 }
753
754 if (gsup_msg->pdp_info_compl) {
755 rc = vlr_subscr_pdp_data_clear(vsub);
756 if (rc > 0)
757 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
758 }
759
760 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
761 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
762 size_t ctx_id = pdp_info->context_id;
763 struct sgsn_subscriber_pdp_data *pdp_data;
764
765 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
766 LOGVSUBP(LOGL_ERROR, vsub,
767 "APN too long, context id = %zu, APN = %s\n",
768 ctx_id, osmo_hexdump(pdp_info->apn_enc,
769 pdp_info->apn_enc_len));
770 continue;
771 }
772
773 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
774 LOGVSUBP(LOGL_ERROR, vsub,
775 "QoS info too long (%zu)\n",
776 pdp_info->qos_enc_len);
777 continue;
778 }
779
780 LOGVSUBP(LOGL_INFO, vsub,
781 "Will set PDP info, context id = %zu, APN = %s\n",
782 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
783
784 /* Set PDP info [ctx_id] */
785 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
786 if (!pdp_data) {
787 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
788 pdp_data->context_id = ctx_id;
789 }
790
791 OSMO_ASSERT(pdp_data != NULL);
792 pdp_data->pdp_type = pdp_info->pdp_type;
793 osmo_apn_to_str(pdp_data->apn_str,
794 pdp_info->apn_enc, pdp_info->apn_enc_len);
795 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
796 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
797 }
798}
799
800
801/* Handle InsertSubscrData Result from HLR */
802static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
803 const struct osmo_gsup_message *gsup)
804{
805 struct osmo_gsup_message gsup_reply = {0};
806
807 vlr_subscr_gsup_insert_data(vsub, gsup);
808 vsub->vlr->ops.subscr_update(vsub);
809
810 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
811 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
812}
813
814/* Handle UpdateLocation Result from HLR */
815static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
816 const struct osmo_gsup_message *gsup)
817{
818 if (!vsub->lu_fsm) {
819 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
820 "without LU in progress\n");
821 return -ENODEV;
822 }
823
824 /* contrary to MAP, we allow piggy-backing subscriber data onto the
825 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
826 * nested INSERT SUBSCRIBER DATA transaction */
827 vlr_subscr_gsup_insert_data(vsub, gsup);
828
829 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
830
831 return 0;
832}
833
834/* Handle UpdateLocation Result from HLR */
835static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
836 const struct osmo_gsup_message *gsup)
837{
838 if (!vsub->lu_fsm) {
839 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
840 "without LU in progress\n");
841 return -ENODEV;
842 }
843
844 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
845 get_value_string(gsm48_gmm_cause_names, gsup->cause));
846
847 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
848 (void *)&gsup->cause);
849
850 return 0;
851}
852
Neels Hofmeyr15809592018-04-06 02:57:51 +0200853static void gmm_cause_to_fsm_and_mm_cause(enum gsm48_gmm_cause gmm_cause,
854 enum osmo_fsm_term_cause *fsm_cause_p,
855 enum gsm48_reject_value *gsm48_rej_p)
856{
857 enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR;
858 enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
859 switch (gmm_cause) {
860 case GMM_CAUSE_IMSI_UNKNOWN:
861 gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
862 break;
863 case GMM_CAUSE_ILLEGAL_MS:
864 gsm48_rej = GSM48_REJECT_ILLEGAL_MS;
865 break;
866 case GMM_CAUSE_IMEI_NOT_ACCEPTED:
867 gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED;
868 break;
869 case GMM_CAUSE_ILLEGAL_ME:
870 gsm48_rej = GSM48_REJECT_ILLEGAL_ME;
871 break;
872 case GMM_CAUSE_GPRS_NOTALLOWED:
873 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED;
874 break;
875 case GMM_CAUSE_GPRS_OTHER_NOTALLOWED:
876 gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED;
877 break;
878 case GMM_CAUSE_MS_ID_NOT_DERIVED:
879 gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE;
880 break;
881 case GMM_CAUSE_IMPL_DETACHED:
882 gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED;
883 break;
884 case GMM_CAUSE_PLMN_NOTALLOWED:
885 gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED;
886 break;
887 case GMM_CAUSE_LA_NOTALLOWED:
888 gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED;
889 break;
890 case GMM_CAUSE_ROAMING_NOTALLOWED:
891 gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED;
892 break;
893 case GMM_CAUSE_NO_GPRS_PLMN:
894 gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN;
895 break;
896 case GMM_CAUSE_MSC_TEMP_NOTREACH:
897 gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE;
898 break;
899 case GMM_CAUSE_SYNC_FAIL:
900 gsm48_rej = GSM48_REJECT_SYNCH_FAILURE;
901 break;
902 case GMM_CAUSE_CONGESTION:
903 gsm48_rej = GSM48_REJECT_CONGESTION;
904 break;
905 case GMM_CAUSE_SEM_INCORR_MSG:
906 gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE;
907 break;
908 case GMM_CAUSE_INV_MAND_INFO:
909 gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF;
910 break;
911 case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL:
912 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED;
913 break;
914 case GMM_CAUSE_MSGT_INCOMP_P_STATE:
915 gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE;
916 break;
917 case GMM_CAUSE_IE_NOTEXIST_NOTIMPL:
918 gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED;
919 break;
920 case GMM_CAUSE_COND_IE_ERR:
921 gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR;
922 break;
923 case GMM_CAUSE_MSG_INCOMP_P_STATE:
924 gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE;
925 break;
926 case GMM_CAUSE_PROTO_ERR_UNSPEC:
927 gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR;
928 break;
929
930 case GMM_CAUSE_NO_SUIT_CELL_IN_LA:
931 case GMM_CAUSE_MAC_FAIL:
932 case GMM_CAUSE_GSM_AUTH_UNACCEPT:
933 case GMM_CAUSE_NOT_AUTH_FOR_CSG:
934 case GMM_CAUSE_SMS_VIA_GPRS_IN_RA:
935 case GMM_CAUSE_NO_PDP_ACTIVATED:
936 case GMM_CAUSE_NET_FAIL:
937 gsm48_rej = GSM48_REJECT_NETWORK_FAILURE;
938 break;
939 }
940 switch (gmm_cause) {
941 /* refine any error causes here? */
942 default:
943 fsm_cause = OSMO_FSM_TERM_ERROR;
944 break;
945 }
946 if (fsm_cause_p)
947 *fsm_cause_p = fsm_cause;
948 if (gsm48_rej_p)
949 *gsm48_rej_p = gsm48_rej;
950}
951
Harald Welteb8b85a12016-06-17 00:06:42 +0200952/* Handle LOCATION CANCEL request from HLR */
953static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
954 struct osmo_gsup_message *gsup_msg)
955{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200956 enum gsm48_reject_value gsm48_rej;
957 enum osmo_fsm_term_cause fsm_cause;
Harald Welteb8b85a12016-06-17 00:06:42 +0200958 struct osmo_gsup_message gsup_reply = {0};
Max770fbd22018-01-24 12:48:33 +0100959 int rc, is_update_procedure = !gsup_msg->cancel_type ||
Harald Welteb8b85a12016-06-17 00:06:42 +0200960 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
961
962 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
963 is_update_procedure ?
964 "update procedure" : "subscription withdraw");
965
966 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Max770fbd22018-01-24 12:48:33 +0100967 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
Harald Welteb8b85a12016-06-17 00:06:42 +0200968
Neels Hofmeyr15809592018-04-06 02:57:51 +0200969 gmm_cause_to_fsm_and_mm_cause(gsup_msg->cause, &fsm_cause, &gsm48_rej);
970 vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej);
Harald Welteb8b85a12016-06-17 00:06:42 +0200971
Max770fbd22018-01-24 12:48:33 +0100972 return rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200973}
974
975/* Incoming handler for GSUP from HLR.
976 * Keep this function non-static for direct invocation by unit tests. */
977int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
978{
979 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
980 struct vlr_subscr *vsub;
981 struct osmo_gsup_message gsup;
982 int rc;
983
984 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
985 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
986
987 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
988 if (rc < 0) {
989 LOGP(DVLR, LOGL_ERROR,
990 "decoding GSUP message fails with error '%s' (%d)\n",
991 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +0100992 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200993 }
994
995 if (!gsup.imsi[0]) {
996 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
Max770fbd22018-01-24 12:48:33 +0100997 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) {
998 rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO);
999 if (rc < 0)
1000 LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi);
1001 }
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001002 rc = -GMM_CAUSE_INV_MAND_INFO;
1003 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001004 }
1005
1006 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
1007 if (!vsub) {
1008 switch (gsup.message_type) {
1009 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1010 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001011 rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
1012 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001013 default:
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001014 rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup);
1015 goto msgb_free_and_return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001016 }
1017 }
1018
1019 switch (gsup.message_type) {
1020 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
1021 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
1022 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
1023 break;
1024 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
1025 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
1026 break;
1027 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
1028 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
1029 break;
1030 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
1031 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
1032 break;
1033 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
1034 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
1035 break;
1036 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
1037 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
1038 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
1039 LOGVSUBP(LOGL_ERROR, vsub,
1040 "Rx GSUP msg_type=%d not yet implemented\n",
1041 gsup.message_type);
1042 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1043 break;
1044 default:
1045 LOGVSUBP(LOGL_ERROR, vsub,
1046 "Rx GSUP msg_type=%d not valid at VLR/SGSN side\n",
1047 gsup.message_type);
1048 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1049 break;
1050 }
1051
1052 vlr_subscr_put(vsub);
Neels Hofmeyrb3fa3552017-11-18 22:22:59 +01001053
1054msgb_free_and_return:
1055 msgb_free(msg);
Harald Welteb8b85a12016-06-17 00:06:42 +02001056 return rc;
1057}
1058
1059/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1060int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
1061 const uint8_t *mi, size_t mi_len)
1062{
1063 char mi_string[GSM48_MI_SIZE];
1064 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
1065
1066 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
1067
1068 /* update the vlr_subscr with the given identity */
1069 switch (mi_type) {
1070 case GSM_MI_TYPE_IMSI:
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001071 if (strlen(mi_string) >= sizeof(vsub->imsi)) {
1072 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP too long (>%zu bytes): %s\n",
1073 sizeof(vsub->imsi) - 1, mi_string);
1074 return -ENOSPC; /* ignore message; do not avance LU FSM */
1075 } else if (vsub->imsi[0]
Harald Welteb8b85a12016-06-17 00:06:42 +02001076 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
1077 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
1078 " %s\n", mi_string);
Stefan Sperling9fbb6002018-06-25 17:31:59 +02001079 /* XXX Should we return an error, e.g. -EINVAL ? */
Harald Welteb8b85a12016-06-17 00:06:42 +02001080 } else
1081 vlr_subscr_set_imsi(vsub, mi_string);
1082 break;
1083 case GSM_MI_TYPE_IMEI:
1084 vlr_subscr_set_imei(vsub, mi_string);
1085 break;
1086 case GSM_MI_TYPE_IMEISV:
1087 vlr_subscr_set_imeisv(vsub, mi_string);
1088 break;
1089 }
1090
1091 if (vsub->auth_fsm) {
1092 switch (mi_type) {
1093 case GSM_MI_TYPE_IMSI:
1094 osmo_fsm_inst_dispatch(vsub->auth_fsm,
1095 VLR_AUTH_E_MS_ID_IMSI, mi_string);
1096 break;
1097 }
1098 }
1099
1100 if (vsub->lu_fsm) {
1101 uint32_t event = 0;
1102 switch (mi_type) {
1103 case GSM_MI_TYPE_IMSI:
1104 event = VLR_ULA_E_ID_IMSI;
1105 break;
1106 case GSM_MI_TYPE_IMEI:
1107 event = VLR_ULA_E_ID_IMEI;
1108 break;
1109 case GSM_MI_TYPE_IMEISV:
1110 event = VLR_ULA_E_ID_IMEISV;
1111 break;
1112 default:
1113 OSMO_ASSERT(0);
1114 break;
1115 }
1116 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
1117 } else {
1118 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
1119 }
1120
1121 return 0;
1122}
1123
1124/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
1125int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
1126{
1127 if (vsub->lu_fsm) {
1128 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
1129 VLR_ULA_E_NEW_TMSI_ACK, NULL);
1130 } else if (vsub->proc_arq_fsm) {
1131 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
1132 PR_ARQ_E_TMSI_ACK, NULL);
1133 } else {
1134 LOGVSUBP(LOGL_NOTICE, vsub,
1135 "gratuitous TMSI REALLOC COMPL");
1136 return -EINVAL;
1137 }
1138}
1139
Maxdcc193d2017-12-27 19:34:15 +01001140bool vlr_subscr_expire(struct vlr_subscr *vsub)
1141{
1142 if (vsub->lu_complete) {
1143 vsub->lu_complete = false;
1144 vlr_subscr_put(vsub);
1145
1146 return true;
1147 }
1148
1149 return false;
1150}
1151
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001152/* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */
Harald Welteb8b85a12016-06-17 00:06:42 +02001153int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
1154{
1155 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001156 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001157
1158 vsub->imsi_detached_flag = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001159 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
Maxdcc193d2017-12-27 19:34:15 +01001160
1161 /* balancing the get from vlr_lu_compl_fsm_success() */
1162 vlr_subscr_expire(vsub);
1163
Harald Welteb8b85a12016-06-17 00:06:42 +02001164 return 0;
1165}
1166
1167/* Tear down any running FSMs due to MSC connection timeout.
1168 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
1169 * message before the entire connection is torn down.
1170 * \param[in] vsub subscriber to tear down
1171 */
1172void vlr_subscr_conn_timeout(struct vlr_subscr *vsub)
1173{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001174 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION);
Harald Welteb8b85a12016-06-17 00:06:42 +02001175}
1176
1177struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
1178{
1179 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
1180 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001181
1182 /* Some of these are needed only on UTRAN, but in case the caller wants
1183 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001184 OSMO_ASSERT(ops->tx_auth_req);
1185 OSMO_ASSERT(ops->tx_auth_rej);
1186 OSMO_ASSERT(ops->tx_id_req);
1187 OSMO_ASSERT(ops->tx_lu_acc);
1188 OSMO_ASSERT(ops->tx_lu_rej);
1189 OSMO_ASSERT(ops->tx_cm_serv_acc);
1190 OSMO_ASSERT(ops->tx_cm_serv_rej);
1191 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001192 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +02001193 OSMO_ASSERT(ops->subscr_update);
1194 OSMO_ASSERT(ops->subscr_assoc);
1195
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
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001218 vlr->gsup_client = gsup_client_create(vlr, gsup_unit_name,
Harald Welteb8b85a12016-06-17 00:06:42 +02001219 gsup_server_addr_str,
1220 gsup_server_port,
1221 &vlr_gsupc_read_cb, NULL);
1222 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}