blob: f59cf9e52962a25e21486ddc21aa8875c124d733 [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
Daniel Willmanna16ecc32021-03-10 09:57:12 +010023#include <osmocom/gbproxy/gb_proxy.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020024
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) {
Daniel Willmanndc763fd2021-09-24 16:45:38 +020065 if (bvc->bvci == bvci && bvc->inactive == false)
66 return bvc;
67 }
68 return NULL;
69}
70
71/* Find the gbproxy_bvc by its BVCI. There can only be one match */
72struct gbproxy_bvc *gbproxy_bvc_by_bvci_inactive(struct gbproxy_nse *nse, uint16_t bvci)
73{
74 struct gbproxy_bvc *bvc;
75 hash_for_each_possible(nse->bvcs, bvc, list, bvci) {
76 if (bvc->bvci == bvci && bvc->inactive == true)
Harald Weltee5209642020-12-05 19:59:45 +010077 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020078 }
79 return NULL;
80}
81
Harald Welte560bdb32020-12-04 22:24:47 +010082struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020083{
Daniel Willmann990b1512021-07-09 13:58:58 +020084 char idbuf[64];
Harald Welte560bdb32020-12-04 22:24:47 +010085 struct gbproxy_bvc *bvc;
Daniel Willmanne50550e2020-11-26 18:19:21 +010086 OSMO_ASSERT(nse);
87 struct gbproxy_config *cfg = nse->cfg;
88 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020089
Harald Welte560bdb32020-12-04 22:24:47 +010090 bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc);
91 if (!bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020092 return NULL;
93
Daniel Willmann990b1512021-07-09 13:58:58 +020094 snprintf(idbuf, sizeof(idbuf), "BVC%05u-NSE%05u", bvci,
95 nse->nsei);
96 osmo_identifier_sanitize_buf(idbuf, NULL, '_');
Harald Welte560bdb32020-12-04 22:24:47 +010097 bvc->bvci = bvci;
Daniel Willmanndc763fd2021-09-24 16:45:38 +020098 bvc->inactive = false;
Harald Welte4bd3e492020-12-07 12:06:52 +010099 bvc->ctrg = rate_ctr_group_alloc(bvc, &bvc_ctrg_desc, (nse->nsei << 16) | bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100100 if (!bvc->ctrg) {
101 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200102 return NULL;
103 }
Daniel Willmann990b1512021-07-09 13:58:58 +0200104 rate_ctr_group_set_name(bvc->ctrg, idbuf);
Harald Welte560bdb32020-12-04 22:24:47 +0100105 bvc->nse = nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200106
Harald Welte8b4c7942020-12-05 10:14:49 +0100107 hash_add(nse->bvcs, &bvc->list, bvc->bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200108
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100109 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Created\n");
110
111 /* We leave allocating the bvc->fi to the caller, as the FSM details depend
112 * on the type of BVC (SIG/PTP) and role (SGSN/BSS) */
Daniel Willmann3ea37932021-02-10 13:41:14 +0100113 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200114}
115
Harald Welte560bdb32020-12-04 22:24:47 +0100116void gbproxy_bvc_free(struct gbproxy_bvc *bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200117{
Harald Welte560bdb32020-12-04 22:24:47 +0100118 if (!bvc)
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100119 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100120
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100121 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n");
122
Harald Welte8b4c7942020-12-05 10:14:49 +0100123 hash_del(&bvc->list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200124
Harald Welte560bdb32020-12-04 22:24:47 +0100125 rate_ctr_group_free(bvc->ctrg);
126 bvc->ctrg = NULL;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200127
Harald Weltee5209642020-12-05 19:59:45 +0100128 osmo_fsm_inst_free(bvc->fi);
129
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200130 if (bvc->cell) {
131 gbproxy_cell_cleanup_bvc(bvc->cell, bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100132 bvc->cell = NULL;
133 }
Harald Welte560bdb32020-12-04 22:24:47 +0100134 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200135}
136
Harald Weltee5209642020-12-05 19:59:45 +0100137/*! remove BVCs on NSE specified by NSEI.
Harald Welte560bdb32020-12-04 22:24:47 +0100138 * \param[in] cfg proxy in which we operate
139 * \param[in] nsei NS entity in which we should clean up
Harald Weltee5209642020-12-05 19:59:45 +0100140 * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */
141int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200142{
Harald Weltee5209642020-12-05 19:59:45 +0100143 struct hlist_node *btmp;
144 struct gbproxy_bvc *bvc;
145 int j, counter = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200146
Harald Weltee5209642020-12-05 19:59:45 +0100147 if (!nse)
148 return 0;
149
150 hash_for_each_safe(nse->bvcs, j, btmp, bvc, list) {
151 if (bvci && bvc->bvci != bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200152 continue;
Harald Weltee5209642020-12-05 19:59:45 +0100153 if (bvci == 0 && bvc->bvci == 0)
154 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200155
Harald Weltee5209642020-12-05 19:59:45 +0100156 gbproxy_bvc_free(bvc);
157 counter += 1;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200158 }
159
160 return counter;
161}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100162
Harald Weltee5209642020-12-05 19:59:45 +0100163
164/***********************************************************************
165 * CELL
166 ***********************************************************************/
167
168/* Allocate a new 'cell' object */
Philipp Maiere4597ec2021-02-09 16:02:00 +0100169struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci,
170 const struct gprs_ra_id *raid, uint16_t cid)
Harald Weltee5209642020-12-05 19:59:45 +0100171{
172 struct gbproxy_cell *cell;
173 OSMO_ASSERT(cfg);
174
175 cell = talloc_zero(cfg, struct gbproxy_cell);
176 if (!cell)
177 return NULL;
178
179 cell->cfg = cfg;
180 cell->bvci = bvci;
Philipp Maiere4597ec2021-02-09 16:02:00 +0100181 cell->id.cid = cid;
182 memcpy(&cell->id.raid, raid, sizeof(cell->id.raid));
Harald Weltee5209642020-12-05 19:59:45 +0100183
184 hash_add(cfg->cells, &cell->list, cell->bvci);
185
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100186 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n");
187
Harald Weltee5209642020-12-05 19:59:45 +0100188 return cell;
189}
190
191/* Find cell by BVCI */
192struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
193{
194 struct gbproxy_cell *cell;
195
196 hash_for_each_possible(cfg->cells, cell, list, bvci) {
197 if (cell->bvci == bvci)
198 return cell;
199 }
200 return NULL;
201}
202
Daniel Willmannfccbef02021-01-19 17:59:19 +0100203struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid)
204{
205 int i;
206 struct gbproxy_cell *cell;
207
208 hash_for_each(cfg->cells, i, cell, list) {
209 if (cell->id.cid == cid && gsm48_ra_equal(&cell->id.raid, raid)) {
210 return cell;
211 }
212 }
213 return NULL;
214}
215
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200216void gbproxy_cell_cleanup_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
217{
218 int i;
219
220 if (cell->bss_bvc == bvc)
221 return gbproxy_cell_free(cell);
222
223 /* we could also be a SGSN-side BVC */
224 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
225 if (cell->sgsn_bvc[i] == bvc)
226 cell->sgsn_bvc[i] = NULL;
227 }
228
229}
230
Harald Weltee5209642020-12-05 19:59:45 +0100231void gbproxy_cell_free(struct gbproxy_cell *cell)
232{
233 unsigned int i;
234
235 if (!cell)
236 return;
237
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100238 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
239
Harald Weltee5209642020-12-05 19:59:45 +0100240 /* remove from cfg.cells */
241 hash_del(&cell->list);
242
243 /* remove back-pointers from the BSS side */
244 if (cell->bss_bvc && cell->bss_bvc->cell)
245 cell->bss_bvc->cell = NULL;
246
247 /* remove back-pointers from the SGSN side */
248 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
249 if (!cell->sgsn_bvc[i])
250 continue;
251 if (cell->sgsn_bvc[i]->cell)
252 cell->sgsn_bvc[i]->cell = NULL;
253 }
254
255 talloc_free(cell);
256}
257
258bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
259{
260 unsigned int i;
261 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
262 if (!cell->sgsn_bvc[i]) {
263 cell->sgsn_bvc[i] = bvc;
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100264 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
265 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100266 return true;
267 }
268 }
269 return false;
270}
271
Daniel Willmann77493b12020-12-29 21:13:31 +0100272
273/***********************************************************************
274 * TLLI cache
275 ***********************************************************************/
276
Daniel Willmann9170c342021-01-17 13:12:46 +0100277static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli)
278{
279 struct gbproxy_tlli_cache_entry *cache_entry;
280
281 hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) {
282 if (cache_entry->tlli == tlli)
283 return cache_entry;
284 }
285 return NULL;
286}
287
Daniel Willmann77493b12020-12-29 21:13:31 +0100288void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli)
289{
290 struct gbproxy_config *cfg = nse->cfg;
291 struct timespec now;
292 struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli);
293
294 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
295
296 if (cache_entry) {
297 /* Update the entry if it already exists */
298 cache_entry->nse = nse;
299 cache_entry->tstamp = now.tv_sec;
300 return;
301 }
302
303 cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry);
304 cache_entry->tlli = tlli;
305 cache_entry->nse = nse;
306 cache_entry->tstamp = now.tv_sec;
307 hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli);
308}
309
310static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) {
311 uint i;
312 struct gbproxy_config *cfg = nse->cfg;
313 struct gbproxy_tlli_cache_entry *tlli_cache;
314 struct hlist_node *tmp;
315
316 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
317 if (tlli_cache->nse == nse) {
318 hash_del(&tlli_cache->list);
319 talloc_free(tlli_cache);
320 }
321 }
322}
323
324void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli)
325{
326 struct gbproxy_tlli_cache_entry *tlli_cache;
327 struct hlist_node *tmp;
328
329 hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) {
330 if (tlli_cache->tlli == tlli) {
331 hash_del(&tlli_cache->list);
332 talloc_free(tlli_cache);
333 return;
334 }
335 }
336}
337
338int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg)
339{
340 int i, count = 0;
341 struct gbproxy_tlli_cache_entry *tlli_cache;
342 struct hlist_node *tmp;
343 struct timespec now;
344 time_t expiry;
345
346 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
347 expiry = now.tv_sec - cfg->tlli_cache.timeout;
348
349 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
350 if (tlli_cache->tstamp < expiry) {
351 count++;
352 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli);
353 hash_del(&tlli_cache->list);
354 talloc_free(tlli_cache);
355 }
356 }
357 return count;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100358
359}
360/***********************************************************************
361 * IMSI cache
362 ***********************************************************************/
363static inline uint16_t _checksum_imsi(const char *imsi)
364{
365 size_t len = strlen(imsi);
366 return osmo_crc16(0, (const uint8_t *)imsi, len);
367}
368
Daniel Willmann361d0b52021-07-09 17:44:30 +0200369static inline struct gbproxy_imsi_cache_entry *_get_imsi_entry(struct gbproxy_config *cfg, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100370{
371 struct gbproxy_imsi_cache_entry *cache_entry;
372 uint16_t imsi_hash = _checksum_imsi(imsi);
373
374 hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200375 if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi)) && cache_entry->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100376 return cache_entry;
377 }
378 return NULL;
379}
380
Daniel Willmann361d0b52021-07-09 17:44:30 +0200381void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100382{
383 struct gbproxy_config *cfg = nse->cfg;
384 struct timespec now;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200385 struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi, usage);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100386 uint16_t imsi_hash = _checksum_imsi(imsi);
387
388 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
389
390 if (cache_entry) {
391 /* Update the entry if it already exists */
392 cache_entry->nse = nse;
393 cache_entry->tstamp = now.tv_sec;
394 return;
395 }
396
397 cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry);
398 OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi);
399 cache_entry->nse = nse;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200400 cache_entry->usage = usage;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100401 cache_entry->tstamp = now.tv_sec;
402 hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash);
403}
404
405static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) {
406 uint i;
407 struct gbproxy_config *cfg = nse->cfg;
408 struct gbproxy_imsi_cache_entry *imsi_cache;
409 struct hlist_node *tmp;
410
411 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
412 if (imsi_cache->nse == nse) {
413 hash_del(&imsi_cache->list);
414 talloc_free(imsi_cache);
415 }
416 }
417}
418
Daniel Willmann361d0b52021-07-09 17:44:30 +0200419void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100420{
421 struct gbproxy_imsi_cache_entry *imsi_cache;
422 struct hlist_node *tmp;
423 uint16_t imsi_hash = _checksum_imsi(imsi);
424
425 hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200426 if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi))) && imsi_cache->usage == usage) {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100427 hash_del(&imsi_cache->list);
428 talloc_free(imsi_cache);
429 return;
430 }
431 }
432}
433
434int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg)
435{
436 int i, count = 0;
437 struct gbproxy_imsi_cache_entry *imsi_cache;
438 struct hlist_node *tmp;
439 struct timespec now;
440 time_t expiry;
441
442 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
443 expiry = now.tv_sec - cfg->imsi_cache.timeout;
444
445 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
446 if (imsi_cache->tstamp < expiry) {
447 count++;
448 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi);
449 hash_del(&imsi_cache->list);
450 talloc_free(imsi_cache);
451 }
452 }
453 return count;
Daniel Willmann77493b12020-12-29 21:13:31 +0100454}
455
Harald Weltee5209642020-12-05 19:59:45 +0100456/***********************************************************************
457 * NSE - NS Entity
458 ***********************************************************************/
459
460struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100461{
462 struct gbproxy_nse *nse;
463 OSMO_ASSERT(cfg);
464
465 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
466 if (!nse)
467 return NULL;
468
469 nse->nsei = nsei;
Daniel Willmanna8b61652021-02-12 05:05:14 +0100470 nse->max_sdu_len = DEFAULT_NSE_SDU;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100471 nse->cfg = cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100472 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100473
Harald Weltee5209642020-12-05 19:59:45 +0100474 if (sgsn_facing)
475 hash_add(cfg->sgsn_nses, &nse->list, nsei);
476 else
477 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100478
Harald Welte8b4c7942020-12-05 10:14:49 +0100479 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100480
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100481 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
482
Daniel Willmanne50550e2020-11-26 18:19:21 +0100483 return nse;
484}
485
Daniel Willmannee834af2020-12-14 16:22:39 +0100486static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100487{
Harald Welte8b4c7942020-12-05 10:14:49 +0100488 struct gbproxy_bvc *bvc;
489 struct hlist_node *tmp;
490 int i;
491
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100492 if (!nse)
493 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100494
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100495 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
496
Harald Welted2fef952020-12-05 00:31:07 +0100497 hash_del(&nse->list);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100498 /* Clear the cache entries of this NSE */
Daniel Willmann77493b12020-12-29 21:13:31 +0100499 _tlli_cache_remove_nse(nse);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100500 _imsi_cache_remove_nse(nse);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100501
Harald Welte8b4c7942020-12-05 10:14:49 +0100502 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100503 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100504
505 talloc_free(nse);
506}
Daniel Willmannee834af2020-12-14 16:22:39 +0100507static void _sgsn_free(struct gbproxy_sgsn *sgsn);
508
509void gbproxy_nse_free(struct gbproxy_nse *nse)
510{
511 if (!nse)
512 return;
513 OSMO_ASSERT(nse->cfg);
514
515 if (nse->sgsn_facing) {
516 struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
517 OSMO_ASSERT(sgsn);
518 _sgsn_free(sgsn);
519 }
520
521 _nse_free(nse);
522}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100523
Harald Weltee5209642020-12-05 19:59:45 +0100524struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100525{
526 struct gbproxy_nse *nse;
527 OSMO_ASSERT(cfg);
528
Harald Weltee5209642020-12-05 19:59:45 +0100529 if (flags & NSE_F_SGSN) {
530 hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) {
531 if (nse->nsei == nsei)
532 return nse;
533 }
534 }
535
536 if (flags & NSE_F_BSS) {
537 hash_for_each_possible(cfg->bss_nses, nse, list, nsei) {
538 if (nse->nsei == nsei)
539 return nse;
540 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100541 }
542
543 return NULL;
544}
545
Harald Weltee5209642020-12-05 19:59:45 +0100546struct 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 +0100547{
548 struct gbproxy_nse *nse;
549 OSMO_ASSERT(cfg);
550
Harald Weltee5209642020-12-05 19:59:45 +0100551 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100552 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100553 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100554
555 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100556}
Daniel Willmannee834af2020-12-14 16:22:39 +0100557
Daniel Willmann77493b12020-12-29 21:13:31 +0100558struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli)
559{
560 struct gbproxy_tlli_cache_entry *tlli_cache;
561
562 hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) {
563 if (tlli_cache->tlli == tlli)
564 return tlli_cache->nse;
565 }
566 return NULL;
567}
568
Daniel Willmann361d0b52021-07-09 17:44:30 +0200569struct gbproxy_nse *gbproxy_nse_by_imsi(struct gbproxy_config *cfg, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100570{
571 struct gbproxy_imsi_cache_entry *imsi_cache;
572 uint16_t imsi_hash = _checksum_imsi(imsi);
573
574 hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200575 if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)) && imsi_cache->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100576 return imsi_cache->nse;
577 }
578 return NULL;
579}
Daniel Willmann77493b12020-12-29 21:13:31 +0100580
Daniel Willmann216dff82020-12-29 16:07:17 +0100581/***********************************************************************
582 * SGSN - Serving GPRS Support Node
583 ***********************************************************************/
584
585/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
586 * \param[in] cfg The gbproxy configuration
587 * \param[in] nsei The nsei where the SGSN can be reached
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100588 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100589 * \return The SGSN, NULL if it couldn't be allocated
590 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100591struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100592{
593 struct gbproxy_sgsn *sgsn;
594 OSMO_ASSERT(cfg);
595
596 sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn);
597 if (!sgsn)
598 return NULL;
599
600 sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
601 if (!sgsn->nse) {
Vadim Yanitskiy6964e882021-01-05 14:42:46 +0100602 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
603 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100604 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100605 }
606
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100607 if (name)
608 sgsn->name = talloc_strdup(sgsn, name);
609 else
610 sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
611 if (!sgsn->name)
612 goto free_sgsn;
613
Daniel Willmannee834af2020-12-14 16:22:39 +0100614 sgsn->pool.allow_attach = true;
615 sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
616
617 llist_add_tail(&sgsn->list, &cfg->sgsns);
618 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
619 return sgsn;
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100620
621free_sgsn:
622 talloc_free(sgsn);
623 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100624}
625
626/* Only free gbproxy_sgsn, sgsn can't be NULL */
627static void _sgsn_free(struct gbproxy_sgsn *sgsn) {
628 struct gbproxy_config *cfg;
629
630 OSMO_ASSERT(sgsn->nse);
631 cfg = sgsn->nse->cfg;
632 OSMO_ASSERT(cfg);
633
634 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
635 llist_del(&sgsn->list);
Daniel Willmann5193f222021-01-11 05:00:46 +0100636 /* talloc will free ->name and ->pool.nri_ranges */
Daniel Willmannee834af2020-12-14 16:22:39 +0100637 talloc_free(sgsn);
638}
639
Daniel Willmann216dff82020-12-29 16:07:17 +0100640/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
641 * \param[in] sgsn The SGSN
642 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100643void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)
644{
645 if (!sgsn)
646 return;
647
Daniel Willmann37518b32021-05-27 18:13:36 +0200648 OSMO_ASSERT(sgsn->nse);
Daniel Willmannee834af2020-12-14 16:22:39 +0100649
650 _nse_free(sgsn->nse);
651 _sgsn_free(sgsn);
652}
653
Daniel Willmann216dff82020-12-29 16:07:17 +0100654/*! Return the SGSN for a given NSEI
655 * \param[in] cfg The gbproxy configuration
656 * \param[in] nsei The nsei where the SGSN can be reached
657 * \return Returns the matching SGSN or NULL if it couldn't be found
658 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100659struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
660{
661 struct gbproxy_sgsn *sgsn;
662 OSMO_ASSERT(cfg);
663
664 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
665 if (!strcmp(sgsn->name, name))
666 return sgsn;
667 }
668
669 return NULL;
670}
671
672/*! Return the SGSN for a given NSEI
673 * \param[in] cfg The gbproxy configuration
674 * \param[in] nsei The nsei where the SGSN can be reached
675 * \return Returns the matching SGSN or NULL if it couldn't be found
676 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100677struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
678{
679 struct gbproxy_sgsn *sgsn;
680 OSMO_ASSERT(cfg);
681
682 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
683 if (sgsn->nse->nsei == nsei)
684 return sgsn;
685 }
686
687 return NULL;
688}
689
Daniel Willmann216dff82020-12-29 16:07:17 +0100690/*! Return the SGSN for a given NSEI, creating a new one if none exists
691 * \param[in] cfg The gbproxy configuration
692 * \param[in] nsei The nsei where the SGSN can be reached
693 * \return Returns the SGSN
694 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100695struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
696{
697 struct gbproxy_sgsn *sgsn;
698 OSMO_ASSERT(cfg);
699
700 sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
701 if (!sgsn)
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100702 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100703
704 return sgsn;
705}
706
707/*! Return the gbproxy_sgsn matching that NRI
708 * \param[in] cfg proxy in which we operate
709 * \param[in] nri NRI to look for
710 * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI
711 * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found
712 */
713struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri)
714{
715 struct gbproxy_sgsn *sgsn;
716 OSMO_ASSERT(cfg);
717
718 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200719 if (!sgsn->nse->alive)
720 continue;
721
Daniel Willmannee834af2020-12-14 16:22:39 +0100722 if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) {
723 /* Also check if the NRI we're looking for is a NULL NRI */
Vadim Yanitskiy4dba67a2021-01-05 14:36:52 +0100724 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100725 if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges))
726 *null_nri = true;
727 else
728 *null_nri = false;
729 }
730 return sgsn;
731 }
732 }
733
734 return NULL;
735}
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100736
Daniel Willmann37518b32021-05-27 18:13:36 +0200737/*! Select a pseudo-random SGSN for a given TLLI, ignoring any SGSN that is not accepting connections or down
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100738 * \param[in] cfg The gbproxy configuration
739 * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution
740 * \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
741 * added/removed or allow_attach changes.
742 * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found
743 */
744struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
745 uint32_t tlli)
746{
747 uint32_t i = 0;
748 uint32_t index, num_sgsns;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100749 OSMO_ASSERT(cfg);
750
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100751 struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override;
752
753 if (sgsn) {
754 LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n");
755 return sgsn;
756 }
757
Daniel Willmann5193f222021-01-11 05:00:46 +0100758 /* TODO: We should keep track of count in cfg */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100759 num_sgsns = llist_count(&cfg->sgsns);
760
761 if (num_sgsns == 0)
762 return NULL;
763
Daniel Willmann5193f222021-01-11 05:00:46 +0100764 /* FIXME: 256 SGSNs ought to be enough for everyone */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100765 index = hash_32(tlli, 8) % num_sgsns;
766
Daniel Willmann5193f222021-01-11 05:00:46 +0100767 /* Get the first enabled SGSN after index */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100768 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200769 if (i >= index && sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100770 return sgsn;
771 }
772 i++;
773 }
Daniel Willmann5193f222021-01-11 05:00:46 +0100774 /* Start again from the beginning */
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100775 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100776 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100777 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100778 break;
Daniel Willmann37518b32021-05-27 18:13:36 +0200779 } else if (sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100780 return sgsn;
781 }
782 i++;
783 }
784
785 return NULL;
786}
Daniel Willmann37518b32021-05-27 18:13:36 +0200787
788/*! Return the first available gbproxy_sgsn
789 * \param[in] cfg proxy in which we operate
790 * \return The SGSN, NULL if no matching SGSN could be found
791 */
792struct gbproxy_sgsn *gbproxy_sgsn_by_available(struct gbproxy_config *cfg)
793{
794 struct gbproxy_sgsn *sgsn;
795 OSMO_ASSERT(cfg);
796
797 llist_for_each_entry(sgsn, &cfg->sgsns, list)
798 if (sgsn->nse->alive &&sgsn->pool.allow_attach)
799 return sgsn;
800
801 return NULL;
802}