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