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