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> |
Alexander Couzens | 620c60f | 2024-09-12 00:56:27 +0200 | [diff] [blame^] | 24 | #include <osmocom/core/stats.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 25 | #include <osmocom/core/utils.h> |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 26 | #include <osmocom/core/timer.h> |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 27 | #include <osmocom/core/tdef.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 28 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/gsm23236.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 30 | #include <osmocom/gsm/gsup.h> |
| 31 | #include <osmocom/gsm/apn.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/gsm48.h> |
Stefan Sperling | afa030d | 2018-12-06 12:06:59 +0100 | [diff] [blame] | 33 | #include <osmocom/gsm/ipa.h> |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 34 | #include <osmocom/gsupclient/gsup_client.h> |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 35 | #include <osmocom/msc/vlr_sgs.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 36 | #include <osmocom/msc/vlr.h> |
| 37 | #include <osmocom/msc/debug.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 38 | #include <osmocom/msc/gsup_client_mux.h> |
Neels Hofmeyr | 2fd69e1 | 2024-03-26 00:50:04 +0100 | [diff] [blame] | 39 | #include <osmocom/msc/paging.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 40 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 43 | #include <errno.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 44 | |
| 45 | #include "vlr_core.h" |
| 46 | #include "vlr_auth_fsm.h" |
| 47 | #include "vlr_lu_fsm.h" |
| 48 | #include "vlr_access_req_fsm.h" |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 49 | #include "vlr_sgs_fsm.h" |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 50 | |
| 51 | #define SGSN_SUBSCR_MAX_RETRIES 3 |
| 52 | #define SGSN_SUBSCR_RETRY_INTERVAL 10 |
| 53 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 54 | enum vlr_stat_item_idx { |
| 55 | VLR_STAT_SUBSCRIBER_COUNT, |
| 56 | VLR_STAT_PDP_COUNT, |
| 57 | }; |
| 58 | |
| 59 | static const struct osmo_stat_item_desc vlr_stat_item_desc[] = { |
| 60 | [VLR_STAT_SUBSCRIBER_COUNT] = { "subscribers", |
| 61 | "Number of subscribers present in VLR" }, |
| 62 | [VLR_STAT_PDP_COUNT] = { "pdp", |
| 63 | "Number of PDP records present in VLR" }, |
| 64 | }; |
| 65 | |
| 66 | static const struct osmo_stat_item_group_desc vlr_statg_desc = { |
| 67 | "vlr", |
| 68 | "visitor location register", |
| 69 | OSMO_STATS_CLASS_GLOBAL, |
| 70 | ARRAY_SIZE(vlr_stat_item_desc), |
| 71 | vlr_stat_item_desc, |
| 72 | }; |
| 73 | |
| 74 | enum vlr_rate_ctr_idx { |
| 75 | VLR_CTR_GSUP_RX_UNKNOWN_IMSI, |
| 76 | VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR, |
| 77 | VLR_CTR_GSUP_RX_TUPLES, |
| 78 | VLR_CTR_GSUP_RX_UL_RES, |
| 79 | VLR_CTR_GSUP_RX_UL_ERR, |
| 80 | VLR_CTR_GSUP_RX_SAI_RES, |
| 81 | VLR_CTR_GSUP_RX_SAI_ERR, |
| 82 | VLR_CTR_GSUP_RX_ISD_REQ, |
| 83 | VLR_CTR_GSUP_RX_CANCEL_REQ, |
| 84 | VLR_CTR_GSUP_RX_CHECK_IMEI_RES, |
| 85 | VLR_CTR_GSUP_RX_CHECK_IMEI_ERR, |
| 86 | VLR_CTR_GSUP_RX_PURGE_MS_RES, |
| 87 | VLR_CTR_GSUP_RX_PURGE_MS_ERR, |
| 88 | VLR_CTR_GSUP_RX_DELETE_DATA_REQ, |
| 89 | VLR_CTR_GSUP_RX_UNKNOWN, |
| 90 | |
| 91 | VLR_CTR_GSUP_TX_UL_REQ, |
| 92 | VLR_CTR_GSUP_TX_ISD_RES, |
| 93 | VLR_CTR_GSUP_TX_SAI_REQ, |
| 94 | VLR_CTR_GSUP_TX_PURGE_MS_REQ, |
| 95 | VLR_CTR_GSUP_TX_CHECK_IMEI_REQ, |
| 96 | VLR_CTR_GSUP_TX_AUTH_FAIL_REP, |
| 97 | VLR_CTR_GSUP_TX_CANCEL_RES, |
| 98 | |
| 99 | VLR_CTR_DETACH_BY_REQ, |
| 100 | VLR_CTR_DETACH_BY_CANCEL, |
| 101 | VLR_CTR_DETACH_BY_T3212, |
| 102 | }; |
| 103 | |
| 104 | static const struct rate_ctr_desc vlr_ctr_desc[] = { |
| 105 | [VLR_CTR_GSUP_RX_UNKNOWN_IMSI] = { "gsup:rx:unknown_imsi", |
| 106 | "Received GSUP messages for unknown IMSI" }, |
| 107 | [VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR] = { "gsup:rx:purge_no_subscr", |
| 108 | "Received GSUP purge for unknown subscriber" }, |
| 109 | [VLR_CTR_GSUP_RX_TUPLES] = { "gsup:rx:auth_tuples", |
| 110 | "Received GSUP authentication tuples" }, |
| 111 | [VLR_CTR_GSUP_RX_UL_RES] = { "gsup:rx:upd_loc:res", |
| 112 | "Received GSUP Update Location Result messages" }, |
| 113 | [VLR_CTR_GSUP_RX_UL_ERR] = { "gsup:rx:upd_loc:err", |
| 114 | "Received GSUP Update Location Error messages" }, |
| 115 | [VLR_CTR_GSUP_RX_SAI_RES] = { "gsup:rx:send_auth_info:res", |
| 116 | "Received GSUP Send Auth Info Result messages" }, |
| 117 | [VLR_CTR_GSUP_RX_SAI_ERR] = { "gsup:rx:send_auth_info:err", |
| 118 | "Received GSUP Send Auth Info Error messages" }, |
| 119 | [VLR_CTR_GSUP_RX_ISD_REQ] = { "gsup:rx:ins_sub_data:req", |
| 120 | "Received GSUP Insert Subscriber Data Request messages" }, |
| 121 | [VLR_CTR_GSUP_RX_CANCEL_REQ] = { "gsup:rx:cancel:req", |
| 122 | "Received GSUP Cancel Subscriber messages" }, |
| 123 | [VLR_CTR_GSUP_RX_CHECK_IMEI_RES] = { "gsup:rx:check_imei:res", |
| 124 | "Received GSUP Check IMEI Result messages" }, |
| 125 | [VLR_CTR_GSUP_RX_CHECK_IMEI_ERR] = { "gsup:rx:check_imei:err", |
| 126 | "Received GSUP Check IMEI Error messages" }, |
| 127 | [VLR_CTR_GSUP_RX_PURGE_MS_RES] = { "gsup:rx:purge_ms:res", |
| 128 | "Received GSUP Purge MS Result messages" }, |
| 129 | [VLR_CTR_GSUP_RX_PURGE_MS_ERR] = { "gsup:rx:purge_ms:err", |
| 130 | "Received GSUP Purge MS Error messages" }, |
| 131 | [VLR_CTR_GSUP_RX_DELETE_DATA_REQ] = { "gsup:rx:del_sub_data:req", |
| 132 | "Received GSUP Delete Subscriber Data Request messages" }, |
| 133 | [VLR_CTR_GSUP_RX_UNKNOWN] = { "gsup:rx:unknown_msgtype", |
| 134 | "Received GSUP message of unknown type" }, |
| 135 | |
| 136 | [VLR_CTR_GSUP_TX_UL_REQ] = { "gsup:tx:upd_loc:req", |
| 137 | "Transmitted GSUP Update Location Request messages" }, |
| 138 | [VLR_CTR_GSUP_TX_ISD_RES] = { "gsup:tx:ins_sub_data:res", |
| 139 | "Transmitted GSUP Insert Subscriber Data Result messages" }, |
| 140 | [VLR_CTR_GSUP_TX_SAI_REQ] = { "gsup:tx:send_auth_info:res", |
| 141 | "Transmitted GSUP Send Auth Info Request messages" }, |
| 142 | [VLR_CTR_GSUP_TX_PURGE_MS_REQ] = { "gsup:tx:purge_ms:req", |
| 143 | "Transmitted GSUP Purge MS Request messages" }, |
| 144 | [VLR_CTR_GSUP_TX_CHECK_IMEI_REQ] = { "gsup:tx:check_imei:req", |
| 145 | "Transmitted GSUP Check IMEI Request messages" }, |
| 146 | [VLR_CTR_GSUP_TX_AUTH_FAIL_REP] = { "gsup:tx:auth_fail:rep", |
| 147 | "Transmitted GSUP Auth Fail Report messages" }, |
| 148 | [VLR_CTR_GSUP_TX_CANCEL_RES] = { "gsup:tx:cancel:res", |
| 149 | "Transmitted GSUP Cancel Result messages" }, |
| 150 | |
| 151 | [VLR_CTR_DETACH_BY_REQ] = { "detach:imsi_det_req", |
| 152 | "VLR Subscriber Detach by IMSI DETACH REQ" }, |
| 153 | [VLR_CTR_DETACH_BY_CANCEL] = { "detach:gsup_cancel_req", |
| 154 | "VLR Subscriber Detach by GSUP CANCEL REQ" }, |
| 155 | [VLR_CTR_DETACH_BY_T3212] = { "detach:t3212_timeout", |
| 156 | "VLR Subscriber Detach by T3212 timeout" }, |
| 157 | }; |
| 158 | |
| 159 | static const struct rate_ctr_group_desc vlr_ctrg_desc = { |
| 160 | "vlr", |
| 161 | "visitor location register", |
| 162 | OSMO_STATS_CLASS_GLOBAL, |
| 163 | ARRAY_SIZE(vlr_ctr_desc), |
| 164 | vlr_ctr_desc, |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | #define vlr_rate_ctr_inc(vlr, idx) \ |
| 169 | rate_ctr_inc(rate_ctr_group_get_ctr((vlr)->ctrg, idx)) |
| 170 | #define vlr_rate_ctr_add(vlr, idx, val) \ |
| 171 | rate_ctr_add(rate_ctr_group_get_ctr((vlr)->ctrg, idx), val) |
| 172 | |
| 173 | #define vlr_stat_item_inc(vlr, idx) \ |
| 174 | osmo_stat_item_inc(osmo_stat_item_group_get_item((vlr)->statg, idx), 1) |
| 175 | #define vlr_stat_item_dec(vlr, idx) \ |
| 176 | osmo_stat_item_dec(osmo_stat_item_group_get_item((vlr)->statg, idx), 1) |
| 177 | #define vlr_stat_item_set(vlr, idx, val) \ |
| 178 | osmo_stat_item_set(osmo_stat_item_group_get_item((vlr)->statg, idx), val) |
| 179 | |
| 180 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 181 | /*********************************************************************** |
| 182 | * Convenience functions |
| 183 | ***********************************************************************/ |
| 184 | |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 185 | static int vlr_subscr_detach(struct vlr_subscr *vsub); |
| 186 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 187 | const struct value_string vlr_ciph_names[] = { |
| 188 | OSMO_VALUE_STRING(VLR_CIPH_NONE), |
| 189 | OSMO_VALUE_STRING(VLR_CIPH_A5_1), |
| 190 | OSMO_VALUE_STRING(VLR_CIPH_A5_2), |
| 191 | OSMO_VALUE_STRING(VLR_CIPH_A5_3), |
| 192 | { 0, NULL } |
| 193 | }; |
| 194 | |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 195 | /* 3GPP TS 24.008, table 11.2 Mobility management timers (network-side) */ |
| 196 | struct osmo_tdef msc_tdefs_vlr[] = { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 197 | { .T = 3212, .default_val = 60, .unit = OSMO_TDEF_M, .desc = "Subscriber expiration timeout" }, |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 198 | { .T = 3250, .default_val = 12, .desc = "TMSI Reallocation procedure" }, |
| 199 | { .T = 3260, .default_val = 12, .desc = "Authentication procedure" }, |
| 200 | { .T = 3270, .default_val = 12, .desc = "Identification procedure" }, |
| 201 | { /* terminator */ } |
| 202 | }; |
| 203 | |
| 204 | /* This is just a wrapper around the osmo_tdef API. |
| 205 | * TODO: we should start using osmo_tdef_fsm_inst_state_chg() */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 206 | uint32_t vlr_timer(struct vlr_instance *vlr, uint32_t timer) |
| 207 | { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 208 | /* NOTE: since we usually do not need more than one instance of the VLR, |
| 209 | * and since libosmocore's osmo_tdef API does not (yet) support dynamic |
| 210 | * configuration, we always use the global instance of msc_tdefs_vlr. */ |
Vadim Yanitskiy | baf71a7 | 2020-01-25 10:49:14 +0700 | [diff] [blame] | 211 | return osmo_tdef_get(msc_tdefs_vlr, timer, OSMO_TDEF_S, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 214 | /* return static buffer with printable name of VLR subscriber */ |
Oliver Smith | 5598aae | 2019-01-08 11:47:21 +0100 | [diff] [blame] | 215 | const char *vlr_subscr_name(const struct vlr_subscr *vsub) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 216 | { |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 217 | static char buf[128]; |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 218 | struct osmo_strbuf sb = { .buf = buf, .len = sizeof(buf) }; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 219 | bool present = false; |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 220 | if (!vsub) |
| 221 | return "unknown"; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 222 | if (vsub->imsi[0]) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 223 | OSMO_STRBUF_PRINTF(sb, "IMSI-%s", vsub->imsi); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 224 | present = true; |
| 225 | } |
| 226 | if (vsub->msisdn[0]) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 227 | OSMO_STRBUF_PRINTF(sb, "%sMSISDN-%s", present? ":" : "", vsub->msisdn); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 228 | present = true; |
| 229 | } |
| 230 | if (vsub->tmsi != GSM_RESERVED_TMSI) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 231 | OSMO_STRBUF_PRINTF(sb, "%sTMSI-0x%08X", present? ":" : "", vsub->tmsi); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 232 | present = true; |
| 233 | } |
| 234 | if (vsub->tmsi_new != GSM_RESERVED_TMSI) { |
Neels Hofmeyr | 83e311f | 2019-03-19 16:39:10 +0100 | [diff] [blame] | 235 | OSMO_STRBUF_PRINTF(sb, "%sTMSInew-0x%08X", present? ":" : "", vsub->tmsi_new); |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 236 | present = true; |
| 237 | } |
| 238 | if (!present) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 239 | return "unknown"; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 240 | |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 241 | return buf; |
| 242 | } |
| 243 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 244 | const char *vlr_subscr_short_name(const struct vlr_subscr *vsub, unsigned int maxlen) |
| 245 | { |
| 246 | /* cast away the const so we can shorten the string within the static buffer */ |
| 247 | char *name = (char*)vlr_subscr_name(vsub); |
| 248 | size_t len = strlen(name); |
| 249 | if (maxlen < 2) |
| 250 | return "-"; |
| 251 | if (len > maxlen) |
| 252 | strcpy(name + maxlen - 2, ".."); |
| 253 | return name; |
| 254 | } |
| 255 | |
Oliver Smith | 5598aae | 2019-01-08 11:47:21 +0100 | [diff] [blame] | 256 | const char *vlr_subscr_msisdn_or_name(const struct vlr_subscr *vsub) |
Neels Hofmeyr | 7a2f58e | 2018-03-22 16:03:49 +0100 | [diff] [blame] | 257 | { |
| 258 | if (!vsub || !vsub->msisdn[0]) |
| 259 | return vlr_subscr_name(vsub); |
| 260 | return vsub->msisdn; |
| 261 | } |
| 262 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 263 | struct vlr_subscr *_vlr_subscr_find_by_imsi(struct vlr_instance *vlr, |
| 264 | const char *imsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 265 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 266 | const char *file, int line) |
| 267 | { |
| 268 | struct vlr_subscr *vsub; |
| 269 | |
| 270 | if (!imsi || !*imsi) |
| 271 | return NULL; |
| 272 | |
| 273 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 274 | if (vlr_subscr_matches_imsi(vsub, imsi)) { |
Neels Hofmeyr | 2fd69e1 | 2024-03-26 00:50:04 +0100 | [diff] [blame] | 275 | if (use) |
| 276 | vlr_subscr_get_src(vsub, use, file, line); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 277 | return vsub; |
| 278 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 279 | } |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | struct vlr_subscr *_vlr_subscr_find_by_tmsi(struct vlr_instance *vlr, |
| 284 | uint32_t tmsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 285 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 286 | const char *file, int line) |
| 287 | { |
| 288 | struct vlr_subscr *vsub; |
| 289 | |
| 290 | if (tmsi == GSM_RESERVED_TMSI) |
| 291 | return NULL; |
| 292 | |
| 293 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 294 | if (vlr_subscr_matches_tmsi(vsub, tmsi)) { |
| 295 | vlr_subscr_get_src(vsub, use, file, line); |
| 296 | return vsub; |
| 297 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 298 | } |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | struct vlr_subscr *_vlr_subscr_find_by_msisdn(struct vlr_instance *vlr, |
| 303 | const char *msisdn, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 304 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 305 | const char *file, int line) |
| 306 | { |
| 307 | struct vlr_subscr *vsub; |
| 308 | |
| 309 | if (!msisdn || !*msisdn) |
| 310 | return NULL; |
| 311 | |
| 312 | llist_for_each_entry(vsub, &vlr->subscribers, list) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 313 | if (vlr_subscr_matches_msisdn(vsub, msisdn)) { |
| 314 | vlr_subscr_get_src(vsub, use, file, line); |
| 315 | return vsub; |
| 316 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 317 | } |
| 318 | return NULL; |
| 319 | } |
| 320 | |
Neels Hofmeyr | 5bdba0d | 2021-07-27 03:46:18 +0200 | [diff] [blame] | 321 | struct vlr_subscr *_vlr_subscr_find_by_mi(struct vlr_instance *vlr, |
| 322 | const struct osmo_mobile_identity *mi, |
| 323 | const char *use, |
| 324 | const char *file, int line) |
| 325 | { |
| 326 | switch (mi->type) { |
| 327 | case GSM_MI_TYPE_IMSI: |
| 328 | return _vlr_subscr_find_by_imsi(vlr, mi->imsi, use, file, line); |
| 329 | case GSM_MI_TYPE_TMSI: |
| 330 | return _vlr_subscr_find_by_tmsi(vlr, mi->tmsi, use, file, line); |
| 331 | default: |
| 332 | return NULL; |
| 333 | } |
| 334 | } |
| 335 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 336 | /* Transmit GSUP message for subscriber to HLR, using IMSI from subscriber */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 337 | static int vlr_subscr_tx_gsup_message(const struct vlr_subscr *vsub, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 338 | struct osmo_gsup_message *gsup_msg) |
| 339 | { |
| 340 | struct vlr_instance *vlr = vsub->vlr; |
| 341 | |
| 342 | if (strlen(gsup_msg->imsi) == 0) |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 343 | OSMO_STRLCPY_ARRAY(gsup_msg->imsi, vsub->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 344 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 345 | gsup_msg->message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 346 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 347 | return gsup_client_mux_tx(vlr->gcm, gsup_msg); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 350 | static int vlr_subscr_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 351 | { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 352 | struct vlr_subscr *vsub = e->use_count->talloc_object; |
| 353 | char buf[128]; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 354 | int32_t total; |
| 355 | int level; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 356 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 357 | if (!e->use) |
| 358 | return -EINVAL; |
| 359 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 360 | total = osmo_use_count_total(&vsub->use_count); |
| 361 | |
| 362 | if (total == 0 |
| 363 | || (total == 1 && old_use_count == 0 && e->count == 1)) |
| 364 | level = LOGL_INFO; |
| 365 | else |
| 366 | level = LOGL_DEBUG; |
| 367 | |
| 368 | LOGPSRC(DREF, level, file, line, "VLR subscr %s %s %s: now used by %s\n", |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 369 | vlr_subscr_name(vsub), (e->count - old_use_count) > 0? "+" : "-", e->use, |
| 370 | osmo_use_count_name_buf(buf, sizeof(buf), e->use_count)); |
| 371 | |
| 372 | if (e->count < 0) |
| 373 | return -ERANGE; |
| 374 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 375 | vsub->max_total_use_count = OSMO_MAX(vsub->max_total_use_count, total); |
| 376 | |
| 377 | if (total <= 0) |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 378 | vlr_subscr_free(vsub); |
| 379 | return 0; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* Allocate a new subscriber and insert it into list */ |
| 383 | static struct vlr_subscr *_vlr_subscr_alloc(struct vlr_instance *vlr) |
| 384 | { |
| 385 | struct vlr_subscr *vsub; |
| 386 | int i; |
| 387 | |
| 388 | vsub = talloc_zero(vlr, struct vlr_subscr); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 389 | *vsub = (struct vlr_subscr){ |
| 390 | .vlr = vlr, |
| 391 | .tmsi = GSM_RESERVED_TMSI, |
| 392 | .tmsi_new = GSM_RESERVED_TMSI, |
| 393 | .use_count = (struct osmo_use_count){ |
| 394 | .talloc_object = vsub, |
| 395 | .use_cb = vlr_subscr_use_cb, |
| 396 | }, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 397 | .expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 398 | }; |
| 399 | osmo_use_count_make_static_entries(&vsub->use_count, vsub->use_count_buf, ARRAY_SIZE(vsub->use_count_buf)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 400 | |
| 401 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
Neels Hofmeyr | 8b6e536 | 2018-11-30 02:57:33 +0100 | [diff] [blame] | 402 | vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 403 | |
| 404 | INIT_LLIST_HEAD(&vsub->cs.requests); |
| 405 | INIT_LLIST_HEAD(&vsub->ps.pdp_list); |
| 406 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 407 | /* Create an SGs FSM, which is needed to control CSFB, |
| 408 | * in cases where CSFB/SGs is not in use, this FSM will |
| 409 | * just do nothing. (see also: sgs_iface.c) */ |
| 410 | vlr_sgs_fsm_create(vsub); |
| 411 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 412 | llist_add_tail(&vsub->list, &vlr->subscribers); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 413 | vlr_stat_item_inc(vlr, VLR_STAT_SUBSCRIBER_COUNT); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 414 | return vsub; |
| 415 | } |
| 416 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 417 | /* Send a GSUP Purge MS request. |
| 418 | * TODO: this should be sent to the *previous* VLR when this VLR is "taking" |
| 419 | * this subscriber, not to the HLR? */ |
| 420 | int vlr_subscr_purge(struct vlr_subscr *vsub) |
| 421 | { |
| 422 | struct osmo_gsup_message gsup_msg = {0}; |
| 423 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 424 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_PURGE_MS_REQ); |
| 425 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 426 | gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST; |
| 427 | |
| 428 | /* provide HLR number in case we know it */ |
| 429 | gsup_msg.hlr_enc_len = vsub->hlr.len; |
| 430 | gsup_msg.hlr_enc = vsub->hlr.buf; |
| 431 | |
| 432 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 433 | } |
| 434 | |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 435 | void vlr_subscr_cancel_attach_fsm(struct vlr_subscr *vsub, |
| 436 | enum osmo_fsm_term_cause fsm_cause, |
| 437 | uint8_t gsm48_cause) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 438 | { |
| 439 | if (!vsub) |
| 440 | return; |
| 441 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 442 | vlr_subscr_get(vsub, __func__); |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 443 | if (vsub->lu_fsm) |
| 444 | vlr_loc_update_cancel(vsub->lu_fsm, fsm_cause, gsm48_cause); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 445 | if (vsub->proc_arq_fsm) |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 446 | vlr_parq_cancel(vsub->proc_arq_fsm, fsm_cause, gsm48_cause); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 447 | vlr_subscr_put(vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Call vlr_subscr_cancel(), then completely drop the entry from the VLR */ |
| 451 | void vlr_subscr_free(struct vlr_subscr *vsub) |
| 452 | { |
| 453 | llist_del(&vsub->list); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 454 | vlr_stat_item_dec(vsub->vlr, VLR_STAT_SUBSCRIBER_COUNT); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 455 | DEBUGP(DVLR, "freeing VLR subscr %s (max total use count was %d)\n", vlr_subscr_name(vsub), |
| 456 | vsub->max_total_use_count); |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 457 | |
| 458 | /* Make sure SGs timer Ts5 is removed */ |
| 459 | osmo_timer_del(&vsub->sgs.Ts5); |
| 460 | |
| 461 | /* Remove SGs FSM (see also: sgs_iface.c) */ |
| 462 | vlr_sgs_fsm_remove(vsub); |
| 463 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 464 | talloc_free(vsub); |
| 465 | } |
| 466 | |
| 467 | /* Generate a new TMSI and store in vsub->tmsi_new. |
| 468 | * Search all known subscribers to ensure that the TMSI is unique. */ |
| 469 | int vlr_subscr_alloc_tmsi(struct vlr_subscr *vsub) |
| 470 | { |
| 471 | struct vlr_instance *vlr = vsub->vlr; |
| 472 | uint32_t tmsi; |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 473 | int tried, rc; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 474 | struct vlr_subscr *other_vsub; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 475 | |
| 476 | for (tried = 0; tried < 100; tried++) { |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 477 | rc = osmo_get_rand_id((uint8_t *) &tmsi, sizeof(tmsi)); |
| 478 | if (rc < 0) { |
| 479 | LOGP(DDB, LOGL_ERROR, "osmo_get_rand_id() failed: %s\n", strerror(-rc)); |
| 480 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 481 | } |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 482 | |
| 483 | if (!llist_empty(&vlr->cfg.nri_ranges->entries)) { |
| 484 | int16_t nri_v; |
| 485 | osmo_tmsi_nri_v_limit_by_ranges(&tmsi, vlr->cfg.nri_ranges, vlr->cfg.nri_bitlen); |
| 486 | osmo_tmsi_nri_v_get(&nri_v, tmsi, vlr->cfg.nri_bitlen); |
| 487 | LOGP(DVLR, LOGL_DEBUG, "New NRI from range [%s] = 0x%x --> TMSI 0x%08x\n", |
| 488 | osmo_nri_ranges_to_str_c(OTC_SELECT, vlr->cfg.nri_ranges), nri_v, tmsi); |
| 489 | } |
| 490 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 491 | /* throw the dice again, if the TSMI doesn't fit */ |
| 492 | if (tmsi == GSM_RESERVED_TMSI) |
| 493 | continue; |
| 494 | |
| 495 | /* Section 2.4 of 23.003: MSC has two MSB 00/01/10, SGSN 11 */ |
| 496 | if (vlr->cfg.is_ps) { |
| 497 | /* SGSN */ |
Eric Wild | 58abc67 | 2019-06-14 16:11:08 +0200 | [diff] [blame] | 498 | tmsi |= GSM23003_TMSI_SGSN_MASK; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 499 | } else { |
| 500 | /* MSC */ |
Eric Wild | 58abc67 | 2019-06-14 16:11:08 +0200 | [diff] [blame] | 501 | if ((tmsi & GSM23003_TMSI_SGSN_MASK) == GSM23003_TMSI_SGSN_MASK) |
| 502 | tmsi &= ~GSM23003_TMSI_SGSN_MASK; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* If this TMSI is already in use, try another one. */ |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 506 | if ((other_vsub = vlr_subscr_find_by_tmsi(vlr, tmsi, __func__))) { |
| 507 | vlr_subscr_put(other_vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 508 | continue; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 509 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 510 | |
| 511 | vsub->tmsi_new = tmsi; |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 512 | vsub->vlr->ops.subscr_update(vsub); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | LOGP(DVLR, LOGL_ERROR, "subscr %s: unable to generate valid TMSI" |
| 517 | " after %d tries\n", vlr_subscr_name(vsub), tried); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* Find subscriber by IMSI, or create new subscriber if not found. |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 522 | * \param[in] vlr VLR instance. |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 523 | * \param[in] imsi IMSI string. |
| 524 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 525 | struct vlr_subscr *_vlr_subscr_find_or_create_by_imsi(struct vlr_instance *vlr, |
| 526 | const char *imsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 527 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 528 | bool *created, |
| 529 | const char *file, |
| 530 | int line) |
| 531 | { |
| 532 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 533 | vsub = _vlr_subscr_find_by_imsi(vlr, imsi, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 534 | if (vsub) { |
| 535 | if (created) |
| 536 | *created = false; |
| 537 | return vsub; |
| 538 | } |
| 539 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 540 | vsub = _vlr_subscr_alloc(vlr); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 541 | if (!vsub) |
| 542 | return NULL; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 543 | vlr_subscr_get_src(vsub, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 544 | vlr_subscr_set_imsi(vsub, imsi); |
| 545 | LOGP(DVLR, LOGL_INFO, "New subscr, IMSI: %s\n", vsub->imsi); |
| 546 | if (created) |
| 547 | *created = true; |
| 548 | return vsub; |
| 549 | } |
| 550 | |
| 551 | /* Find subscriber by TMSI, or create new subscriber if not found. |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 552 | * \param[in] vlr VLR instance. |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 553 | * \param[in] tmsi TMSI. |
| 554 | * \param[out] created if non-NULL, returns whether a new entry was created. */ |
| 555 | struct vlr_subscr *_vlr_subscr_find_or_create_by_tmsi(struct vlr_instance *vlr, |
| 556 | uint32_t tmsi, |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 557 | const char *use, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 558 | bool *created, |
| 559 | const char *file, |
| 560 | int line) |
| 561 | { |
| 562 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 563 | vsub = _vlr_subscr_find_by_tmsi(vlr, tmsi, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 564 | if (vsub) { |
| 565 | if (created) |
| 566 | *created = false; |
| 567 | return vsub; |
| 568 | } |
| 569 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 570 | vsub = _vlr_subscr_alloc(vlr); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 571 | if (!vsub) |
| 572 | return NULL; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 573 | vlr_subscr_get_src(vsub, use, file, line); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 574 | vsub->tmsi = tmsi; |
| 575 | LOGP(DVLR, LOGL_INFO, "New subscr, TMSI: 0x%08x\n", vsub->tmsi); |
| 576 | if (created) |
| 577 | *created = true; |
| 578 | return vsub; |
| 579 | } |
| 580 | |
Neels Hofmeyr | 2fd69e1 | 2024-03-26 00:50:04 +0100 | [diff] [blame] | 581 | static void dedup_vsub(struct vlr_subscr *exists, struct vlr_subscr *vsub) |
| 582 | { |
| 583 | struct vlr_instance *vlr = exists->vlr; |
| 584 | int i; |
| 585 | int j; |
| 586 | LOGP(DVLR, LOGL_NOTICE, |
| 587 | "There is an existing subscriber for IMSI %s used by %s, replacing with new VLR subscr: %s used by %s\n", |
| 588 | exists->imsi, osmo_use_count_to_str_c(OTC_SELECT, &exists->use_count), |
| 589 | vlr_subscr_name(vsub), |
| 590 | osmo_use_count_to_str_c(OTC_SELECT, &vsub->use_count)); |
| 591 | |
| 592 | /* Take over some state from the previous vsub */ |
| 593 | paging_request_join_vsub(vsub, exists); |
| 594 | if (!vsub->msisdn[0]) |
| 595 | OSMO_STRLCPY_ARRAY(vsub->msisdn, exists->msisdn); |
| 596 | if (!vsub->name[0]) |
| 597 | OSMO_STRLCPY_ARRAY(vsub->name, exists->name); |
| 598 | /* Copy valid auth tuples we may already have, to reduce the need to ask for new ones from the HLR */ |
| 599 | for (i = 0; i < ARRAY_SIZE(exists->auth_tuples); i++) { |
| 600 | if (exists->auth_tuples[i].key_seq == VLR_KEY_SEQ_INVAL) |
| 601 | continue; |
| 602 | for (j = 0; j < ARRAY_SIZE(vsub->auth_tuples); j++) { |
| 603 | if (vsub->auth_tuples[j].key_seq != VLR_KEY_SEQ_INVAL) |
| 604 | continue; |
| 605 | vsub->auth_tuples[j] = exists->auth_tuples[i]; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (exists->msc_conn_ref) |
| 610 | LOGVSUBP(LOGL_ERROR, vsub, |
| 611 | "There is an existing VLR entry for this same subscriber with an active connection." |
| 612 | " That should not be possible. Discarding old subscriber entry %s.\n", |
| 613 | exists->imsi); |
| 614 | |
| 615 | if (vlr->ops.subscr_inval) |
| 616 | vlr->ops.subscr_inval(exists->msc_conn_ref, exists); |
| 617 | vlr_subscr_free(exists); |
| 618 | } |
| 619 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 620 | void vlr_subscr_set_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 621 | { |
Neels Hofmeyr | 2fd69e1 | 2024-03-26 00:50:04 +0100 | [diff] [blame] | 622 | struct vlr_subscr *exists; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 623 | if (!vsub) |
| 624 | return; |
Stefan Sperling | 9fbb600 | 2018-06-25 17:31:59 +0200 | [diff] [blame] | 625 | |
Neels Hofmeyr | 2fd69e1 | 2024-03-26 00:50:04 +0100 | [diff] [blame] | 626 | /* If the same IMSI is already set, nothing changes. */ |
| 627 | if (!strcmp(vsub->imsi, imsi)) |
| 628 | return; |
| 629 | |
| 630 | /* We've just learned about this new IMSI, our primary key in the VLR. make sure to invalidate any prior VLR |
| 631 | * entries for this IMSI. */ |
| 632 | exists = vlr_subscr_find_by_imsi(vsub->vlr, imsi, NULL); |
| 633 | |
| 634 | if (exists) |
| 635 | dedup_vsub(exists, vsub); |
| 636 | |
| 637 | /* Set the IMSI on the new subscriber, here. */ |
Stefan Sperling | 9fbb600 | 2018-06-25 17:31:59 +0200 | [diff] [blame] | 638 | if (OSMO_STRLCPY_ARRAY(vsub->imsi, imsi) >= sizeof(vsub->imsi)) { |
| 639 | LOGP(DVLR, LOGL_NOTICE, "IMSI was truncated: full IMSI=%s, truncated IMSI=%s\n", |
| 640 | imsi, vsub->imsi); |
| 641 | /* XXX Set truncated IMSI anyway, we currently cannot return an error from here. */ |
| 642 | } |
| 643 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 644 | vsub->id = atoll(vsub->imsi); |
| 645 | DEBUGP(DVLR, "set IMSI on subscriber; IMSI=%s id=%llu\n", |
| 646 | vsub->imsi, vsub->id); |
| 647 | } |
| 648 | |
| 649 | void vlr_subscr_set_imei(struct vlr_subscr *vsub, const char *imei) |
| 650 | { |
| 651 | if (!vsub) |
| 652 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 653 | OSMO_STRLCPY_ARRAY(vsub->imei, imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 654 | DEBUGP(DVLR, "set IMEI on subscriber; IMSI=%s IMEI=%s\n", |
| 655 | vsub->imsi, vsub->imei); |
| 656 | } |
| 657 | |
| 658 | void vlr_subscr_set_imeisv(struct vlr_subscr *vsub, const char *imeisv) |
| 659 | { |
| 660 | if (!vsub) |
| 661 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 662 | OSMO_STRLCPY_ARRAY(vsub->imeisv, imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 663 | DEBUGP(DVLR, "set IMEISV on subscriber; IMSI=%s IMEISV=%s\n", |
| 664 | vsub->imsi, vsub->imeisv); |
Oliver Smith | b8077b0 | 2019-05-07 14:13:55 +0200 | [diff] [blame] | 665 | |
| 666 | /* Copy IMEISV to IMEI (additional SV digits get cut off) */ |
| 667 | vlr_subscr_set_imei(vsub, imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /* Safely copy the given MSISDN string to vsub->msisdn */ |
| 671 | void vlr_subscr_set_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 672 | { |
| 673 | if (!vsub) |
| 674 | return; |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 675 | OSMO_STRLCPY_ARRAY(vsub->msisdn, msisdn); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 676 | DEBUGP(DVLR, "set MSISDN on subscriber; IMSI=%s MSISDN=%s\n", |
| 677 | vsub->imsi, vsub->msisdn); |
| 678 | } |
| 679 | |
Pau Espin Pedrol | 6710670 | 2021-04-27 18:20:15 +0200 | [diff] [blame] | 680 | void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub, |
| 681 | const struct osmo_plmn_id *last_eutran_plmn) |
| 682 | { |
| 683 | if (!vsub) |
| 684 | return; |
| 685 | if (last_eutran_plmn) { |
| 686 | vsub->sgs.last_eutran_plmn_present = true; |
| 687 | memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn)); |
| 688 | } else { |
| 689 | vsub->sgs.last_eutran_plmn_present = false; |
| 690 | } |
| 691 | DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n", |
| 692 | vsub->sgs.last_eutran_plmn_present ? |
| 693 | osmo_plmn_name(&vsub->sgs.last_eutran_plmn) : |
| 694 | "(none)"); |
| 695 | } |
| 696 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 697 | bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi) |
| 698 | { |
| 699 | return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi); |
| 700 | } |
| 701 | |
| 702 | bool vlr_subscr_matches_tmsi(struct vlr_subscr *vsub, uint32_t tmsi) |
| 703 | { |
| 704 | return vsub && tmsi != GSM_RESERVED_TMSI |
| 705 | && (vsub->tmsi == tmsi || vsub->tmsi_new == tmsi); |
| 706 | } |
| 707 | |
| 708 | bool vlr_subscr_matches_msisdn(struct vlr_subscr *vsub, const char *msisdn) |
| 709 | { |
| 710 | return vsub && msisdn && vsub->msisdn[0] |
| 711 | && !strcmp(vsub->msisdn, msisdn); |
| 712 | } |
| 713 | |
| 714 | bool vlr_subscr_matches_imei(struct vlr_subscr *vsub, const char *imei) |
| 715 | { |
| 716 | return vsub && imei && vsub->imei[0] |
| 717 | && !strcmp(vsub->imei, imei); |
| 718 | } |
| 719 | |
| 720 | /* Send updated subscriber information to HLR */ |
| 721 | int vlr_subscr_changed(struct vlr_subscr *vsub) |
| 722 | { |
| 723 | /* FIXME */ |
| 724 | LOGP(DVLR, LOGL_ERROR, "Not implemented: %s\n", __func__); |
| 725 | return 0; |
| 726 | } |
| 727 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 728 | void vlr_subscr_enable_expire_lu(struct vlr_subscr *vsub) |
| 729 | { |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 730 | struct timespec now; |
| 731 | |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 732 | /* Mark the subscriber as inactive if it stopped to do periodical location updates. */ |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 733 | if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) == 0) { |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 734 | vsub->expire_lu = now.tv_sec + vlr_timer(vsub->vlr, 3212); |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 735 | } else { |
| 736 | LOGP(DVLR, LOGL_ERROR, |
| 737 | "%s: Could not enable Location Update expiry: unable to read current time\n", vlr_subscr_name(vsub)); |
| 738 | /* Disable LU expiry for this subscriber. This subscriber will only be freed after an explicit IMSI detach. */ |
| 739 | vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void vlr_subscr_expire_lu(void *data) |
| 744 | { |
| 745 | struct vlr_instance *vlr = data; |
| 746 | struct vlr_subscr *vsub, *vsub_tmp; |
| 747 | struct timespec now; |
| 748 | |
Vadim Yanitskiy | 718f32f | 2019-06-19 03:13:40 +0700 | [diff] [blame] | 749 | /* Periodic location update might be disabled from the VTY, |
| 750 | * so we shall not expire subscribers until explicit IMSI Detach. */ |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 751 | if (!vlr_timer(vlr, 3212)) |
Vadim Yanitskiy | 718f32f | 2019-06-19 03:13:40 +0700 | [diff] [blame] | 752 | goto done; |
| 753 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 754 | if (llist_empty(&vlr->subscribers)) |
| 755 | goto done; |
| 756 | |
| 757 | if (osmo_clock_gettime(CLOCK_MONOTONIC, &now) != 0) { |
| 758 | LOGP(DVLR, LOGL_ERROR, "Skipping Location Update expiry: Could not read current time\n"); |
| 759 | goto done; |
| 760 | } |
| 761 | |
| 762 | llist_for_each_entry_safe(vsub, vsub_tmp, &vlr->subscribers, list) { |
| 763 | if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION || vsub->expire_lu > now.tv_sec) |
| 764 | continue; |
| 765 | |
| 766 | LOGP(DVLR, LOGL_DEBUG, "%s: Location Update expired\n", vlr_subscr_name(vsub)); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 767 | vlr_rate_ctr_inc(vlr, VLR_CTR_DETACH_BY_T3212); |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 768 | vlr_subscr_detach(vsub); |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | done: |
| 772 | osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0); |
| 773 | } |
| 774 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 775 | /*********************************************************************** |
| 776 | * PDP context data |
| 777 | ***********************************************************************/ |
| 778 | |
Neels Hofmeyr | bac2276 | 2017-07-06 18:39:28 +0200 | [diff] [blame] | 779 | #define GSM_APN_LENGTH 102 |
| 780 | |
| 781 | /* see GSM 09.02, 17.7.1, PDP-Context and GPRSSubscriptionData */ |
| 782 | /* see GSM 09.02, B.1, gprsSubscriptionData */ |
| 783 | struct sgsn_subscriber_pdp_data { |
Pau Espin Pedrol | a456583 | 2024-01-19 21:09:27 +0100 | [diff] [blame] | 784 | struct llist_head list; |
Neels Hofmeyr | bac2276 | 2017-07-06 18:39:28 +0200 | [diff] [blame] | 785 | |
Pau Espin Pedrol | a456583 | 2024-01-19 21:09:27 +0100 | [diff] [blame] | 786 | unsigned int context_id; |
Pau Espin Pedrol | 6f2749c | 2024-01-19 21:10:12 +0100 | [diff] [blame] | 787 | enum gsm48_pdp_type_org pdp_type_org; |
| 788 | enum gsm48_pdp_type_nr pdp_type_nr; |
| 789 | struct osmo_sockaddr pdp_address[2]; |
Pau Espin Pedrol | a456583 | 2024-01-19 21:09:27 +0100 | [diff] [blame] | 790 | char apn_str[GSM_APN_LENGTH]; |
| 791 | uint8_t qos_subscribed[20]; |
| 792 | size_t qos_subscribed_len; |
Neels Hofmeyr | bac2276 | 2017-07-06 18:39:28 +0200 | [diff] [blame] | 793 | }; |
| 794 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 795 | struct sgsn_subscriber_pdp_data * |
| 796 | vlr_subscr_pdp_data_alloc(struct vlr_subscr *vsub) |
| 797 | { |
| 798 | struct sgsn_subscriber_pdp_data* pdata; |
| 799 | |
| 800 | pdata = talloc_zero(vsub, struct sgsn_subscriber_pdp_data); |
| 801 | |
| 802 | llist_add_tail(&pdata->list, &vsub->ps.pdp_list); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 803 | vlr_stat_item_inc(vsub->vlr, VLR_STAT_PDP_COUNT); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 804 | |
| 805 | return pdata; |
| 806 | } |
| 807 | |
| 808 | static int vlr_subscr_pdp_data_clear(struct vlr_subscr *vsub) |
| 809 | { |
| 810 | struct sgsn_subscriber_pdp_data *pdp, *pdp2; |
| 811 | int count = 0; |
| 812 | |
| 813 | llist_for_each_entry_safe(pdp, pdp2, &vsub->ps.pdp_list, list) { |
| 814 | llist_del(&pdp->list); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 815 | vlr_stat_item_dec(vsub->vlr, VLR_STAT_PDP_COUNT); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 816 | talloc_free(pdp); |
| 817 | count += 1; |
| 818 | } |
| 819 | |
| 820 | return count; |
| 821 | } |
| 822 | |
| 823 | static struct sgsn_subscriber_pdp_data * |
| 824 | vlr_subscr_pdp_data_get_by_id(struct vlr_subscr *vsub, unsigned context_id) |
| 825 | { |
| 826 | struct sgsn_subscriber_pdp_data *pdp; |
| 827 | |
| 828 | llist_for_each_entry(pdp, &vsub->ps.pdp_list, list) { |
| 829 | if (pdp->context_id == context_id) |
| 830 | return pdp; |
| 831 | } |
| 832 | |
| 833 | return NULL; |
| 834 | } |
| 835 | |
| 836 | /*********************************************************************** |
| 837 | * Actual Implementation |
| 838 | ***********************************************************************/ |
| 839 | |
| 840 | static int vlr_rx_gsup_unknown_imsi(struct vlr_instance *vlr, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 841 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 842 | { |
| 843 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 844 | LOGP(DVLR, LOGL_NOTICE, |
| 845 | "Unknown IMSI %s, discarding GSUP request " |
| 846 | "of type 0x%02x\n", |
| 847 | gsup_msg->imsi, gsup_msg->message_type); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 848 | gsup_client_mux_tx_error_reply(vlr->gcm, gsup_msg, GMM_CAUSE_IMSI_UNKNOWN); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 849 | } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 850 | LOGP(DVLR, LOGL_NOTICE, |
| 851 | "Unknown IMSI %s, discarding GSUP error " |
| 852 | "of type 0x%02x, cause '%s' (%d)\n", |
| 853 | gsup_msg->imsi, gsup_msg->message_type, |
| 854 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 855 | gsup_msg->cause); |
| 856 | } else { |
| 857 | LOGP(DVLR, LOGL_NOTICE, |
| 858 | "Unknown IMSI %s, discarding GSUP response " |
| 859 | "of type 0x%02x\n", |
| 860 | gsup_msg->imsi, gsup_msg->message_type); |
| 861 | } |
| 862 | |
| 863 | return -GMM_CAUSE_IMSI_UNKNOWN; |
| 864 | } |
| 865 | |
| 866 | static int vlr_rx_gsup_purge_no_subscr(struct vlr_instance *vlr, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 867 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 868 | { |
| 869 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
| 870 | LOGGSUPP(LOGL_NOTICE, gsup_msg, |
| 871 | "Purge MS has failed with cause '%s' (%d)\n", |
| 872 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 873 | gsup_msg->cause); |
| 874 | return -gsup_msg->cause; |
| 875 | } |
| 876 | LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n"); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | /* VLR internal call to request UpdateLocation from HLR */ |
Philipp Maier | 6038ad4 | 2018-11-13 13:55:09 +0100 | [diff] [blame] | 881 | int vlr_subscr_req_lu(struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 882 | { |
| 883 | struct osmo_gsup_message gsup_msg = {0}; |
| 884 | int rc; |
| 885 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 886 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_UL_REQ); |
| 887 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 888 | gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; |
Neels Hofmeyr | d0756b1 | 2018-09-28 02:41:39 +0200 | [diff] [blame] | 889 | gsup_msg.cn_domain = vsub->vlr->cfg.is_ps ? OSMO_GSUP_CN_DOMAIN_PS : OSMO_GSUP_CN_DOMAIN_CS; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 890 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 891 | |
| 892 | return rc; |
| 893 | } |
| 894 | |
| 895 | /* VLR internal call to request tuples from HLR */ |
| 896 | int vlr_subscr_req_sai(struct vlr_subscr *vsub, |
| 897 | const uint8_t *auts, const uint8_t *auts_rand) |
| 898 | { |
| 899 | struct osmo_gsup_message gsup_msg = {0}; |
| 900 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 901 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_SAI_REQ); |
| 902 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 903 | gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; |
| 904 | gsup_msg.auts = auts; |
| 905 | gsup_msg.rand = auts_rand; |
Neels Hofmeyr | 63b2464 | 2019-12-12 01:31:04 +0100 | [diff] [blame] | 906 | gsup_msg.cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 907 | |
| 908 | return vlr_subscr_tx_gsup_message(vsub, &gsup_msg); |
| 909 | } |
| 910 | |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 911 | /* Initiate Check_IMEI_VLR Procedure (23.018 Chapter 7.1.2.9) */ |
| 912 | int vlr_subscr_tx_req_check_imei(const struct vlr_subscr *vsub) |
| 913 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 914 | struct osmo_gsup_message gsup_msg = { |
| 915 | .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT, |
Vadim Yanitskiy | ed73ae1 | 2019-08-15 22:51:20 +0200 | [diff] [blame] | 916 | .message_type = OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 917 | }; |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 918 | uint8_t imei_enc[GSM23003_IMEI_NUM_DIGITS+2]; /* +2: IE header */ |
| 919 | int len; |
| 920 | |
| 921 | /* Encode IMEI */ |
| 922 | len = gsm48_encode_bcd_number(imei_enc, sizeof(imei_enc), 0, vsub->imei); |
| 923 | if (len < 1) { |
| 924 | LOGVSUBP(LOGL_ERROR, vsub, "Error: cannot encode IMEI '%s'\n", vsub->imei); |
| 925 | return -ENOSPC; |
| 926 | } |
| 927 | gsup_msg.imei_enc = imei_enc; |
| 928 | gsup_msg.imei_enc_len = len; |
| 929 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 930 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CHECK_IMEI_REQ); |
| 931 | |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 932 | /* Send CHECK_IMEI_REQUEST */ |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 933 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 934 | return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg); |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 937 | /* Tell HLR that authentication failure occurred */ |
Max | 923a239 | 2018-01-24 13:55:03 +0100 | [diff] [blame] | 938 | int vlr_subscr_tx_auth_fail_rep(const struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 939 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 940 | struct osmo_gsup_message gsup_msg = { |
| 941 | .message_class = OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT, |
Vadim Yanitskiy | ed73ae1 | 2019-08-15 22:51:20 +0200 | [diff] [blame] | 942 | .message_type = OSMO_GSUP_MSGT_AUTH_FAIL_REPORT, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 943 | }; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 944 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 945 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_AUTH_FAIL_REP); |
| 946 | |
Max | 98f7467 | 2018-02-05 12:57:06 +0100 | [diff] [blame] | 947 | OSMO_STRLCPY_ARRAY(gsup_msg.imsi, vsub->imsi); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 948 | return gsup_client_mux_tx(vsub->vlr->gcm, &gsup_msg); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /* Update the subscriber with GSUP-received auth tuples */ |
| 952 | void vlr_subscr_update_tuples(struct vlr_subscr *vsub, |
| 953 | const struct osmo_gsup_message *gsup) |
| 954 | { |
| 955 | unsigned int i; |
| 956 | unsigned int got_tuples; |
| 957 | |
| 958 | if (gsup->num_auth_vectors) { |
| 959 | memset(&vsub->auth_tuples, 0, sizeof(vsub->auth_tuples)); |
| 960 | for (i = 0; i < ARRAY_SIZE(vsub->auth_tuples); i++) |
Neels Hofmeyr | 8b6e536 | 2018-11-30 02:57:33 +0100 | [diff] [blame] | 961 | vsub->auth_tuples[i].key_seq = VLR_KEY_SEQ_INVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | got_tuples = 0; |
| 965 | for (i = 0; i < gsup->num_auth_vectors; i++) { |
| 966 | size_t key_seq = i; |
| 967 | |
| 968 | if (key_seq >= ARRAY_SIZE(vsub->auth_tuples)) { |
| 969 | LOGVSUBP(LOGL_NOTICE, vsub, |
Martin Hauke | 3f07dac | 2019-11-14 17:49:08 +0100 | [diff] [blame] | 970 | "Skipping auth tuple with invalid cksn %zu\n", |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 971 | key_seq); |
| 972 | continue; |
| 973 | } |
| 974 | vsub->auth_tuples[i].vec = gsup->auth_vectors[i]; |
| 975 | vsub->auth_tuples[i].key_seq = key_seq; |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 976 | got_tuples++; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | LOGVSUBP(LOGL_DEBUG, vsub, "Received %u auth tuples\n", got_tuples); |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 980 | vlr_rate_ctr_add(vsub->vlr, VLR_CTR_GSUP_RX_TUPLES, got_tuples); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 981 | |
| 982 | if (!got_tuples) { |
| 983 | /* FIXME what now? */ |
| 984 | // vlr_subscr_cancel(vsub, GMM_CAUSE_GSM_AUTH_UNACCEPT); ? |
| 985 | } |
| 986 | |
| 987 | /* New tuples means last_tuple becomes invalid */ |
| 988 | vsub->last_tuple = NULL; |
| 989 | } |
| 990 | |
| 991 | /* Handle SendAuthInfo Result/Error from HLR */ |
| 992 | static int vlr_subscr_handle_sai_res(struct vlr_subscr *vsub, |
| 993 | const struct osmo_gsup_message *gsup) |
| 994 | { |
| 995 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 996 | void *data = (void *) gsup; |
| 997 | |
Neels Hofmeyr | 1bfe0e1 | 2019-09-16 18:07:54 +0200 | [diff] [blame] | 998 | if (!auth_fi) { |
| 999 | LOGVSUBP(LOGL_ERROR, vsub, "Received GSUP %s, but there is no auth_fsm\n", |
| 1000 | osmo_gsup_message_type_name(gsup->message_type)); |
| 1001 | return -1; |
| 1002 | } |
| 1003 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1004 | switch (gsup->message_type) { |
| 1005 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 1006 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_ACK, data); |
| 1007 | break; |
| 1008 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
| 1009 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_HLR_SAI_NACK, data); |
| 1010 | break; |
| 1011 | default: |
| 1012 | return -1; |
| 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1018 | static void vlr_subscr_gsup_insert_data(struct vlr_subscr *vsub, |
| 1019 | const struct osmo_gsup_message *gsup_msg) |
| 1020 | { |
| 1021 | unsigned idx; |
| 1022 | int rc; |
| 1023 | |
Neels Hofmeyr | 02dd265 | 2019-11-13 07:05:11 +0100 | [diff] [blame] | 1024 | if (gsup_msg->msisdn_enc_len) {//FIXME: vlr_subscr_set_msisdn()? |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1025 | gsm48_decode_bcd_number2(vsub->msisdn, sizeof(vsub->msisdn), |
| 1026 | gsup_msg->msisdn_enc, |
| 1027 | gsup_msg->msisdn_enc_len, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1028 | LOGP(DVLR, LOGL_DEBUG, "IMSI:%s has MSISDN:%s\n", |
| 1029 | vsub->imsi, vsub->msisdn); |
| 1030 | } |
| 1031 | |
| 1032 | if (gsup_msg->hlr_enc) { |
| 1033 | if (gsup_msg->hlr_enc_len > sizeof(vsub->hlr.buf)) { |
| 1034 | LOGP(DVLR, LOGL_ERROR, "HLR-Number too long (%zu)\n", |
| 1035 | gsup_msg->hlr_enc_len); |
| 1036 | vsub->hlr.len = 0; |
| 1037 | } else { |
| 1038 | memcpy(vsub->hlr.buf, gsup_msg->hlr_enc, |
| 1039 | gsup_msg->hlr_enc_len); |
| 1040 | vsub->hlr.len = gsup_msg->hlr_enc_len; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | if (gsup_msg->pdp_info_compl) { |
| 1045 | rc = vlr_subscr_pdp_data_clear(vsub); |
| 1046 | if (rc > 0) |
| 1047 | LOGP(DVLR, LOGL_INFO, "Cleared existing PDP info\n"); |
| 1048 | } |
| 1049 | |
| 1050 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
| 1051 | const struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx]; |
| 1052 | size_t ctx_id = pdp_info->context_id; |
| 1053 | struct sgsn_subscriber_pdp_data *pdp_data; |
| 1054 | |
| 1055 | if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) { |
| 1056 | LOGVSUBP(LOGL_ERROR, vsub, |
| 1057 | "APN too long, context id = %zu, APN = %s\n", |
| 1058 | ctx_id, osmo_hexdump(pdp_info->apn_enc, |
| 1059 | pdp_info->apn_enc_len)); |
| 1060 | continue; |
| 1061 | } |
| 1062 | |
| 1063 | if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) { |
| 1064 | LOGVSUBP(LOGL_ERROR, vsub, |
| 1065 | "QoS info too long (%zu)\n", |
| 1066 | pdp_info->qos_enc_len); |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | LOGVSUBP(LOGL_INFO, vsub, |
| 1071 | "Will set PDP info, context id = %zu, APN = %s\n", |
| 1072 | ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len)); |
| 1073 | |
| 1074 | /* Set PDP info [ctx_id] */ |
| 1075 | pdp_data = vlr_subscr_pdp_data_get_by_id(vsub, ctx_id); |
| 1076 | if (!pdp_data) { |
| 1077 | pdp_data = vlr_subscr_pdp_data_alloc(vsub); |
| 1078 | pdp_data->context_id = ctx_id; |
| 1079 | } |
| 1080 | |
| 1081 | OSMO_ASSERT(pdp_data != NULL); |
Pau Espin Pedrol | 6f2749c | 2024-01-19 21:10:12 +0100 | [diff] [blame] | 1082 | pdp_data->pdp_type_org = pdp_info->pdp_type_org; |
| 1083 | pdp_data->pdp_type_nr = pdp_info->pdp_type_nr; |
| 1084 | memcpy(&pdp_data->pdp_address[0], &pdp_info->pdp_address[0], sizeof(pdp_data->pdp_address[0])); |
| 1085 | memcpy(&pdp_data->pdp_address[1], &pdp_info->pdp_address[1], sizeof(pdp_data->pdp_address[1])); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1086 | osmo_apn_to_str(pdp_data->apn_str, |
| 1087 | pdp_info->apn_enc, pdp_info->apn_enc_len); |
| 1088 | memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len); |
| 1089 | pdp_data->qos_subscribed_len = pdp_info->qos_enc_len; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | /* Handle InsertSubscrData Result from HLR */ |
| 1095 | static int vlr_subscr_handle_isd_req(struct vlr_subscr *vsub, |
| 1096 | const struct osmo_gsup_message *gsup) |
| 1097 | { |
| 1098 | struct osmo_gsup_message gsup_reply = {0}; |
| 1099 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1100 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_ISD_RES); |
| 1101 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1102 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 1103 | vsub->vlr->ops.subscr_update(vsub); |
| 1104 | |
| 1105 | gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; |
| 1106 | return vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
| 1107 | } |
| 1108 | |
| 1109 | /* Handle UpdateLocation Result from HLR */ |
| 1110 | static int vlr_subscr_handle_lu_res(struct vlr_subscr *vsub, |
| 1111 | const struct osmo_gsup_message *gsup) |
| 1112 | { |
Philipp Maier | 483cea8 | 2019-04-03 16:23:29 +0200 | [diff] [blame] | 1113 | struct sgs_lu_response sgs_lu_response = {0}; |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1114 | bool sgs_lu_in_progress = false; |
| 1115 | |
| 1116 | if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES) |
| 1117 | sgs_lu_in_progress = true; |
| 1118 | |
| 1119 | if (!vsub->lu_fsm && !sgs_lu_in_progress) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1120 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Result " |
| 1121 | "without LU in progress\n"); |
| 1122 | return -ENODEV; |
| 1123 | } |
| 1124 | |
| 1125 | /* contrary to MAP, we allow piggy-backing subscriber data onto the |
| 1126 | * UPDATE LOCATION RESULT, and don't mandate the use of a separate |
| 1127 | * nested INSERT SUBSCRIBER DATA transaction */ |
| 1128 | vlr_subscr_gsup_insert_data(vsub, gsup); |
| 1129 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1130 | if (sgs_lu_in_progress) { |
| 1131 | sgs_lu_response.accepted = true; |
| 1132 | sgs_lu_response.vsub = vsub; |
| 1133 | vsub->sgs.response_cb(&sgs_lu_response); |
| 1134 | } else |
| 1135 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, NULL); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1136 | |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | /* Handle UpdateLocation Result from HLR */ |
| 1141 | static int vlr_subscr_handle_lu_err(struct vlr_subscr *vsub, |
| 1142 | const struct osmo_gsup_message *gsup) |
| 1143 | { |
Philipp Maier | 483cea8 | 2019-04-03 16:23:29 +0200 | [diff] [blame] | 1144 | struct sgs_lu_response sgs_lu_response = {0}; |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1145 | bool sgs_lu_in_progress = false; |
| 1146 | |
| 1147 | if (vsub->sgs_fsm->state == SGS_UE_ST_LA_UPD_PRES) |
| 1148 | sgs_lu_in_progress = true; |
| 1149 | |
| 1150 | if (!vsub->lu_fsm && !sgs_lu_in_progress) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1151 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP LU Error " |
| 1152 | "without LU in progress\n"); |
| 1153 | return -ENODEV; |
| 1154 | } |
| 1155 | |
| 1156 | LOGVSUBP(LOGL_DEBUG, vsub, "UpdateLocation failed; gmm_cause: %s\n", |
| 1157 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 1158 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1159 | if (sgs_lu_in_progress) { |
| 1160 | sgs_lu_response.accepted = false; |
| 1161 | sgs_lu_response.vsub = vsub; |
| 1162 | vsub->sgs.response_cb(&sgs_lu_response); |
| 1163 | } else |
| 1164 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_LU_RES, |
| 1165 | (void *)&gsup->cause); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
Alexander Couzens | 7312b15 | 2019-08-19 15:30:12 +0200 | [diff] [blame] | 1169 | void vlr_gmm_cause_to_mm_cause(enum gsm48_gmm_cause gmm_cause, |
| 1170 | enum gsm48_reject_value *gsm48_rej_p) |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1171 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1172 | enum gsm48_reject_value gsm48_rej = GSM48_REJECT_NETWORK_FAILURE; |
| 1173 | switch (gmm_cause) { |
| 1174 | case GMM_CAUSE_IMSI_UNKNOWN: |
| 1175 | gsm48_rej = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR; |
| 1176 | break; |
| 1177 | case GMM_CAUSE_ILLEGAL_MS: |
| 1178 | gsm48_rej = GSM48_REJECT_ILLEGAL_MS; |
| 1179 | break; |
| 1180 | case GMM_CAUSE_IMEI_NOT_ACCEPTED: |
| 1181 | gsm48_rej = GSM48_REJECT_IMEI_NOT_ACCEPTED; |
| 1182 | break; |
| 1183 | case GMM_CAUSE_ILLEGAL_ME: |
| 1184 | gsm48_rej = GSM48_REJECT_ILLEGAL_ME; |
| 1185 | break; |
| 1186 | case GMM_CAUSE_GPRS_NOTALLOWED: |
| 1187 | gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED; |
| 1188 | break; |
| 1189 | case GMM_CAUSE_GPRS_OTHER_NOTALLOWED: |
| 1190 | gsm48_rej = GSM48_REJECT_SERVICES_NOT_ALLOWED; |
| 1191 | break; |
| 1192 | case GMM_CAUSE_MS_ID_NOT_DERIVED: |
| 1193 | gsm48_rej = GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE; |
| 1194 | break; |
| 1195 | case GMM_CAUSE_IMPL_DETACHED: |
| 1196 | gsm48_rej = GSM48_REJECT_IMPLICITLY_DETACHED; |
| 1197 | break; |
| 1198 | case GMM_CAUSE_PLMN_NOTALLOWED: |
| 1199 | gsm48_rej = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 1200 | break; |
| 1201 | case GMM_CAUSE_LA_NOTALLOWED: |
| 1202 | gsm48_rej = GSM48_REJECT_LOC_NOT_ALLOWED; |
| 1203 | break; |
| 1204 | case GMM_CAUSE_ROAMING_NOTALLOWED: |
| 1205 | gsm48_rej = GSM48_REJECT_ROAMING_NOT_ALLOWED; |
| 1206 | break; |
| 1207 | case GMM_CAUSE_NO_GPRS_PLMN: |
| 1208 | gsm48_rej = GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN; |
| 1209 | break; |
| 1210 | case GMM_CAUSE_MSC_TEMP_NOTREACH: |
| 1211 | gsm48_rej = GSM48_REJECT_MSC_TMP_NOT_REACHABLE; |
| 1212 | break; |
| 1213 | case GMM_CAUSE_SYNC_FAIL: |
| 1214 | gsm48_rej = GSM48_REJECT_SYNCH_FAILURE; |
| 1215 | break; |
| 1216 | case GMM_CAUSE_CONGESTION: |
| 1217 | gsm48_rej = GSM48_REJECT_CONGESTION; |
| 1218 | break; |
| 1219 | case GMM_CAUSE_SEM_INCORR_MSG: |
| 1220 | gsm48_rej = GSM48_REJECT_INCORRECT_MESSAGE; |
| 1221 | break; |
| 1222 | case GMM_CAUSE_INV_MAND_INFO: |
| 1223 | gsm48_rej = GSM48_REJECT_INVALID_MANDANTORY_INF; |
| 1224 | break; |
| 1225 | case GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL: |
| 1226 | gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED; |
| 1227 | break; |
| 1228 | case GMM_CAUSE_MSGT_INCOMP_P_STATE: |
| 1229 | gsm48_rej = GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE; |
| 1230 | break; |
| 1231 | case GMM_CAUSE_IE_NOTEXIST_NOTIMPL: |
| 1232 | gsm48_rej = GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED; |
| 1233 | break; |
| 1234 | case GMM_CAUSE_COND_IE_ERR: |
| 1235 | gsm48_rej = GSM48_REJECT_CONDTIONAL_IE_ERROR; |
| 1236 | break; |
| 1237 | case GMM_CAUSE_MSG_INCOMP_P_STATE: |
| 1238 | gsm48_rej = GSM48_REJECT_MSG_NOT_COMPATIBLE; |
| 1239 | break; |
| 1240 | case GMM_CAUSE_PROTO_ERR_UNSPEC: |
| 1241 | gsm48_rej = GSM48_REJECT_PROTOCOL_ERROR; |
| 1242 | break; |
| 1243 | |
| 1244 | case GMM_CAUSE_NO_SUIT_CELL_IN_LA: |
| 1245 | case GMM_CAUSE_MAC_FAIL: |
| 1246 | case GMM_CAUSE_GSM_AUTH_UNACCEPT: |
| 1247 | case GMM_CAUSE_NOT_AUTH_FOR_CSG: |
| 1248 | case GMM_CAUSE_SMS_VIA_GPRS_IN_RA: |
| 1249 | case GMM_CAUSE_NO_PDP_ACTIVATED: |
| 1250 | case GMM_CAUSE_NET_FAIL: |
| 1251 | gsm48_rej = GSM48_REJECT_NETWORK_FAILURE; |
| 1252 | break; |
| 1253 | } |
Alexander Couzens | eb1b03a | 2019-08-19 15:22:25 +0200 | [diff] [blame] | 1254 | |
| 1255 | *gsm48_rej_p = gsm48_rej; |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1258 | /* Handle LOCATION CANCEL request from HLR */ |
| 1259 | static int vlr_subscr_handle_cancel_req(struct vlr_subscr *vsub, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1260 | const struct osmo_gsup_message *gsup_msg) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1261 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1262 | enum gsm48_reject_value gsm48_rej; |
Alexander Couzens | eb1b03a | 2019-08-19 15:22:25 +0200 | [diff] [blame] | 1263 | enum osmo_fsm_term_cause fsm_cause = OSMO_FSM_TERM_ERROR; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1264 | struct osmo_gsup_message gsup_reply = {0}; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1265 | int rc, is_update_procedure = !gsup_msg->cancel_type || |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1266 | gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE; |
| 1267 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1268 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_GSUP_TX_CANCEL_RES); |
| 1269 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1270 | LOGVSUBP(LOGL_INFO, vsub, "Cancelling MS subscriber (%s)\n", |
| 1271 | is_update_procedure ? |
| 1272 | "update procedure" : "subscription withdraw"); |
| 1273 | |
| 1274 | gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT; |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1275 | rc = vlr_subscr_tx_gsup_message(vsub, &gsup_reply); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1276 | |
Alexander Couzens | 7312b15 | 2019-08-19 15:30:12 +0200 | [diff] [blame] | 1277 | vlr_gmm_cause_to_mm_cause(gsup_msg->cause, &gsm48_rej); |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1278 | vlr_subscr_cancel_attach_fsm(vsub, fsm_cause, gsm48_rej); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1279 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1280 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_CANCEL); |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 1281 | vlr_subscr_detach(vsub); |
Stefan Sperling | ad797ce | 2018-12-10 18:33:30 +0100 | [diff] [blame] | 1282 | |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 1283 | return rc; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1286 | /* Handle Check_IMEI_VLR result and error from HLR */ |
| 1287 | static int vlr_subscr_handle_check_imei(struct vlr_subscr *vsub, const struct osmo_gsup_message *gsup) |
| 1288 | { |
| 1289 | if (!vsub->lu_fsm) { |
| 1290 | LOGVSUBP(LOGL_ERROR, vsub, "Rx %s without LU in progress\n", |
| 1291 | osmo_gsup_message_type_name(gsup->message_type)); |
| 1292 | return -ENODEV; |
| 1293 | } |
| 1294 | |
Oliver Smith | cbf2c93 | 2019-05-06 13:09:55 +0200 | [diff] [blame] | 1295 | /* Dispatch result to vsub->lu_fsm, which will either handle the result by itself (Check IMEI early) or dispatch |
| 1296 | * it further to lu_compl_vlr_fsm (Check IMEI after LU). */ |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1297 | if (gsup->message_type == OSMO_GSUP_MSGT_CHECK_IMEI_RESULT) { |
| 1298 | if (gsup->imei_result == OSMO_GSUP_IMEI_RESULT_ACK) |
| 1299 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_ACK, NULL); |
| 1300 | else |
| 1301 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL); |
| 1302 | } else { |
| 1303 | LOGVSUBP(LOGL_ERROR, vsub, "Check_IMEI_VLR failed; gmm_cause: %s\n", |
| 1304 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 1305 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_HLR_IMEI_NACK, NULL); |
| 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1311 | /* Incoming handler for GSUP from HLR. |
| 1312 | * Keep this function non-static for direct invocation by unit tests. */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1313 | int vlr_gsup_rx(struct gsup_client_mux *gcm, void *data, const struct osmo_gsup_message *gsup) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1314 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1315 | struct vlr_instance *vlr = data; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1316 | struct vlr_subscr *vsub; |
Neels Hofmeyr | 27c8b03 | 2019-12-23 19:12:54 +0100 | [diff] [blame] | 1317 | int rc = 0; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1318 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1319 | vsub = vlr_subscr_find_by_imsi(vlr, gsup->imsi, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1320 | if (!vsub) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1321 | switch (gsup->message_type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1322 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 1323 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1324 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_NO_SUBSCR); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1325 | return vlr_rx_gsup_purge_no_subscr(vlr, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1326 | default: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1327 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN_IMSI); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1328 | return vlr_rx_gsup_unknown_imsi(vlr, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1332 | switch (gsup->message_type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1333 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1334 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_RES); |
| 1335 | rc = vlr_subscr_handle_sai_res(vsub, gsup); |
| 1336 | break; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1337 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1338 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_SAI_ERR); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1339 | rc = vlr_subscr_handle_sai_res(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1340 | break; |
| 1341 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1342 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_ISD_REQ); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1343 | rc = vlr_subscr_handle_isd_req(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1344 | break; |
| 1345 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1346 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CANCEL_REQ); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1347 | rc = vlr_subscr_handle_cancel_req(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1348 | break; |
| 1349 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1350 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_RES); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1351 | rc = vlr_subscr_handle_lu_res(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1352 | break; |
| 1353 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1354 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UL_ERR); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1355 | rc = vlr_subscr_handle_lu_err(vsub, gsup); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1356 | break; |
| 1357 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1358 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_ERR); |
| 1359 | goto out_unimpl; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1360 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1361 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_PURGE_MS_RES); |
| 1362 | goto out_unimpl; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1363 | case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1364 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_DELETE_DATA_REQ); |
| 1365 | goto out_unimpl; |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1366 | case OSMO_GSUP_MSGT_CHECK_IMEI_ERROR: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1367 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_ERR); |
| 1368 | rc = vlr_subscr_handle_check_imei(vsub, gsup); |
| 1369 | break; |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1370 | case OSMO_GSUP_MSGT_CHECK_IMEI_RESULT: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1371 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_CHECK_IMEI_RES); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1372 | rc = vlr_subscr_handle_check_imei(vsub, gsup); |
Oliver Smith | 7d05309 | 2018-12-14 17:37:38 +0100 | [diff] [blame] | 1373 | break; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1374 | default: |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1375 | vlr_rate_ctr_inc(vlr, VLR_CTR_GSUP_RX_UNKNOWN); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1376 | LOGP(DLGSUP, LOGL_ERROR, "GSUP Message type not handled by VLR: %d\n", gsup->message_type); |
| 1377 | rc = -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1381 | vlr_subscr_put(vsub, __func__); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1382 | return rc; |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1383 | |
| 1384 | out_unimpl: |
| 1385 | LOGVSUBP(LOGL_ERROR, vsub, "Rx GSUP msg_type=%d not yet implemented\n", gsup->message_type); |
| 1386 | vlr_subscr_put(vsub, __func__); |
| 1387 | return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1391 | int vlr_subscr_rx_id_resp(struct vlr_subscr *vsub, const struct osmo_mobile_identity *mi) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1392 | { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1393 | /* update the vlr_subscr with the given identity */ |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1394 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1395 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1396 | if (vsub->imsi[0] |
| 1397 | && !vlr_subscr_matches_imsi(vsub, mi->imsi)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1398 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1399 | " %s\n", mi->imsi); |
Stefan Sperling | 9fbb600 | 2018-06-25 17:31:59 +0200 | [diff] [blame] | 1400 | /* XXX Should we return an error, e.g. -EINVAL ? */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1401 | } else |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1402 | vlr_subscr_set_imsi(vsub, mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1403 | break; |
| 1404 | case GSM_MI_TYPE_IMEI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1405 | vlr_subscr_set_imei(vsub, mi->imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1406 | break; |
| 1407 | case GSM_MI_TYPE_IMEISV: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1408 | vlr_subscr_set_imeisv(vsub, mi->imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1409 | break; |
Neels Hofmeyr | a40adf7 | 2020-06-02 22:02:01 +0200 | [diff] [blame] | 1410 | default: |
| 1411 | return -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | if (vsub->auth_fsm) { |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1415 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1416 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1417 | return osmo_fsm_inst_dispatch(vsub->auth_fsm, |
| 1418 | VLR_AUTH_E_MS_ID_IMSI, (void*)mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1419 | break; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | if (vsub->lu_fsm) { |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1424 | switch (mi->type) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1425 | case GSM_MI_TYPE_IMSI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1426 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMSI, (void*)mi->imsi); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1427 | case GSM_MI_TYPE_IMEI: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1428 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEI, (void*)mi->imei); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1429 | case GSM_MI_TYPE_IMEISV: |
Neels Hofmeyr | 46d526a | 2020-05-29 03:27:50 +0200 | [diff] [blame] | 1430 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_ID_IMEISV, (void*)mi->imeisv); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1431 | default: |
Neels Hofmeyr | a40adf7 | 2020-06-02 22:02:01 +0200 | [diff] [blame] | 1432 | return -EINVAL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1433 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | /* MSC->VLR: Subscriber has provided IDENTITY RESPONSE */ |
| 1440 | int vlr_subscr_rx_tmsi_reall_compl(struct vlr_subscr *vsub) |
| 1441 | { |
| 1442 | if (vsub->lu_fsm) { |
| 1443 | return osmo_fsm_inst_dispatch(vsub->lu_fsm, |
| 1444 | VLR_ULA_E_NEW_TMSI_ACK, NULL); |
| 1445 | } else if (vsub->proc_arq_fsm) { |
| 1446 | return osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, |
| 1447 | PR_ARQ_E_TMSI_ACK, NULL); |
| 1448 | } else { |
| 1449 | LOGVSUBP(LOGL_NOTICE, vsub, |
Neels Hofmeyr | 5b1e030 | 2019-05-06 23:45:09 +0200 | [diff] [blame] | 1450 | "gratuitous TMSI REALLOC COMPL\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | } |
| 1454 | |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1455 | bool vlr_subscr_expire(struct vlr_subscr *vsub) |
| 1456 | { |
| 1457 | if (vsub->lu_complete) { |
Neels Hofmeyr | 5c8b144 | 2018-12-11 12:43:10 +0100 | [diff] [blame] | 1458 | /* balancing the get from vlr_lu_compl_fsm_success() */ |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1459 | vsub->lu_complete = false; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1460 | vlr_subscr_put(vsub, VSUB_USE_ATTACHED); |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1461 | |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 1468 | static int vlr_subscr_detach(struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1469 | { |
| 1470 | /* paranoia: should any LU or PARQ FSMs still be running, stop them. */ |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1471 | vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1472 | |
| 1473 | vsub->imsi_detached_flag = true; |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1474 | vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION; |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1475 | |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1476 | /* Inform the UE-SGs FSM that the subscriber has been detached */ |
| 1477 | osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_DETACH_IND_FROM_UE, NULL); |
| 1478 | |
Max | dcc193d | 2017-12-27 19:34:15 +0100 | [diff] [blame] | 1479 | vlr_subscr_expire(vsub); |
| 1480 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1481 | return 0; |
| 1482 | } |
| 1483 | |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 1484 | /* See TS 23.012 version 9.10.0 4.3.2.1 "Process Detach_IMSI_VLR" */ |
| 1485 | int vlr_subscr_rx_imsi_detach(struct vlr_subscr *vsub) |
| 1486 | { |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1487 | vlr_rate_ctr_inc(vsub->vlr, VLR_CTR_DETACH_BY_REQ); |
Harald Welte | 173bdf3 | 2022-05-15 12:12:54 +0200 | [diff] [blame] | 1488 | return vlr_subscr_detach(vsub); |
| 1489 | } |
| 1490 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1491 | /* Tear down any running FSMs due to MSC connection timeout. |
| 1492 | * Visit all vsub->*_fsm pointers and give them a queue to send a final reject |
| 1493 | * message before the entire connection is torn down. |
| 1494 | * \param[in] vsub subscriber to tear down |
| 1495 | */ |
Neels Hofmeyr | c036b79 | 2018-11-29 22:37:51 +0100 | [diff] [blame] | 1496 | void vlr_ran_conn_timeout(struct vlr_subscr *vsub) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1497 | { |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 1498 | vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_TIMEOUT, GSM48_REJECT_CONGESTION); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | struct vlr_instance *vlr_alloc(void *ctx, const struct vlr_ops *ops) |
| 1502 | { |
| 1503 | struct vlr_instance *vlr = talloc_zero(ctx, struct vlr_instance); |
| 1504 | OSMO_ASSERT(vlr); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1505 | |
| 1506 | /* Some of these are needed only on UTRAN, but in case the caller wants |
| 1507 | * only GERAN, she should just provide dummy callbacks. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1508 | OSMO_ASSERT(ops->tx_auth_req); |
| 1509 | OSMO_ASSERT(ops->tx_auth_rej); |
| 1510 | OSMO_ASSERT(ops->tx_id_req); |
| 1511 | OSMO_ASSERT(ops->tx_lu_acc); |
| 1512 | OSMO_ASSERT(ops->tx_lu_rej); |
| 1513 | OSMO_ASSERT(ops->tx_cm_serv_acc); |
| 1514 | OSMO_ASSERT(ops->tx_cm_serv_rej); |
| 1515 | OSMO_ASSERT(ops->set_ciph_mode); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1516 | OSMO_ASSERT(ops->tx_common_id); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1517 | OSMO_ASSERT(ops->subscr_update); |
| 1518 | OSMO_ASSERT(ops->subscr_assoc); |
| 1519 | |
| 1520 | INIT_LLIST_HEAD(&vlr->subscribers); |
| 1521 | INIT_LLIST_HEAD(&vlr->operations); |
| 1522 | memcpy(&vlr->ops, ops, sizeof(vlr->ops)); |
| 1523 | |
Neels Hofmeyr | 0b8dec7 | 2017-10-29 01:57:35 +0200 | [diff] [blame] | 1524 | /* defaults */ |
| 1525 | vlr->cfg.assign_tmsi = true; |
Neels Hofmeyr | 9aac5c2 | 2020-05-27 00:04:26 +0200 | [diff] [blame] | 1526 | vlr->cfg.nri_bitlen = OSMO_NRI_BITLEN_DEFAULT; |
| 1527 | vlr->cfg.nri_ranges = osmo_nri_ranges_alloc(vlr); |
Neels Hofmeyr | 0b8dec7 | 2017-10-29 01:57:35 +0200 | [diff] [blame] | 1528 | |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1529 | vlr->statg = osmo_stat_item_group_alloc(vlr, &vlr_statg_desc, 0); |
| 1530 | if (!vlr->statg) |
| 1531 | goto err_free; |
| 1532 | |
| 1533 | vlr->ctrg = rate_ctr_group_alloc(vlr, &vlr_ctrg_desc, 0); |
| 1534 | if (!vlr->ctrg) |
| 1535 | goto err_statg; |
| 1536 | |
Vadim Yanitskiy | fc2b019 | 2020-01-18 07:20:14 +0700 | [diff] [blame] | 1537 | /* reset shared timer definitions */ |
| 1538 | osmo_tdefs_reset(msc_tdefs_vlr); |
| 1539 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1540 | /* osmo_auth_fsm.c */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1541 | OSMO_ASSERT(osmo_fsm_register(&vlr_auth_fsm) == 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1542 | /* osmo_lu_fsm.c */ |
| 1543 | vlr_lu_fsm_init(); |
| 1544 | /* vlr_access_request_fsm.c */ |
| 1545 | vlr_parq_fsm_init(); |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 1546 | /* vlr_sgs_fsm.c */ |
| 1547 | vlr_sgs_fsm_init(); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1548 | |
| 1549 | return vlr; |
Harald Welte | 123261e | 2022-05-15 11:43:35 +0200 | [diff] [blame] | 1550 | |
| 1551 | err_statg: |
| 1552 | osmo_stat_item_group_free(vlr->statg); |
| 1553 | err_free: |
| 1554 | talloc_free(vlr); |
| 1555 | return NULL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1556 | } |
| 1557 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1558 | int vlr_start(struct vlr_instance *vlr, struct gsup_client_mux *gcm) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1559 | { |
| 1560 | OSMO_ASSERT(vlr); |
| 1561 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1562 | vlr->gcm = gcm; |
| 1563 | gcm->rx_cb[OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT] = (struct gsup_client_mux_rx_cb){ |
| 1564 | .func = vlr_gsup_rx, |
| 1565 | .data = vlr, |
| 1566 | }; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1567 | |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1568 | osmo_timer_setup(&vlr->lu_expire_timer, vlr_subscr_expire_lu, vlr); |
| 1569 | osmo_timer_schedule(&vlr->lu_expire_timer, VLR_SUBSCRIBER_LU_EXPIRATION_INTERVAL, 0); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
| 1573 | /* MSC->VLR: Subscribre has disconnected */ |
| 1574 | int vlr_subscr_disconnected(struct vlr_subscr *vsub) |
| 1575 | { |
| 1576 | /* This corresponds to a MAP-ABORT from MSC->VLR on a classic B |
| 1577 | * interface */ |
| 1578 | osmo_fsm_inst_term(vsub->lu_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1579 | osmo_fsm_inst_term(vsub->auth_fsm, OSMO_FSM_TERM_REQUEST, NULL); |
| 1580 | vsub->msc_conn_ref = NULL; |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* MSC->VLR: Receive Authentication Failure from Subscriber */ |
| 1586 | int vlr_subscr_rx_auth_fail(struct vlr_subscr *vsub, const uint8_t *auts) |
| 1587 | { |
| 1588 | struct vlr_auth_resp_par par = {0}; |
| 1589 | par.auts = auts; |
| 1590 | |
| 1591 | osmo_fsm_inst_dispatch(vsub->auth_fsm, VLR_AUTH_E_MS_AUTH_FAIL, &par); |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | /* MSC->VLR: Receive Authentication Response from MS |
| 1596 | * \returns 1 in case of success, 0 in case of delay, -1 on auth error */ |
| 1597 | int vlr_subscr_rx_auth_resp(struct vlr_subscr *vsub, bool is_r99, |
| 1598 | bool is_utran, const uint8_t *res, uint8_t res_len) |
| 1599 | { |
| 1600 | struct osmo_fsm_inst *auth_fi = vsub->auth_fsm; |
| 1601 | struct vlr_auth_resp_par par; |
| 1602 | |
| 1603 | par.is_r99 = is_r99; |
| 1604 | par.is_utran = is_utran; |
| 1605 | par.res = res; |
| 1606 | par.res_len = res_len; |
| 1607 | osmo_fsm_inst_dispatch(auth_fi, VLR_AUTH_E_MS_AUTH_RESP, (void *) &par); |
| 1608 | |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | /* MSC->VLR: Receive result of Ciphering Mode Command from MS */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1613 | void vlr_subscr_rx_ciph_res(struct vlr_subscr *vsub, enum vlr_ciph_result_cause result) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1614 | { |
| 1615 | if (vsub->lu_fsm && vsub->lu_fsm->state == VLR_ULA_S_WAIT_CIPH) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1616 | osmo_fsm_inst_dispatch(vsub->lu_fsm, VLR_ULA_E_CIPH_RES, &result); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1617 | if (vsub->proc_arq_fsm |
| 1618 | && vsub->proc_arq_fsm->state == PR_ARQ_S_WAIT_CIPH) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1619 | osmo_fsm_inst_dispatch(vsub->proc_arq_fsm, PR_ARQ_E_CIPH_RES, &result); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* Internal evaluation of requested ciphering mode. |
| 1623 | * Send set_ciph_mode() to MSC depending on the ciph_mode argument. |
| 1624 | * \param[in] vlr VLR instance. |
| 1625 | * \param[in] fi Calling FSM instance, for logging. |
| 1626 | * \param[in] msc_conn_ref MSC conn to send to. |
| 1627 | * \param[in] ciph_mode Ciphering config, to decide whether to do ciphering. |
| 1628 | * \returns 0 if no ciphering is needed or message was sent successfully, |
| 1629 | * or a negative value if ciph_mode is invalid or sending failed. |
| 1630 | */ |
| 1631 | int vlr_set_ciph_mode(struct vlr_instance *vlr, |
| 1632 | struct osmo_fsm_inst *fi, |
| 1633 | void *msc_conn_ref, |
Neels Hofmeyr | 2ef2da5 | 2017-12-18 01:23:42 +0100 | [diff] [blame] | 1634 | bool umts_aka, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1635 | bool retrieve_imeisv) |
| 1636 | { |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 1637 | LOGPFSML(fi, LOGL_DEBUG, "Set Ciphering Mode\n"); |
| 1638 | 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] | 1639 | } |
| 1640 | |
Neels Hofmeyr | e3d72d7 | 2017-12-18 02:06:44 +0100 | [diff] [blame] | 1641 | /* Decide whether UMTS AKA should be used. |
| 1642 | * UTRAN networks are by definition R99 capable, and the auth vector is required to contain UMTS AKA |
| 1643 | * tokens. This is expected to be verified by the caller. On GERAN, UMTS AKA must be used iff MS and |
| 1644 | * GERAN are R99 capable and UMTS AKA tokens are available. |
| 1645 | * \param[in] vec Auth tokens (received from the HLR). |
| 1646 | * \param[in] is_r99 True when BTS and GERAN are R99 capable. |
| 1647 | * \returns true to use UMTS AKA, false to use pre-R99 GSM AKA. |
| 1648 | */ |
| 1649 | bool vlr_use_umts_aka(struct osmo_auth_vector *vec, bool is_r99) |
| 1650 | { |
| 1651 | if (!is_r99) |
| 1652 | return false; |
| 1653 | if (!(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) |
| 1654 | return false; |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1658 | void log_set_filter_vlr_subscr(struct log_target *target, |
| 1659 | struct vlr_subscr *vlr_subscr) |
| 1660 | { |
| 1661 | struct vlr_subscr **fsub = (void*)&target->filter_data[LOG_FLT_VLR_SUBSCR]; |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1662 | const char *use = "logfilter"; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1663 | |
| 1664 | /* free the old data */ |
| 1665 | if (*fsub) { |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1666 | vlr_subscr_put(*fsub, use); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1667 | *fsub = NULL; |
| 1668 | } |
| 1669 | |
| 1670 | if (vlr_subscr) { |
| 1671 | target->filter_map |= (1 << LOG_FLT_VLR_SUBSCR); |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 1672 | vlr_subscr_get(vlr_subscr, use); |
| 1673 | *fsub = vlr_subscr; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1674 | } else |
| 1675 | target->filter_map &= ~(1 << LOG_FLT_VLR_SUBSCR); |
| 1676 | } |