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> |
| 25 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
| 26 | #include <osmocom/gsm/gsup.h> |
| 27 | #include <osmocom/gsm/apn.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/gsm48.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 29 | #include <osmocom/msc/gsm_subscriber.h> |
| 30 | #include <osmocom/msc/gsup_client.h> |
| 31 | #include <osmocom/msc/vlr.h> |
| 32 | #include <osmocom/msc/debug.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 33 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 34 | #include <netinet/in.h> |
| 35 | #include <arpa/inet.h> |
| 36 | #include <limits.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 37 | #include <errno.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 38 | |
| 39 | #include "vlr_core.h" |
| 40 | #include "vlr_auth_fsm.h" |
| 41 | #include "vlr_lu_fsm.h" |
| 42 | #include "vlr_access_req_fsm.h" |
| 43 | |
| 44 | #define SGSN_SUBSCR_MAX_RETRIES 3 |
| 45 | #define SGSN_SUBSCR_RETRY_INTERVAL 10 |
| 46 | |
| 47 | /*********************************************************************** |
| 48 | * Convenience functions |
| 49 | ***********************************************************************/ |
| 50 | |
| 51 | const struct value_string vlr_ciph_names[] = { |
| 52 | OSMO_VALUE_STRING(VLR_CIPH_NONE), |
| 53 | OSMO_VALUE_STRING(VLR_CIPH_A5_1), |
| 54 | OSMO_VALUE_STRING(VLR_CIPH_A5_2), |
| 55 | OSMO_VALUE_STRING(VLR_CIPH_A5_3), |
| 56 | { 0, NULL } |
| 57 | }; |
| 58 | |
| 59 | uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer) |
| 60 | { |
| 61 | uint32_t tidx = 0xffffffff; |
| 62 | |
| 63 | switch (timer) { |
| 64 | case 3270: |
| 65 | tidx = VLR_T_3270; |
| 66 | break; |
| 67 | case 3260: |
| 68 | tidx = VLR_T_3260; |
| 69 | break; |
| 70 | case 3250: |
| 71 | tidx = VLR_T_3250; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | OSMO_ASSERT(tidx < sizeof(vlr->cfg.timer)); |
| 76 | return vlr->cfg.timer[tidx]; |
| 77 | } |
| 78 | |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 79 | /* return static buffer with printable name of VLR subscriber */ |
| 80 | const char *vlr_subscr_name(struct vlr_subscr *vsub) |
| 81 | { |
| 82 | static char buf[32]; |
| 83 | if (!vsub) |
| 84 | return "unknown"; |
| 85 | if (vsub->msisdn[0]) |
| 86 | snprintf(buf, sizeof(buf), "MSISDN:%s", vsub->msisdn); |
| 87 | else if (vsub->imsi[0]) |
| 88 | snprintf(buf, sizeof(buf), "IMSI:%s", vsub->imsi); |
| 89 | else if (vsub->tmsi != GSM_RESERVED_TMSI) |
| 90 | snprintf(buf, sizeof(buf), "TMSI:0x%08x", vsub->tmsi); |
| 91 | else if (vsub->tmsi_new != GSM_RESERVED_TMSI) |
| 92 | snprintf(buf, sizeof(buf), "TMSI(new):0x%08x", vsub->tmsi_new); |
| 93 | else |
| 94 | return "unknown"; |
| 95 | buf[sizeof(buf)-1] = '\0'; |
| 96 | return buf; |
| 97 | } |
| 98 | |
| 99 | const char *vlr_subscr_msisdn_or_name(struct vlr_subscr *vsub) |
| 100 | { |
| 101 | if (!vsub || !vsub->msisdn[0]) |
| 102 | return vlr_subscr_name(vsub); |
| 103 | return vsub->msisdn; |
| 104 | } |
| 105 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 106 | struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr, |
| 107 | const char *imsi, |
| 108 | const char *file, int line) |
| 109 | { |
| 110 | struct vlr_subscr *vsub; |
| 111 | |
| 112 | if (!imsi || !*imsi) |
| 113 | return NULL; |
| 114 | |
| 115 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
| 116 | if (vlr_subscr_matches_imsi(vsub, imsi)) |
| 117 | return _vlr_subscr_get(vsub, file, line); |
| 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr, |
| 123 | uint32_t tmsi, |
| 124 | const char *file, int line) |
| 125 | { |
| 126 | struct vlr_subscr *vsub; |
| 127 | |
| 128 | if (tmsi == GSM_RESERVED_TMSI) |
| 129 | return NULL; |
| 130 | |
| 131 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
| 132 | if (vlr_subscr_matches_tmsi(vsub, tmsi)) |
| 133 | return _vlr_subscr_get(vsub, file, line); |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr, |
| 139 | const char *msisdn, |
| 140 | const char *file, int line) |
| 141 | { |
| 142 | struct vlr_subscr *vsub; |
| 143 | |
| 144 | if (!msisdn || !*msisdn) |
| 145 | return NULL; |
| 146 | |
| 147 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
| 148 | if (vlr_subscr_matches_msisdn(vsub, msisdn)) |
| 149 | return _vlr_subscr_get(vsub, file, line); |
| 150 | } |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /* Transmit GSUP message to HLR */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 155 | static int vlr_tx_gsup_message(const struct vlr_instance *vlr, |
| 156 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 157 | { |
| 158 | struct msgb *msg = gsup_client_msgb_alloc(); |
| 159 | |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 160 | int rc = osmo_gsup_encode(msg, gsup_msg); |
| 161 | if (rc < 0) { |
| 162 | LOGP(DVLR, LOGL_ERROR, "GSUP encoding failure: %s\n", strerror(-rc)); |
| 163 | return rc; |
| 164 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 165 | |
| 166 | if (!vlr->gsup_client) { |
| 167 | LOGP(DVLR, LOGL_NOTICE, "GSUP link is down, cannot " |
| 168 | "send GSUP: %s\n", msgb_hexdump(msg)); |
| 169 | msgb_free(msg); |
| 170 | return -ENOTSUP; |
| 171 | } |
| 172 | |
| 173 | LOGP(DVLR, LOGL_DEBUG, "GSUP tx: %s\n", |
| 174 | osmo_hexdump_nospc(msg->data, msg->len)); |
| 175 | |
| 176 | return gsup_client_send(vlr->gsup_client, msg); |
| 177 | } |
| 178 | |
| 179 | /* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 180 | static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 181 | struct osmo_gsup_message *gsup_msg) |
| 182 | { |
| 183 | struct vlr_instance *vlr = vsub->vlr; |
| 184 | |
| 185 | if (strlen(gsup_msg->imsi) == 0) |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 186 | OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 187 | |
| 188 | return vlr_tx_gsup_message(vlr, gsup_msg); |
| 189 | } |
| 190 | |
| 191 | /* Transmit GSUP error in response to original message */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 192 | static int vlr_tx_gsup_error_reply(const struct vlr_instance *vlr, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 193 | struct osmo_gsup_message *gsup_orig, |
| 194 | enum gsm48_gmm_cause cause) |
| 195 | { |
| 196 | struct osmo_gsup_message gsup_reply = {0}; |
| 197 | |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 198 | OSMO_STRLCPY_ARRAY(gsup_reply.imsi, gsup_orig->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 199 | gsup_reply.cause = cause; |
| 200 | gsup_reply.message_type = |
| 201 | OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type); |
| 202 | |
| 203 | return vlr_tx_gsup_message(vlr, &gsup_reply); |
| 204 | } |
| 205 | |
| 206 | struct vlr_subscr *_vlr_subscr_get(struct vlr_subscr *sub, const char *file, int line) |
| 207 | { |
| 208 | if (!sub) |
| 209 | return NULL; |
| 210 | OSMO_ASSERT(sub->use_count < INT_MAX); |
| 211 | sub->use_count++; |
| 212 | LOGPSRC(DREF, LOGL_DEBUG, file, line, |
| 213 | "VLR subscr %s usage increases to: %d\n", |
| 214 | vlr_subscr_name(sub), sub->use_count); |
| 215 | return sub; |
| 216 | } |
| 217 | |
| 218 | struct vlr_subscr *_vlr_subscr_put(struct vlr_subscr *sub, const char *file, int line) |
| 219 | { |
| 220 | if (!sub) |
| 221 | return NULL; |
| 222 | sub->use_count--; |
| 223 | LOGPSRC(DREF, sub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR, |
| 224 | file, line, |
| 225 | "VLR subscr %s usage decreases to: %d\n", |
| 226 | vlr_subscr_name(sub), sub->use_count); |
| 227 | if (sub->use_count <= 0) |
| 228 | vlr_subscr_free(sub); |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | /* Allocate a new subscriber and insert it into list */ |
| 233 | static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr) |
| 234 | { |
| 235 | struct vlr_subscr *vsub; |
| 236 | int i; |
| 237 | |
| 238 | vsub = talloc_zero(vlr, struct vlr_subscr); |
| 239 | vsub->vlr = vlr; |
| 240 | vsub->tmsi = GSM_RESERVED_TMSI; |
| 241 | vsub->tmsi_new = GSM_RESERVED_TMSI; |
| 242 | |
| 243 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
| 244 | vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL; |
| 245 | |
| 246 | INIT_LLIST_HEAD(&vsub->cs.requests); |
| 247 | INIT_LLIST_HEAD(&vsub->ps.pdp_list); |
| 248 | |
| 249 | llist_add_tail(&vsub->list, &vlr->subscribers); |
| 250 | return vsub; |
| 251 | } |
| 252 | |
| 253 | struct vlr_subscr *vlr_subscr_alloc(struct vlr_instance *vlr) |
| 254 | { |
| 255 | return vlr_subscr_get(_vlr_subscr_alloc(vlr)); |
| 256 | } |
| 257 | |
| 258 | /* Send a GSUP Purge MS request. |
| 259 | * TODO: this should be sent to the *previous* VLR when this VLR is "taking" |
| 260 | * this subscriber, not to the HLR? */ |
| 261 | int vlr_subscr_purge(struct vlr_subscr *vsub) |
| 262 | { |
| 263 | struct osmo_gsup_message gsup_msg = {0}; |
| 264 | |
| 265 | gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST; |
| 266 | |
| 267 | /* provide HLR number in case we know it */ |
| 268 | gsup_msg.hlr_enc_len = vsub->hlr.len; |
| 269 | gsup_msg.hlr_enc = vsub->hlr.buf; |
| 270 | |
| 271 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 272 | } |
| 273 | |
| 274 | void vlr_subscr_cancel(struct vlr_subscr *vsub, enum gsm48_gmm_cause cause) |
| 275 | { |
| 276 | if (!vsub) |
| 277 | return; |
| 278 | |
| 279 | if (vsub->lu_fsm) { |
| 280 | if (vsub->lu_fsm->state == VLR_ULA_S_WAIT_HLR_UPD) |
| 281 | osmo_fsm_inst_dispatch(vsub->lu_fsm, |
| 282 | VLR_ULA_E_HLR_LU_RES, |
| 283 | (void*)&cause); |
| 284 | else |
| 285 | osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_ERROR, |
| 286 | 0); |
| 287 | } |
| 288 | |
| 289 | if (vsub->proc_arq_fsm) |
| 290 | osmo_fsm_inst_term(vsub->proc_arq_fsm, OSMO_FSM_TERM_ERROR, 0); |
| 291 | } |
| 292 | |
| 293 | /* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */ |
| 294 | void vlr_subscr_free(struct vlr_subscr *vsub) |
| 295 | { |
| 296 | llist_del(&vsub->list); |
| 297 | DEBUGP(DREF, "freeing VLR subscr %s\n", vlr_subscr_name(vsub)); |
| 298 | talloc_free(vsub); |
| 299 | } |
| 300 | |
| 301 | /* Generate a new TMSI and store in vsub->tmsi_new. |
| 302 | * Search all known subscribers to ensure that the TMSI is unique. */ |
| 303 | int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub) |
| 304 | { |
| 305 | struct vlr_instance *vlr = vsub->vlr; |
| 306 | uint32_t tmsi; |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 307 | int tried, rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 308 | |
| 309 | for (tried = 0; tried < 100; tried++) { |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 310 | rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi)); |
| 311 | if (rc < 0) { |
| 312 | LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc)); |
| 313 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 314 | } |
| 315 | /* throw the dice again, if the TSMI doesn't fit */ |
| 316 | if (tmsi == GSM_RESERVED_TMSI) |
| 317 | continue; |
| 318 | |
| 319 | /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */ |
| 320 | if (vlr->cfg.is_ps) { |
| 321 | /* SGSN */ |
| 322 | tmsi |= 0xC000000; |
| 323 | } else { |
| 324 | /* MSC */ |
| 325 | if ((tmsi & 0xC0000000) == 0xC0000000) |
| 326 | tmsi &= ~0xC0000000; |
| 327 | } |
| 328 | |
| 329 | /* If this TMSI is already in use, try another one. */ |
| 330 | if (vlr_subscr_find_by_tmsi(vlr, tmsi)) |
| 331 | continue; |
| 332 | |
| 333 | vsub->tmsi_new = tmsi; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI" |
| 338 | " after %d tries\n", vlr_subscr_name(vsub), tried); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | /* Find subscriber by IMSI, or create new subscriber if not found. |
| 343 | * \param[in] vlr VLR instace. |
| 344 | * \param[in] imsi IMSI string. |
| 345 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 346 | struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr, |
| 347 | const char *imsi, |
| 348 | bool *created, |
| 349 | const char *file, |
| 350 | int line) |
| 351 | { |
| 352 | struct vlr_subscr *vsub; |
| 353 | vsub = _vlr_subscr_find_by_imsi(vlr, imsi, file, line); |
| 354 | if (vsub) { |
| 355 | if (created) |
| 356 | *created = false; |
| 357 | return vsub; |
| 358 | } |
| 359 | |
| 360 | vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line); |
| 361 | if (!vsub) |
| 362 | return NULL; |
| 363 | vlr_subscr_set_imsi(vsub, imsi); |
| 364 | LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi); |
| 365 | if (created) |
| 366 | *created = true; |
| 367 | return vsub; |
| 368 | } |
| 369 | |
| 370 | /* Find subscriber by TMSI, or create new subscriber if not found. |
| 371 | * \param[in] vlr VLR instace. |
| 372 | * \param[in] tmsi TMSI. |
| 373 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 374 | struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr, |
| 375 | uint32_t tmsi, |
| 376 | bool *created, |
| 377 | const char *file, |
| 378 | int line) |
| 379 | { |
| 380 | struct vlr_subscr *vsub; |
| 381 | vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, file, line); |
| 382 | if (vsub) { |
| 383 | if (created) |
| 384 | *created = false; |
| 385 | return vsub; |
| 386 | } |
| 387 | |
| 388 | vsub = _vlr_subscr_get(_vlr_subscr_alloc(vlr), file, line); |
| 389 | if (!vsub) |
| 390 | return NULL; |
| 391 | vsub->tmsi = tmsi; |
| 392 | LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi); |
| 393 | if (created) |
| 394 | *created = true; |
| 395 | return vsub; |
| 396 | } |
| 397 | |
| 398 | void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 399 | { |
| 400 | if (!vsub) |
| 401 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 402 | OSMO_STRLCPY_ARRAY(vsub->imsi, imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 403 | vsub->id = atoll(vsub->imsi); |
| 404 | DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n", |
| 405 | vsub->imsi, vsub->id); |
| 406 | } |
| 407 | |
| 408 | void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei) |
| 409 | { |
| 410 | if (!vsub) |
| 411 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 412 | OSMO_STRLCPY_ARRAY(vsub->imei, imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 413 | DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n", |
| 414 | vsub->imsi, vsub->imei); |
| 415 | } |
| 416 | |
| 417 | void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv) |
| 418 | { |
| 419 | if (!vsub) |
| 420 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 421 | OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 422 | DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n", |
| 423 | vsub->imsi, vsub->imeisv); |
| 424 | } |
| 425 | |
| 426 | /* Safely copy the given MSISDN string to vsub->msisdn */ |
| 427 | void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 428 | { |
| 429 | if (!vsub) |
| 430 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 431 | OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 432 | DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n", |
| 433 | vsub->imsi, vsub->msisdn); |
| 434 | } |
| 435 | |
| 436 | bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 437 | { |
| 438 | return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi); |
| 439 | } |
| 440 | |
| 441 | bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi) |
| 442 | { |
| 443 | return vsub && tmsi != GSM_RESERVED_TMSI |
| 444 | && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi); |
| 445 | } |
| 446 | |
| 447 | bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 448 | { |
| 449 | return vsub && msisdn && vsub->msisdn[0] |
| 450 | && !strcmp(vsub->msisdn, msisdn); |
| 451 | } |
| 452 | |
| 453 | bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei) |
| 454 | { |
| 455 | return vsub && imei && vsub->imei[0] |
| 456 | && !strcmp(vsub->imei, imei); |
| 457 | } |
| 458 | |
| 459 | /* Send updated subscriber information to HLR */ |
| 460 | int vlr_subscr_changed(struct vlr_subscr *vsub) |
| 461 | { |
| 462 | /* FIXME */ |
| 463 | LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__); |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /*********************************************************************** |
| 468 | * PDP context data |
| 469 | ***********************************************************************/ |
| 470 | |
Neels Hofmeyr | bac2276 | 2017-07-06 18:39:28 +0200 | [diff] [blame] | 471 | #define GSM_APN_LENGTH 102 |
| 472 | |
| 473 | /* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */ |
| 474 | /* see GSM 09.02, B.1, gprsSubscriptionData */ |
| 475 | struct sgsn_subscriber_pdp_data { |
| 476 | struct llist_head list; |
| 477 | |
| 478 | unsigned int context_id; |
| 479 | uint16_t pdp_type; |
| 480 | char apn_str[GSM_APN_LENGTH]; |
| 481 | uint8_t qos_subscribed[20]; |
| 482 | size_t qos_subscribed_len; |
| 483 | }; |
| 484 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 485 | struct sgsn_subscriber_pdp_data * |
| 486 | vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub) |
| 487 | { |
| 488 | struct sgsn_subscriber_pdp_data* pdata; |
| 489 | |
| 490 | pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data); |
| 491 | |
| 492 | llist_add_tail(&pdata->list, &vsub->ps.pdp_list); |
| 493 | |
| 494 | return pdata; |
| 495 | } |
| 496 | |
| 497 | static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub) |
| 498 | { |
| 499 | struct sgsn_subscriber_pdp_data *pdp, *pdp2; |
| 500 | int count = 0; |
| 501 | |
| 502 | llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) { |
| 503 | llist_del(&pdp->list); |
| 504 | talloc_free(pdp); |
| 505 | count += 1; |
| 506 | } |
| 507 | |
| 508 | return count; |
| 509 | } |
| 510 | |
| 511 | static struct sgsn_subscriber_pdp_data * |
| 512 | vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id) |
| 513 | { |
| 514 | struct sgsn_subscriber_pdp_data *pdp; |
| 515 | |
| 516 | llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) { |
| 517 | if (pdp->context_id == context_id) |
| 518 | return pdp; |
| 519 | } |
| 520 | |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | /*********************************************************************** |
| 525 | * Actual Implementation |
| 526 | ***********************************************************************/ |
| 527 | |
| 528 | static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr, |
| 529 | struct osmo_gsup_message *gsup_msg) |
| 530 | { |
| 531 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) { |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 532 | int rc = vlr_tx_gsup_error_reply(vlr, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN); |
| 533 | if (rc < 0) |
| 534 | LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup_msg->imsi); |
| 535 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 536 | LOGP(DVLR, LOGL_NOTICE, |
| 537 | "Unknown IMSI %s, discarding GSUP request " |
| 538 | "of type 0x%02x\n", |
| 539 | gsup_msg->imsi, gsup_msg->message_type); |
| 540 | } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 541 | LOGP(DVLR, LOGL_NOTICE, |
| 542 | "Unknown IMSI %s, discarding GSUP error " |
| 543 | "of type 0x%02x, cause '%s' (%d)\n", |
| 544 | gsup_msg->imsi, gsup_msg->message_type, |
| 545 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 546 | gsup_msg->cause); |
| 547 | } else { |
| 548 | LOGP(DVLR, LOGL_NOTICE, |
| 549 | "Unknown IMSI %s, discarding GSUP response " |
| 550 | "of type 0x%02x\n", |
| 551 | gsup_msg->imsi, gsup_msg->message_type); |
| 552 | } |
| 553 | |
| 554 | return -GMM_CAUSE_IMSI_UNKNOWN; |
| 555 | } |
| 556 | |
| 557 | static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr, |
| 558 | struct osmo_gsup_message *gsup_msg) |
| 559 | { |
| 560 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 561 | LOGGSUPP(LOGL_NOTICE, gsup_msg, |
| 562 | "Purge MS has failed with cause '%s' (%d)\n", |
| 563 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 564 | gsup_msg->cause); |
| 565 | return -gsup_msg->cause; |
| 566 | } |
| 567 | LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n"); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* VLR internal call to request UpdateLocation from HLR */ |
| 572 | int vlr_subscr_req_lu(struct vlr_subscr *vsub, bool is_ps) |
| 573 | { |
| 574 | struct osmo_gsup_message gsup_msg = {0}; |
| 575 | int rc; |
| 576 | |
| 577 | gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; |
| 578 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 579 | |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | /* VLR internal call to request tuples from HLR */ |
| 584 | int vlr_subscr_req_sai(struct vlr_subscr *vsub, |
| 585 | const uint8_t *auts, const uint8_t *auts_rand) |
| 586 | { |
| 587 | struct osmo_gsup_message gsup_msg = {0}; |
| 588 | |
| 589 | gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; |
| 590 | gsup_msg.auts = auts; |
| 591 | gsup_msg.rand = auts_rand; |
| 592 | |
| 593 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 594 | } |
| 595 | |
| 596 | /* Tell HLR that authentication failure occurred */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 597 | int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 598 | { |
| 599 | struct osmo_gsup_message gsup_msg = {0}; |
| 600 | |
| 601 | gsup_msg.message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 602 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 603 | return vlr_tx_gsup_message(vsub->vlr, &gsup_msg); |
| 604 | } |
| 605 | |
| 606 | /* Update the subscriber with GSUP-received auth tuples */ |
| 607 | void vlr_subscr_update_tuples(struct vlr_subscr *vsub, |
| 608 | const struct osmo_gsup_message *gsup) |
| 609 | { |
| 610 | unsigned int i; |
| 611 | unsigned int got_tuples; |
| 612 | |
| 613 | if (gsup->num_auth_vectors) { |
| 614 | memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples)); |
| 615 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
| 616 | vsub->auth_tuples[i].key_seq = GSM_KEY_SEQ_INVAL; |
| 617 | } |
| 618 | |
| 619 | got_tuples = 0; |
| 620 | for (i = 0; i < gsup->num_auth_vectors; i++) { |
| 621 | size_t key_seq = i; |
| 622 | |
| 623 | if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) { |
| 624 | LOGVSUBP(LOGL_NOTICE, vsub, |
| 625 | "Skipping auth tuple wih invalid cksn %zu\n", |
| 626 | key_seq); |
| 627 | continue; |
| 628 | } |
| 629 | vsub->auth_tuples[i].vec = gsup->auth_vectors[i]; |
| 630 | vsub->auth_tuples[i].key_seq = key_seq; |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 631 | got_tuples++; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples); |
| 635 | |
| 636 | if (!got_tuples) { |
| 637 | /* FIXME what now? */ |
| 638 | // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ? |
| 639 | } |
| 640 | |
| 641 | /* New tuples means last_tuple becomes invalid */ |
| 642 | vsub->last_tuple = NULL; |
| 643 | } |
| 644 | |
| 645 | /* Handle SendAuthInfo Result/Error from HLR */ |
| 646 | static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub, |
| 647 | const struct osmo_gsup_message *gsup) |
| 648 | { |
| 649 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 650 | void *data = (void *) gsup; |
| 651 | |
| 652 | switch (gsup->message_type) { |
| 653 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 654 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data); |
| 655 | break; |
| 656 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
| 657 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data); |
| 658 | break; |
| 659 | default: |
| 660 | return -1; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int decode_bcd_number_safe(char *output, int output_len, |
| 667 | const uint8_t *bcd_lv, int input_len, |
| 668 | int h_len) |
| 669 | { |
| 670 | uint8_t len; |
| 671 | OSMO_ASSERT(output_len >= 1); |
| 672 | *output = '\0'; |
| 673 | if (input_len < 1) |
| 674 | return -EIO; |
| 675 | len = bcd_lv[0]; |
| 676 | if (input_len < len) |
| 677 | return -EIO; |
| 678 | return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len); |
| 679 | } |
| 680 | |
| 681 | static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub, |
| 682 | const struct osmo_gsup_message *gsup_msg) |
| 683 | { |
| 684 | unsigned idx; |
| 685 | int rc; |
| 686 | |
Max | a263bb2 | 2017-12-27 13:23:44 +0100 | [diff] [blame] | 687 | if (gsup_msg->msisdn_enc) {//FIXME: vlr_subscr_set_msisdn()? |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 688 | decode_bcd_number_safe(vsub->msisdn, sizeof(vsub->msisdn), |
| 689 | gsup_msg->msisdn_enc, |
| 690 | gsup_msg->msisdn_enc_len, 0); |
| 691 | LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n", |
| 692 | vsub->imsi, vsub->msisdn); |
| 693 | } |
| 694 | |
| 695 | if (gsup_msg->hlr_enc) { |
| 696 | if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) { |
| 697 | LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n", |
| 698 | gsup_msg->hlr_enc_len); |
| 699 | vsub->hlr.len = 0; |
| 700 | } else { |
| 701 | memcpy(vsub->hlr.buf, gsup_msg->hlr_enc, |
| 702 | gsup_msg->hlr_enc_len); |
| 703 | vsub->hlr.len = gsup_msg->hlr_enc_len; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (gsup_msg->pdp_info_compl) { |
| 708 | rc = vlr_subscr_pdp_data_clear(vsub); |
| 709 | if (rc > 0) |
| 710 | LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n"); |
| 711 | } |
| 712 | |
| 713 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
| 714 | const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx]; |
| 715 | size_t ctx_id = pdp_info->context_id; |
| 716 | struct sgsn_subscriber_pdp_data *pdp_data; |
| 717 | |
| 718 | if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) { |
| 719 | LOGVSUBP(LOGL_ERROR, vsub, |
| 720 | "APN too long, context id = %zu, APN = %s\n", |
| 721 | ctx_id, osmo_hexdump(pdp_info->apn_enc, |
| 722 | pdp_info->apn_enc_len)); |
| 723 | continue; |
| 724 | } |
| 725 | |
| 726 | if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) { |
| 727 | LOGVSUBP(LOGL_ERROR, vsub, |
| 728 | "QoS info too long (%zu)\n", |
| 729 | pdp_info->qos_enc_len); |
| 730 | continue; |
| 731 | } |
| 732 | |
| 733 | LOGVSUBP(LOGL_INFO, vsub, |
| 734 | "Will set PDP info, context id = %zu, APN = %s\n", |
| 735 | ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len)); |
| 736 | |
| 737 | /* Set PDP info [ctx_id] */ |
| 738 | pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id); |
| 739 | if (!pdp_data) { |
| 740 | pdp_data = vlr_subscr_pdp_data_alloc(vsub); |
| 741 | pdp_data->context_id = ctx_id; |
| 742 | } |
| 743 | |
| 744 | OSMO_ASSERT(pdp_data != NULL); |
| 745 | pdp_data->pdp_type = pdp_info->pdp_type; |
| 746 | osmo_apn_to_str(pdp_data->apn_str, |
| 747 | pdp_info->apn_enc, pdp_info->apn_enc_len); |
| 748 | memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len); |
| 749 | pdp_data->qos_subscribed_len = pdp_info->qos_enc_len; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /* Handle InsertSubscrData Result from HLR */ |
| 755 | static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub, |
| 756 | const struct osmo_gsup_message *gsup) |
| 757 | { |
| 758 | struct osmo_gsup_message gsup_reply = {0}; |
| 759 | |
| 760 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 761 | vsub->vlr->ops.subscr_update(vsub); |
| 762 | |
| 763 | gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; |
| 764 | return vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
| 765 | } |
| 766 | |
| 767 | /* Handle UpdateLocation Result from HLR */ |
| 768 | static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub, |
| 769 | const struct osmo_gsup_message *gsup) |
| 770 | { |
| 771 | if (!vsub->lu_fsm) { |
| 772 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result " |
| 773 | "without LU in progress\n"); |
| 774 | return -ENODEV; |
| 775 | } |
| 776 | |
| 777 | /* contrary to MAP, we allow piggy-backing subscriber data onto the |
| 778 | * UPDATE LOCATION RESULT, and don't mandate the use of a separate |
| 779 | * nested INSERT SUBSCRIBER DATA transaction */ |
| 780 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 781 | |
| 782 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL); |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /* Handle UpdateLocation Result from HLR */ |
| 788 | static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub, |
| 789 | const struct osmo_gsup_message *gsup) |
| 790 | { |
| 791 | if (!vsub->lu_fsm) { |
| 792 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error " |
| 793 | "without LU in progress\n"); |
| 794 | return -ENODEV; |
| 795 | } |
| 796 | |
| 797 | LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n", |
| 798 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 799 | |
| 800 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, |
| 801 | (void *)&gsup->cause); |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /* Handle LOCATION CANCEL request from HLR */ |
| 807 | static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub, |
| 808 | struct osmo_gsup_message *gsup_msg) |
| 809 | { |
| 810 | struct osmo_gsup_message gsup_reply = {0}; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 811 | int rc, is_update_procedure = !gsup_msg->cancel_type || |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 812 | gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE; |
| 813 | |
| 814 | LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n", |
| 815 | is_update_procedure ? |
| 816 | "update procedure" : "subscription withdraw"); |
| 817 | |
| 818 | gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 819 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 820 | |
| 821 | vlr_subscr_cancel(vsub, gsup_msg->cause); |
| 822 | |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 823 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* Incoming handler for GSUP from HLR. |
| 827 | * Keep this function non-static for direct invocation by unit tests. */ |
| 828 | int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg) |
| 829 | { |
| 830 | struct vlr_instance *vlr = (struct vlr_instance *) gsupc->data; |
| 831 | struct vlr_subscr *vsub; |
| 832 | struct osmo_gsup_message gsup; |
| 833 | int rc; |
| 834 | |
| 835 | DEBUGP(DVLR, "GSUP rx %u: %s\n", msgb_l2len(msg), |
| 836 | osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg))); |
| 837 | |
| 838 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
| 839 | if (rc < 0) { |
| 840 | LOGP(DVLR, LOGL_ERROR, |
| 841 | "decoding GSUP message fails with error '%s' (%d)\n", |
| 842 | get_value_string(gsm48_gmm_cause_names, -rc), -rc); |
Neels Hofmeyr | b3fa355 | 2017-11-18 22:22:59 +0100 | [diff] [blame] | 843 | goto msgb_free_and_return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (!gsup.imsi[0]) { |
| 847 | LOGP(DVLR, LOGL_ERROR, "Missing IMSI in GSUP message\n"); |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 848 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup.message_type)) { |
| 849 | rc = vlr_tx_gsup_error_reply(vlr, &gsup, GMM_CAUSE_INV_MAND_INFO); |
| 850 | if (rc < 0) |
| 851 | LOGP(DVLR, LOGL_ERROR, "Failed to send error reply for IMSI %s\n", gsup.imsi); |
| 852 | } |
Neels Hofmeyr | b3fa355 | 2017-11-18 22:22:59 +0100 | [diff] [blame] | 853 | rc = -GMM_CAUSE_INV_MAND_INFO; |
| 854 | goto msgb_free_and_return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | vsub = vlr_subscr_find_by_imsi(vlr, gsup.imsi); |
| 858 | if (!vsub) { |
| 859 | switch (gsup.message_type) { |
| 860 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 861 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Neels Hofmeyr | b3fa355 | 2017-11-18 22:22:59 +0100 | [diff] [blame] | 862 | rc = vlr_rx_gsup_purge_no_subscr(vlr, &gsup); |
| 863 | goto msgb_free_and_return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 864 | default: |
Neels Hofmeyr | b3fa355 | 2017-11-18 22:22:59 +0100 | [diff] [blame] | 865 | rc = vlr_rx_gsup_unknown_imsi(vlr, &gsup); |
| 866 | goto msgb_free_and_return; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
| 870 | switch (gsup.message_type) { |
| 871 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 872 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
| 873 | rc = vlr_subscr_handle_sai_res(vsub, &gsup); |
| 874 | break; |
| 875 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
| 876 | rc = vlr_subscr_handle_isd_req(vsub, &gsup); |
| 877 | break; |
| 878 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST: |
| 879 | rc = vlr_subscr_handle_cancel_req(vsub, &gsup); |
| 880 | break; |
| 881 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
| 882 | rc = vlr_subscr_handle_lu_res(vsub, &gsup); |
| 883 | break; |
| 884 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: |
| 885 | rc = vlr_subscr_handle_lu_err(vsub, &gsup); |
| 886 | break; |
| 887 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
| 888 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 889 | case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST: |
| 890 | LOGVSUBP(LOGL_ERROR, vsub, |
| 891 | "Rx GSUP msg_type=%d not yet implemented\n", |
| 892 | gsup.message_type); |
| 893 | rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
| 894 | break; |
| 895 | default: |
| 896 | LOGVSUBP(LOGL_ERROR, vsub, |
| 897 | "Rx GSUP msg_type=%d not valid at VLR/SGSN side\n", |
| 898 | gsup.message_type); |
| 899 | rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
| 900 | break; |
| 901 | } |
| 902 | |
| 903 | vlr_subscr_put(vsub); |
Neels Hofmeyr | b3fa355 | 2017-11-18 22:22:59 +0100 | [diff] [blame] | 904 | |
| 905 | msgb_free_and_return: |
| 906 | msgb_free(msg); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 907 | return rc; |
| 908 | } |
| 909 | |
| 910 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
| 911 | int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, |
| 912 | const uint8_t *mi, size_t mi_len) |
| 913 | { |
| 914 | char mi_string[GSM48_MI_SIZE]; |
| 915 | uint8_t mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 916 | |
| 917 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 918 | |
| 919 | /* update the vlr_subscr with the given identity */ |
| 920 | switch (mi_type) { |
| 921 | case GSM_MI_TYPE_IMSI: |
| 922 | if (vsub->imsi[0] |
| 923 | && !vlr_subscr_matches_imsi(vsub, mi_string)) { |
| 924 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
| 925 | " %s\n", mi_string); |
| 926 | } else |
| 927 | vlr_subscr_set_imsi(vsub, mi_string); |
| 928 | break; |
| 929 | case GSM_MI_TYPE_IMEI: |
| 930 | vlr_subscr_set_imei(vsub, mi_string); |
| 931 | break; |
| 932 | case GSM_MI_TYPE_IMEISV: |
| 933 | vlr_subscr_set_imeisv(vsub, mi_string); |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | if (vsub->auth_fsm) { |
| 938 | switch (mi_type) { |
| 939 | case GSM_MI_TYPE_IMSI: |
| 940 | osmo_fsm_inst_dispatch(vsub->auth_fsm, |
| 941 | VLR_AUTH_E_MS_ID_IMSI, mi_string); |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if (vsub->lu_fsm) { |
| 947 | uint32_t event = 0; |
| 948 | switch (mi_type) { |
| 949 | case GSM_MI_TYPE_IMSI: |
| 950 | event = VLR_ULA_E_ID_IMSI; |
| 951 | break; |
| 952 | case GSM_MI_TYPE_IMEI: |
| 953 | event = VLR_ULA_E_ID_IMEI; |
| 954 | break; |
| 955 | case GSM_MI_TYPE_IMEISV: |
| 956 | event = VLR_ULA_E_ID_IMEISV; |
| 957 | break; |
| 958 | default: |
| 959 | OSMO_ASSERT(0); |
| 960 | break; |
| 961 | } |
| 962 | osmo_fsm_inst_dispatch(vsub->lu_fsm, event, mi_string); |
| 963 | } else { |
| 964 | LOGVSUBP(LOGL_NOTICE, vsub, "gratuitous ID RESPONSE?!?\n"); |
| 965 | } |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
| 971 | int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub) |
| 972 | { |
| 973 | if (vsub->lu_fsm) { |
| 974 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, |
| 975 | VLR_ULA_E_NEW_TMSI_ACK, NULL); |
| 976 | } else if (vsub->proc_arq_fsm) { |
| 977 | return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, |
| 978 | PR_ARQ_E_TMSI_ACK, NULL); |
| 979 | } else { |
| 980 | LOGVSUBP(LOGL_NOTICE, vsub, |
| 981 | "gratuitous TMSI REALLOC COMPL"); |
| 982 | return -EINVAL; |
| 983 | } |
| 984 | } |
| 985 | |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 986 | bool vlr_subscr_expire(struct vlr_subscr *vsub) |
| 987 | { |
| 988 | if (vsub->lu_complete) { |
| 989 | vsub->lu_complete = false; |
| 990 | vlr_subscr_put(vsub); |
| 991 | |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | return false; |
| 996 | } |
| 997 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 998 | int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub) |
| 999 | { |
| 1000 | /* paranoia: should any LU or PARQ FSMs still be running, stop them. */ |
| 1001 | vlr_subscr_cancel(vsub, GMM_CAUSE_IMPL_DETACHED); |
| 1002 | |
| 1003 | vsub->imsi_detached_flag = true; |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1004 | |
| 1005 | /* balancing the get from vlr_lu_compl_fsm_success() */ |
| 1006 | vlr_subscr_expire(vsub); |
| 1007 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | /* Tear down any running FSMs due to MSC connection timeout. |
| 1012 | * Visit all vsub->*_fsm pointers and give them a queue to send a final reject |
| 1013 | * message before the entire connection is torn down. |
| 1014 | * \param[in] vsub subscriber to tear down |
| 1015 | */ |
| 1016 | void vlr_subscr_conn_timeout(struct vlr_subscr *vsub) |
| 1017 | { |
| 1018 | if (!vsub) |
| 1019 | return; |
| 1020 | |
Neels Hofmeyr | 3bae836 | 2017-11-18 23:26:24 +0100 | [diff] [blame] | 1021 | vlr_subscr_get(vsub); |
| 1022 | if (vsub->lu_fsm) |
| 1023 | vlr_loc_update_conn_timeout(vsub->lu_fsm); |
| 1024 | if (vsub->proc_arq_fsm) |
| 1025 | vlr_parq_conn_timeout(vsub->proc_arq_fsm); |
| 1026 | vlr_subscr_put(vsub); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops) |
| 1030 | { |
| 1031 | struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance); |
| 1032 | OSMO_ASSERT(vlr); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* Some of these are needed only on UTRAN, but in case the caller wants |
| 1035 | * only GERAN, she should just provide dummy callbacks. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1036 | OSMO_ASSERT(ops->tx_auth_req); |
| 1037 | OSMO_ASSERT(ops->tx_auth_rej); |
| 1038 | OSMO_ASSERT(ops->tx_id_req); |
| 1039 | OSMO_ASSERT(ops->tx_lu_acc); |
| 1040 | OSMO_ASSERT(ops->tx_lu_rej); |
| 1041 | OSMO_ASSERT(ops->tx_cm_serv_acc); |
| 1042 | OSMO_ASSERT(ops->tx_cm_serv_rej); |
| 1043 | OSMO_ASSERT(ops->set_ciph_mode); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1044 | OSMO_ASSERT(ops->tx_common_id); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1045 | OSMO_ASSERT(ops->subscr_update); |
| 1046 | OSMO_ASSERT(ops->subscr_assoc); |
| 1047 | |
| 1048 | INIT_LLIST_HEAD(&vlr->subscribers); |
| 1049 | INIT_LLIST_HEAD(&vlr->operations); |
| 1050 | memcpy(&vlr->ops, ops, sizeof(vlr->ops)); |
| 1051 | |
Neels Hofmeyr | 0b8dec7 | 2017-10-29 01:57:35 +0200 | [diff] [blame] | 1052 | /* defaults */ |
| 1053 | vlr->cfg.assign_tmsi = true; |
| 1054 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1055 | /* osmo_auth_fsm.c */ |
| 1056 | osmo_fsm_register(&vlr_auth_fsm); |
| 1057 | /* osmo_lu_fsm.c */ |
| 1058 | vlr_lu_fsm_init(); |
| 1059 | /* vlr_access_request_fsm.c */ |
| 1060 | vlr_parq_fsm_init(); |
| 1061 | |
| 1062 | return vlr; |
| 1063 | } |
| 1064 | |
| 1065 | int vlr_start(const char *gsup_unit_name, struct vlr_instance *vlr, |
| 1066 | const char *gsup_server_addr_str, uint16_t gsup_server_port) |
| 1067 | { |
| 1068 | OSMO_ASSERT(vlr); |
| 1069 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 1070 | vlr->gsup_client = gsup_client_create(vlr, gsup_unit_name, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1071 | gsup_server_addr_str, |
| 1072 | gsup_server_port, |
| 1073 | &vlr_gsupc_read_cb, NULL); |
| 1074 | if (!vlr->gsup_client) |
| 1075 | return -ENOMEM; |
| 1076 | vlr->gsup_client->data = vlr; |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* MSC->VLR: Subscribre has disconnected */ |
| 1082 | int vlr_subscr_disconnected(struct vlr_subscr *vsub) |
| 1083 | { |
| 1084 | /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B |
| 1085 | * interface */ |
| 1086 | osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1087 | osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1088 | vsub->msc_conn_ref = NULL; |
| 1089 | |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | /* MSC->VLR: Receive Authentication Failure from Subscriber */ |
| 1094 | int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts) |
| 1095 | { |
| 1096 | struct vlr_auth_resp_par par = {0}; |
| 1097 | par.auts = auts; |
| 1098 | |
| 1099 | osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /* MSC->VLR: Receive Authentication Response from MS |
| 1104 | * \returns 1 in case of success, 0 in case of delay, -1 on auth error */ |
| 1105 | int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99, |
| 1106 | bool is_utran, const uint8_t *res, uint8_t res_len) |
| 1107 | { |
| 1108 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 1109 | struct vlr_auth_resp_par par; |
| 1110 | |
| 1111 | par.is_r99 = is_r99; |
| 1112 | par.is_utran = is_utran; |
| 1113 | par.res = res; |
| 1114 | par.res_len = res_len; |
| 1115 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par); |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | /* MSC->VLR: Receive result of Ciphering Mode Command from MS */ |
| 1121 | void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, struct vlr_ciph_result *res) |
| 1122 | { |
| 1123 | if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH) |
| 1124 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, res); |
| 1125 | if (vsub->proc_arq_fsm |
| 1126 | && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH) |
| 1127 | osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, |
| 1128 | res); |
| 1129 | } |
| 1130 | |
| 1131 | /* Internal evaluation of requested ciphering mode. |
| 1132 | * Send set_ciph_mode() to MSC depending on the ciph_mode argument. |
| 1133 | * \param[in] vlr VLR instance. |
| 1134 | * \param[in] fi Calling FSM instance, for logging. |
| 1135 | * \param[in] msc_conn_ref MSC conn to send to. |
| 1136 | * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering. |
| 1137 | * \returns 0 if no ciphering is needed or message was sent successfully, |
| 1138 | * or a negative value if ciph_mode is invalid or sending failed. |
| 1139 | */ |
| 1140 | int vlr_set_ciph_mode(struct vlr_instance *vlr, |
| 1141 | struct osmo_fsm_inst *fi, |
| 1142 | void *msc_conn_ref, |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1143 | bool ciph_required, |
Neels Hofmeyr | 2ef2da5 | 2017-12-18 01:23:42 +0100 | [diff] [blame] | 1144 | bool umts_aka, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1145 | bool retrieve_imeisv) |
| 1146 | { |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1147 | if (!ciph_required) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1148 | return 0; |
| 1149 | |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1150 | LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n"); |
| 1151 | 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] | 1152 | } |
| 1153 | |
Neels Hofmeyr | e3d72d7 | 2017-12-18 02:06:44 +0100 | [diff] [blame] | 1154 | /* Decide whether UMTS AKA should be used. |
| 1155 | * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA |
| 1156 | * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and |
| 1157 | * GERAN are R99 capable and UMTS AKA tokens are available. |
| 1158 | * \param[in] vec Auth tokens (received from the HLR). |
| 1159 | * \param[in] is_r99 True when BTS and GERAN are R99 capable. |
| 1160 | * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA. |
| 1161 | */ |
| 1162 | bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99) |
| 1163 | { |
| 1164 | if (!is_r99) |
| 1165 | return false; |
| 1166 | if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) |
| 1167 | return false; |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1171 | void log_set_filter_vlr_subscr(struct log_target *target, |
| 1172 | struct vlr_subscr *vlr_subscr) |
| 1173 | { |
| 1174 | struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR]; |
| 1175 | |
| 1176 | /* free the old data */ |
| 1177 | if (*fsub) { |
| 1178 | vlr_subscr_put(*fsub); |
| 1179 | *fsub = NULL; |
| 1180 | } |
| 1181 | |
| 1182 | if (vlr_subscr) { |
| 1183 | target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR); |
| 1184 | *fsub = vlr_subscr_get(vlr_subscr); |
| 1185 | } else |
| 1186 | target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR); |
| 1187 | } |