blob: a9aacbd6f7898edb1e58a7360b41f3267e3b75bf [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>
25#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
26#include <osmocom/gsm/gsup.h>
27#include <osmocom/gsm/apn.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020028#include <osmocom/msc/gsm_subscriber.h>
29#include <osmocom/msc/gsup_client.h>
30#include <osmocom/msc/vlr.h>
31#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020032
33#include <openssl/rand.h>
34
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <limits.h>
38
39#include "vlr_core.h"
40#include "vlr_auth_fsm.h"
41#include "vlr_lu_fsm.h"
42#include "vlr_access_req_fsm.h"
43
44#define SGSN_SUBSCR_MAX_RETRIES 3
45#define SGSN_SUBSCR_RETRY_INTERVAL 10
46
47/***********************************************************************
48 * Convenience functions
49 ***********************************************************************/
50
51const struct value_string vlr_ciph_names[] = {
52 OSMO_VALUE_STRING(VLR_CIPH_NONE),
53 OSMO_VALUE_STRING(VLR_CIPH_A5_1),
54 OSMO_VALUE_STRING(VLR_CIPH_A5_2),
55 OSMO_VALUE_STRING(VLR_CIPH_A5_3),
56 { 0, NULL }
57};
58
59uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer)
60{
61 uint32_t tidx = 0xffffffff;
62
63 switch (timer) {
64 case 3270:
65 tidx = VLR_T_3270;
66 break;
67 case 3260:
68 tidx = VLR_T_3260;
69 break;
70 case 3250:
71 tidx = VLR_T_3250;
72 break;
73 }
74
75 OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer));
76 return vlr->cfg.timer[tidx];
77}
78
79struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr,
80 const char *imsi,
81 const char *file, int line)
82{
83 struct vlr_subscr *vsub;
84
85 if (!imsi || !*imsi)
86 return NULL;
87
88 llist_for_each_entry(vsub, &vlr->subscribers, list) {
89 if (vlr_subscr_matches_imsi(vsub, imsi))
90 return _vlr_subscr_get(vsub, file, line);
91 }
92 return NULL;
93}
94
95struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr,
96 uint32_t tmsi,
97 const char *file, int line)
98{
99 struct vlr_subscr *vsub;
100
101 if (tmsi == GSM_RESERVED_TMSI)
102 return NULL;
103
104 llist_for_each_entry(vsub, &vlr->subscribers, list) {
105 if (vlr_subscr_matches_tmsi(vsub, tmsi))
106 return _vlr_subscr_get(vsub, file, line);
107 }
108 return NULL;
109}
110
111struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr,
112 const char *msisdn,
113 const char *file, int line)
114{
115 struct vlr_subscr *vsub;
116
117 if (!msisdn || !*msisdn)
118 return NULL;
119
120 llist_for_each_entry(vsub, &vlr->subscribers, list) {
121 if (vlr_subscr_matches_msisdn(vsub, msisdn))
122 return _vlr_subscr_get(vsub, file, line);
123 }
124 return NULL;
125}
126
127/* Transmit GSUP message to HLR */
128static int vlr_tx_gsup_message(struct vlr_instance *vlr,
129 struct osmo_gsup_message *gsup_msg)
130{
131 struct msgb *msg = gsup_client_msgb_alloc();
132
133 osmo_gsup_encode(msg, gsup_msg);
134
135 if (!vlr->gsup_client) {
136 LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot "
137 "send GSUP: %s\n", msgb_hexdump(msg));
138 msgb_free(msg);
139 return -ENOTSUP;
140 }
141
142 LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n",
143 osmo_hexdump_nospc(msg->data, msg->len));
144
145 return gsup_client_send(vlr->gsup_client, msg);
146}
147
148/* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */
149static int vlr_subscr_tx_gsup_message(struct vlr_subscr *vsub,
150 struct osmo_gsup_message *gsup_msg)
151{
152 struct vlr_instance *vlr = vsub->vlr;
153
154 if (strlen(gsup_msg->imsi) == 0)
155 osmo_strlcpy(gsup_msg->imsi, vsub->imsi, sizeof(gsup_msg->imsi));
156
157 return vlr_tx_gsup_message(vlr, gsup_msg);
158}
159
160/* Transmit GSUP error in response to original message */
161static int vlr_tx_gsup_error_reply(struct vlr_instance *vlr,
162 struct osmo_gsup_message *gsup_orig,
163 enum gsm48_gmm_cause cause)
164{
165 struct osmo_gsup_message gsup_reply = {0};
166
167 osmo_strlcpy(gsup_reply.imsi, gsup_orig->imsi, sizeof(gsup_reply.imsi));
168 gsup_reply.cause = cause;
169 gsup_reply.message_type =
170 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
171
172 return vlr_tx_gsup_message(vlr, &gsup_reply);
173}
174
175struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line)
176{
177 if (!sub)
178 return NULL;
179 OSMO_ASSERT(sub->use_count < INT_MAX);
180 sub->use_count++;
181 LOGPSRC(DREF, LOGL_DEBUG, file, line,
182 "VLR subscr %s usage increases to: %d\n",
183 vlr_subscr_name(sub), sub->use_count);
184 return sub;
185}
186
187struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line)
188{
189 if (!sub)
190 return NULL;
191 sub->use_count--;
192 LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
193 file, line,
194 "VLR subscr %s usage decreases to: %d\n",
195 vlr_subscr_name(sub), sub->use_count);
196 if (sub->use_count <= 0)
197 vlr_subscr_free(sub);
198 return NULL;
199}
200
201/* Allocate a new subscriber and insert it into list */
202static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr)
203{
204 struct vlr_subscr *vsub;
205 int i;
206
207 vsub = talloc_zero(vlr, struct vlr_subscr);
208 vsub->vlr = vlr;
209 vsub->tmsi = GSM_RESERVED_TMSI;
210 vsub->tmsi_new = GSM_RESERVED_TMSI;
211
212 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
213 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
214
215 INIT_LLIST_HEAD(&vsub->cs.requests);
216 INIT_LLIST_HEAD(&vsub->ps.pdp_list);
217
218 llist_add_tail(&vsub->list, &vlr->subscribers);
219 return vsub;
220}
221
222struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr)
223{
224 return vlr_subscr_get(_vlr_subscr_alloc(vlr));
225}
226
227/* Send a GSUP Purge MS request.
228 * TODO: this should be sent to the *previous* VLR when this VLR is "taking"
229 * this subscriber, not to the HLR? */
230int vlr_subscr_purge(struct vlr_subscr *vsub)
231{
232 struct osmo_gsup_message gsup_msg = {0};
233
234 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
235
236 /* provide HLR number in case we know it */
237 gsup_msg.hlr_enc_len = vsub->hlr.len;
238 gsup_msg.hlr_enc = vsub->hlr.buf;
239
240 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
241}
242
243void vlr_subscr_cancel(struct vlr_subscr *vsub, enum gsm48_gmm_cause cause)
244{
245 if (!vsub)
246 return;
247
248 if (vsub->lu_fsm) {
249 if (vsub->lu_fsm->state == VLR_ULA_S_WAIT_HLR_UPD)
250 osmo_fsm_inst_dispatch(vsub->lu_fsm,
251 VLR_ULA_E_HLR_LU_RES,
252 (void*)&cause);
253 else
254 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_ERROR,
255 0);
256 }
257
258 if (vsub->proc_arq_fsm)
259 osmo_fsm_inst_term(vsub->proc_arq_fsm, OSMO_FSM_TERM_ERROR, 0);
260}
261
262/* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */
263void vlr_subscr_free(struct vlr_subscr *vsub)
264{
265 llist_del(&vsub->list);
266 DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub));
267 talloc_free(vsub);
268}
269
270/* Generate a new TMSI and store in vsub->tmsi_new.
271 * Search all known subscribers to ensure that the TMSI is unique. */
272int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub)
273{
274 struct vlr_instance *vlr = vsub->vlr;
275 uint32_t tmsi;
276 int tried;
277
278 for (tried = 0; tried < 100; tried++) {
279 if (RAND_bytes((uint8_t *) &tmsi, sizeof(tmsi)) != 1) {
280 LOGP(DVLR, LOGL_ERROR, "RAND_bytes failed\n");
281 return -1;
282 }
283 /* throw the dice again, if the TSMI doesn't fit */
284 if (tmsi == GSM_RESERVED_TMSI)
285 continue;
286
287 /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */
288 if (vlr->cfg.is_ps) {
289 /* SGSN */
290 tmsi |= 0xC000000;
291 } else {
292 /* MSC */
293 if ((tmsi & 0xC0000000) == 0xC0000000)
294 tmsi &= ~0xC0000000;
295 }
296
297 /* If this TMSI is already in use, try another one. */
298 if (vlr_subscr_find_by_tmsi(vlr, tmsi))
299 continue;
300
301 vsub->tmsi_new = tmsi;
302 return 0;
303 }
304
305 LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI"
306 " after %d tries\n", vlr_subscr_name(vsub), tried);
307 return -1;
308}
309
310/* Find subscriber by IMSI, or create new subscriber if not found.
311 * \param[in] vlr VLR instace.
312 * \param[in] imsi IMSI string.
313 * \param[out] created if non-NULL, returns whether a new entry was created. */
314struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr,
315 const char *imsi,
316 bool *created,
317 const char *file,
318 int line)
319{
320 struct vlr_subscr *vsub;
321 vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line);
322 if (vsub) {
323 if (created)
324 *created = false;
325 return vsub;
326 }
327
328 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
329 if (!vsub)
330 return NULL;
331 vlr_subscr_set_imsi(vsub, imsi);
332 LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi);
333 if (created)
334 *created = true;
335 return vsub;
336}
337
338/* Find subscriber by TMSI, or create new subscriber if not found.
339 * \param[in] vlr VLR instace.
340 * \param[in] tmsi TMSI.
341 * \param[out] created if non-NULL, returns whether a new entry was created. */
342struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr,
343 uint32_t tmsi,
344 bool *created,
345 const char *file,
346 int line)
347{
348 struct vlr_subscr *vsub;
349 vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line);
350 if (vsub) {
351 if (created)
352 *created = false;
353 return vsub;
354 }
355
356 vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line);
357 if (!vsub)
358 return NULL;
359 vsub->tmsi = tmsi;
360 LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi);
361 if (created)
362 *created = true;
363 return vsub;
364}
365
366void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi)
367{
368 if (!vsub)
369 return;
370 osmo_strlcpy(vsub->imsi, imsi, sizeof(vsub->imsi));
371 vsub->id = atoll(vsub->imsi);
372 DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n",
373 vsub->imsi, vsub->id);
374}
375
376void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei)
377{
378 if (!vsub)
379 return;
380 osmo_strlcpy(vsub->imei, imei, sizeof(vsub->imei));
381 DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n",
382 vsub->imsi, vsub->imei);
383}
384
385void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv)
386{
387 if (!vsub)
388 return;
389 osmo_strlcpy(vsub->imeisv, imeisv, sizeof(vsub->imeisv));
390 DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n",
391 vsub->imsi, vsub->imeisv);
392}
393
394/* Safely copy the given MSISDN string to vsub->msisdn */
395void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn)
396{
397 if (!vsub)
398 return;
399 osmo_strlcpy(vsub->msisdn, msisdn, sizeof(vsub->msisdn));
400 DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n",
401 vsub->imsi, vsub->msisdn);
402}
403
404bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
405{
406 return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
407}
408
409bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi)
410{
411 return vsub && tmsi != GSM_RESERVED_TMSI
412 && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi);
413}
414
415bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn)
416{
417 return vsub && msisdn && vsub->msisdn[0]
418 && !strcmp(vsub->msisdn, msisdn);
419}
420
421bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei)
422{
423 return vsub && imei && vsub->imei[0]
424 && !strcmp(vsub->imei, imei);
425}
426
427/* Send updated subscriber information to HLR */
428int vlr_subscr_changed(struct vlr_subscr *vsub)
429{
430 /* FIXME */
431 LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__);
432 return 0;
433}
434
435/***********************************************************************
436 * PDP context data
437 ***********************************************************************/
438
Neels Hofmeyrbac22762017-07-06 18:39:28 +0200439#define GSM_APN_LENGTH 102
440
441/* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */
442/* see GSM 09.02, B.1, gprsSubscriptionData */
443struct sgsn_subscriber_pdp_data {
444 struct llist_head list;
445
446 unsigned int context_id;
447 uint16_t pdp_type;
448 char apn_str[GSM_APN_LENGTH];
449 uint8_t qos_subscribed[20];
450 size_t qos_subscribed_len;
451};
452
Harald Welteb8b85a12016-06-17 00:06:42 +0200453struct sgsn_subscriber_pdp_data *
454vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub)
455{
456 struct sgsn_subscriber_pdp_data* pdata;
457
458 pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data);
459
460 llist_add_tail(&pdata->list, &vsub->ps.pdp_list);
461
462 return pdata;
463}
464
465static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub)
466{
467 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
468 int count = 0;
469
470 llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) {
471 llist_del(&pdp->list);
472 talloc_free(pdp);
473 count += 1;
474 }
475
476 return count;
477}
478
479static struct sgsn_subscriber_pdp_data *
480vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id)
481{
482 struct sgsn_subscriber_pdp_data *pdp;
483
484 llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) {
485 if (pdp->context_id == context_id)
486 return pdp;
487 }
488
489 return NULL;
490}
491
492/***********************************************************************
493 * Actual Implementation
494 ***********************************************************************/
495
496static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr,
497 struct osmo_gsup_message *gsup_msg)
498{
499 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
500 vlr_tx_gsup_error_reply(vlr, gsup_msg,
501 GMM_CAUSE_IMSI_UNKNOWN);
502 LOGP(DVLR, LOGL_NOTICE,
503 "Unknown IMSI %s, discarding GSUP request "
504 "of type 0x%02x\n",
505 gsup_msg->imsi, gsup_msg->message_type);
506 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
507 LOGP(DVLR, LOGL_NOTICE,
508 "Unknown IMSI %s, discarding GSUP error "
509 "of type 0x%02x, cause '%s' (%d)\n",
510 gsup_msg->imsi, gsup_msg->message_type,
511 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
512 gsup_msg->cause);
513 } else {
514 LOGP(DVLR, LOGL_NOTICE,
515 "Unknown IMSI %s, discarding GSUP response "
516 "of type 0x%02x\n",
517 gsup_msg->imsi, gsup_msg->message_type);
518 }
519
520 return -GMM_CAUSE_IMSI_UNKNOWN;
521}
522
523static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr,
524 struct osmo_gsup_message *gsup_msg)
525{
526 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
527 LOGGSUPP(LOGL_NOTICE, gsup_msg,
528 "Purge MS has failed with cause '%s' (%d)\n",
529 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
530 gsup_msg->cause);
531 return -gsup_msg->cause;
532 }
533 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
534 return 0;
535}
536
537/* VLR internal call to request UpdateLocation from HLR */
538int vlr_subscr_req_lu(struct vlr_subscr *vsub, bool is_ps)
539{
540 struct osmo_gsup_message gsup_msg = {0};
541 int rc;
542
543 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
544 rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
545
546 return rc;
547}
548
549/* VLR internal call to request tuples from HLR */
550int vlr_subscr_req_sai(struct vlr_subscr *vsub,
551 const uint8_t *auts, const uint8_t *auts_rand)
552{
553 struct osmo_gsup_message gsup_msg = {0};
554
555 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
556 gsup_msg.auts = auts;
557 gsup_msg.rand = auts_rand;
558
559 return vlr_subscr_tx_gsup_message(vsub, &gsup_msg);
560}
561
562/* Tell HLR that authentication failure occurred */
563int vlr_subscr_tx_auth_fail_rep(struct vlr_subscr *vsub)
564{
565 struct osmo_gsup_message gsup_msg = {0};
566
567 gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT;
568 osmo_strlcpy(gsup_msg.imsi, vsub->imsi, sizeof(gsup_msg.imsi));
569 return vlr_tx_gsup_message(vsub->vlr, &gsup_msg);
570}
571
572/* Update the subscriber with GSUP-received auth tuples */
573void vlr_subscr_update_tuples(struct vlr_subscr *vsub,
574 const struct osmo_gsup_message *gsup)
575{
576 unsigned int i;
577 unsigned int got_tuples;
578
579 if (gsup->num_auth_vectors) {
580 memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples));
581 for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++)
582 vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL;
583 }
584
585 got_tuples = 0;
586 for (i = 0; i < gsup->num_auth_vectors; i++) {
587 size_t key_seq = i;
588
589 if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) {
590 LOGVSUBP(LOGL_NOTICE, vsub,
591 "Skipping auth tuple wih invalid cksn %zu\n",
592 key_seq);
593 continue;
594 }
595 vsub->auth_tuples[i].vec = gsup->auth_vectors[i];
596 vsub->auth_tuples[i].key_seq = key_seq;
597 got_tuples ++;
598 }
599
600 LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples);
601
602 if (!got_tuples) {
603 /* FIXME what now? */
604 // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ?
605 }
606
607 /* New tuples means last_tuple becomes invalid */
608 vsub->last_tuple = NULL;
609}
610
611/* Handle SendAuthInfo Result/Error from HLR */
612static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub,
613 const struct osmo_gsup_message *gsup)
614{
615 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
616 void *data = (void *) gsup;
617
618 switch (gsup->message_type) {
619 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
620 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data);
621 break;
622 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
623 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data);
624 break;
625 default:
626 return -1;
627 }
628
629 return 0;
630}
631
632static int decode_bcd_number_safe(char *output, int output_len,
633 const uint8_t *bcd_lv, int input_len,
634 int h_len)
635{
636 uint8_t len;
637 OSMO_ASSERT(output_len >= 1);
638 *output = '\0';
639 if (input_len < 1)
640 return -EIO;
641 len = bcd_lv[0];
642 if (input_len < len)
643 return -EIO;
644 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
645}
646
647static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub,
648 const struct osmo_gsup_message *gsup_msg)
649{
650 unsigned idx;
651 int rc;
652
653 if (gsup_msg->msisdn_enc) {
654 decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn),
655 gsup_msg->msisdn_enc,
656 gsup_msg->msisdn_enc_len, 0);
657 LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n",
658 vsub->imsi, vsub->msisdn);
659 }
660
661 if (gsup_msg->hlr_enc) {
662 if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) {
663 LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n",
664 gsup_msg->hlr_enc_len);
665 vsub->hlr.len = 0;
666 } else {
667 memcpy(vsub->hlr.buf, gsup_msg->hlr_enc,
668 gsup_msg->hlr_enc_len);
669 vsub->hlr.len = gsup_msg->hlr_enc_len;
670 }
671 }
672
673 if (gsup_msg->pdp_info_compl) {
674 rc = vlr_subscr_pdp_data_clear(vsub);
675 if (rc > 0)
676 LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n");
677 }
678
679 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
680 const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
681 size_t ctx_id = pdp_info->context_id;
682 struct sgsn_subscriber_pdp_data *pdp_data;
683
684 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
685 LOGVSUBP(LOGL_ERROR, vsub,
686 "APN too long, context id = %zu, APN = %s\n",
687 ctx_id, osmo_hexdump(pdp_info->apn_enc,
688 pdp_info->apn_enc_len));
689 continue;
690 }
691
692 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
693 LOGVSUBP(LOGL_ERROR, vsub,
694 "QoS info too long (%zu)\n",
695 pdp_info->qos_enc_len);
696 continue;
697 }
698
699 LOGVSUBP(LOGL_INFO, vsub,
700 "Will set PDP info, context id = %zu, APN = %s\n",
701 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
702
703 /* Set PDP info [ctx_id] */
704 pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id);
705 if (!pdp_data) {
706 pdp_data = vlr_subscr_pdp_data_alloc(vsub);
707 pdp_data->context_id = ctx_id;
708 }
709
710 OSMO_ASSERT(pdp_data != NULL);
711 pdp_data->pdp_type = pdp_info->pdp_type;
712 osmo_apn_to_str(pdp_data->apn_str,
713 pdp_info->apn_enc, pdp_info->apn_enc_len);
714 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
715 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
716 }
717}
718
719
720/* Handle InsertSubscrData Result from HLR */
721static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub,
722 const struct osmo_gsup_message *gsup)
723{
724 struct osmo_gsup_message gsup_reply = {0};
725
726 vlr_subscr_gsup_insert_data(vsub, gsup);
727 vsub->vlr->ops.subscr_update(vsub);
728
729 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
730 return vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
731}
732
733/* Handle UpdateLocation Result from HLR */
734static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub,
735 const struct osmo_gsup_message *gsup)
736{
737 if (!vsub->lu_fsm) {
738 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result "
739 "without LU in progress\n");
740 return -ENODEV;
741 }
742
743 /* contrary to MAP, we allow piggy-backing subscriber data onto the
744 * UPDATE LOCATION RESULT, and don't mandate the use of a separate
745 * nested INSERT SUBSCRIBER DATA transaction */
746 vlr_subscr_gsup_insert_data(vsub, gsup);
747
748 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL);
749
750 return 0;
751}
752
753/* Handle UpdateLocation Result from HLR */
754static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub,
755 const struct osmo_gsup_message *gsup)
756{
757 if (!vsub->lu_fsm) {
758 LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error "
759 "without LU in progress\n");
760 return -ENODEV;
761 }
762
763 LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n",
764 get_value_string(gsm48_gmm_cause_names, gsup->cause));
765
766 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES,
767 (void *)&gsup->cause);
768
769 return 0;
770}
771
772/* Handle LOCATION CANCEL request from HLR */
773static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub,
774 struct osmo_gsup_message *gsup_msg)
775{
776 struct osmo_gsup_message gsup_reply = {0};
777 int is_update_procedure = !gsup_msg->cancel_type ||
778 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
779
780 LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n",
781 is_update_procedure ?
782 "update procedure" : "subscription withdraw");
783
784 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
785 vlr_subscr_tx_gsup_message(vsub, &gsup_reply);
786
787 vlr_subscr_cancel(vsub, gsup_msg->cause);
788
789 return 0;
790}
791
792/* Incoming handler for GSUP from HLR.
793 * Keep this function non-static for direct invocation by unit tests. */
794int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
795{
796 struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data;
797 struct vlr_subscr *vsub;
798 struct osmo_gsup_message gsup;
799 int rc;
800
801 DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg),
802 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
803
804 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
805 if (rc < 0) {
806 LOGP(DVLR, LOGL_ERROR,
807 "decoding GSUP message fails with error '%s' (%d)\n",
808 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
809 return rc;
810 }
811
812 if (!gsup.imsi[0]) {
813 LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n");
814 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type))
815 vlr_tx_gsup_error_reply(vlr, &gsup,
816 GMM_CAUSE_INV_MAND_INFO);
817 return -GMM_CAUSE_INV_MAND_INFO;
818 }
819
820 vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi);
821 if (!vsub) {
822 switch (gsup.message_type) {
823 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
824 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
825 return vlr_rx_gsup_purge_no_subscr(vlr, &gsup);
826 default:
827 return vlr_rx_gsup_unknown_imsi(vlr, &gsup);
828 }
829 }
830
831 switch (gsup.message_type) {
832 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
833 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
834 rc = vlr_subscr_handle_sai_res(vsub, &gsup);
835 break;
836 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
837 rc = vlr_subscr_handle_isd_req(vsub, &gsup);
838 break;
839 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
840 rc = vlr_subscr_handle_cancel_req(vsub, &gsup);
841 break;
842 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
843 rc = vlr_subscr_handle_lu_res(vsub, &gsup);
844 break;
845 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
846 rc = vlr_subscr_handle_lu_err(vsub, &gsup);
847 break;
848 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
849 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
850 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
851 LOGVSUBP(LOGL_ERROR, vsub,
852 "Rx GSUP msg_type=%d not yet implemented\n",
853 gsup.message_type);
854 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
855 break;
856 default:
857 LOGVSUBP(LOGL_ERROR, vsub,
858 "Rx GSUP msg_type=%d not valid at VLR/SGSN side\n",
859 gsup.message_type);
860 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
861 break;
862 }
863
864 vlr_subscr_put(vsub);
865 return rc;
866}
867
868/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
869int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub,
870 const uint8_t *mi, size_t mi_len)
871{
872 char mi_string[GSM48_MI_SIZE];
873 uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK;
874
875 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
876
877 /* update the vlr_subscr with the given identity */
878 switch (mi_type) {
879 case GSM_MI_TYPE_IMSI:
880 if (vsub->imsi[0]
881 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
882 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
883 " %s\n", mi_string);
884 } else
885 vlr_subscr_set_imsi(vsub, mi_string);
886 break;
887 case GSM_MI_TYPE_IMEI:
888 vlr_subscr_set_imei(vsub, mi_string);
889 break;
890 case GSM_MI_TYPE_IMEISV:
891 vlr_subscr_set_imeisv(vsub, mi_string);
892 break;
893 }
894
895 if (vsub->auth_fsm) {
896 switch (mi_type) {
897 case GSM_MI_TYPE_IMSI:
898 osmo_fsm_inst_dispatch(vsub->auth_fsm,
899 VLR_AUTH_E_MS_ID_IMSI, mi_string);
900 break;
901 }
902 }
903
904 if (vsub->lu_fsm) {
905 uint32_t event = 0;
906 switch (mi_type) {
907 case GSM_MI_TYPE_IMSI:
908 event = VLR_ULA_E_ID_IMSI;
909 break;
910 case GSM_MI_TYPE_IMEI:
911 event = VLR_ULA_E_ID_IMEI;
912 break;
913 case GSM_MI_TYPE_IMEISV:
914 event = VLR_ULA_E_ID_IMEISV;
915 break;
916 default:
917 OSMO_ASSERT(0);
918 break;
919 }
920 osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string);
921 } else {
922 LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n");
923 }
924
925 return 0;
926}
927
928/* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */
929int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub)
930{
931 if (vsub->lu_fsm) {
932 return osmo_fsm_inst_dispatch(vsub->lu_fsm,
933 VLR_ULA_E_NEW_TMSI_ACK, NULL);
934 } else if (vsub->proc_arq_fsm) {
935 return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm,
936 PR_ARQ_E_TMSI_ACK, NULL);
937 } else {
938 LOGVSUBP(LOGL_NOTICE, vsub,
939 "gratuitous TMSI REALLOC COMPL");
940 return -EINVAL;
941 }
942}
943
944int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub)
945{
946 /* paranoia: should any LU or PARQ FSMs still be running, stop them. */
947 vlr_subscr_cancel(vsub, GMM_CAUSE_IMPL_DETACHED);
948
949 vsub->imsi_detached_flag = true;
950 if (vsub->lu_complete) {
951 vsub->lu_complete = false;
952 /* balancing the get from vlr_lu_compl_fsm_success() */
953 vlr_subscr_put(vsub);
954 }
955 return 0;
956}
957
958/* Tear down any running FSMs due to MSC connection timeout.
959 * Visit all vsub->*_fsm pointers and give them a queue to send a final reject
960 * message before the entire connection is torn down.
961 * \param[in] vsub subscriber to tear down
962 */
963void vlr_subscr_conn_timeout(struct vlr_subscr *vsub)
964{
965 if (!vsub)
966 return;
967
968 vlr_loc_update_conn_timeout(vsub->lu_fsm);
969 vlr_parq_conn_timeout(vsub->proc_arq_fsm);
970}
971
972struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops)
973{
974 struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance);
975 OSMO_ASSERT(vlr);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200976
977 /* Some of these are needed only on UTRAN, but in case the caller wants
978 * only GERAN, she should just provide dummy callbacks. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200979 OSMO_ASSERT(ops->tx_auth_req);
980 OSMO_ASSERT(ops->tx_auth_rej);
981 OSMO_ASSERT(ops->tx_id_req);
982 OSMO_ASSERT(ops->tx_lu_acc);
983 OSMO_ASSERT(ops->tx_lu_rej);
984 OSMO_ASSERT(ops->tx_cm_serv_acc);
985 OSMO_ASSERT(ops->tx_cm_serv_rej);
986 OSMO_ASSERT(ops->set_ciph_mode);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200987 OSMO_ASSERT(ops->tx_common_id);
Harald Welteb8b85a12016-06-17 00:06:42 +0200988 OSMO_ASSERT(ops->subscr_update);
989 OSMO_ASSERT(ops->subscr_assoc);
990
991 INIT_LLIST_HEAD(&vlr->subscribers);
992 INIT_LLIST_HEAD(&vlr->operations);
993 memcpy(&vlr->ops, ops, sizeof(vlr->ops));
994
995 /* osmo_auth_fsm.c */
996 osmo_fsm_register(&vlr_auth_fsm);
997 /* osmo_lu_fsm.c */
998 vlr_lu_fsm_init();
999 /* vlr_access_request_fsm.c */
1000 vlr_parq_fsm_init();
1001
1002 return vlr;
1003}
1004
1005int vlr_start(const char *gsup_unit_name, struct vlr_instance *vlr,
1006 const char *gsup_server_addr_str, uint16_t gsup_server_port)
1007{
1008 OSMO_ASSERT(vlr);
1009
1010 vlr->gsup_client = gsup_client_create(gsup_unit_name,
1011 gsup_server_addr_str,
1012 gsup_server_port,
1013 &vlr_gsupc_read_cb, NULL);
1014 if (!vlr->gsup_client)
1015 return -ENOMEM;
1016 vlr->gsup_client->data = vlr;
1017
1018 return 0;
1019}
1020
1021/* MSC->VLR: Subscribre has disconnected */
1022int vlr_subscr_disconnected(struct vlr_subscr *vsub)
1023{
1024 /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B
1025 * interface */
1026 osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1027 osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL);
1028 vsub->msc_conn_ref = NULL;
1029
1030 return 0;
1031}
1032
1033/* MSC->VLR: Receive Authentication Failure from Subscriber */
1034int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts)
1035{
1036 struct vlr_auth_resp_par par = {0};
1037 par.auts = auts;
1038
1039 osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par);
1040 return 0;
1041}
1042
1043/* MSC->VLR: Receive Authentication Response from MS
1044 * \returns 1 in case of success, 0 in case of delay, -1 on auth error */
1045int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99,
1046 bool is_utran, const uint8_t *res, uint8_t res_len)
1047{
1048 struct osmo_fsm_inst *auth_fi = vsub->auth_fsm;
1049 struct vlr_auth_resp_par par;
1050
1051 par.is_r99 = is_r99;
1052 par.is_utran = is_utran;
1053 par.res = res;
1054 par.res_len = res_len;
1055 osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par);
1056
1057 return 0;
1058}
1059
1060/* MSC->VLR: Receive result of Ciphering Mode Command from MS */
1061void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res)
1062{
1063 if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH)
1064 osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res);
1065 if (vsub->proc_arq_fsm
1066 && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH)
1067 osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES,
1068 res);
1069}
1070
1071/* Internal evaluation of requested ciphering mode.
1072 * Send set_ciph_mode() to MSC depending on the ciph_mode argument.
1073 * \param[in] vlr VLR instance.
1074 * \param[in] fi Calling FSM instance, for logging.
1075 * \param[in] msc_conn_ref MSC conn to send to.
1076 * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering.
1077 * \returns 0 if no ciphering is needed or message was sent successfully,
1078 * or a negative value if ciph_mode is invalid or sending failed.
1079 */
1080int vlr_set_ciph_mode(struct vlr_instance *vlr,
1081 struct osmo_fsm_inst *fi,
1082 void *msc_conn_ref,
1083 enum vlr_ciph ciph_mode,
1084 bool retrieve_imeisv)
1085{
1086 switch (ciph_mode) {
1087 case VLR_CIPH_NONE:
1088 return 0;
1089
1090 case VLR_CIPH_A5_1:
1091 case VLR_CIPH_A5_3:
1092 return vlr->ops.set_ciph_mode(msc_conn_ref,
1093 ciph_mode,
1094 retrieve_imeisv);
1095
1096 case VLR_CIPH_A5_2:
1097 /* TODO policy by user config? */
1098 LOGPFSML(fi, LOGL_ERROR, "A5/2 ciphering is not allowed\n");
1099 return -EINVAL;
1100
1101 default:
1102 LOGPFSML(fi, LOGL_ERROR, "unknown ciphering value: %d\n",
1103 ciph_mode);
1104 return -EINVAL;
1105 }
1106}
1107
1108void log_set_filter_vlr_subscr(struct log_target *target,
1109 struct vlr_subscr *vlr_subscr)
1110{
1111 struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR];
1112
1113 /* free the old data */
1114 if (*fsub) {
1115 vlr_subscr_put(*fsub);
1116 *fsub = NULL;
1117 }
1118
1119 if (vlr_subscr) {
1120 target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR);
1121 *fsub = vlr_subscr_get(vlr_subscr);
1122 } else
1123 target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR);
1124}