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