blob: d2ddfc17e0465079acdc53f2b23ad71689584a06 [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001/* 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 Willmann8f407b12020-12-02 19:33:50 +010028#include <osmocom/core/logging.h>
Daniel Willmannee834af2020-12-14 16:22:39 +010029#include <osmocom/core/linuxlist.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020030#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
37extern void *tall_sgsn_ctx;
38
Harald Welte560bdb32020-12-04 22:24:47 +010039static const struct rate_ctr_desc bvc_ctr_description[] = {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020040 { "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 Pedrol1ddefb12019-08-30 19:48:34 +020045};
46
Harald Welte560bdb32020-12-04 22:24:47 +010047osmo_static_assert(ARRAY_SIZE(bvc_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020048
Harald Welte560bdb32020-12-04 22:24:47 +010049static const struct rate_ctr_group_desc bvc_ctrg_desc = {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020050 .group_name_prefix = "gbproxy:peer",
51 .group_description = "GBProxy Peer Statistics",
Harald Welte560bdb32020-12-04 22:24:47 +010052 .num_ctr = ARRAY_SIZE(bvc_ctr_description),
53 .ctr_desc = bvc_ctr_description,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020054 .class_id = OSMO_STATS_CLASS_PEER,
55};
56
57
Harald Welte560bdb32020-12-04 22:24:47 +010058/* Find the gbproxy_bvc by its BVCI. There can only be one match */
Harald Weltee5209642020-12-05 19:59:45 +010059struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020060{
Harald Welte8b4c7942020-12-05 10:14:49 +010061 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +010062 hash_for_each_possible(nse->bvcs, bvc, list, bvci) {
63 if (bvc->bvci == bvci)
64 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020065 }
66 return NULL;
67}
68
Harald Welte560bdb32020-12-04 22:24:47 +010069struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020070{
Harald Welte560bdb32020-12-04 22:24:47 +010071 struct gbproxy_bvc *bvc;
Daniel Willmanne50550e2020-11-26 18:19:21 +010072 OSMO_ASSERT(nse);
73 struct gbproxy_config *cfg = nse->cfg;
74 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020075
Harald Welte560bdb32020-12-04 22:24:47 +010076 bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc);
77 if (!bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020078 return NULL;
79
Harald Welte560bdb32020-12-04 22:24:47 +010080 bvc->bvci = bvci;
Harald Welte4bd3e492020-12-07 12:06:52 +010081 bvc->ctrg = rate_ctr_group_alloc(bvc, &bvc_ctrg_desc, (nse->nsei << 16) | bvci);
Harald Welte560bdb32020-12-04 22:24:47 +010082 if (!bvc->ctrg) {
83 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020084 return NULL;
85 }
Harald Welte560bdb32020-12-04 22:24:47 +010086 bvc->nse = nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020087
Harald Welte8b4c7942020-12-05 10:14:49 +010088 hash_add(nse->bvcs, &bvc->list, bvc->bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020089
Harald Weltecfc7e8e2020-12-07 12:03:10 +010090 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 Weltee5209642020-12-05 19:59:45 +010095 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020096}
97
Harald Welte560bdb32020-12-04 22:24:47 +010098void gbproxy_bvc_free(struct gbproxy_bvc *bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020099{
Harald Weltee5209642020-12-05 19:59:45 +0100100 struct gbproxy_cell *cell;
101
Harald Welte560bdb32020-12-04 22:24:47 +0100102 if (!bvc)
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100103 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100104
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100105 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n");
106
Harald Welte8b4c7942020-12-05 10:14:49 +0100107 hash_del(&bvc->list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200108
Harald Welte560bdb32020-12-04 22:24:47 +0100109 rate_ctr_group_free(bvc->ctrg);
110 bvc->ctrg = NULL;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200111
Harald Weltee5209642020-12-05 19:59:45 +0100112 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 Welte560bdb32020-12-04 22:24:47 +0100129 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200130}
131
Harald Weltee5209642020-12-05 19:59:45 +0100132/*! remove BVCs on NSE specified by NSEI.
Harald Welte560bdb32020-12-04 22:24:47 +0100133 * \param[in] cfg proxy in which we operate
134 * \param[in] nsei NS entity in which we should clean up
Harald Weltee5209642020-12-05 19:59:45 +0100135 * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */
136int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200137{
Harald Weltee5209642020-12-05 19:59:45 +0100138 struct hlist_node *btmp;
139 struct gbproxy_bvc *bvc;
140 int j, counter = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200141
Harald Weltee5209642020-12-05 19:59:45 +0100142 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 Pedrol1ddefb12019-08-30 19:48:34 +0200147 continue;
Harald Weltee5209642020-12-05 19:59:45 +0100148 if (bvci == 0 && bvc->bvci == 0)
149 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200150
Harald Weltee5209642020-12-05 19:59:45 +0100151 gbproxy_bvc_free(bvc);
152 counter += 1;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200153 }
154
155 return counter;
156}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100157
Harald Weltee5209642020-12-05 19:59:45 +0100158
159/***********************************************************************
160 * CELL
161 ***********************************************************************/
162
163/* Allocate a new 'cell' object */
164struct 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 Weltecfc7e8e2020-12-07 12:03:10 +0100178 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n");
179
Harald Weltee5209642020-12-05 19:59:45 +0100180 return cell;
181}
182
183/* Find cell by BVCI */
184struct 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
195struct 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
207void gbproxy_cell_free(struct gbproxy_cell *cell)
208{
209 unsigned int i;
210
211 if (!cell)
212 return;
213
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100214 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
215
Harald Weltee5209642020-12-05 19:59:45 +0100216 /* 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
234bool 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 Weltecfc7e8e2020-12-07 12:03:10 +0100240 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
241 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100242 return true;
243 }
244 }
245 return false;
246}
247
248/***********************************************************************
249 * NSE - NS Entity
250 ***********************************************************************/
251
252struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100253{
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 Weltee5209642020-12-05 19:59:45 +0100263 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100264
Harald Weltee5209642020-12-05 19:59:45 +0100265 if (sgsn_facing)
266 hash_add(cfg->sgsn_nses, &nse->list, nsei);
267 else
268 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100269
Harald Welte8b4c7942020-12-05 10:14:49 +0100270 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100271
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100272 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
273
Daniel Willmanne50550e2020-11-26 18:19:21 +0100274 return nse;
275}
276
Daniel Willmannee834af2020-12-14 16:22:39 +0100277static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100278{
Harald Welte8b4c7942020-12-05 10:14:49 +0100279 struct gbproxy_bvc *bvc;
280 struct hlist_node *tmp;
281 int i;
282
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100283 if (!nse)
284 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100285
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100286 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
287
Harald Welted2fef952020-12-05 00:31:07 +0100288 hash_del(&nse->list);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100289
Harald Welte8b4c7942020-12-05 10:14:49 +0100290 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100291 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100292
293 talloc_free(nse);
294}
Daniel Willmannee834af2020-12-14 16:22:39 +0100295static void _sgsn_free(struct gbproxy_sgsn *sgsn);
296
297void 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 Willmanne50550e2020-11-26 18:19:21 +0100311
Harald Weltee5209642020-12-05 19:59:45 +0100312struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100313{
314 struct gbproxy_nse *nse;
315 OSMO_ASSERT(cfg);
316
Harald Weltee5209642020-12-05 19:59:45 +0100317 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 Willmanne50550e2020-11-26 18:19:21 +0100329 }
330
331 return NULL;
332}
333
Harald Weltee5209642020-12-05 19:59:45 +0100334struct gbproxy_nse *gbproxy_nse_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100335{
336 struct gbproxy_nse *nse;
337 OSMO_ASSERT(cfg);
338
Harald Weltee5209642020-12-05 19:59:45 +0100339 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100340 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100341 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100342
343 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100344}
Daniel Willmannee834af2020-12-14 16:22:39 +0100345
Daniel Willmann216dff82020-12-29 16:07:17 +0100346/***********************************************************************
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 Willmanna648f3c2020-12-28 18:07:27 +0100353 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100354 * \return The SGSN, NULL if it couldn't be allocated
355 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100356struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100357{
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 Yanitskiy6964e882021-01-05 14:42:46 +0100367 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
368 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100369 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100370 }
371
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100372 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 Willmannee834af2020-12-14 16:22:39 +0100379 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 Willmanna648f3c2020-12-28 18:07:27 +0100385
386free_sgsn:
387 talloc_free(sgsn);
388 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100389}
390
391/* Only free gbproxy_sgsn, sgsn can't be NULL */
392static 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 Willmanna648f3c2020-12-28 18:07:27 +0100401 // talloc will free ->name and ->pool.nri_ranges
Daniel Willmannee834af2020-12-14 16:22:39 +0100402 talloc_free(sgsn);
403}
404
Daniel Willmann216dff82020-12-29 16:07:17 +0100405/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
406 * \param[in] sgsn The SGSN
407 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100408void 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 Willmann216dff82020-12-29 16:07:17 +0100419/*! 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 Willmanna648f3c2020-12-28 18:07:27 +0100424struct 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 Willmannee834af2020-12-14 16:22:39 +0100442struct 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 Willmann216dff82020-12-29 16:07:17 +0100455/*! 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 Willmannee834af2020-12-14 16:22:39 +0100460struct 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 Willmanna648f3c2020-12-28 18:07:27 +0100467 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100468
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 */
478struct 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 Yanitskiy4dba67a2021-01-05 14:36:52 +0100486 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100487 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 Willmannd4ab1f92020-12-21 18:53:55 +0100498
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 */
506struct 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 Willmannd4ab1f92020-12-21 18:53:55 +0100511 OSMO_ASSERT(cfg);
512
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100513 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 Willmannd4ab1f92020-12-21 18:53:55 +0100520 // 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 Willmann3c56a2a2021-01-05 15:52:05 +0100537 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100538 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100539 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100540 break;
541 } else if (sgsn->pool.allow_attach) {
542 return sgsn;
543 }
544 i++;
545 }
546
547 return NULL;
548}