Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1 | /* 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 Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 25 | #include <osmocom/core/timer.h> |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 26 | #include <osmocom/core/tdef.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 27 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/gsm23236.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/gsup.h> |
| 30 | #include <osmocom/gsm/apn.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm48.h> |
Stefan Sperling | afa030d | 2018-12-06 12:06:59 +0100 | [diff] [blame] | 32 | #include <osmocom/gsm/ipa.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 33 | #include <osmocom/msc/gsm_subscriber.h> |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 34 | #include <osmocom/gsupclient/gsup_client.h> |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 35 | #include <osmocom/msc/vlr_sgs.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 36 | #include <osmocom/msc/vlr.h> |
| 37 | #include <osmocom/msc/debug.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 38 | #include <osmocom/msc/gsup_client_mux.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 39 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 40 | #include <netinet/in.h> |
| 41 | #include <arpa/inet.h> |
| 42 | #include <limits.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 43 | #include <errno.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 44 | |
| 45 | #include "vlr_core.h" |
| 46 | #include "vlr_auth_fsm.h" |
| 47 | #include "vlr_lu_fsm.h" |
| 48 | #include "vlr_access_req_fsm.h" |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 49 | #include "vlr_sgs_fsm.h" |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 50 | |
| 51 | #define SGSN_SUBSCR_MAX_RETRIES 3 |
| 52 | #define SGSN_SUBSCR_RETRY_INTERVAL 10 |
| 53 | |
| 54 | /*********************************************************************** |
| 55 | * Convenience functions |
| 56 | ***********************************************************************/ |
| 57 | |
| 58 | const struct value_string vlr_ciph_names[] = { |
| 59 | OSMO_VALUE_STRING(VLR_CIPH_NONE), |
| 60 | OSMO_VALUE_STRING(VLR_CIPH_A5_1), |
| 61 | OSMO_VALUE_STRING(VLR_CIPH_A5_2), |
| 62 | OSMO_VALUE_STRING(VLR_CIPH_A5_3), |
| 63 | { 0, NULL } |
| 64 | }; |
| 65 | |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 66 | /* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */ |
| 67 | struct osmo_tdef msc_tdefs_vlr[] = { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 68 | { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" }, |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 69 | { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" }, |
| 70 | { .T = 3260, .default_val = 12, .desc = "Authentication procedure" }, |
| 71 | { .T = 3270, .default_val = 12, .desc = "Identification procedure" }, |
| 72 | { /* terminator */ } |
| 73 | }; |
| 74 | |
| 75 | /* This is just a wrapper around the osmo_tdef API. |
| 76 | * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 77 | uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer) |
| 78 | { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 79 | /* NOTE: since we usually do not need more than one instance of the VLR, |
| 80 | * and since libosmocore's osmo_tdef API does not (yet) support dynamic |
| 81 | * configuration, we always use the global instance of msc_tdefs_vlr. */ |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 82 | return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 85 | /* return static buffer with printable name of VLR subscriber */ |
Oliver Smith | 5598aae | 2019-01-08 11:47:21 +0100 | [diff] [blame] | 86 | const char *vlr_subscr_name(const struct vlr_subscr *vsub) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 87 | { |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 88 | static char buf[128]; |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 89 | struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) }; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 90 | bool present = false; |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 91 | if (!vsub) |
| 92 | return "unknown"; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 93 | if (vsub->imsi[0]) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 94 | OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 95 | present = true; |
| 96 | } |
| 97 | if (vsub->msisdn[0]) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 98 | OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 99 | present = true; |
| 100 | } |
| 101 | if (vsub->tmsi != GSM_RESERVED_TMSI) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 102 | OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 103 | present = true; |
| 104 | } |
| 105 | if (vsub->tmsi_new != GSM_RESERVED_TMSI) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 106 | OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 107 | present = true; |
| 108 | } |
| 109 | if (!present) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 110 | return "unknown"; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 111 | |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 112 | return buf; |
| 113 | } |
| 114 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 115 | const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen) |
| 116 | { |
| 117 | /* cast away the const so we can shorten the string within the static buffer */ |
| 118 | char *name = (char*)vlr_subscr_name(vsub); |
| 119 | size_t len = strlen(name); |
| 120 | if (maxlen < 2) |
| 121 | return "-"; |
| 122 | if (len > maxlen) |
| 123 | strcpy(name + maxlen - 2, ".."); |
| 124 | return name; |
| 125 | } |
| 126 | |
Oliver Smith | 5598aae | 2019-01-08 11:47:21 +0100 | [diff] [blame] | 127 | const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 128 | { |
| 129 | if (!vsub || !vsub->msisdn[0]) |
| 130 | return vlr_subscr_name(vsub); |
| 131 | return vsub->msisdn; |
| 132 | } |
| 133 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 134 | struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr, |
| 135 | const char *imsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 136 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 137 | const char *file, int line) |
| 138 | { |
| 139 | struct vlr_subscr *vsub; |
| 140 | |
| 141 | if (!imsi || !*imsi) |
| 142 | return NULL; |
| 143 | |
| 144 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 145 | if (vlr_subscr_matches_imsi(vsub, imsi)) { |
| 146 | vlr_subscr_get_src(vsub, use, file, line); |
| 147 | return vsub; |
| 148 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 149 | } |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr, |
| 154 | uint32_t tmsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 155 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 156 | const char *file, int line) |
| 157 | { |
| 158 | struct vlr_subscr *vsub; |
| 159 | |
| 160 | if (tmsi == GSM_RESERVED_TMSI) |
| 161 | return NULL; |
| 162 | |
| 163 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 164 | if (vlr_subscr_matches_tmsi(vsub, tmsi)) { |
| 165 | vlr_subscr_get_src(vsub, use, file, line); |
| 166 | return vsub; |
| 167 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr, |
| 173 | const char *msisdn, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 174 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 175 | const char *file, int line) |
| 176 | { |
| 177 | struct vlr_subscr *vsub; |
| 178 | |
| 179 | if (!msisdn || !*msisdn) |
| 180 | return NULL; |
| 181 | |
| 182 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 183 | if (vlr_subscr_matches_msisdn(vsub, msisdn)) { |
| 184 | vlr_subscr_get_src(vsub, use, file, line); |
| 185 | return vsub; |
| 186 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 187 | } |
| 188 | return NULL; |
| 189 | } |
| 190 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 191 | /* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 192 | static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 193 | struct osmo_gsup_message *gsup_msg) |
| 194 | { |
| 195 | struct vlr_instance *vlr = vsub->vlr; |
| 196 | |
| 197 | if (strlen(gsup_msg->imsi) == 0) |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 198 | OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 199 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 200 | gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 201 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 202 | return gsup_client_mux_tx(vlr->gcm, gsup_msg); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 205 | static int vlr_subscr_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 206 | { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 207 | struct vlr_subscr *vsub = e->use_count->talloc_object; |
| 208 | char buf[128]; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 209 | int32_t total; |
| 210 | int level; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 211 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 212 | if (!e->use) |
| 213 | return -EINVAL; |
| 214 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 215 | total = osmo_use_count_total(&vsub->use_count); |
| 216 | |
| 217 | if (total == 0 |
| 218 | || (total == 1 && old_use_count == 0 && e->count == 1)) |
| 219 | level = LOGL_INFO; |
| 220 | else |
| 221 | level = LOGL_DEBUG; |
| 222 | |
| 223 | LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n", |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 224 | vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use, |
| 225 | osmo_use_count_name_buf(buf, sizeof(buf), e->use_count)); |
| 226 | |
| 227 | if (e->count < 0) |
| 228 | return -ERANGE; |
| 229 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 230 | vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total); |
| 231 | |
| 232 | if (total <= 0) |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 233 | vlr_subscr_free(vsub); |
| 234 | return 0; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* Allocate a new subscriber and insert it into list */ |
| 238 | static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr) |
| 239 | { |
| 240 | struct vlr_subscr *vsub; |
| 241 | int i; |
| 242 | |
| 243 | vsub = talloc_zero(vlr, struct vlr_subscr); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 244 | *vsub = (struct vlr_subscr){ |
| 245 | .vlr = vlr, |
| 246 | .tmsi = GSM_RESERVED_TMSI, |
| 247 | .tmsi_new = GSM_RESERVED_TMSI, |
| 248 | .use_count = (struct osmo_use_count){ |
| 249 | .talloc_object = vsub, |
| 250 | .use_cb = vlr_subscr_use_cb, |
| 251 | }, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 252 | .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 253 | }; |
| 254 | osmo_use_count_make_static_entries(&vsub->use_count, vsub->use_count_buf, ARRAY_SIZE(vsub->use_count_buf)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 255 | |
| 256 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
Neels Hofmeyr | 8b6e536 | 2018-11-30 02:57:33 +0100 | [diff] [blame] | 257 | vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 258 | |
| 259 | INIT_LLIST_HEAD(&vsub->cs.requests); |
| 260 | INIT_LLIST_HEAD(&vsub->ps.pdp_list); |
| 261 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 262 | /* Create an SGs FSM, which is needed to control CSFB, |
| 263 | * in cases where CSFB/SGs is not in use, this FSM will |
| 264 | * just do nothing. (see also: sgs_iface.c) */ |
| 265 | vlr_sgs_fsm_create(vsub); |
| 266 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 267 | llist_add_tail(&vsub->list, &vlr->subscribers); |
| 268 | return vsub; |
| 269 | } |
| 270 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 271 | /* Send a GSUP Purge MS request. |
| 272 | * TODO: this should be sent to the *previous* VLR when this VLR is "taking" |
| 273 | * this subscriber, not to the HLR? */ |
| 274 | int vlr_subscr_purge(struct vlr_subscr *vsub) |
| 275 | { |
| 276 | struct osmo_gsup_message gsup_msg = {0}; |
| 277 | |
| 278 | gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST; |
| 279 | |
| 280 | /* provide HLR number in case we know it */ |
| 281 | gsup_msg.hlr_enc_len = vsub->hlr.len; |
| 282 | gsup_msg.hlr_enc = vsub->hlr.buf; |
| 283 | |
| 284 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 285 | } |
| 286 | |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 287 | void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub, |
| 288 | enum osmo_fsm_term_cause fsm_cause, |
| 289 | uint8_t gsm48_cause) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 290 | { |
| 291 | if (!vsub) |
| 292 | return; |
| 293 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 294 | vlr_subscr_get(vsub, __func__); |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 295 | if (vsub->lu_fsm) |
| 296 | vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 297 | if (vsub->proc_arq_fsm) |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 298 | vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 299 | vlr_subscr_put(vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */ |
| 303 | void vlr_subscr_free(struct vlr_subscr *vsub) |
| 304 | { |
| 305 | llist_del(&vsub->list); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 306 | DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub), |
| 307 | vsub->max_total_use_count); |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 308 | |
| 309 | /* Make sure SGs timer Ts5 is removed */ |
| 310 | osmo_timer_del(&vsub->sgs.Ts5); |
| 311 | |
| 312 | /* Remove SGs FSM (see also: sgs_iface.c) */ |
| 313 | vlr_sgs_fsm_remove(vsub); |
| 314 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 315 | talloc_free(vsub); |
| 316 | } |
| 317 | |
| 318 | /* Generate a new TMSI and store in vsub->tmsi_new. |
| 319 | * Search all known subscribers to ensure that the TMSI is unique. */ |
| 320 | int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub) |
| 321 | { |
| 322 | struct vlr_instance *vlr = vsub->vlr; |
| 323 | uint32_t tmsi; |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 324 | int tried, rc; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 325 | struct vlr_subscr *other_vsub; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 326 | |
| 327 | for (tried = 0; tried < 100; tried++) { |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 328 | rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi)); |
| 329 | if (rc < 0) { |
| 330 | LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc)); |
| 331 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 332 | } |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 333 | |
| 334 | if (!llist_empty(&vlr->cfg.nri_ranges->entries)) { |
| 335 | int16_t nri_v; |
| 336 | osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen); |
| 337 | osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen); |
| 338 | LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n", |
| 339 | osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi); |
| 340 | } |
| 341 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 342 | /* throw the dice again, if the TSMI doesn't fit */ |
| 343 | if (tmsi == GSM_RESERVED_TMSI) |
| 344 | continue; |
| 345 | |
| 346 | /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */ |
| 347 | if (vlr->cfg.is_ps) { |
| 348 | /* SGSN */ |
Eric Wild | 58abc67 | 2019-06-14 16:11:08 +0200 | [diff] [blame] | 349 | tmsi |= GSM23003_TMSI_SGSN_MASK; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 350 | } else { |
| 351 | /* MSC */ |
Eric Wild | 58abc67 | 2019-06-14 16:11:08 +0200 | [diff] [blame] | 352 | if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK) |
| 353 | tmsi &= ~GSM23003_TMSI_SGSN_MASK; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* If this TMSI is already in use, try another one. */ |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 357 | if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) { |
| 358 | vlr_subscr_put(other_vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 359 | continue; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 360 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 361 | |
| 362 | vsub->tmsi_new = tmsi; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 363 | vsub->vlr->ops.subscr_update(vsub); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI" |
| 368 | " after %d tries\n", vlr_subscr_name(vsub), tried); |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | /* Find subscriber by IMSI, or create new subscriber if not found. |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 373 | * \param[in] vlr VLR instance. |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 374 | * \param[in] imsi IMSI string. |
| 375 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 376 | struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr, |
| 377 | const char *imsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 378 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 379 | bool *created, |
| 380 | const char *file, |
| 381 | int line) |
| 382 | { |
| 383 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 384 | vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 385 | if (vsub) { |
| 386 | if (created) |
| 387 | *created = false; |
| 388 | return vsub; |
| 389 | } |
| 390 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 391 | vsub = _vlr_subscr_alloc(vlr); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 392 | if (!vsub) |
| 393 | return NULL; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 394 | vlr_subscr_get_src(vsub, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 395 | vlr_subscr_set_imsi(vsub, imsi); |
| 396 | LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi); |
| 397 | if (created) |
| 398 | *created = true; |
| 399 | return vsub; |
| 400 | } |
| 401 | |
| 402 | /* Find subscriber by TMSI, or create new subscriber if not found. |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 403 | * \param[in] vlr VLR instance. |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 404 | * \param[in] tmsi TMSI. |
| 405 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 406 | struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr, |
| 407 | uint32_t tmsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 408 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 409 | bool *created, |
| 410 | const char *file, |
| 411 | int line) |
| 412 | { |
| 413 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 414 | vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 415 | if (vsub) { |
| 416 | if (created) |
| 417 | *created = false; |
| 418 | return vsub; |
| 419 | } |
| 420 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 421 | vsub = _vlr_subscr_alloc(vlr); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 422 | if (!vsub) |
| 423 | return NULL; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 424 | vlr_subscr_get_src(vsub, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 425 | vsub->tmsi = tmsi; |
| 426 | LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi); |
| 427 | if (created) |
| 428 | *created = true; |
| 429 | return vsub; |
| 430 | } |
| 431 | |
| 432 | void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 433 | { |
| 434 | if (!vsub) |
| 435 | return; |
Stefan Sperling | 9fbb600 | 2018-06-25 17:31:59 +0200 | [diff] [blame] | 436 | |
| 437 | if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) { |
| 438 | LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n", |
| 439 | imsi, vsub->imsi); |
| 440 | /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */ |
| 441 | } |
| 442 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 443 | vsub->id = atoll(vsub->imsi); |
| 444 | DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n", |
| 445 | vsub->imsi, vsub->id); |
| 446 | } |
| 447 | |
| 448 | void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei) |
| 449 | { |
| 450 | if (!vsub) |
| 451 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 452 | OSMO_STRLCPY_ARRAY(vsub->imei, imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 453 | DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n", |
| 454 | vsub->imsi, vsub->imei); |
| 455 | } |
| 456 | |
| 457 | void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv) |
| 458 | { |
| 459 | if (!vsub) |
| 460 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 461 | OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 462 | DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n", |
| 463 | vsub->imsi, vsub->imeisv); |
Oliver Smith | b8077b0 | 2019-05-07 14:13:55 +0200 | [diff] [blame] | 464 | |
| 465 | /* Copy IMEISV to IMEI (additional SV digits get cut off) */ |
| 466 | vlr_subscr_set_imei(vsub, imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Safely copy the given MSISDN string to vsub->msisdn */ |
| 470 | void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 471 | { |
| 472 | if (!vsub) |
| 473 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 474 | OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 475 | DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n", |
| 476 | vsub->imsi, vsub->msisdn); |
| 477 | } |
| 478 | |
| 479 | bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 480 | { |
| 481 | return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi); |
| 482 | } |
| 483 | |
| 484 | bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi) |
| 485 | { |
| 486 | return vsub && tmsi != GSM_RESERVED_TMSI |
| 487 | && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi); |
| 488 | } |
| 489 | |
| 490 | bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 491 | { |
| 492 | return vsub && msisdn && vsub->msisdn[0] |
| 493 | && !strcmp(vsub->msisdn, msisdn); |
| 494 | } |
| 495 | |
| 496 | bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei) |
| 497 | { |
| 498 | return vsub && imei && vsub->imei[0] |
| 499 | && !strcmp(vsub->imei, imei); |
| 500 | } |
| 501 | |
| 502 | /* Send updated subscriber information to HLR */ |
| 503 | int vlr_subscr_changed(struct vlr_subscr *vsub) |
| 504 | { |
| 505 | /* FIXME */ |
| 506 | LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__); |
| 507 | return 0; |
| 508 | } |
| 509 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 510 | void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub) |
| 511 | { |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 512 | struct timespec now; |
| 513 | |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 514 | /* Mark the subscriber as inactive if it stopped to do periodical location updates. */ |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 515 | if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 516 | vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212); |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 517 | } else { |
| 518 | LOGP(DVLR, LOGL_ERROR, |
| 519 | "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub)); |
| 520 | /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */ |
| 521 | vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void vlr_subscr_expire_lu(void *data) |
| 526 | { |
| 527 | struct vlr_instance *vlr = data; |
| 528 | struct vlr_subscr *vsub, *vsub_tmp; |
| 529 | struct timespec now; |
| 530 | |
Vadim Yanitskiy | 718f32f | 2019-06-19 03:13:40 +0700 | [diff] [blame] | 531 | /* Periodic location update might be disabled from the VTY, |
| 532 | * so we shall not expire subscribers until explicit IMSI Detach. */ |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 533 | if (!vlr_timer(vlr, 3212)) |
Vadim Yanitskiy | 718f32f | 2019-06-19 03:13:40 +0700 | [diff] [blame] | 534 | goto done; |
| 535 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 536 | if (llist_empty(&vlr->subscribers)) |
| 537 | goto done; |
| 538 | |
| 539 | if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) { |
| 540 | LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n"); |
| 541 | goto done; |
| 542 | } |
| 543 | |
| 544 | llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) { |
| 545 | if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec) |
| 546 | continue; |
| 547 | |
| 548 | LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub)); |
| 549 | vlr_subscr_rx_imsi_detach(vsub); |
| 550 | } |
| 551 | |
| 552 | done: |
| 553 | osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0); |
| 554 | } |
| 555 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 556 | /*********************************************************************** |
| 557 | * PDP context data |
| 558 | ***********************************************************************/ |
| 559 | |
Neels Hofmeyr | bac2276 | 2017-07-06 18:39:28 +0200 | [diff] [blame] | 560 | #define GSM_APN_LENGTH 102 |
| 561 | |
| 562 | /* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */ |
| 563 | /* see GSM 09.02, B.1, gprsSubscriptionData */ |
| 564 | struct sgsn_subscriber_pdp_data { |
| 565 | struct llist_head list; |
| 566 | |
| 567 | unsigned int context_id; |
| 568 | uint16_t pdp_type; |
| 569 | char apn_str[GSM_APN_LENGTH]; |
| 570 | uint8_t qos_subscribed[20]; |
| 571 | size_t qos_subscribed_len; |
| 572 | }; |
| 573 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 574 | struct sgsn_subscriber_pdp_data * |
| 575 | vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub) |
| 576 | { |
| 577 | struct sgsn_subscriber_pdp_data* pdata; |
| 578 | |
| 579 | pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data); |
| 580 | |
| 581 | llist_add_tail(&pdata->list, &vsub->ps.pdp_list); |
| 582 | |
| 583 | return pdata; |
| 584 | } |
| 585 | |
| 586 | static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub) |
| 587 | { |
| 588 | struct sgsn_subscriber_pdp_data *pdp, *pdp2; |
| 589 | int count = 0; |
| 590 | |
| 591 | llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) { |
| 592 | llist_del(&pdp->list); |
| 593 | talloc_free(pdp); |
| 594 | count += 1; |
| 595 | } |
| 596 | |
| 597 | return count; |
| 598 | } |
| 599 | |
| 600 | static struct sgsn_subscriber_pdp_data * |
| 601 | vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id) |
| 602 | { |
| 603 | struct sgsn_subscriber_pdp_data *pdp; |
| 604 | |
| 605 | llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) { |
| 606 | if (pdp->context_id == context_id) |
| 607 | return pdp; |
| 608 | } |
| 609 | |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | /*********************************************************************** |
| 614 | * Actual Implementation |
| 615 | ***********************************************************************/ |
| 616 | |
| 617 | static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 618 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 619 | { |
| 620 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 621 | LOGP(DVLR, LOGL_NOTICE, |
| 622 | "Unknown IMSI %s, discarding GSUP request " |
| 623 | "of type 0x%02x\n", |
| 624 | gsup_msg->imsi, gsup_msg->message_type); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 625 | gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 626 | } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 627 | LOGP(DVLR, LOGL_NOTICE, |
| 628 | "Unknown IMSI %s, discarding GSUP error " |
| 629 | "of type 0x%02x, cause '%s' (%d)\n", |
| 630 | gsup_msg->imsi, gsup_msg->message_type, |
| 631 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 632 | gsup_msg->cause); |
| 633 | } else { |
| 634 | LOGP(DVLR, LOGL_NOTICE, |
| 635 | "Unknown IMSI %s, discarding GSUP response " |
| 636 | "of type 0x%02x\n", |
| 637 | gsup_msg->imsi, gsup_msg->message_type); |
| 638 | } |
| 639 | |
| 640 | return -GMM_CAUSE_IMSI_UNKNOWN; |
| 641 | } |
| 642 | |
| 643 | static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 644 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 645 | { |
| 646 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 647 | LOGGSUPP(LOGL_NOTICE, gsup_msg, |
| 648 | "Purge MS has failed with cause '%s' (%d)\n", |
| 649 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 650 | gsup_msg->cause); |
| 651 | return -gsup_msg->cause; |
| 652 | } |
| 653 | LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n"); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* VLR internal call to request UpdateLocation from HLR */ |
Philipp Maier | 6038ad4 | 2018-11-13 13:55:09 +0100 | [diff] [blame] | 658 | int vlr_subscr_req_lu(struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 659 | { |
| 660 | struct osmo_gsup_message gsup_msg = {0}; |
| 661 | int rc; |
| 662 | |
| 663 | gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; |
Neels Hofmeyr | d0756b1 | 2018-09-28 02:41:39 +0200 | [diff] [blame] | 664 | gsup_msg.cn_domain = vsub->vlr->cfg.is_ps ? OSMO_GSUP_CN_DOMAIN_PS : OSMO_GSUP_CN_DOMAIN_CS; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 665 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 666 | |
| 667 | return rc; |
| 668 | } |
| 669 | |
| 670 | /* VLR internal call to request tuples from HLR */ |
| 671 | int vlr_subscr_req_sai(struct vlr_subscr *vsub, |
| 672 | const uint8_t *auts, const uint8_t *auts_rand) |
| 673 | { |
| 674 | struct osmo_gsup_message gsup_msg = {0}; |
| 675 | |
| 676 | gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; |
| 677 | gsup_msg.auts = auts; |
| 678 | gsup_msg.rand = auts_rand; |
Neels Hofmeyr | 63b2464 | 2019-12-12 01:31:04 +0100 | [diff] [blame] | 679 | gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 680 | |
| 681 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 682 | } |
| 683 | |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 684 | /* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */ |
| 685 | int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub) |
| 686 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 687 | struct osmo_gsup_message gsup_msg = { |
| 688 | .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT, |
Vadim Yanitskiy | ed73ae1 | 2019-08-15 22:51:20 +0200 | [diff] [blame] | 689 | .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 690 | }; |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 691 | uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */ |
| 692 | int len; |
| 693 | |
| 694 | /* Encode IMEI */ |
| 695 | len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei); |
| 696 | if (len < 1) { |
| 697 | LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei); |
| 698 | return -ENOSPC; |
| 699 | } |
| 700 | gsup_msg.imei_enc = imei_enc; |
| 701 | gsup_msg.imei_enc_len = len; |
| 702 | |
| 703 | /* Send CHECK_IMEI_REQUEST */ |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 704 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 705 | return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg); |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 708 | /* Tell HLR that authentication failure occurred */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 709 | int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 710 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 711 | struct osmo_gsup_message gsup_msg = { |
| 712 | .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT, |
Vadim Yanitskiy | ed73ae1 | 2019-08-15 22:51:20 +0200 | [diff] [blame] | 713 | .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 714 | }; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 715 | |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 716 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 717 | return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* Update the subscriber with GSUP-received auth tuples */ |
| 721 | void vlr_subscr_update_tuples(struct vlr_subscr *vsub, |
| 722 | const struct osmo_gsup_message *gsup) |
| 723 | { |
| 724 | unsigned int i; |
| 725 | unsigned int got_tuples; |
| 726 | |
| 727 | if (gsup->num_auth_vectors) { |
| 728 | memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples)); |
| 729 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
Neels Hofmeyr | 8b6e536 | 2018-11-30 02:57:33 +0100 | [diff] [blame] | 730 | vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | got_tuples = 0; |
| 734 | for (i = 0; i < gsup->num_auth_vectors; i++) { |
| 735 | size_t key_seq = i; |
| 736 | |
| 737 | if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) { |
| 738 | LOGVSUBP(LOGL_NOTICE, vsub, |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 739 | "Skipping auth tuple with invalid cksn %zu\n", |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 740 | key_seq); |
| 741 | continue; |
| 742 | } |
| 743 | vsub->auth_tuples[i].vec = gsup->auth_vectors[i]; |
| 744 | vsub->auth_tuples[i].key_seq = key_seq; |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 745 | got_tuples++; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples); |
| 749 | |
| 750 | if (!got_tuples) { |
| 751 | /* FIXME what now? */ |
| 752 | // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ? |
| 753 | } |
| 754 | |
| 755 | /* New tuples means last_tuple becomes invalid */ |
| 756 | vsub->last_tuple = NULL; |
| 757 | } |
| 758 | |
| 759 | /* Handle SendAuthInfo Result/Error from HLR */ |
| 760 | static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub, |
| 761 | const struct osmo_gsup_message *gsup) |
| 762 | { |
| 763 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 764 | void *data = (void *) gsup; |
| 765 | |
Neels Hofmeyr | 1bfe0e1 | 2019-09-16 18:07:54 +0200 | [diff] [blame] | 766 | if (!auth_fi) { |
| 767 | LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n", |
| 768 | osmo_gsup_message_type_name(gsup->message_type)); |
| 769 | return -1; |
| 770 | } |
| 771 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 772 | switch (gsup->message_type) { |
| 773 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 774 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data); |
| 775 | break; |
| 776 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
| 777 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data); |
| 778 | break; |
| 779 | default: |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 786 | static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub, |
| 787 | const struct osmo_gsup_message *gsup_msg) |
| 788 | { |
| 789 | unsigned idx; |
| 790 | int rc; |
| 791 | |
Neels Hofmeyr | 02dd265 | 2019-11-13 07:05:11 +0100 | [diff] [blame] | 792 | if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()? |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 793 | gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn), |
| 794 | gsup_msg->msisdn_enc, |
| 795 | gsup_msg->msisdn_enc_len, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 796 | LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n", |
| 797 | vsub->imsi, vsub->msisdn); |
| 798 | } |
| 799 | |
| 800 | if (gsup_msg->hlr_enc) { |
| 801 | if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) { |
| 802 | LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n", |
| 803 | gsup_msg->hlr_enc_len); |
| 804 | vsub->hlr.len = 0; |
| 805 | } else { |
| 806 | memcpy(vsub->hlr.buf, gsup_msg->hlr_enc, |
| 807 | gsup_msg->hlr_enc_len); |
| 808 | vsub->hlr.len = gsup_msg->hlr_enc_len; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | if (gsup_msg->pdp_info_compl) { |
| 813 | rc = vlr_subscr_pdp_data_clear(vsub); |
| 814 | if (rc > 0) |
| 815 | LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n"); |
| 816 | } |
| 817 | |
| 818 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
| 819 | const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx]; |
| 820 | size_t ctx_id = pdp_info->context_id; |
| 821 | struct sgsn_subscriber_pdp_data *pdp_data; |
| 822 | |
| 823 | if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) { |
| 824 | LOGVSUBP(LOGL_ERROR, vsub, |
| 825 | "APN too long, context id = %zu, APN = %s\n", |
| 826 | ctx_id, osmo_hexdump(pdp_info->apn_enc, |
| 827 | pdp_info->apn_enc_len)); |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) { |
| 832 | LOGVSUBP(LOGL_ERROR, vsub, |
| 833 | "QoS info too long (%zu)\n", |
| 834 | pdp_info->qos_enc_len); |
| 835 | continue; |
| 836 | } |
| 837 | |
| 838 | LOGVSUBP(LOGL_INFO, vsub, |
| 839 | "Will set PDP info, context id = %zu, APN = %s\n", |
| 840 | ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len)); |
| 841 | |
| 842 | /* Set PDP info [ctx_id] */ |
| 843 | pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id); |
| 844 | if (!pdp_data) { |
| 845 | pdp_data = vlr_subscr_pdp_data_alloc(vsub); |
| 846 | pdp_data->context_id = ctx_id; |
| 847 | } |
| 848 | |
| 849 | OSMO_ASSERT(pdp_data != NULL); |
| 850 | pdp_data->pdp_type = pdp_info->pdp_type; |
| 851 | osmo_apn_to_str(pdp_data->apn_str, |
| 852 | pdp_info->apn_enc, pdp_info->apn_enc_len); |
| 853 | memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len); |
| 854 | pdp_data->qos_subscribed_len = pdp_info->qos_enc_len; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | |
| 859 | /* Handle InsertSubscrData Result from HLR */ |
| 860 | static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub, |
| 861 | const struct osmo_gsup_message *gsup) |
| 862 | { |
| 863 | struct osmo_gsup_message gsup_reply = {0}; |
| 864 | |
| 865 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 866 | vsub->vlr->ops.subscr_update(vsub); |
| 867 | |
| 868 | gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; |
| 869 | return vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
| 870 | } |
| 871 | |
| 872 | /* Handle UpdateLocation Result from HLR */ |
| 873 | static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub, |
| 874 | const struct osmo_gsup_message *gsup) |
| 875 | { |
Philipp Maier | 483cea8 | 2019-04-03 16:23:29 +0200 | [diff] [blame] | 876 | struct sgs_lu_response sgs_lu_response = {0}; |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 877 | bool sgs_lu_in_progress = false; |
| 878 | |
| 879 | if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES) |
| 880 | sgs_lu_in_progress = true; |
| 881 | |
| 882 | if (!vsub->lu_fsm && !sgs_lu_in_progress) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 883 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result " |
| 884 | "without LU in progress\n"); |
| 885 | return -ENODEV; |
| 886 | } |
| 887 | |
| 888 | /* contrary to MAP, we allow piggy-backing subscriber data onto the |
| 889 | * UPDATE LOCATION RESULT, and don't mandate the use of a separate |
| 890 | * nested INSERT SUBSCRIBER DATA transaction */ |
| 891 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 892 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 893 | if (sgs_lu_in_progress) { |
| 894 | sgs_lu_response.accepted = true; |
| 895 | sgs_lu_response.vsub = vsub; |
| 896 | vsub->sgs.response_cb(&sgs_lu_response); |
| 897 | } else |
| 898 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | /* Handle UpdateLocation Result from HLR */ |
| 904 | static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub, |
| 905 | const struct osmo_gsup_message *gsup) |
| 906 | { |
Philipp Maier | 483cea8 | 2019-04-03 16:23:29 +0200 | [diff] [blame] | 907 | struct sgs_lu_response sgs_lu_response = {0}; |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 908 | bool sgs_lu_in_progress = false; |
| 909 | |
| 910 | if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES) |
| 911 | sgs_lu_in_progress = true; |
| 912 | |
| 913 | if (!vsub->lu_fsm && !sgs_lu_in_progress) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 914 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error " |
| 915 | "without LU in progress\n"); |
| 916 | return -ENODEV; |
| 917 | } |
| 918 | |
| 919 | LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n", |
| 920 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 921 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 922 | if (sgs_lu_in_progress) { |
| 923 | sgs_lu_response.accepted = false; |
| 924 | sgs_lu_response.vsub = vsub; |
| 925 | vsub->sgs.response_cb(&sgs_lu_response); |
| 926 | } else |
| 927 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, |
| 928 | (void *)&gsup->cause); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
Alexander Couzens | 7312b15 | 2019-08-19 15:30:12 +0200 | [diff] [blame] | 932 | void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause, |
| 933 | enum gsm48_reject_value *gsm48_rej_p) |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 934 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 935 | enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE; |
| 936 | switch (gmm_cause) { |
| 937 | case GMM_CAUSE_IMSI_UNKNOWN: |
| 938 | gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR; |
| 939 | break; |
| 940 | case GMM_CAUSE_ILLEGAL_MS: |
| 941 | gsm48_rej = GSM48_REJECT_ILLEGAL_MS; |
| 942 | break; |
| 943 | case GMM_CAUSE_IMEI_NOT_ACCEPTED: |
| 944 | gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED; |
| 945 | break; |
| 946 | case GMM_CAUSE_ILLEGAL_ME: |
| 947 | gsm48_rej = GSM48_REJECT_ILLEGAL_ME; |
| 948 | break; |
| 949 | case GMM_CAUSE_GPRS_NOTALLOWED: |
| 950 | gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED; |
| 951 | break; |
| 952 | case GMM_CAUSE_GPRS_OTHER_NOTALLOWED: |
| 953 | gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED; |
| 954 | break; |
| 955 | case GMM_CAUSE_MS_ID_NOT_DERIVED: |
| 956 | gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE; |
| 957 | break; |
| 958 | case GMM_CAUSE_IMPL_DETACHED: |
| 959 | gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED; |
| 960 | break; |
| 961 | case GMM_CAUSE_PLMN_NOTALLOWED: |
| 962 | gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 963 | break; |
| 964 | case GMM_CAUSE_LA_NOTALLOWED: |
| 965 | gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED; |
| 966 | break; |
| 967 | case GMM_CAUSE_ROAMING_NOTALLOWED: |
| 968 | gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED; |
| 969 | break; |
| 970 | case GMM_CAUSE_NO_GPRS_PLMN: |
| 971 | gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN; |
| 972 | break; |
| 973 | case GMM_CAUSE_MSC_TEMP_NOTREACH: |
| 974 | gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE; |
| 975 | break; |
| 976 | case GMM_CAUSE_SYNC_FAIL: |
| 977 | gsm48_rej = GSM48_REJECT_SYNCH_FAILURE; |
| 978 | break; |
| 979 | case GMM_CAUSE_CONGESTION: |
| 980 | gsm48_rej = GSM48_REJECT_CONGESTION; |
| 981 | break; |
| 982 | case GMM_CAUSE_SEM_INCORR_MSG: |
| 983 | gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE; |
| 984 | break; |
| 985 | case GMM_CAUSE_INV_MAND_INFO: |
| 986 | gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF; |
| 987 | break; |
| 988 | case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL: |
| 989 | gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED; |
| 990 | break; |
| 991 | case GMM_CAUSE_MSGT_INCOMP_P_STATE: |
| 992 | gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE; |
| 993 | break; |
| 994 | case GMM_CAUSE_IE_NOTEXIST_NOTIMPL: |
| 995 | gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED; |
| 996 | break; |
| 997 | case GMM_CAUSE_COND_IE_ERR: |
| 998 | gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR; |
| 999 | break; |
| 1000 | case GMM_CAUSE_MSG_INCOMP_P_STATE: |
| 1001 | gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE; |
| 1002 | break; |
| 1003 | case GMM_CAUSE_PROTO_ERR_UNSPEC: |
| 1004 | gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR; |
| 1005 | break; |
| 1006 | |
| 1007 | case GMM_CAUSE_NO_SUIT_CELL_IN_LA: |
| 1008 | case GMM_CAUSE_MAC_FAIL: |
| 1009 | case GMM_CAUSE_GSM_AUTH_UNACCEPT: |
| 1010 | case GMM_CAUSE_NOT_AUTH_FOR_CSG: |
| 1011 | case GMM_CAUSE_SMS_VIA_GPRS_IN_RA: |
| 1012 | case GMM_CAUSE_NO_PDP_ACTIVATED: |
| 1013 | case GMM_CAUSE_NET_FAIL: |
| 1014 | gsm48_rej = GSM48_REJECT_NETWORK_FAILURE; |
| 1015 | break; |
| 1016 | } |
Alexander Couzens | eb1b03a | 2019-08-19 15:22:25 +0200 | [diff] [blame] | 1017 | |
| 1018 | *gsm48_rej_p = gsm48_rej; |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1021 | /* Handle LOCATION CANCEL request from HLR */ |
| 1022 | static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1023 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1024 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1025 | enum gsm48_reject_value gsm48_rej; |
Alexander Couzens | eb1b03a | 2019-08-19 15:22:25 +0200 | [diff] [blame] | 1026 | enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1027 | struct osmo_gsup_message gsup_reply = {0}; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1028 | int rc, is_update_procedure = !gsup_msg->cancel_type || |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1029 | gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE; |
| 1030 | |
| 1031 | LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n", |
| 1032 | is_update_procedure ? |
| 1033 | "update procedure" : "subscription withdraw"); |
| 1034 | |
| 1035 | gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1036 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1037 | |
Alexander Couzens | 7312b15 | 2019-08-19 15:30:12 +0200 | [diff] [blame] | 1038 | vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej); |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1039 | vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1040 | |
Stefan Sperling | ad797ce | 2018-12-10 18:33:30 +0100 | [diff] [blame] | 1041 | vlr_subscr_rx_imsi_detach(vsub); |
| 1042 | |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1043 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1046 | /* Handle Check_IMEI_VLR result and error from HLR */ |
| 1047 | static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup) |
| 1048 | { |
| 1049 | if (!vsub->lu_fsm) { |
| 1050 | LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n", |
| 1051 | osmo_gsup_message_type_name(gsup->message_type)); |
| 1052 | return -ENODEV; |
| 1053 | } |
| 1054 | |
Oliver Smith | cbf2c93 | 2019-05-06 13:09:55 +0200 | [diff] [blame] | 1055 | /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch |
| 1056 | * it further to lu_compl_vlr_fsm (Check IMEI after LU). */ |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1057 | if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) { |
| 1058 | if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK) |
| 1059 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL); |
| 1060 | else |
| 1061 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL); |
| 1062 | } else { |
| 1063 | LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n", |
| 1064 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 1065 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL); |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1071 | /* Incoming handler for GSUP from HLR. |
| 1072 | * Keep this function non-static for direct invocation by unit tests. */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1073 | int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1074 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1075 | struct vlr_instance *vlr = data; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1076 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 27c8b03 | 2019-12-23 19:12:54 +0100 | [diff] [blame] | 1077 | int rc = 0; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1078 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1079 | vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1080 | if (!vsub) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1081 | switch (gsup->message_type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1082 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 1083 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1084 | return vlr_rx_gsup_purge_no_subscr(vlr, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1085 | default: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1086 | return vlr_rx_gsup_unknown_imsi(vlr, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1090 | switch (gsup->message_type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1091 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 1092 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1093 | rc = vlr_subscr_handle_sai_res(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1094 | break; |
| 1095 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1096 | rc = vlr_subscr_handle_isd_req(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1097 | break; |
| 1098 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1099 | rc = vlr_subscr_handle_cancel_req(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1100 | break; |
| 1101 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1102 | rc = vlr_subscr_handle_lu_res(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1103 | break; |
| 1104 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1105 | rc = vlr_subscr_handle_lu_err(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
| 1108 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 1109 | case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST: |
| 1110 | LOGVSUBP(LOGL_ERROR, vsub, |
| 1111 | "Rx GSUP msg_type=%d not yet implemented\n", |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1112 | gsup->message_type); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1113 | rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
| 1114 | break; |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1115 | case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR: |
| 1116 | case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1117 | rc = vlr_subscr_handle_check_imei(vsub, gsup); |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1118 | break; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1119 | default: |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1120 | LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type); |
| 1121 | rc = -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1125 | vlr_subscr_put(vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1126 | return rc; |
| 1127 | } |
| 1128 | |
| 1129 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1130 | int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1131 | { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1132 | /* update the vlr_subscr with the given identity */ |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1133 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1134 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1135 | if (vsub->imsi[0] |
| 1136 | && !vlr_subscr_matches_imsi(vsub, mi->imsi)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1137 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1138 | " %s\n", mi->imsi); |
Stefan Sperling | 9fbb600 | 2018-06-25 17:31:59 +0200 | [diff] [blame] | 1139 | /* XXX Should we return an error, e.g. -EINVAL ? */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1140 | } else |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1141 | vlr_subscr_set_imsi(vsub, mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1142 | break; |
| 1143 | case GSM_MI_TYPE_IMEI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1144 | vlr_subscr_set_imei(vsub, mi->imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1145 | break; |
| 1146 | case GSM_MI_TYPE_IMEISV: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1147 | vlr_subscr_set_imeisv(vsub, mi->imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1148 | break; |
Neels Hofmeyr | a40adf7 | 2020-06-02 22:02:01 +0200 | [diff] [blame] | 1149 | default: |
| 1150 | return -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (vsub->auth_fsm) { |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1154 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1155 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1156 | return osmo_fsm_inst_dispatch(vsub->auth_fsm, |
| 1157 | VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1158 | break; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (vsub->lu_fsm) { |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1163 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1164 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1165 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1166 | case GSM_MI_TYPE_IMEI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1167 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1168 | case GSM_MI_TYPE_IMEISV: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1169 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1170 | default: |
Neels Hofmeyr | a40adf7 | 2020-06-02 22:02:01 +0200 | [diff] [blame] | 1171 | return -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1172 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
| 1179 | int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub) |
| 1180 | { |
| 1181 | if (vsub->lu_fsm) { |
| 1182 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, |
| 1183 | VLR_ULA_E_NEW_TMSI_ACK, NULL); |
| 1184 | } else if (vsub->proc_arq_fsm) { |
| 1185 | return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, |
| 1186 | PR_ARQ_E_TMSI_ACK, NULL); |
| 1187 | } else { |
| 1188 | LOGVSUBP(LOGL_NOTICE, vsub, |
Neels Hofmeyr | 5b1e030 | 2019-05-06 23:45:09 +0200 | [diff] [blame] | 1189 | "gratuitous TMSI REALLOC COMPL\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1190 | return -EINVAL; |
| 1191 | } |
| 1192 | } |
| 1193 | |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1194 | bool vlr_subscr_expire(struct vlr_subscr *vsub) |
| 1195 | { |
| 1196 | if (vsub->lu_complete) { |
Neels Hofmeyr | 5c8b144 | 2018-12-11 12:43:10 +0100 | [diff] [blame] | 1197 | /* balancing the get from vlr_lu_compl_fsm_success() */ |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1198 | vsub->lu_complete = false; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1199 | vlr_subscr_put(vsub, VSUB_USE_ATTACHED); |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1200 | |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | return false; |
| 1205 | } |
| 1206 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1207 | /* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1208 | int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub) |
| 1209 | { |
| 1210 | /* paranoia: should any LU or PARQ FSMs still be running, stop them. */ |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1211 | vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1212 | |
| 1213 | vsub->imsi_detached_flag = true; |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1214 | vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION; |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1215 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1216 | /* Inform the UE-SGs FSM that the subscriber has been detached */ |
| 1217 | osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL); |
| 1218 | |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1219 | vlr_subscr_expire(vsub); |
| 1220 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /* Tear down any running FSMs due to MSC connection timeout. |
| 1225 | * Visit all vsub->*_fsm pointers and give them a queue to send a final reject |
| 1226 | * message before the entire connection is torn down. |
| 1227 | * \param[in] vsub subscriber to tear down |
| 1228 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 1229 | void vlr_ran_conn_timeout(struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1230 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1231 | vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops) |
| 1235 | { |
| 1236 | struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance); |
| 1237 | OSMO_ASSERT(vlr); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1238 | |
| 1239 | /* Some of these are needed only on UTRAN, but in case the caller wants |
| 1240 | * only GERAN, she should just provide dummy callbacks. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1241 | OSMO_ASSERT(ops->tx_auth_req); |
| 1242 | OSMO_ASSERT(ops->tx_auth_rej); |
| 1243 | OSMO_ASSERT(ops->tx_id_req); |
| 1244 | OSMO_ASSERT(ops->tx_lu_acc); |
| 1245 | OSMO_ASSERT(ops->tx_lu_rej); |
| 1246 | OSMO_ASSERT(ops->tx_cm_serv_acc); |
| 1247 | OSMO_ASSERT(ops->tx_cm_serv_rej); |
| 1248 | OSMO_ASSERT(ops->set_ciph_mode); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1249 | OSMO_ASSERT(ops->tx_common_id); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1250 | OSMO_ASSERT(ops->subscr_update); |
| 1251 | OSMO_ASSERT(ops->subscr_assoc); |
| 1252 | |
| 1253 | INIT_LLIST_HEAD(&vlr->subscribers); |
| 1254 | INIT_LLIST_HEAD(&vlr->operations); |
| 1255 | memcpy(&vlr->ops, ops, sizeof(vlr->ops)); |
| 1256 | |
Neels Hofmeyr | 0b8dec7 | 2017-10-29 01:57:35 +0200 | [diff] [blame] | 1257 | /* defaults */ |
| 1258 | vlr->cfg.assign_tmsi = true; |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 1259 | vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT; |
| 1260 | vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr); |
Neels Hofmeyr | 0b8dec7 | 2017-10-29 01:57:35 +0200 | [diff] [blame] | 1261 | |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 1262 | /* reset shared timer definitions */ |
| 1263 | osmo_tdefs_reset(msc_tdefs_vlr); |
| 1264 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1265 | /* osmo_auth_fsm.c */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1266 | OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1267 | /* osmo_lu_fsm.c */ |
| 1268 | vlr_lu_fsm_init(); |
| 1269 | /* vlr_access_request_fsm.c */ |
| 1270 | vlr_parq_fsm_init(); |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1271 | /* vlr_sgs_fsm.c */ |
| 1272 | vlr_sgs_fsm_init(); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1273 | |
| 1274 | return vlr; |
| 1275 | } |
| 1276 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1277 | int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1278 | { |
| 1279 | OSMO_ASSERT(vlr); |
| 1280 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1281 | vlr->gcm = gcm; |
| 1282 | gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){ |
| 1283 | .func = vlr_gsup_rx, |
| 1284 | .data = vlr, |
| 1285 | }; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1286 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1287 | osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr); |
| 1288 | osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* MSC->VLR: Subscribre has disconnected */ |
| 1293 | int vlr_subscr_disconnected(struct vlr_subscr *vsub) |
| 1294 | { |
| 1295 | /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B |
| 1296 | * interface */ |
| 1297 | osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1298 | osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1299 | vsub->msc_conn_ref = NULL; |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | /* MSC->VLR: Receive Authentication Failure from Subscriber */ |
| 1305 | int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts) |
| 1306 | { |
| 1307 | struct vlr_auth_resp_par par = {0}; |
| 1308 | par.auts = auts; |
| 1309 | |
| 1310 | osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | /* MSC->VLR: Receive Authentication Response from MS |
| 1315 | * \returns 1 in case of success, 0 in case of delay, -1 on auth error */ |
| 1316 | int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99, |
| 1317 | bool is_utran, const uint8_t *res, uint8_t res_len) |
| 1318 | { |
| 1319 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 1320 | struct vlr_auth_resp_par par; |
| 1321 | |
| 1322 | par.is_r99 = is_r99; |
| 1323 | par.is_utran = is_utran; |
| 1324 | par.res = res; |
| 1325 | par.res_len = res_len; |
| 1326 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par); |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* MSC->VLR: Receive result of Ciphering Mode Command from MS */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1332 | void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1333 | { |
| 1334 | if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1335 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1336 | if (vsub->proc_arq_fsm |
| 1337 | && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1338 | osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* Internal evaluation of requested ciphering mode. |
| 1342 | * Send set_ciph_mode() to MSC depending on the ciph_mode argument. |
| 1343 | * \param[in] vlr VLR instance. |
| 1344 | * \param[in] fi Calling FSM instance, for logging. |
| 1345 | * \param[in] msc_conn_ref MSC conn to send to. |
| 1346 | * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering. |
| 1347 | * \returns 0 if no ciphering is needed or message was sent successfully, |
| 1348 | * or a negative value if ciph_mode is invalid or sending failed. |
| 1349 | */ |
| 1350 | int vlr_set_ciph_mode(struct vlr_instance *vlr, |
| 1351 | struct osmo_fsm_inst *fi, |
| 1352 | void *msc_conn_ref, |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1353 | bool ciph_required, |
Neels Hofmeyr | 2ef2da5 | 2017-12-18 01:23:42 +0100 | [diff] [blame] | 1354 | bool umts_aka, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1355 | bool retrieve_imeisv) |
| 1356 | { |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1357 | if (!ciph_required) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1358 | return 0; |
| 1359 | |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1360 | LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n"); |
| 1361 | return vlr->ops.set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
Neels Hofmeyr | e3d72d7 | 2017-12-18 02:06:44 +0100 | [diff] [blame] | 1364 | /* Decide whether UMTS AKA should be used. |
| 1365 | * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA |
| 1366 | * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and |
| 1367 | * GERAN are R99 capable and UMTS AKA tokens are available. |
| 1368 | * \param[in] vec Auth tokens (received from the HLR). |
| 1369 | * \param[in] is_r99 True when BTS and GERAN are R99 capable. |
| 1370 | * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA. |
| 1371 | */ |
| 1372 | bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99) |
| 1373 | { |
| 1374 | if (!is_r99) |
| 1375 | return false; |
| 1376 | if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) |
| 1377 | return false; |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1381 | void log_set_filter_vlr_subscr(struct log_target *target, |
| 1382 | struct vlr_subscr *vlr_subscr) |
| 1383 | { |
| 1384 | struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR]; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1385 | const char *use = "logfilter"; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1386 | |
| 1387 | /* free the old data */ |
| 1388 | if (*fsub) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1389 | vlr_subscr_put(*fsub, use); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1390 | *fsub = NULL; |
| 1391 | } |
| 1392 | |
| 1393 | if (vlr_subscr) { |
| 1394 | target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1395 | vlr_subscr_get(vlr_subscr, use); |
| 1396 | *fsub = vlr_subscr; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1397 | } else |
| 1398 | target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR); |
| 1399 | } |