blob: 104902bba31f6754a255986c6151d470d4c4a97a [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
Daniel Willmann77493b12020-12-29 21:13:31 +0100195static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli)
196{
197 struct gbproxy_tlli_cache_entry *cache_entry;
198
199 hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) {
200 if (cache_entry->tlli == tlli)
201 return cache_entry;
202 }
203 return NULL;
204}
205
Harald Weltee5209642020-12-05 19:59:45 +0100206struct gbproxy_cell *gbproxy_cell_by_bvci_or_new(struct gbproxy_config *cfg, uint16_t bvci)
207{
208 struct gbproxy_cell *cell;
209 OSMO_ASSERT(cfg);
210
211 cell = gbproxy_cell_by_bvci(cfg, bvci);
212 if (!cell)
213 cell = gbproxy_cell_alloc(cfg, bvci);
214
215 return cell;
216}
217
218void gbproxy_cell_free(struct gbproxy_cell *cell)
219{
220 unsigned int i;
221
222 if (!cell)
223 return;
224
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100225 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
226
Harald Weltee5209642020-12-05 19:59:45 +0100227 /* remove from cfg.cells */
228 hash_del(&cell->list);
229
230 /* remove back-pointers from the BSS side */
231 if (cell->bss_bvc && cell->bss_bvc->cell)
232 cell->bss_bvc->cell = NULL;
233
234 /* remove back-pointers from the SGSN side */
235 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
236 if (!cell->sgsn_bvc[i])
237 continue;
238 if (cell->sgsn_bvc[i]->cell)
239 cell->sgsn_bvc[i]->cell = NULL;
240 }
241
242 talloc_free(cell);
243}
244
245bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
246{
247 unsigned int i;
248 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
249 if (!cell->sgsn_bvc[i]) {
250 cell->sgsn_bvc[i] = bvc;
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100251 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
252 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100253 return true;
254 }
255 }
256 return false;
257}
258
Daniel Willmann77493b12020-12-29 21:13:31 +0100259
260/***********************************************************************
261 * TLLI cache
262 ***********************************************************************/
263
264void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli)
265{
266 struct gbproxy_config *cfg = nse->cfg;
267 struct timespec now;
268 struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli);
269
270 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
271
272 if (cache_entry) {
273 /* Update the entry if it already exists */
274 cache_entry->nse = nse;
275 cache_entry->tstamp = now.tv_sec;
276 return;
277 }
278
279 cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry);
280 cache_entry->tlli = tlli;
281 cache_entry->nse = nse;
282 cache_entry->tstamp = now.tv_sec;
283 hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli);
284}
285
286static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) {
287 uint i;
288 struct gbproxy_config *cfg = nse->cfg;
289 struct gbproxy_tlli_cache_entry *tlli_cache;
290 struct hlist_node *tmp;
291
292 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
293 if (tlli_cache->nse == nse) {
294 hash_del(&tlli_cache->list);
295 talloc_free(tlli_cache);
296 }
297 }
298}
299
300void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli)
301{
302 struct gbproxy_tlli_cache_entry *tlli_cache;
303 struct hlist_node *tmp;
304
305 hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) {
306 if (tlli_cache->tlli == tlli) {
307 hash_del(&tlli_cache->list);
308 talloc_free(tlli_cache);
309 return;
310 }
311 }
312}
313
314int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg)
315{
316 int i, count = 0;
317 struct gbproxy_tlli_cache_entry *tlli_cache;
318 struct hlist_node *tmp;
319 struct timespec now;
320 time_t expiry;
321
322 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
323 expiry = now.tv_sec - cfg->tlli_cache.timeout;
324
325 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
326 if (tlli_cache->tstamp < expiry) {
327 count++;
328 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli);
329 hash_del(&tlli_cache->list);
330 talloc_free(tlli_cache);
331 }
332 }
333 return count;
334}
335
Harald Weltee5209642020-12-05 19:59:45 +0100336/***********************************************************************
337 * NSE - NS Entity
338 ***********************************************************************/
339
340struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100341{
342 struct gbproxy_nse *nse;
343 OSMO_ASSERT(cfg);
344
345 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
346 if (!nse)
347 return NULL;
348
349 nse->nsei = nsei;
350 nse->cfg = cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100351 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100352
Harald Weltee5209642020-12-05 19:59:45 +0100353 if (sgsn_facing)
354 hash_add(cfg->sgsn_nses, &nse->list, nsei);
355 else
356 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100357
Harald Welte8b4c7942020-12-05 10:14:49 +0100358 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100359
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100360 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
361
Daniel Willmanne50550e2020-11-26 18:19:21 +0100362 return nse;
363}
364
Daniel Willmannee834af2020-12-14 16:22:39 +0100365static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100366{
Harald Welte8b4c7942020-12-05 10:14:49 +0100367 struct gbproxy_bvc *bvc;
368 struct hlist_node *tmp;
369 int i;
370
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100371 if (!nse)
372 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100373
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100374 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
375
Harald Welted2fef952020-12-05 00:31:07 +0100376 hash_del(&nse->list);
Daniel Willmann77493b12020-12-29 21:13:31 +0100377 /* Clear the tlli_cache from this NSE */
378 _tlli_cache_remove_nse(nse);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100379
Harald Welte8b4c7942020-12-05 10:14:49 +0100380 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100381 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100382
383 talloc_free(nse);
384}
Daniel Willmannee834af2020-12-14 16:22:39 +0100385static void _sgsn_free(struct gbproxy_sgsn *sgsn);
386
387void gbproxy_nse_free(struct gbproxy_nse *nse)
388{
389 if (!nse)
390 return;
391 OSMO_ASSERT(nse->cfg);
392
393 if (nse->sgsn_facing) {
394 struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
395 OSMO_ASSERT(sgsn);
396 _sgsn_free(sgsn);
397 }
398
399 _nse_free(nse);
400}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100401
Harald Weltee5209642020-12-05 19:59:45 +0100402struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100403{
404 struct gbproxy_nse *nse;
405 OSMO_ASSERT(cfg);
406
Harald Weltee5209642020-12-05 19:59:45 +0100407 if (flags & NSE_F_SGSN) {
408 hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) {
409 if (nse->nsei == nsei)
410 return nse;
411 }
412 }
413
414 if (flags & NSE_F_BSS) {
415 hash_for_each_possible(cfg->bss_nses, nse, list, nsei) {
416 if (nse->nsei == nsei)
417 return nse;
418 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100419 }
420
421 return NULL;
422}
423
Harald Weltee5209642020-12-05 19:59:45 +0100424struct 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 +0100425{
426 struct gbproxy_nse *nse;
427 OSMO_ASSERT(cfg);
428
Harald Weltee5209642020-12-05 19:59:45 +0100429 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100430 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100431 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100432
433 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100434}
Daniel Willmannee834af2020-12-14 16:22:39 +0100435
Daniel Willmann77493b12020-12-29 21:13:31 +0100436struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli)
437{
438 struct gbproxy_tlli_cache_entry *tlli_cache;
439
440 hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) {
441 if (tlli_cache->tlli == tlli)
442 return tlli_cache->nse;
443 }
444 return NULL;
445}
446
447
Daniel Willmann216dff82020-12-29 16:07:17 +0100448/***********************************************************************
449 * SGSN - Serving GPRS Support Node
450 ***********************************************************************/
451
452/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
453 * \param[in] cfg The gbproxy configuration
454 * \param[in] nsei The nsei where the SGSN can be reached
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100455 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100456 * \return The SGSN, NULL if it couldn't be allocated
457 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100458struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100459{
460 struct gbproxy_sgsn *sgsn;
461 OSMO_ASSERT(cfg);
462
463 sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn);
464 if (!sgsn)
465 return NULL;
466
467 sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
468 if (!sgsn->nse) {
Vadim Yanitskiy6964e882021-01-05 14:42:46 +0100469 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
470 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100471 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100472 }
473
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100474 if (name)
475 sgsn->name = talloc_strdup(sgsn, name);
476 else
477 sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
478 if (!sgsn->name)
479 goto free_sgsn;
480
Daniel Willmannee834af2020-12-14 16:22:39 +0100481 sgsn->pool.allow_attach = true;
482 sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
483
484 llist_add_tail(&sgsn->list, &cfg->sgsns);
485 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
486 return sgsn;
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100487
488free_sgsn:
489 talloc_free(sgsn);
490 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100491}
492
493/* Only free gbproxy_sgsn, sgsn can't be NULL */
494static void _sgsn_free(struct gbproxy_sgsn *sgsn) {
495 struct gbproxy_config *cfg;
496
497 OSMO_ASSERT(sgsn->nse);
498 cfg = sgsn->nse->cfg;
499 OSMO_ASSERT(cfg);
500
501 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
502 llist_del(&sgsn->list);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100503 // talloc will free ->name and ->pool.nri_ranges
Daniel Willmannee834af2020-12-14 16:22:39 +0100504 talloc_free(sgsn);
505}
506
Daniel Willmann216dff82020-12-29 16:07:17 +0100507/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
508 * \param[in] sgsn The SGSN
509 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100510void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)
511{
512 if (!sgsn)
513 return;
514
515 OSMO_ASSERT(sgsn->nse)
516
517 _nse_free(sgsn->nse);
518 _sgsn_free(sgsn);
519}
520
Daniel Willmann216dff82020-12-29 16:07:17 +0100521/*! Return the SGSN for a given NSEI
522 * \param[in] cfg The gbproxy configuration
523 * \param[in] nsei The nsei where the SGSN can be reached
524 * \return Returns the matching SGSN or NULL if it couldn't be found
525 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100526struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
527{
528 struct gbproxy_sgsn *sgsn;
529 OSMO_ASSERT(cfg);
530
531 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
532 if (!strcmp(sgsn->name, name))
533 return sgsn;
534 }
535
536 return NULL;
537}
538
539/*! Return the SGSN for a given NSEI
540 * \param[in] cfg The gbproxy configuration
541 * \param[in] nsei The nsei where the SGSN can be reached
542 * \return Returns the matching SGSN or NULL if it couldn't be found
543 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100544struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
545{
546 struct gbproxy_sgsn *sgsn;
547 OSMO_ASSERT(cfg);
548
549 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
550 if (sgsn->nse->nsei == nsei)
551 return sgsn;
552 }
553
554 return NULL;
555}
556
Daniel Willmann216dff82020-12-29 16:07:17 +0100557/*! Return the SGSN for a given NSEI, creating a new one if none exists
558 * \param[in] cfg The gbproxy configuration
559 * \param[in] nsei The nsei where the SGSN can be reached
560 * \return Returns the SGSN
561 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100562struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
563{
564 struct gbproxy_sgsn *sgsn;
565 OSMO_ASSERT(cfg);
566
567 sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
568 if (!sgsn)
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100569 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100570
571 return sgsn;
572}
573
574/*! Return the gbproxy_sgsn matching that NRI
575 * \param[in] cfg proxy in which we operate
576 * \param[in] nri NRI to look for
577 * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI
578 * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found
579 */
580struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri)
581{
582 struct gbproxy_sgsn *sgsn;
583 OSMO_ASSERT(cfg);
584
585 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
586 if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) {
587 /* Also check if the NRI we're looking for is a NULL NRI */
Vadim Yanitskiy4dba67a2021-01-05 14:36:52 +0100588 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100589 if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges))
590 *null_nri = true;
591 else
592 *null_nri = false;
593 }
594 return sgsn;
595 }
596 }
597
598 return NULL;
599}
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100600
601/*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections
602 * \param[in] cfg The gbproxy configuration
603 * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution
604 * \param[in] tlli The tlli to choose an SGSN for. The same tlli will map to the same SGSN as long as no SGSN is
605 * added/removed or allow_attach changes.
606 * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found
607 */
608struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
609 uint32_t tlli)
610{
611 uint32_t i = 0;
612 uint32_t index, num_sgsns;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100613 OSMO_ASSERT(cfg);
614
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100615 struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override;
616
617 if (sgsn) {
618 LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n");
619 return sgsn;
620 }
621
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100622 // TODO: We should keep track of count in cfg
623 num_sgsns = llist_count(&cfg->sgsns);
624
625 if (num_sgsns == 0)
626 return NULL;
627
628 // FIXME: 256 SGSNs ought to be enough for everyone
629 index = hash_32(tlli, 8) % num_sgsns;
630
631 // Get the first enabled SGSN after index
632 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
633 if (i >= index && sgsn->pool.allow_attach) {
634 return sgsn;
635 }
636 i++;
637 }
638 // Start again from the beginning
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100639 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100640 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100641 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100642 break;
643 } else if (sgsn->pool.allow_attach) {
644 return sgsn;
645 }
646 i++;
647 }
648
649 return NULL;
650}