blob: 8855a4475f5f0449fcd7913d251845643c9c3885 [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
Oliver Smith29532c22021-01-29 11:13:00 +010025#include "debug.h"
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020026
27#include <osmocom/gprs/protocol/gsm_08_18.h>
Daniel Willmannc8a50092021-01-17 13:11:41 +010028#include <osmocom/core/crc16.h>
Daniel Willmann8f407b12020-12-02 19:33:50 +010029#include <osmocom/core/logging.h>
Daniel Willmannee834af2020-12-14 16:22:39 +010030#include <osmocom/core/linuxlist.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020031#include <osmocom/core/rate_ctr.h>
32#include <osmocom/core/stats.h>
33#include <osmocom/core/talloc.h>
Daniel Willmannc8a50092021-01-17 13:11:41 +010034#include <osmocom/core/utils.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020035#include <osmocom/gsm/tlv.h>
36
37#include <string.h>
38
39extern void *tall_sgsn_ctx;
40
Harald Welte560bdb32020-12-04 22:24:47 +010041static const struct rate_ctr_desc bvc_ctr_description[] = {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020042 { "blocked", "BVC Block " },
43 { "unblocked", "BVC Unblock " },
44 { "dropped", "BVC blocked, dropped packet " },
45 { "inv-nsei", "NSEI mismatch " },
46 { "tx-err", "NS Transmission error " },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020047};
48
Harald Welte560bdb32020-12-04 22:24:47 +010049osmo_static_assert(ARRAY_SIZE(bvc_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020050
Harald Welte560bdb32020-12-04 22:24:47 +010051static const struct rate_ctr_group_desc bvc_ctrg_desc = {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020052 .group_name_prefix = "gbproxy:peer",
53 .group_description = "GBProxy Peer Statistics",
Harald Welte560bdb32020-12-04 22:24:47 +010054 .num_ctr = ARRAY_SIZE(bvc_ctr_description),
55 .ctr_desc = bvc_ctr_description,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020056 .class_id = OSMO_STATS_CLASS_PEER,
57};
58
59
Harald Welte560bdb32020-12-04 22:24:47 +010060/* Find the gbproxy_bvc by its BVCI. There can only be one match */
Harald Weltee5209642020-12-05 19:59:45 +010061struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020062{
Harald Welte8b4c7942020-12-05 10:14:49 +010063 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +010064 hash_for_each_possible(nse->bvcs, bvc, list, bvci) {
65 if (bvc->bvci == bvci)
66 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020067 }
68 return NULL;
69}
70
Harald Welte560bdb32020-12-04 22:24:47 +010071struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020072{
Harald Welte560bdb32020-12-04 22:24:47 +010073 struct gbproxy_bvc *bvc;
Daniel Willmanne50550e2020-11-26 18:19:21 +010074 OSMO_ASSERT(nse);
75 struct gbproxy_config *cfg = nse->cfg;
76 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020077
Harald Welte560bdb32020-12-04 22:24:47 +010078 bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc);
79 if (!bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020080 return NULL;
81
Harald Welte560bdb32020-12-04 22:24:47 +010082 bvc->bvci = bvci;
Harald Welte4bd3e492020-12-07 12:06:52 +010083 bvc->ctrg = rate_ctr_group_alloc(bvc, &bvc_ctrg_desc, (nse->nsei << 16) | bvci);
Harald Welte560bdb32020-12-04 22:24:47 +010084 if (!bvc->ctrg) {
85 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020086 return NULL;
87 }
Harald Welte560bdb32020-12-04 22:24:47 +010088 bvc->nse = nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020089
Harald Welte8b4c7942020-12-05 10:14:49 +010090 hash_add(nse->bvcs, &bvc->list, bvc->bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020091
Harald Weltecfc7e8e2020-12-07 12:03:10 +010092 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Created\n");
93
94 /* We leave allocating the bvc->fi to the caller, as the FSM details depend
95 * on the type of BVC (SIG/PTP) and role (SGSN/BSS) */
96
Harald Weltee5209642020-12-05 19:59:45 +010097 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020098}
99
Harald Welte560bdb32020-12-04 22:24:47 +0100100void gbproxy_bvc_free(struct gbproxy_bvc *bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200101{
Harald Weltee5209642020-12-05 19:59:45 +0100102 struct gbproxy_cell *cell;
103
Harald Welte560bdb32020-12-04 22:24:47 +0100104 if (!bvc)
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100105 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100106
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100107 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n");
108
Harald Welte8b4c7942020-12-05 10:14:49 +0100109 hash_del(&bvc->list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200110
Harald Welte560bdb32020-12-04 22:24:47 +0100111 rate_ctr_group_free(bvc->ctrg);
112 bvc->ctrg = NULL;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200113
Harald Weltee5209642020-12-05 19:59:45 +0100114 osmo_fsm_inst_free(bvc->fi);
115
116 cell = bvc->cell;
117 if (cell) {
118 int i;
119
120 if (cell->bss_bvc == bvc)
121 cell->bss_bvc = NULL;
122
123 /* we could also be a SGSN-side BVC */
124 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
125 if (cell->sgsn_bvc[i] == bvc)
126 cell->sgsn_bvc[i] = NULL;
127 }
128 bvc->cell = NULL;
129 }
130
Harald Welte560bdb32020-12-04 22:24:47 +0100131 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200132}
133
Harald Weltee5209642020-12-05 19:59:45 +0100134/*! remove BVCs on NSE specified by NSEI.
Harald Welte560bdb32020-12-04 22:24:47 +0100135 * \param[in] cfg proxy in which we operate
136 * \param[in] nsei NS entity in which we should clean up
Harald Weltee5209642020-12-05 19:59:45 +0100137 * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */
138int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200139{
Harald Weltee5209642020-12-05 19:59:45 +0100140 struct hlist_node *btmp;
141 struct gbproxy_bvc *bvc;
142 int j, counter = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200143
Harald Weltee5209642020-12-05 19:59:45 +0100144 if (!nse)
145 return 0;
146
147 hash_for_each_safe(nse->bvcs, j, btmp, bvc, list) {
148 if (bvci && bvc->bvci != bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200149 continue;
Harald Weltee5209642020-12-05 19:59:45 +0100150 if (bvci == 0 && bvc->bvci == 0)
151 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200152
Harald Weltee5209642020-12-05 19:59:45 +0100153 gbproxy_bvc_free(bvc);
154 counter += 1;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200155 }
156
157 return counter;
158}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100159
Harald Weltee5209642020-12-05 19:59:45 +0100160
161/***********************************************************************
162 * CELL
163 ***********************************************************************/
164
165/* Allocate a new 'cell' object */
166struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci)
167{
168 struct gbproxy_cell *cell;
169 OSMO_ASSERT(cfg);
170
171 cell = talloc_zero(cfg, struct gbproxy_cell);
172 if (!cell)
173 return NULL;
174
175 cell->cfg = cfg;
176 cell->bvci = bvci;
177
178 hash_add(cfg->cells, &cell->list, cell->bvci);
179
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100180 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n");
181
Harald Weltee5209642020-12-05 19:59:45 +0100182 return cell;
183}
184
185/* Find cell by BVCI */
186struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
187{
188 struct gbproxy_cell *cell;
189
190 hash_for_each_possible(cfg->cells, cell, list, bvci) {
191 if (cell->bvci == bvci)
192 return cell;
193 }
194 return NULL;
195}
196
197struct gbproxy_cell *gbproxy_cell_by_bvci_or_new(struct gbproxy_config *cfg, uint16_t bvci)
198{
199 struct gbproxy_cell *cell;
200 OSMO_ASSERT(cfg);
201
202 cell = gbproxy_cell_by_bvci(cfg, bvci);
203 if (!cell)
204 cell = gbproxy_cell_alloc(cfg, bvci);
205
206 return cell;
207}
208
Daniel Willmannfccbef02021-01-19 17:59:19 +0100209struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid)
210{
211 int i;
212 struct gbproxy_cell *cell;
213
214 hash_for_each(cfg->cells, i, cell, list) {
215 if (cell->id.cid == cid && gsm48_ra_equal(&cell->id.raid, raid)) {
216 return cell;
217 }
218 }
219 return NULL;
220}
221
Harald Weltee5209642020-12-05 19:59:45 +0100222void gbproxy_cell_free(struct gbproxy_cell *cell)
223{
224 unsigned int i;
225
226 if (!cell)
227 return;
228
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100229 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
230
Harald Weltee5209642020-12-05 19:59:45 +0100231 /* remove from cfg.cells */
232 hash_del(&cell->list);
233
234 /* remove back-pointers from the BSS side */
235 if (cell->bss_bvc && cell->bss_bvc->cell)
236 cell->bss_bvc->cell = NULL;
237
238 /* remove back-pointers from the SGSN side */
239 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
240 if (!cell->sgsn_bvc[i])
241 continue;
242 if (cell->sgsn_bvc[i]->cell)
243 cell->sgsn_bvc[i]->cell = NULL;
244 }
245
246 talloc_free(cell);
247}
248
249bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
250{
251 unsigned int i;
252 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
253 if (!cell->sgsn_bvc[i]) {
254 cell->sgsn_bvc[i] = bvc;
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100255 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
256 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100257 return true;
258 }
259 }
260 return false;
261}
262
Daniel Willmann77493b12020-12-29 21:13:31 +0100263
264/***********************************************************************
265 * TLLI cache
266 ***********************************************************************/
267
Daniel Willmann9170c342021-01-17 13:12:46 +0100268static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli)
269{
270 struct gbproxy_tlli_cache_entry *cache_entry;
271
272 hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) {
273 if (cache_entry->tlli == tlli)
274 return cache_entry;
275 }
276 return NULL;
277}
278
Daniel Willmann77493b12020-12-29 21:13:31 +0100279void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli)
280{
281 struct gbproxy_config *cfg = nse->cfg;
282 struct timespec now;
283 struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli);
284
285 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
286
287 if (cache_entry) {
288 /* Update the entry if it already exists */
289 cache_entry->nse = nse;
290 cache_entry->tstamp = now.tv_sec;
291 return;
292 }
293
294 cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry);
295 cache_entry->tlli = tlli;
296 cache_entry->nse = nse;
297 cache_entry->tstamp = now.tv_sec;
298 hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli);
299}
300
301static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) {
302 uint i;
303 struct gbproxy_config *cfg = nse->cfg;
304 struct gbproxy_tlli_cache_entry *tlli_cache;
305 struct hlist_node *tmp;
306
307 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
308 if (tlli_cache->nse == nse) {
309 hash_del(&tlli_cache->list);
310 talloc_free(tlli_cache);
311 }
312 }
313}
314
315void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli)
316{
317 struct gbproxy_tlli_cache_entry *tlli_cache;
318 struct hlist_node *tmp;
319
320 hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) {
321 if (tlli_cache->tlli == tlli) {
322 hash_del(&tlli_cache->list);
323 talloc_free(tlli_cache);
324 return;
325 }
326 }
327}
328
329int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg)
330{
331 int i, count = 0;
332 struct gbproxy_tlli_cache_entry *tlli_cache;
333 struct hlist_node *tmp;
334 struct timespec now;
335 time_t expiry;
336
337 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
338 expiry = now.tv_sec - cfg->tlli_cache.timeout;
339
340 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
341 if (tlli_cache->tstamp < expiry) {
342 count++;
343 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli);
344 hash_del(&tlli_cache->list);
345 talloc_free(tlli_cache);
346 }
347 }
348 return count;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100349
350}
351/***********************************************************************
352 * IMSI cache
353 ***********************************************************************/
354static inline uint16_t _checksum_imsi(const char *imsi)
355{
356 size_t len = strlen(imsi);
357 return osmo_crc16(0, (const uint8_t *)imsi, len);
358}
359
360static inline struct gbproxy_imsi_cache_entry *_get_imsi_entry(struct gbproxy_config *cfg, const char *imsi)
361{
362 struct gbproxy_imsi_cache_entry *cache_entry;
363 uint16_t imsi_hash = _checksum_imsi(imsi);
364
365 hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) {
366 if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi)))
367 return cache_entry;
368 }
369 return NULL;
370}
371
372void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi)
373{
374 struct gbproxy_config *cfg = nse->cfg;
375 struct timespec now;
376 struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi);
377 uint16_t imsi_hash = _checksum_imsi(imsi);
378
379 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
380
381 if (cache_entry) {
382 /* Update the entry if it already exists */
383 cache_entry->nse = nse;
384 cache_entry->tstamp = now.tv_sec;
385 return;
386 }
387
388 cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry);
389 OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi);
390 cache_entry->nse = nse;
391 cache_entry->tstamp = now.tv_sec;
392 hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash);
393}
394
395static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) {
396 uint i;
397 struct gbproxy_config *cfg = nse->cfg;
398 struct gbproxy_imsi_cache_entry *imsi_cache;
399 struct hlist_node *tmp;
400
401 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
402 if (imsi_cache->nse == nse) {
403 hash_del(&imsi_cache->list);
404 talloc_free(imsi_cache);
405 }
406 }
407}
408
409void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi)
410{
411 struct gbproxy_imsi_cache_entry *imsi_cache;
412 struct hlist_node *tmp;
413 uint16_t imsi_hash = _checksum_imsi(imsi);
414
415 hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) {
416 if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)))) {
417 hash_del(&imsi_cache->list);
418 talloc_free(imsi_cache);
419 return;
420 }
421 }
422}
423
424int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg)
425{
426 int i, count = 0;
427 struct gbproxy_imsi_cache_entry *imsi_cache;
428 struct hlist_node *tmp;
429 struct timespec now;
430 time_t expiry;
431
432 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
433 expiry = now.tv_sec - cfg->imsi_cache.timeout;
434
435 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
436 if (imsi_cache->tstamp < expiry) {
437 count++;
438 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi);
439 hash_del(&imsi_cache->list);
440 talloc_free(imsi_cache);
441 }
442 }
443 return count;
Daniel Willmann77493b12020-12-29 21:13:31 +0100444}
445
Harald Weltee5209642020-12-05 19:59:45 +0100446/***********************************************************************
447 * NSE - NS Entity
448 ***********************************************************************/
449
450struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100451{
452 struct gbproxy_nse *nse;
453 OSMO_ASSERT(cfg);
454
455 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
456 if (!nse)
457 return NULL;
458
459 nse->nsei = nsei;
460 nse->cfg = cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100461 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100462
Harald Weltee5209642020-12-05 19:59:45 +0100463 if (sgsn_facing)
464 hash_add(cfg->sgsn_nses, &nse->list, nsei);
465 else
466 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100467
Harald Welte8b4c7942020-12-05 10:14:49 +0100468 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100469
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100470 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
471
Daniel Willmanne50550e2020-11-26 18:19:21 +0100472 return nse;
473}
474
Daniel Willmannee834af2020-12-14 16:22:39 +0100475static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100476{
Harald Welte8b4c7942020-12-05 10:14:49 +0100477 struct gbproxy_bvc *bvc;
478 struct hlist_node *tmp;
479 int i;
480
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100481 if (!nse)
482 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100483
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100484 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
485
Harald Welted2fef952020-12-05 00:31:07 +0100486 hash_del(&nse->list);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100487 /* Clear the cache entries of this NSE */
Daniel Willmann77493b12020-12-29 21:13:31 +0100488 _tlli_cache_remove_nse(nse);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100489 _imsi_cache_remove_nse(nse);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100490
Harald Welte8b4c7942020-12-05 10:14:49 +0100491 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100492 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100493
494 talloc_free(nse);
495}
Daniel Willmannee834af2020-12-14 16:22:39 +0100496static void _sgsn_free(struct gbproxy_sgsn *sgsn);
497
498void gbproxy_nse_free(struct gbproxy_nse *nse)
499{
500 if (!nse)
501 return;
502 OSMO_ASSERT(nse->cfg);
503
504 if (nse->sgsn_facing) {
505 struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
506 OSMO_ASSERT(sgsn);
507 _sgsn_free(sgsn);
508 }
509
510 _nse_free(nse);
511}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100512
Harald Weltee5209642020-12-05 19:59:45 +0100513struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100514{
515 struct gbproxy_nse *nse;
516 OSMO_ASSERT(cfg);
517
Harald Weltee5209642020-12-05 19:59:45 +0100518 if (flags & NSE_F_SGSN) {
519 hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) {
520 if (nse->nsei == nsei)
521 return nse;
522 }
523 }
524
525 if (flags & NSE_F_BSS) {
526 hash_for_each_possible(cfg->bss_nses, nse, list, nsei) {
527 if (nse->nsei == nsei)
528 return nse;
529 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100530 }
531
532 return NULL;
533}
534
Harald Weltee5209642020-12-05 19:59:45 +0100535struct 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 +0100536{
537 struct gbproxy_nse *nse;
538 OSMO_ASSERT(cfg);
539
Harald Weltee5209642020-12-05 19:59:45 +0100540 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100541 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100542 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100543
544 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100545}
Daniel Willmannee834af2020-12-14 16:22:39 +0100546
Daniel Willmann77493b12020-12-29 21:13:31 +0100547struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli)
548{
549 struct gbproxy_tlli_cache_entry *tlli_cache;
550
551 hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) {
552 if (tlli_cache->tlli == tlli)
553 return tlli_cache->nse;
554 }
555 return NULL;
556}
557
Daniel Willmannc8a50092021-01-17 13:11:41 +0100558struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi)
559{
560 struct gbproxy_imsi_cache_entry *imsi_cache;
561 uint16_t imsi_hash = _checksum_imsi(imsi);
562
563 hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) {
564 if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)))
565 return imsi_cache->nse;
566 }
567 return NULL;
568}
Daniel Willmann77493b12020-12-29 21:13:31 +0100569
Daniel Willmann216dff82020-12-29 16:07:17 +0100570/***********************************************************************
571 * SGSN - Serving GPRS Support Node
572 ***********************************************************************/
573
574/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
575 * \param[in] cfg The gbproxy configuration
576 * \param[in] nsei The nsei where the SGSN can be reached
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100577 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100578 * \return The SGSN, NULL if it couldn't be allocated
579 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100580struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100581{
582 struct gbproxy_sgsn *sgsn;
583 OSMO_ASSERT(cfg);
584
585 sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn);
586 if (!sgsn)
587 return NULL;
588
589 sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
590 if (!sgsn->nse) {
Vadim Yanitskiy6964e882021-01-05 14:42:46 +0100591 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
592 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100593 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100594 }
595
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100596 if (name)
597 sgsn->name = talloc_strdup(sgsn, name);
598 else
599 sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
600 if (!sgsn->name)
601 goto free_sgsn;
602
Daniel Willmannee834af2020-12-14 16:22:39 +0100603 sgsn->pool.allow_attach = true;
604 sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
605
606 llist_add_tail(&sgsn->list, &cfg->sgsns);
607 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
608 return sgsn;
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100609
610free_sgsn:
611 talloc_free(sgsn);
612 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100613}
614
615/* Only free gbproxy_sgsn, sgsn can't be NULL */
616static void _sgsn_free(struct gbproxy_sgsn *sgsn) {
617 struct gbproxy_config *cfg;
618
619 OSMO_ASSERT(sgsn->nse);
620 cfg = sgsn->nse->cfg;
621 OSMO_ASSERT(cfg);
622
623 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
624 llist_del(&sgsn->list);
Daniel Willmann5193f222021-01-11 05:00:46 +0100625 /* talloc will free ->name and ->pool.nri_ranges */
Daniel Willmannee834af2020-12-14 16:22:39 +0100626 talloc_free(sgsn);
627}
628
Daniel Willmann216dff82020-12-29 16:07:17 +0100629/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
630 * \param[in] sgsn The SGSN
631 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100632void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)
633{
634 if (!sgsn)
635 return;
636
637 OSMO_ASSERT(sgsn->nse)
638
639 _nse_free(sgsn->nse);
640 _sgsn_free(sgsn);
641}
642
Daniel Willmann216dff82020-12-29 16:07:17 +0100643/*! Return the SGSN for a given NSEI
644 * \param[in] cfg The gbproxy configuration
645 * \param[in] nsei The nsei where the SGSN can be reached
646 * \return Returns the matching SGSN or NULL if it couldn't be found
647 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100648struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
649{
650 struct gbproxy_sgsn *sgsn;
651 OSMO_ASSERT(cfg);
652
653 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
654 if (!strcmp(sgsn->name, name))
655 return sgsn;
656 }
657
658 return NULL;
659}
660
661/*! Return the SGSN for a given NSEI
662 * \param[in] cfg The gbproxy configuration
663 * \param[in] nsei The nsei where the SGSN can be reached
664 * \return Returns the matching SGSN or NULL if it couldn't be found
665 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100666struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
667{
668 struct gbproxy_sgsn *sgsn;
669 OSMO_ASSERT(cfg);
670
671 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
672 if (sgsn->nse->nsei == nsei)
673 return sgsn;
674 }
675
676 return NULL;
677}
678
Daniel Willmann216dff82020-12-29 16:07:17 +0100679/*! Return the SGSN for a given NSEI, creating a new one if none exists
680 * \param[in] cfg The gbproxy configuration
681 * \param[in] nsei The nsei where the SGSN can be reached
682 * \return Returns the SGSN
683 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100684struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
685{
686 struct gbproxy_sgsn *sgsn;
687 OSMO_ASSERT(cfg);
688
689 sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
690 if (!sgsn)
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100691 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100692
693 return sgsn;
694}
695
696/*! Return the gbproxy_sgsn matching that NRI
697 * \param[in] cfg proxy in which we operate
698 * \param[in] nri NRI to look for
699 * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI
700 * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found
701 */
702struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri)
703{
704 struct gbproxy_sgsn *sgsn;
705 OSMO_ASSERT(cfg);
706
707 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
708 if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) {
709 /* Also check if the NRI we're looking for is a NULL NRI */
Vadim Yanitskiy4dba67a2021-01-05 14:36:52 +0100710 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100711 if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges))
712 *null_nri = true;
713 else
714 *null_nri = false;
715 }
716 return sgsn;
717 }
718 }
719
720 return NULL;
721}
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100722
723/*! Seleect a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections
724 * \param[in] cfg The gbproxy configuration
725 * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution
726 * \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
727 * added/removed or allow_attach changes.
728 * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found
729 */
730struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
731 uint32_t tlli)
732{
733 uint32_t i = 0;
734 uint32_t index, num_sgsns;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100735 OSMO_ASSERT(cfg);
736
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100737 struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override;
738
739 if (sgsn) {
740 LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n");
741 return sgsn;
742 }
743
Daniel Willmann5193f222021-01-11 05:00:46 +0100744 /* TODO: We should keep track of count in cfg */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100745 num_sgsns = llist_count(&cfg->sgsns);
746
747 if (num_sgsns == 0)
748 return NULL;
749
Daniel Willmann5193f222021-01-11 05:00:46 +0100750 /* FIXME: 256 SGSNs ought to be enough for everyone */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100751 index = hash_32(tlli, 8) % num_sgsns;
752
Daniel Willmann5193f222021-01-11 05:00:46 +0100753 /* Get the first enabled SGSN after index */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100754 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
755 if (i >= index && sgsn->pool.allow_attach) {
756 return sgsn;
757 }
758 i++;
759 }
Daniel Willmann5193f222021-01-11 05:00:46 +0100760 /* Start again from the beginning */
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100761 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100762 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100763 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100764 break;
765 } else if (sgsn->pool.allow_attach) {
766 return sgsn;
767 }
768 i++;
769 }
770
771 return NULL;
772}