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