Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1 | /* Gb proxy peer handling */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <osmocom/sgsn/gb_proxy.h> |
| 24 | |
| 25 | #include <osmocom/sgsn/debug.h> |
| 26 | |
| 27 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 28 | #include <osmocom/core/crc16.h> |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 29 | #include <osmocom/core/logging.h> |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 30 | #include <osmocom/core/linuxlist.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 31 | #include <osmocom/core/rate_ctr.h> |
| 32 | #include <osmocom/core/stats.h> |
| 33 | #include <osmocom/core/talloc.h> |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 35 | #include <osmocom/gsm/tlv.h> |
| 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | extern void *tall_sgsn_ctx; |
| 40 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 41 | static const struct rate_ctr_desc bvc_ctr_description[] = { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 42 | { "blocked", "BVC Block " }, |
| 43 | { "unblocked", "BVC Unblock " }, |
| 44 | { "dropped", "BVC blocked, dropped packet " }, |
| 45 | { "inv-nsei", "NSEI mismatch " }, |
| 46 | { "tx-err", "NS Transmission error " }, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 49 | osmo_static_assert(ARRAY_SIZE(bvc_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 50 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 51 | static const struct rate_ctr_group_desc bvc_ctrg_desc = { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 52 | .group_name_prefix = "gbproxy:peer", |
| 53 | .group_description = "GBProxy Peer Statistics", |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 54 | .num_ctr = ARRAY_SIZE(bvc_ctr_description), |
| 55 | .ctr_desc = bvc_ctr_description, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 56 | .class_id = OSMO_STATS_CLASS_PEER, |
| 57 | }; |
| 58 | |
| 59 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 60 | /* Find the gbproxy_bvc by its BVCI. There can only be one match */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 61 | struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 62 | { |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 63 | struct gbproxy_bvc *bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 64 | hash_for_each_possible(nse->bvcs, bvc, list, bvci) { |
| 65 | if (bvc->bvci == bvci) |
| 66 | return bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 71 | struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 72 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 73 | struct gbproxy_bvc *bvc; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 74 | OSMO_ASSERT(nse); |
| 75 | struct gbproxy_config *cfg = nse->cfg; |
| 76 | OSMO_ASSERT(cfg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 77 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 78 | bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc); |
| 79 | if (!bvc) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 80 | return NULL; |
| 81 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 82 | bvc->bvci = bvci; |
Harald Welte | 4bd3e49 | 2020-12-07 12:06:52 +0100 | [diff] [blame] | 83 | bvc->ctrg = rate_ctr_group_alloc(bvc, &bvc_ctrg_desc, (nse->nsei << 16) | bvci); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 84 | if (!bvc->ctrg) { |
| 85 | talloc_free(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 88 | bvc->nse = nse; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 89 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 90 | hash_add(nse->bvcs, &bvc->list, bvc->bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 91 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 92 | LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Created\n"); |
| 93 | |
| 94 | /* We leave allocating the bvc->fi to the caller, as the FSM details depend |
| 95 | * on the type of BVC (SIG/PTP) and role (SGSN/BSS) */ |
| 96 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 97 | return bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 100 | void gbproxy_bvc_free(struct gbproxy_bvc *bvc) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 101 | { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 102 | struct gbproxy_cell *cell; |
| 103 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 104 | if (!bvc) |
Daniel Willmann | f6a36cc | 2020-12-04 17:38:46 +0100 | [diff] [blame] | 105 | return; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 106 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 107 | LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n"); |
| 108 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 109 | hash_del(&bvc->list); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 110 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 111 | rate_ctr_group_free(bvc->ctrg); |
| 112 | bvc->ctrg = NULL; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 113 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 114 | osmo_fsm_inst_free(bvc->fi); |
| 115 | |
| 116 | cell = bvc->cell; |
| 117 | if (cell) { |
| 118 | int i; |
| 119 | |
| 120 | if (cell->bss_bvc == bvc) |
| 121 | cell->bss_bvc = NULL; |
| 122 | |
| 123 | /* we could also be a SGSN-side BVC */ |
| 124 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 125 | if (cell->sgsn_bvc[i] == bvc) |
| 126 | cell->sgsn_bvc[i] = NULL; |
| 127 | } |
| 128 | bvc->cell = NULL; |
| 129 | } |
| 130 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 131 | talloc_free(bvc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 134 | /*! remove BVCs on NSE specified by NSEI. |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 135 | * \param[in] cfg proxy in which we operate |
| 136 | * \param[in] nsei NS entity in which we should clean up |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 137 | * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */ |
| 138 | int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 139 | { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 140 | struct hlist_node *btmp; |
| 141 | struct gbproxy_bvc *bvc; |
| 142 | int j, counter = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 143 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 144 | if (!nse) |
| 145 | return 0; |
| 146 | |
| 147 | hash_for_each_safe(nse->bvcs, j, btmp, bvc, list) { |
| 148 | if (bvci && bvc->bvci != bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 149 | continue; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 150 | if (bvci == 0 && bvc->bvci == 0) |
| 151 | continue; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 152 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 153 | gbproxy_bvc_free(bvc); |
| 154 | counter += 1; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return counter; |
| 158 | } |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 159 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 160 | |
| 161 | /*********************************************************************** |
| 162 | * CELL |
| 163 | ***********************************************************************/ |
| 164 | |
| 165 | /* Allocate a new 'cell' object */ |
| 166 | struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci) |
| 167 | { |
| 168 | struct gbproxy_cell *cell; |
| 169 | OSMO_ASSERT(cfg); |
| 170 | |
| 171 | cell = talloc_zero(cfg, struct gbproxy_cell); |
| 172 | if (!cell) |
| 173 | return NULL; |
| 174 | |
| 175 | cell->cfg = cfg; |
| 176 | cell->bvci = bvci; |
| 177 | |
| 178 | hash_add(cfg->cells, &cell->list, cell->bvci); |
| 179 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 180 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n"); |
| 181 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 182 | return cell; |
| 183 | } |
| 184 | |
| 185 | /* Find cell by BVCI */ |
| 186 | struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci) |
| 187 | { |
| 188 | struct gbproxy_cell *cell; |
| 189 | |
| 190 | hash_for_each_possible(cfg->cells, cell, list, bvci) { |
| 191 | if (cell->bvci == bvci) |
| 192 | return cell; |
| 193 | } |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | struct gbproxy_cell *gbproxy_cell_by_bvci_or_new(struct gbproxy_config *cfg, uint16_t bvci) |
| 198 | { |
| 199 | struct gbproxy_cell *cell; |
| 200 | OSMO_ASSERT(cfg); |
| 201 | |
| 202 | cell = gbproxy_cell_by_bvci(cfg, bvci); |
| 203 | if (!cell) |
| 204 | cell = gbproxy_cell_alloc(cfg, bvci); |
| 205 | |
| 206 | return cell; |
| 207 | } |
| 208 | |
| 209 | void gbproxy_cell_free(struct gbproxy_cell *cell) |
| 210 | { |
| 211 | unsigned int i; |
| 212 | |
| 213 | if (!cell) |
| 214 | return; |
| 215 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 216 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n"); |
| 217 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 218 | /* remove from cfg.cells */ |
| 219 | hash_del(&cell->list); |
| 220 | |
| 221 | /* remove back-pointers from the BSS side */ |
| 222 | if (cell->bss_bvc && cell->bss_bvc->cell) |
| 223 | cell->bss_bvc->cell = NULL; |
| 224 | |
| 225 | /* remove back-pointers from the SGSN side */ |
| 226 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 227 | if (!cell->sgsn_bvc[i]) |
| 228 | continue; |
| 229 | if (cell->sgsn_bvc[i]->cell) |
| 230 | cell->sgsn_bvc[i]->cell = NULL; |
| 231 | } |
| 232 | |
| 233 | talloc_free(cell); |
| 234 | } |
| 235 | |
| 236 | bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc) |
| 237 | { |
| 238 | unsigned int i; |
| 239 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 240 | if (!cell->sgsn_bvc[i]) { |
| 241 | cell->sgsn_bvc[i] = bvc; |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 242 | LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n"); |
| 243 | LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n"); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 250 | |
| 251 | /*********************************************************************** |
| 252 | * TLLI cache |
| 253 | ***********************************************************************/ |
| 254 | |
Daniel Willmann | 9170c34 | 2021-01-17 13:12:46 +0100 | [diff] [blame] | 255 | static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli) |
| 256 | { |
| 257 | struct gbproxy_tlli_cache_entry *cache_entry; |
| 258 | |
| 259 | hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) { |
| 260 | if (cache_entry->tlli == tlli) |
| 261 | return cache_entry; |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 266 | void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli) |
| 267 | { |
| 268 | struct gbproxy_config *cfg = nse->cfg; |
| 269 | struct timespec now; |
| 270 | struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli); |
| 271 | |
| 272 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 273 | |
| 274 | if (cache_entry) { |
| 275 | /* Update the entry if it already exists */ |
| 276 | cache_entry->nse = nse; |
| 277 | cache_entry->tstamp = now.tv_sec; |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry); |
| 282 | cache_entry->tlli = tlli; |
| 283 | cache_entry->nse = nse; |
| 284 | cache_entry->tstamp = now.tv_sec; |
| 285 | hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli); |
| 286 | } |
| 287 | |
| 288 | static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) { |
| 289 | uint i; |
| 290 | struct gbproxy_config *cfg = nse->cfg; |
| 291 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 292 | struct hlist_node *tmp; |
| 293 | |
| 294 | hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) { |
| 295 | if (tlli_cache->nse == nse) { |
| 296 | hash_del(&tlli_cache->list); |
| 297 | talloc_free(tlli_cache); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli) |
| 303 | { |
| 304 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 305 | struct hlist_node *tmp; |
| 306 | |
| 307 | hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) { |
| 308 | if (tlli_cache->tlli == tlli) { |
| 309 | hash_del(&tlli_cache->list); |
| 310 | talloc_free(tlli_cache); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg) |
| 317 | { |
| 318 | int i, count = 0; |
| 319 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 320 | struct hlist_node *tmp; |
| 321 | struct timespec now; |
| 322 | time_t expiry; |
| 323 | |
| 324 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 325 | expiry = now.tv_sec - cfg->tlli_cache.timeout; |
| 326 | |
| 327 | hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) { |
| 328 | if (tlli_cache->tstamp < expiry) { |
| 329 | count++; |
| 330 | LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli); |
| 331 | hash_del(&tlli_cache->list); |
| 332 | talloc_free(tlli_cache); |
| 333 | } |
| 334 | } |
| 335 | return count; |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 336 | |
| 337 | } |
| 338 | /*********************************************************************** |
| 339 | * IMSI cache |
| 340 | ***********************************************************************/ |
| 341 | static inline uint16_t _checksum_imsi(const char *imsi) |
| 342 | { |
| 343 | size_t len = strlen(imsi); |
| 344 | return osmo_crc16(0, (const uint8_t *)imsi, len); |
| 345 | } |
| 346 | |
| 347 | static inline struct gbproxy_imsi_cache_entry *_get_imsi_entry(struct gbproxy_config *cfg, const char *imsi) |
| 348 | { |
| 349 | struct gbproxy_imsi_cache_entry *cache_entry; |
| 350 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 351 | |
| 352 | hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) { |
| 353 | if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi))) |
| 354 | return cache_entry; |
| 355 | } |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi) |
| 360 | { |
| 361 | struct gbproxy_config *cfg = nse->cfg; |
| 362 | struct timespec now; |
| 363 | struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi); |
| 364 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 365 | |
| 366 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 367 | |
| 368 | if (cache_entry) { |
| 369 | /* Update the entry if it already exists */ |
| 370 | cache_entry->nse = nse; |
| 371 | cache_entry->tstamp = now.tv_sec; |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry); |
| 376 | OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi); |
| 377 | cache_entry->nse = nse; |
| 378 | cache_entry->tstamp = now.tv_sec; |
| 379 | hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash); |
| 380 | } |
| 381 | |
| 382 | static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) { |
| 383 | uint i; |
| 384 | struct gbproxy_config *cfg = nse->cfg; |
| 385 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 386 | struct hlist_node *tmp; |
| 387 | |
| 388 | hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) { |
| 389 | if (imsi_cache->nse == nse) { |
| 390 | hash_del(&imsi_cache->list); |
| 391 | talloc_free(imsi_cache); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi) |
| 397 | { |
| 398 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 399 | struct hlist_node *tmp; |
| 400 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 401 | |
| 402 | hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) { |
| 403 | if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)))) { |
| 404 | hash_del(&imsi_cache->list); |
| 405 | talloc_free(imsi_cache); |
| 406 | return; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg) |
| 412 | { |
| 413 | int i, count = 0; |
| 414 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 415 | struct hlist_node *tmp; |
| 416 | struct timespec now; |
| 417 | time_t expiry; |
| 418 | |
| 419 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 420 | expiry = now.tv_sec - cfg->imsi_cache.timeout; |
| 421 | |
| 422 | hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) { |
| 423 | if (imsi_cache->tstamp < expiry) { |
| 424 | count++; |
| 425 | LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi); |
| 426 | hash_del(&imsi_cache->list); |
| 427 | talloc_free(imsi_cache); |
| 428 | } |
| 429 | } |
| 430 | return count; |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 433 | /*********************************************************************** |
| 434 | * NSE - NS Entity |
| 435 | ***********************************************************************/ |
| 436 | |
| 437 | struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing) |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 438 | { |
| 439 | struct gbproxy_nse *nse; |
| 440 | OSMO_ASSERT(cfg); |
| 441 | |
| 442 | nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse); |
| 443 | if (!nse) |
| 444 | return NULL; |
| 445 | |
| 446 | nse->nsei = nsei; |
| 447 | nse->cfg = cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 448 | nse->sgsn_facing = sgsn_facing; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 449 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 450 | if (sgsn_facing) |
| 451 | hash_add(cfg->sgsn_nses, &nse->list, nsei); |
| 452 | else |
| 453 | hash_add(cfg->bss_nses, &nse->list, nsei); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 454 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 455 | hash_init(nse->bvcs); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 456 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 457 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n"); |
| 458 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 459 | return nse; |
| 460 | } |
| 461 | |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 462 | static void _nse_free(struct gbproxy_nse *nse) |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 463 | { |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 464 | struct gbproxy_bvc *bvc; |
| 465 | struct hlist_node *tmp; |
| 466 | int i; |
| 467 | |
Daniel Willmann | f6a36cc | 2020-12-04 17:38:46 +0100 | [diff] [blame] | 468 | if (!nse) |
| 469 | return; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 470 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 471 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n"); |
| 472 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 473 | hash_del(&nse->list); |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 474 | /* Clear the cache entries of this NSE */ |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 475 | _tlli_cache_remove_nse(nse); |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 476 | _imsi_cache_remove_nse(nse); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 477 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 478 | hash_for_each_safe(nse->bvcs, i, tmp, bvc, list) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 479 | gbproxy_bvc_free(bvc); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 480 | |
| 481 | talloc_free(nse); |
| 482 | } |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 483 | static void _sgsn_free(struct gbproxy_sgsn *sgsn); |
| 484 | |
| 485 | void gbproxy_nse_free(struct gbproxy_nse *nse) |
| 486 | { |
| 487 | if (!nse) |
| 488 | return; |
| 489 | OSMO_ASSERT(nse->cfg); |
| 490 | |
| 491 | if (nse->sgsn_facing) { |
| 492 | struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei); |
| 493 | OSMO_ASSERT(sgsn); |
| 494 | _sgsn_free(sgsn); |
| 495 | } |
| 496 | |
| 497 | _nse_free(nse); |
| 498 | } |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 499 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 500 | struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags) |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 501 | { |
| 502 | struct gbproxy_nse *nse; |
| 503 | OSMO_ASSERT(cfg); |
| 504 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 505 | if (flags & NSE_F_SGSN) { |
| 506 | hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) { |
| 507 | if (nse->nsei == nsei) |
| 508 | return nse; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (flags & NSE_F_BSS) { |
| 513 | hash_for_each_possible(cfg->bss_nses, nse, list, nsei) { |
| 514 | if (nse->nsei == nsei) |
| 515 | return nse; |
| 516 | } |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | return NULL; |
| 520 | } |
| 521 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 522 | struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing) |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 523 | { |
| 524 | struct gbproxy_nse *nse; |
| 525 | OSMO_ASSERT(cfg); |
| 526 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 527 | nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 528 | if (!nse) |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 529 | nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 530 | |
| 531 | return nse; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 532 | } |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 533 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 534 | struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli) |
| 535 | { |
| 536 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 537 | |
| 538 | hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) { |
| 539 | if (tlli_cache->tlli == tlli) |
| 540 | return tlli_cache->nse; |
| 541 | } |
| 542 | return NULL; |
| 543 | } |
| 544 | |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 545 | struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi) |
| 546 | { |
| 547 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 548 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 549 | |
| 550 | hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) { |
| 551 | if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi))) |
| 552 | return imsi_cache->nse; |
| 553 | } |
| 554 | return NULL; |
| 555 | } |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 556 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 557 | /*********************************************************************** |
| 558 | * SGSN - Serving GPRS Support Node |
| 559 | ***********************************************************************/ |
| 560 | |
| 561 | /*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well |
| 562 | * \param[in] cfg The gbproxy configuration |
| 563 | * \param[in] nsei The nsei where the SGSN can be reached |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 564 | * \param[in] name A name to give the SGSN |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 565 | * \return The SGSN, NULL if it couldn't be allocated |
| 566 | */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 567 | struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name) |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 568 | { |
| 569 | struct gbproxy_sgsn *sgsn; |
| 570 | OSMO_ASSERT(cfg); |
| 571 | |
| 572 | sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn); |
| 573 | if (!sgsn) |
| 574 | return NULL; |
| 575 | |
| 576 | sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true); |
| 577 | if (!sgsn->nse) { |
Vadim Yanitskiy | 6964e88 | 2021-01-05 14:42:46 +0100 | [diff] [blame] | 578 | LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n", |
| 579 | nsei, sgsn->name); |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 580 | goto free_sgsn; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 583 | if (name) |
| 584 | sgsn->name = talloc_strdup(sgsn, name); |
| 585 | else |
| 586 | sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei); |
| 587 | if (!sgsn->name) |
| 588 | goto free_sgsn; |
| 589 | |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 590 | sgsn->pool.allow_attach = true; |
| 591 | sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn); |
| 592 | |
| 593 | llist_add_tail(&sgsn->list, &cfg->sgsns); |
| 594 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n"); |
| 595 | return sgsn; |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 596 | |
| 597 | free_sgsn: |
| 598 | talloc_free(sgsn); |
| 599 | return NULL; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Only free gbproxy_sgsn, sgsn can't be NULL */ |
| 603 | static void _sgsn_free(struct gbproxy_sgsn *sgsn) { |
| 604 | struct gbproxy_config *cfg; |
| 605 | |
| 606 | OSMO_ASSERT(sgsn->nse); |
| 607 | cfg = sgsn->nse->cfg; |
| 608 | OSMO_ASSERT(cfg); |
| 609 | |
| 610 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n"); |
| 611 | llist_del(&sgsn->list); |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 612 | /* talloc will free ->name and ->pool.nri_ranges */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 613 | talloc_free(sgsn); |
| 614 | } |
| 615 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 616 | /*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well |
| 617 | * \param[in] sgsn The SGSN |
| 618 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 619 | void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn) |
| 620 | { |
| 621 | if (!sgsn) |
| 622 | return; |
| 623 | |
| 624 | OSMO_ASSERT(sgsn->nse) |
| 625 | |
| 626 | _nse_free(sgsn->nse); |
| 627 | _sgsn_free(sgsn); |
| 628 | } |
| 629 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 630 | /*! Return the SGSN for a given NSEI |
| 631 | * \param[in] cfg The gbproxy configuration |
| 632 | * \param[in] nsei The nsei where the SGSN can be reached |
| 633 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 634 | */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 635 | struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name) |
| 636 | { |
| 637 | struct gbproxy_sgsn *sgsn; |
| 638 | OSMO_ASSERT(cfg); |
| 639 | |
| 640 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 641 | if (!strcmp(sgsn->name, name)) |
| 642 | return sgsn; |
| 643 | } |
| 644 | |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | /*! Return the SGSN for a given NSEI |
| 649 | * \param[in] cfg The gbproxy configuration |
| 650 | * \param[in] nsei The nsei where the SGSN can be reached |
| 651 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 652 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 653 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 654 | { |
| 655 | struct gbproxy_sgsn *sgsn; |
| 656 | OSMO_ASSERT(cfg); |
| 657 | |
| 658 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 659 | if (sgsn->nse->nsei == nsei) |
| 660 | return sgsn; |
| 661 | } |
| 662 | |
| 663 | return NULL; |
| 664 | } |
| 665 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 666 | /*! Return the SGSN for a given NSEI, creating a new one if none exists |
| 667 | * \param[in] cfg The gbproxy configuration |
| 668 | * \param[in] nsei The nsei where the SGSN can be reached |
| 669 | * \return Returns the SGSN |
| 670 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 671 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei) |
| 672 | { |
| 673 | struct gbproxy_sgsn *sgsn; |
| 674 | OSMO_ASSERT(cfg); |
| 675 | |
| 676 | sgsn = gbproxy_sgsn_by_nsei(cfg, nsei); |
| 677 | if (!sgsn) |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 678 | sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL); |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 679 | |
| 680 | return sgsn; |
| 681 | } |
| 682 | |
| 683 | /*! Return the gbproxy_sgsn matching that NRI |
| 684 | * \param[in] cfg proxy in which we operate |
| 685 | * \param[in] nri NRI to look for |
| 686 | * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI |
| 687 | * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found |
| 688 | */ |
| 689 | struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri) |
| 690 | { |
| 691 | struct gbproxy_sgsn *sgsn; |
| 692 | OSMO_ASSERT(cfg); |
| 693 | |
| 694 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 695 | if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) { |
| 696 | /* Also check if the NRI we're looking for is a NULL NRI */ |
Vadim Yanitskiy | 4dba67a | 2021-01-05 14:36:52 +0100 | [diff] [blame] | 697 | if (null_nri) { |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 698 | if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges)) |
| 699 | *null_nri = true; |
| 700 | else |
| 701 | *null_nri = false; |
| 702 | } |
| 703 | return sgsn; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return NULL; |
| 708 | } |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 709 | |
| 710 | /*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections |
| 711 | * \param[in] cfg The gbproxy configuration |
| 712 | * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution |
| 713 | * \param[in] tlli The tlli to choose an SGSN for. The same tlli will map to the same SGSN as long as no SGSN is |
| 714 | * added/removed or allow_attach changes. |
| 715 | * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found |
| 716 | */ |
| 717 | struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid, |
| 718 | uint32_t tlli) |
| 719 | { |
| 720 | uint32_t i = 0; |
| 721 | uint32_t index, num_sgsns; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 722 | OSMO_ASSERT(cfg); |
| 723 | |
Daniel Willmann | b387c1e | 2020-12-27 18:14:39 +0100 | [diff] [blame] | 724 | struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override; |
| 725 | |
| 726 | if (sgsn) { |
| 727 | LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n"); |
| 728 | return sgsn; |
| 729 | } |
| 730 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 731 | /* TODO: We should keep track of count in cfg */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 732 | num_sgsns = llist_count(&cfg->sgsns); |
| 733 | |
| 734 | if (num_sgsns == 0) |
| 735 | return NULL; |
| 736 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 737 | /* FIXME: 256 SGSNs ought to be enough for everyone */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 738 | index = hash_32(tlli, 8) % num_sgsns; |
| 739 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 740 | /* Get the first enabled SGSN after index */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 741 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 742 | if (i >= index && sgsn->pool.allow_attach) { |
| 743 | return sgsn; |
| 744 | } |
| 745 | i++; |
| 746 | } |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 747 | /* Start again from the beginning */ |
Daniel Willmann | 3c56a2a | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 748 | i = 0; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 749 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
Daniel Willmann | 3c56a2a | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 750 | if (i >= index) { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 751 | break; |
| 752 | } else if (sgsn->pool.allow_attach) { |
| 753 | return sgsn; |
| 754 | } |
| 755 | i++; |
| 756 | } |
| 757 | |
| 758 | return NULL; |
| 759 | } |