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