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