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 | |
Oliver Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 25 | #include "debug.h" |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 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 */ |
Philipp Maier | e4597ec | 2021-02-09 16:02:00 +0100 | [diff] [blame^] | 166 | struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci, |
| 167 | const struct gprs_ra_id *raid, uint16_t cid) |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 168 | { |
| 169 | struct gbproxy_cell *cell; |
| 170 | OSMO_ASSERT(cfg); |
| 171 | |
| 172 | cell = talloc_zero(cfg, struct gbproxy_cell); |
| 173 | if (!cell) |
| 174 | return NULL; |
| 175 | |
| 176 | cell->cfg = cfg; |
| 177 | cell->bvci = bvci; |
Philipp Maier | e4597ec | 2021-02-09 16:02:00 +0100 | [diff] [blame^] | 178 | cell->id.cid = cid; |
| 179 | memcpy(&cell->id.raid, raid, sizeof(cell->id.raid)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 180 | |
| 181 | hash_add(cfg->cells, &cell->list, cell->bvci); |
| 182 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 183 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n"); |
| 184 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 185 | return cell; |
| 186 | } |
| 187 | |
| 188 | /* Find cell by BVCI */ |
| 189 | struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci) |
| 190 | { |
| 191 | struct gbproxy_cell *cell; |
| 192 | |
| 193 | hash_for_each_possible(cfg->cells, cell, list, bvci) { |
| 194 | if (cell->bvci == bvci) |
| 195 | return cell; |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Daniel Willmann | fccbef0 | 2021-01-19 17:59:19 +0100 | [diff] [blame] | 200 | struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid) |
| 201 | { |
| 202 | int i; |
| 203 | struct gbproxy_cell *cell; |
| 204 | |
| 205 | hash_for_each(cfg->cells, i, cell, list) { |
| 206 | if (cell->id.cid == cid && gsm48_ra_equal(&cell->id.raid, raid)) { |
| 207 | return cell; |
| 208 | } |
| 209 | } |
| 210 | return NULL; |
| 211 | } |
| 212 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 213 | void gbproxy_cell_free(struct gbproxy_cell *cell) |
| 214 | { |
| 215 | unsigned int i; |
| 216 | |
| 217 | if (!cell) |
| 218 | return; |
| 219 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 220 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n"); |
| 221 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 222 | /* remove from cfg.cells */ |
| 223 | hash_del(&cell->list); |
| 224 | |
| 225 | /* remove back-pointers from the BSS side */ |
| 226 | if (cell->bss_bvc && cell->bss_bvc->cell) |
| 227 | cell->bss_bvc->cell = NULL; |
| 228 | |
| 229 | /* remove back-pointers from the SGSN side */ |
| 230 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 231 | if (!cell->sgsn_bvc[i]) |
| 232 | continue; |
| 233 | if (cell->sgsn_bvc[i]->cell) |
| 234 | cell->sgsn_bvc[i]->cell = NULL; |
| 235 | } |
| 236 | |
| 237 | talloc_free(cell); |
| 238 | } |
| 239 | |
| 240 | bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc) |
| 241 | { |
| 242 | unsigned int i; |
| 243 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 244 | if (!cell->sgsn_bvc[i]) { |
| 245 | cell->sgsn_bvc[i] = bvc; |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 246 | LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n"); |
| 247 | LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n"); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 254 | |
| 255 | /*********************************************************************** |
| 256 | * TLLI cache |
| 257 | ***********************************************************************/ |
| 258 | |
Daniel Willmann | 9170c34 | 2021-01-17 13:12:46 +0100 | [diff] [blame] | 259 | static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli) |
| 260 | { |
| 261 | struct gbproxy_tlli_cache_entry *cache_entry; |
| 262 | |
| 263 | hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) { |
| 264 | if (cache_entry->tlli == tlli) |
| 265 | return cache_entry; |
| 266 | } |
| 267 | return NULL; |
| 268 | } |
| 269 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 270 | void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli) |
| 271 | { |
| 272 | struct gbproxy_config *cfg = nse->cfg; |
| 273 | struct timespec now; |
| 274 | struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli); |
| 275 | |
| 276 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 277 | |
| 278 | if (cache_entry) { |
| 279 | /* Update the entry if it already exists */ |
| 280 | cache_entry->nse = nse; |
| 281 | cache_entry->tstamp = now.tv_sec; |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry); |
| 286 | cache_entry->tlli = tlli; |
| 287 | cache_entry->nse = nse; |
| 288 | cache_entry->tstamp = now.tv_sec; |
| 289 | hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli); |
| 290 | } |
| 291 | |
| 292 | static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) { |
| 293 | uint i; |
| 294 | struct gbproxy_config *cfg = nse->cfg; |
| 295 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 296 | struct hlist_node *tmp; |
| 297 | |
| 298 | hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) { |
| 299 | if (tlli_cache->nse == nse) { |
| 300 | hash_del(&tlli_cache->list); |
| 301 | talloc_free(tlli_cache); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli) |
| 307 | { |
| 308 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 309 | struct hlist_node *tmp; |
| 310 | |
| 311 | hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) { |
| 312 | if (tlli_cache->tlli == tlli) { |
| 313 | hash_del(&tlli_cache->list); |
| 314 | talloc_free(tlli_cache); |
| 315 | return; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg) |
| 321 | { |
| 322 | int i, count = 0; |
| 323 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 324 | struct hlist_node *tmp; |
| 325 | struct timespec now; |
| 326 | time_t expiry; |
| 327 | |
| 328 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 329 | expiry = now.tv_sec - cfg->tlli_cache.timeout; |
| 330 | |
| 331 | hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) { |
| 332 | if (tlli_cache->tstamp < expiry) { |
| 333 | count++; |
| 334 | LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli); |
| 335 | hash_del(&tlli_cache->list); |
| 336 | talloc_free(tlli_cache); |
| 337 | } |
| 338 | } |
| 339 | return count; |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 340 | |
| 341 | } |
| 342 | /*********************************************************************** |
| 343 | * IMSI cache |
| 344 | ***********************************************************************/ |
| 345 | static inline uint16_t _checksum_imsi(const char *imsi) |
| 346 | { |
| 347 | size_t len = strlen(imsi); |
| 348 | return osmo_crc16(0, (const uint8_t *)imsi, len); |
| 349 | } |
| 350 | |
| 351 | static inline struct gbproxy_imsi_cache_entry *_get_imsi_entry(struct gbproxy_config *cfg, const char *imsi) |
| 352 | { |
| 353 | struct gbproxy_imsi_cache_entry *cache_entry; |
| 354 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 355 | |
| 356 | hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) { |
| 357 | if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi))) |
| 358 | return cache_entry; |
| 359 | } |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi) |
| 364 | { |
| 365 | struct gbproxy_config *cfg = nse->cfg; |
| 366 | struct timespec now; |
| 367 | struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi); |
| 368 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 369 | |
| 370 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 371 | |
| 372 | if (cache_entry) { |
| 373 | /* Update the entry if it already exists */ |
| 374 | cache_entry->nse = nse; |
| 375 | cache_entry->tstamp = now.tv_sec; |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry); |
| 380 | OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi); |
| 381 | cache_entry->nse = nse; |
| 382 | cache_entry->tstamp = now.tv_sec; |
| 383 | hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash); |
| 384 | } |
| 385 | |
| 386 | static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) { |
| 387 | uint i; |
| 388 | struct gbproxy_config *cfg = nse->cfg; |
| 389 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 390 | struct hlist_node *tmp; |
| 391 | |
| 392 | hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) { |
| 393 | if (imsi_cache->nse == nse) { |
| 394 | hash_del(&imsi_cache->list); |
| 395 | talloc_free(imsi_cache); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi) |
| 401 | { |
| 402 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 403 | struct hlist_node *tmp; |
| 404 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 405 | |
| 406 | hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) { |
| 407 | if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)))) { |
| 408 | hash_del(&imsi_cache->list); |
| 409 | talloc_free(imsi_cache); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg) |
| 416 | { |
| 417 | int i, count = 0; |
| 418 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 419 | struct hlist_node *tmp; |
| 420 | struct timespec now; |
| 421 | time_t expiry; |
| 422 | |
| 423 | osmo_clock_gettime(CLOCK_MONOTONIC, &now); |
| 424 | expiry = now.tv_sec - cfg->imsi_cache.timeout; |
| 425 | |
| 426 | hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) { |
| 427 | if (imsi_cache->tstamp < expiry) { |
| 428 | count++; |
| 429 | LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi); |
| 430 | hash_del(&imsi_cache->list); |
| 431 | talloc_free(imsi_cache); |
| 432 | } |
| 433 | } |
| 434 | return count; |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 437 | /*********************************************************************** |
| 438 | * NSE - NS Entity |
| 439 | ***********************************************************************/ |
| 440 | |
| 441 | 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] | 442 | { |
| 443 | struct gbproxy_nse *nse; |
| 444 | OSMO_ASSERT(cfg); |
| 445 | |
| 446 | nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse); |
| 447 | if (!nse) |
| 448 | return NULL; |
| 449 | |
| 450 | nse->nsei = nsei; |
| 451 | nse->cfg = cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 452 | nse->sgsn_facing = sgsn_facing; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 453 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 454 | if (sgsn_facing) |
| 455 | hash_add(cfg->sgsn_nses, &nse->list, nsei); |
| 456 | else |
| 457 | hash_add(cfg->bss_nses, &nse->list, nsei); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 458 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 459 | hash_init(nse->bvcs); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 460 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 461 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n"); |
| 462 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 463 | return nse; |
| 464 | } |
| 465 | |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 466 | static void _nse_free(struct gbproxy_nse *nse) |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 467 | { |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 468 | struct gbproxy_bvc *bvc; |
| 469 | struct hlist_node *tmp; |
| 470 | int i; |
| 471 | |
Daniel Willmann | f6a36cc | 2020-12-04 17:38:46 +0100 | [diff] [blame] | 472 | if (!nse) |
| 473 | return; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 474 | |
Harald Welte | cfc7e8e | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 475 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n"); |
| 476 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 477 | hash_del(&nse->list); |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 478 | /* Clear the cache entries of this NSE */ |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 479 | _tlli_cache_remove_nse(nse); |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 480 | _imsi_cache_remove_nse(nse); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 481 | |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 482 | hash_for_each_safe(nse->bvcs, i, tmp, bvc, list) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 483 | gbproxy_bvc_free(bvc); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 484 | |
| 485 | talloc_free(nse); |
| 486 | } |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 487 | static void _sgsn_free(struct gbproxy_sgsn *sgsn); |
| 488 | |
| 489 | void gbproxy_nse_free(struct gbproxy_nse *nse) |
| 490 | { |
| 491 | if (!nse) |
| 492 | return; |
| 493 | OSMO_ASSERT(nse->cfg); |
| 494 | |
| 495 | if (nse->sgsn_facing) { |
| 496 | struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei); |
| 497 | OSMO_ASSERT(sgsn); |
| 498 | _sgsn_free(sgsn); |
| 499 | } |
| 500 | |
| 501 | _nse_free(nse); |
| 502 | } |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 503 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | struct gbproxy_nse *nse; |
| 507 | OSMO_ASSERT(cfg); |
| 508 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 509 | if (flags & NSE_F_SGSN) { |
| 510 | hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) { |
| 511 | if (nse->nsei == nsei) |
| 512 | return nse; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (flags & NSE_F_BSS) { |
| 517 | hash_for_each_possible(cfg->bss_nses, nse, list, nsei) { |
| 518 | if (nse->nsei == nsei) |
| 519 | return nse; |
| 520 | } |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 526 | 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] | 527 | { |
| 528 | struct gbproxy_nse *nse; |
| 529 | OSMO_ASSERT(cfg); |
| 530 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 531 | 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] | 532 | if (!nse) |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 533 | nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 534 | |
| 535 | return nse; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 536 | } |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 537 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 538 | struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli) |
| 539 | { |
| 540 | struct gbproxy_tlli_cache_entry *tlli_cache; |
| 541 | |
| 542 | hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) { |
| 543 | if (tlli_cache->tlli == tlli) |
| 544 | return tlli_cache->nse; |
| 545 | } |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 549 | struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi) |
| 550 | { |
| 551 | struct gbproxy_imsi_cache_entry *imsi_cache; |
| 552 | uint16_t imsi_hash = _checksum_imsi(imsi); |
| 553 | |
| 554 | hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) { |
| 555 | if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi))) |
| 556 | return imsi_cache->nse; |
| 557 | } |
| 558 | return NULL; |
| 559 | } |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 560 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 561 | /*********************************************************************** |
| 562 | * SGSN - Serving GPRS Support Node |
| 563 | ***********************************************************************/ |
| 564 | |
| 565 | /*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well |
| 566 | * \param[in] cfg The gbproxy configuration |
| 567 | * \param[in] nsei The nsei where the SGSN can be reached |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 568 | * \param[in] name A name to give the SGSN |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 569 | * \return The SGSN, NULL if it couldn't be allocated |
| 570 | */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 571 | 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] | 572 | { |
| 573 | struct gbproxy_sgsn *sgsn; |
| 574 | OSMO_ASSERT(cfg); |
| 575 | |
| 576 | sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn); |
| 577 | if (!sgsn) |
| 578 | return NULL; |
| 579 | |
| 580 | sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true); |
| 581 | if (!sgsn->nse) { |
Vadim Yanitskiy | 6964e88 | 2021-01-05 14:42:46 +0100 | [diff] [blame] | 582 | LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n", |
| 583 | nsei, sgsn->name); |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 584 | goto free_sgsn; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 587 | if (name) |
| 588 | sgsn->name = talloc_strdup(sgsn, name); |
| 589 | else |
| 590 | sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei); |
| 591 | if (!sgsn->name) |
| 592 | goto free_sgsn; |
| 593 | |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 594 | sgsn->pool.allow_attach = true; |
| 595 | sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn); |
| 596 | |
| 597 | llist_add_tail(&sgsn->list, &cfg->sgsns); |
| 598 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n"); |
| 599 | return sgsn; |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 600 | |
| 601 | free_sgsn: |
| 602 | talloc_free(sgsn); |
| 603 | return NULL; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* Only free gbproxy_sgsn, sgsn can't be NULL */ |
| 607 | static void _sgsn_free(struct gbproxy_sgsn *sgsn) { |
| 608 | struct gbproxy_config *cfg; |
| 609 | |
| 610 | OSMO_ASSERT(sgsn->nse); |
| 611 | cfg = sgsn->nse->cfg; |
| 612 | OSMO_ASSERT(cfg); |
| 613 | |
| 614 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n"); |
| 615 | llist_del(&sgsn->list); |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 616 | /* talloc will free ->name and ->pool.nri_ranges */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 617 | talloc_free(sgsn); |
| 618 | } |
| 619 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 620 | /*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well |
| 621 | * \param[in] sgsn The SGSN |
| 622 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 623 | void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn) |
| 624 | { |
| 625 | if (!sgsn) |
| 626 | return; |
| 627 | |
| 628 | OSMO_ASSERT(sgsn->nse) |
| 629 | |
| 630 | _nse_free(sgsn->nse); |
| 631 | _sgsn_free(sgsn); |
| 632 | } |
| 633 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 634 | /*! Return the SGSN for a given NSEI |
| 635 | * \param[in] cfg The gbproxy configuration |
| 636 | * \param[in] nsei The nsei where the SGSN can be reached |
| 637 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 638 | */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 639 | struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name) |
| 640 | { |
| 641 | struct gbproxy_sgsn *sgsn; |
| 642 | OSMO_ASSERT(cfg); |
| 643 | |
| 644 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 645 | if (!strcmp(sgsn->name, name)) |
| 646 | return sgsn; |
| 647 | } |
| 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | /*! Return the SGSN for a given NSEI |
| 653 | * \param[in] cfg The gbproxy configuration |
| 654 | * \param[in] nsei The nsei where the SGSN can be reached |
| 655 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 656 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 657 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 658 | { |
| 659 | struct gbproxy_sgsn *sgsn; |
| 660 | OSMO_ASSERT(cfg); |
| 661 | |
| 662 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 663 | if (sgsn->nse->nsei == nsei) |
| 664 | return sgsn; |
| 665 | } |
| 666 | |
| 667 | return NULL; |
| 668 | } |
| 669 | |
Daniel Willmann | 216dff8 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 670 | /*! Return the SGSN for a given NSEI, creating a new one if none exists |
| 671 | * \param[in] cfg The gbproxy configuration |
| 672 | * \param[in] nsei The nsei where the SGSN can be reached |
| 673 | * \return Returns the SGSN |
| 674 | */ |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 675 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei) |
| 676 | { |
| 677 | struct gbproxy_sgsn *sgsn; |
| 678 | OSMO_ASSERT(cfg); |
| 679 | |
| 680 | sgsn = gbproxy_sgsn_by_nsei(cfg, nsei); |
| 681 | if (!sgsn) |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 682 | sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL); |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 683 | |
| 684 | return sgsn; |
| 685 | } |
| 686 | |
| 687 | /*! Return the gbproxy_sgsn matching that NRI |
| 688 | * \param[in] cfg proxy in which we operate |
| 689 | * \param[in] nri NRI to look for |
| 690 | * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI |
| 691 | * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found |
| 692 | */ |
| 693 | struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri) |
| 694 | { |
| 695 | struct gbproxy_sgsn *sgsn; |
| 696 | OSMO_ASSERT(cfg); |
| 697 | |
| 698 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 699 | if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) { |
| 700 | /* 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] | 701 | if (null_nri) { |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 702 | if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges)) |
| 703 | *null_nri = true; |
| 704 | else |
| 705 | *null_nri = false; |
| 706 | } |
| 707 | return sgsn; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return NULL; |
| 712 | } |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 713 | |
| 714 | /*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections |
| 715 | * \param[in] cfg The gbproxy configuration |
| 716 | * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution |
| 717 | * \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 |
| 718 | * added/removed or allow_attach changes. |
| 719 | * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found |
| 720 | */ |
| 721 | struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid, |
| 722 | uint32_t tlli) |
| 723 | { |
| 724 | uint32_t i = 0; |
| 725 | uint32_t index, num_sgsns; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 726 | OSMO_ASSERT(cfg); |
| 727 | |
Daniel Willmann | b387c1e | 2020-12-27 18:14:39 +0100 | [diff] [blame] | 728 | struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override; |
| 729 | |
| 730 | if (sgsn) { |
| 731 | LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n"); |
| 732 | return sgsn; |
| 733 | } |
| 734 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 735 | /* TODO: We should keep track of count in cfg */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 736 | num_sgsns = llist_count(&cfg->sgsns); |
| 737 | |
| 738 | if (num_sgsns == 0) |
| 739 | return NULL; |
| 740 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 741 | /* FIXME: 256 SGSNs ought to be enough for everyone */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 742 | index = hash_32(tlli, 8) % num_sgsns; |
| 743 | |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 744 | /* Get the first enabled SGSN after index */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 745 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 746 | if (i >= index && sgsn->pool.allow_attach) { |
| 747 | return sgsn; |
| 748 | } |
| 749 | i++; |
| 750 | } |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 751 | /* Start again from the beginning */ |
Daniel Willmann | 3c56a2a | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 752 | i = 0; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 753 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
Daniel Willmann | 3c56a2a | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 754 | if (i >= index) { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 755 | break; |
| 756 | } else if (sgsn->pool.allow_attach) { |
| 757 | return sgsn; |
| 758 | } |
| 759 | i++; |
| 760 | } |
| 761 | |
| 762 | return NULL; |
| 763 | } |