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