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