Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +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 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 23 | #include <osmocom/sgsn/gb_proxy.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 24 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 25 | #include <osmocom/sgsn/debug.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 28 | #include <osmocom/core/logging.h> |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 29 | #include <osmocom/core/linuxlist.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 30 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 31 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 33 | #include <osmocom/gsm/tlv.h> |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 34 | |
| 35 | #include <string.h> |
| 36 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 37 | extern void *tall_sgsn_ctx; |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 38 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 39 | static const struct rate_ctr_desc bvc_ctr_description[] = { |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +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 " }, |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 47 | osmo_static_assert(ARRAY_SIZE(bvc_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described); |
Holger Hans Peter Freyther | 98fa3dc | 2015-11-09 14:30:22 +0100 | [diff] [blame] | 48 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 49 | static const struct rate_ctr_group_desc bvc_ctrg_desc = { |
Max | 8a01a80 | 2017-12-20 13:10:11 +0100 | [diff] [blame] | 50 | .group_name_prefix = "gbproxy:peer", |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 51 | .group_description = "GBProxy Peer Statistics", |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 52 | .num_ctr = ARRAY_SIZE(bvc_ctr_description), |
| 53 | .ctr_desc = bvc_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 54 | .class_id = OSMO_STATS_CLASS_PEER, |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | |
Harald Welte | 8cd7440 | 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 | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 59 | struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 60 | { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 61 | struct gbproxy_bvc *bvc; |
Harald Welte | 5687ae6 | 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; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 65 | } |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 69 | struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 70 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 71 | struct gbproxy_bvc *bvc; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 72 | OSMO_ASSERT(nse); |
| 73 | struct gbproxy_config *cfg = nse->cfg; |
| 74 | OSMO_ASSERT(cfg); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 75 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 76 | bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc); |
| 77 | if (!bvc) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 78 | return NULL; |
| 79 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 80 | bvc->bvci = bvci; |
Harald Welte | c91f53c | 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 | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 82 | if (!bvc->ctrg) { |
| 83 | talloc_free(bvc); |
Harald Welte | 26c1465 | 2017-07-12 00:25:51 +0200 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 86 | bvc->nse = nse; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 87 | |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 88 | hash_add(nse->bvcs, &bvc->list, bvc->bvci); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 89 | |
Harald Welte | 7cb76a4 | 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 | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 95 | return bvc; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 98 | void gbproxy_bvc_free(struct gbproxy_bvc *bvc) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 99 | { |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 100 | struct gbproxy_cell *cell; |
| 101 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 102 | if (!bvc) |
Daniel Willmann | e794c1f | 2020-12-04 17:38:46 +0100 | [diff] [blame] | 103 | return; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 104 | |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 105 | LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n"); |
| 106 | |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 107 | hash_del(&bvc->list); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 108 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 109 | rate_ctr_group_free(bvc->ctrg); |
| 110 | bvc->ctrg = NULL; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 111 | |
Harald Welte | 5687ae6 | 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 | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 129 | talloc_free(bvc); |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 132 | /*! remove BVCs on NSE specified by NSEI. |
Harald Welte | 8cd7440 | 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 | 5687ae6 | 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) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 137 | { |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 138 | struct hlist_node *btmp; |
| 139 | struct gbproxy_bvc *bvc; |
| 140 | int j, counter = 0; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 141 | |
Harald Welte | 5687ae6 | 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) |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 147 | continue; |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 148 | if (bvci == 0 && bvc->bvci == 0) |
| 149 | continue; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 150 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 151 | gbproxy_bvc_free(bvc); |
| 152 | counter += 1; |
Jacob Erlbeck | 5f1faa3 | 2014-08-21 10:01:30 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return counter; |
| 156 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 157 | |
Harald Welte | 5687ae6 | 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 | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 178 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n"); |
| 179 | |
Harald Welte | 5687ae6 | 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 | |
| 195 | struct gbproxy_cell *gbproxy_cell_by_bvci_or_new(struct gbproxy_config *cfg, uint16_t bvci) |
| 196 | { |
| 197 | struct gbproxy_cell *cell; |
| 198 | OSMO_ASSERT(cfg); |
| 199 | |
| 200 | cell = gbproxy_cell_by_bvci(cfg, bvci); |
| 201 | if (!cell) |
| 202 | cell = gbproxy_cell_alloc(cfg, bvci); |
| 203 | |
| 204 | return cell; |
| 205 | } |
| 206 | |
| 207 | void gbproxy_cell_free(struct gbproxy_cell *cell) |
| 208 | { |
| 209 | unsigned int i; |
| 210 | |
| 211 | if (!cell) |
| 212 | return; |
| 213 | |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 214 | LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n"); |
| 215 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 216 | /* remove from cfg.cells */ |
| 217 | hash_del(&cell->list); |
| 218 | |
| 219 | /* remove back-pointers from the BSS side */ |
| 220 | if (cell->bss_bvc && cell->bss_bvc->cell) |
| 221 | cell->bss_bvc->cell = NULL; |
| 222 | |
| 223 | /* remove back-pointers from the SGSN side */ |
| 224 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 225 | if (!cell->sgsn_bvc[i]) |
| 226 | continue; |
| 227 | if (cell->sgsn_bvc[i]->cell) |
| 228 | cell->sgsn_bvc[i]->cell = NULL; |
| 229 | } |
| 230 | |
| 231 | talloc_free(cell); |
| 232 | } |
| 233 | |
| 234 | bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc) |
| 235 | { |
| 236 | unsigned int i; |
| 237 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 238 | if (!cell->sgsn_bvc[i]) { |
| 239 | cell->sgsn_bvc[i] = bvc; |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 240 | LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n"); |
| 241 | LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n"); |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | /*********************************************************************** |
| 249 | * NSE - NS Entity |
| 250 | ***********************************************************************/ |
| 251 | |
| 252 | struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing) |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 253 | { |
| 254 | struct gbproxy_nse *nse; |
| 255 | OSMO_ASSERT(cfg); |
| 256 | |
| 257 | nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse); |
| 258 | if (!nse) |
| 259 | return NULL; |
| 260 | |
| 261 | nse->nsei = nsei; |
| 262 | nse->cfg = cfg; |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 263 | nse->sgsn_facing = sgsn_facing; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 264 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 265 | if (sgsn_facing) |
| 266 | hash_add(cfg->sgsn_nses, &nse->list, nsei); |
| 267 | else |
| 268 | hash_add(cfg->bss_nses, &nse->list, nsei); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 269 | |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 270 | hash_init(nse->bvcs); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 271 | |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 272 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n"); |
| 273 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 274 | return nse; |
| 275 | } |
| 276 | |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 277 | static void _nse_free(struct gbproxy_nse *nse) |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 278 | { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 279 | struct gbproxy_bvc *bvc; |
| 280 | struct hlist_node *tmp; |
| 281 | int i; |
| 282 | |
Daniel Willmann | e794c1f | 2020-12-04 17:38:46 +0100 | [diff] [blame] | 283 | if (!nse) |
| 284 | return; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 285 | |
Harald Welte | 7cb76a4 | 2020-12-07 12:03:10 +0100 | [diff] [blame] | 286 | LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n"); |
| 287 | |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 288 | hash_del(&nse->list); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 289 | |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 290 | hash_for_each_safe(nse->bvcs, i, tmp, bvc, list) |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 291 | gbproxy_bvc_free(bvc); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 292 | |
| 293 | talloc_free(nse); |
| 294 | } |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 295 | static void _sgsn_free(struct gbproxy_sgsn *sgsn); |
| 296 | |
| 297 | void gbproxy_nse_free(struct gbproxy_nse *nse) |
| 298 | { |
| 299 | if (!nse) |
| 300 | return; |
| 301 | OSMO_ASSERT(nse->cfg); |
| 302 | |
| 303 | if (nse->sgsn_facing) { |
| 304 | struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei); |
| 305 | OSMO_ASSERT(sgsn); |
| 306 | _sgsn_free(sgsn); |
| 307 | } |
| 308 | |
| 309 | _nse_free(nse); |
| 310 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 311 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 312 | struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags) |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 313 | { |
| 314 | struct gbproxy_nse *nse; |
| 315 | OSMO_ASSERT(cfg); |
| 316 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 317 | if (flags & NSE_F_SGSN) { |
| 318 | hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) { |
| 319 | if (nse->nsei == nsei) |
| 320 | return nse; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (flags & NSE_F_BSS) { |
| 325 | hash_for_each_possible(cfg->bss_nses, nse, list, nsei) { |
| 326 | if (nse->nsei == nsei) |
| 327 | return nse; |
| 328 | } |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return NULL; |
| 332 | } |
| 333 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 334 | struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing) |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 335 | { |
| 336 | struct gbproxy_nse *nse; |
| 337 | OSMO_ASSERT(cfg); |
| 338 | |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 339 | nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 340 | if (!nse) |
Harald Welte | 5687ae6 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 341 | nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 342 | |
| 343 | return nse; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 344 | } |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 345 | |
Daniel Willmann | 00c1f91 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 346 | /*********************************************************************** |
| 347 | * SGSN - Serving GPRS Support Node |
| 348 | ***********************************************************************/ |
| 349 | |
| 350 | /*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well |
| 351 | * \param[in] cfg The gbproxy configuration |
| 352 | * \param[in] nsei The nsei where the SGSN can be reached |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 353 | * \param[in] name A name to give the SGSN |
Daniel Willmann | 00c1f91 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 354 | * \return The SGSN, NULL if it couldn't be allocated |
| 355 | */ |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 356 | struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name) |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 357 | { |
| 358 | struct gbproxy_sgsn *sgsn; |
| 359 | OSMO_ASSERT(cfg); |
| 360 | |
| 361 | sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn); |
| 362 | if (!sgsn) |
| 363 | return NULL; |
| 364 | |
| 365 | sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true); |
| 366 | if (!sgsn->nse) { |
Vadim Yanitskiy | e245677 | 2021-01-05 14:42:46 +0100 | [diff] [blame] | 367 | LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n", |
| 368 | nsei, sgsn->name); |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 369 | goto free_sgsn; |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 372 | if (name) |
| 373 | sgsn->name = talloc_strdup(sgsn, name); |
| 374 | else |
| 375 | sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei); |
| 376 | if (!sgsn->name) |
| 377 | goto free_sgsn; |
| 378 | |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 379 | sgsn->pool.allow_attach = true; |
| 380 | sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn); |
| 381 | |
| 382 | llist_add_tail(&sgsn->list, &cfg->sgsns); |
| 383 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n"); |
| 384 | return sgsn; |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 385 | |
| 386 | free_sgsn: |
| 387 | talloc_free(sgsn); |
| 388 | return NULL; |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Only free gbproxy_sgsn, sgsn can't be NULL */ |
| 392 | static void _sgsn_free(struct gbproxy_sgsn *sgsn) { |
| 393 | struct gbproxy_config *cfg; |
| 394 | |
| 395 | OSMO_ASSERT(sgsn->nse); |
| 396 | cfg = sgsn->nse->cfg; |
| 397 | OSMO_ASSERT(cfg); |
| 398 | |
| 399 | LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n"); |
| 400 | llist_del(&sgsn->list); |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 401 | // talloc will free ->name and ->pool.nri_ranges |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 402 | talloc_free(sgsn); |
| 403 | } |
| 404 | |
Daniel Willmann | 00c1f91 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 405 | /*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well |
| 406 | * \param[in] sgsn The SGSN |
| 407 | */ |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 408 | void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn) |
| 409 | { |
| 410 | if (!sgsn) |
| 411 | return; |
| 412 | |
| 413 | OSMO_ASSERT(sgsn->nse) |
| 414 | |
| 415 | _nse_free(sgsn->nse); |
| 416 | _sgsn_free(sgsn); |
| 417 | } |
| 418 | |
Daniel Willmann | 00c1f91 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 419 | /*! Return the SGSN for a given NSEI |
| 420 | * \param[in] cfg The gbproxy configuration |
| 421 | * \param[in] nsei The nsei where the SGSN can be reached |
| 422 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 423 | */ |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 424 | struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name) |
| 425 | { |
| 426 | struct gbproxy_sgsn *sgsn; |
| 427 | OSMO_ASSERT(cfg); |
| 428 | |
| 429 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 430 | if (!strcmp(sgsn->name, name)) |
| 431 | return sgsn; |
| 432 | } |
| 433 | |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | /*! Return the SGSN for a given NSEI |
| 438 | * \param[in] cfg The gbproxy configuration |
| 439 | * \param[in] nsei The nsei where the SGSN can be reached |
| 440 | * \return Returns the matching SGSN or NULL if it couldn't be found |
| 441 | */ |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 442 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 443 | { |
| 444 | struct gbproxy_sgsn *sgsn; |
| 445 | OSMO_ASSERT(cfg); |
| 446 | |
| 447 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 448 | if (sgsn->nse->nsei == nsei) |
| 449 | return sgsn; |
| 450 | } |
| 451 | |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Daniel Willmann | 00c1f91 | 2020-12-29 16:07:17 +0100 | [diff] [blame] | 455 | /*! Return the SGSN for a given NSEI, creating a new one if none exists |
| 456 | * \param[in] cfg The gbproxy configuration |
| 457 | * \param[in] nsei The nsei where the SGSN can be reached |
| 458 | * \return Returns the SGSN |
| 459 | */ |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 460 | struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei) |
| 461 | { |
| 462 | struct gbproxy_sgsn *sgsn; |
| 463 | OSMO_ASSERT(cfg); |
| 464 | |
| 465 | sgsn = gbproxy_sgsn_by_nsei(cfg, nsei); |
| 466 | if (!sgsn) |
Daniel Willmann | dee0bcc | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 467 | sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL); |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 468 | |
| 469 | return sgsn; |
| 470 | } |
| 471 | |
| 472 | /*! Return the gbproxy_sgsn matching that NRI |
| 473 | * \param[in] cfg proxy in which we operate |
| 474 | * \param[in] nri NRI to look for |
| 475 | * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI |
| 476 | * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found |
| 477 | */ |
| 478 | struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri) |
| 479 | { |
| 480 | struct gbproxy_sgsn *sgsn; |
| 481 | OSMO_ASSERT(cfg); |
| 482 | |
| 483 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 484 | if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) { |
| 485 | /* Also check if the NRI we're looking for is a NULL NRI */ |
Vadim Yanitskiy | 1c33e4a | 2021-01-05 14:36:52 +0100 | [diff] [blame] | 486 | if (null_nri) { |
Daniel Willmann | ef3c9af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 487 | if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges)) |
| 488 | *null_nri = true; |
| 489 | else |
| 490 | *null_nri = false; |
| 491 | } |
| 492 | return sgsn; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return NULL; |
| 497 | } |
Daniel Willmann | 8d382c5 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 498 | |
| 499 | /*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections |
| 500 | * \param[in] cfg The gbproxy configuration |
| 501 | * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution |
| 502 | * \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 |
| 503 | * added/removed or allow_attach changes. |
| 504 | * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found |
| 505 | */ |
| 506 | struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid, |
| 507 | uint32_t tlli) |
| 508 | { |
| 509 | uint32_t i = 0; |
| 510 | uint32_t index, num_sgsns; |
Daniel Willmann | 8d382c5 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 511 | OSMO_ASSERT(cfg); |
| 512 | |
Daniel Willmann | 3844da9 | 2020-12-27 18:14:39 +0100 | [diff] [blame] | 513 | struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override; |
| 514 | |
| 515 | if (sgsn) { |
| 516 | LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n"); |
| 517 | return sgsn; |
| 518 | } |
| 519 | |
Daniel Willmann | 8d382c5 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 520 | // TODO: We should keep track of count in cfg |
| 521 | num_sgsns = llist_count(&cfg->sgsns); |
| 522 | |
| 523 | if (num_sgsns == 0) |
| 524 | return NULL; |
| 525 | |
| 526 | // FIXME: 256 SGSNs ought to be enough for everyone |
| 527 | index = hash_32(tlli, 8) % num_sgsns; |
| 528 | |
| 529 | // Get the first enabled SGSN after index |
| 530 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
| 531 | if (i >= index && sgsn->pool.allow_attach) { |
| 532 | return sgsn; |
| 533 | } |
| 534 | i++; |
| 535 | } |
| 536 | // Start again from the beginning |
Daniel Willmann | ddb3fbb | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 537 | i = 0; |
Daniel Willmann | 8d382c5 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 538 | llist_for_each_entry(sgsn, &cfg->sgsns, list) { |
Daniel Willmann | ddb3fbb | 2021-01-05 15:52:05 +0100 | [diff] [blame] | 539 | if (i >= index) { |
Daniel Willmann | 8d382c5 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 540 | break; |
| 541 | } else if (sgsn->pool.allow_attach) { |
| 542 | return sgsn; |
| 543 | } |
| 544 | i++; |
| 545 | } |
| 546 | |
| 547 | return NULL; |
| 548 | } |