blob: 1e300adaced0598e9d8fedf1901ba42ad34a7571 [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[] = {
Daniel Willmannd71aa6b2022-02-28 18:19:27 +010042 [GBPROX_PEER_CTR_BLOCKED] = { "blocked", "BVC Block " },
43 [GBPROX_PEER_CTR_UNBLOCKED] = { "unblocked", "BVC Unblock " },
44 [GBPROX_PEER_CTR_DROPPED] = { "dropped", "BVC blocked, dropped packet " },
45 [GBPROX_PEER_CTR_INV_NSEI] = { "inv-nsei", "NSEI mismatch " },
46 [GBPROX_PEER_CTR_TX_ERR] = { "tx-err", "NS Transmission error " },
47 [GBPROX_PEER_CTR_FWD_FROM_SGSN_ERR] = { "fwd-sgsn-err", "Forwarding data from SGSN failed" },
48 [GBPROX_PEER_CTR_FWD_FROM_BSS_ERR] = { "fwd-bss-err", "Forwarding data from BSS failed " },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020049};
50
Harald Welte560bdb32020-12-04 22:24:47 +010051osmo_static_assert(ARRAY_SIZE(bvc_ctr_description) == GBPROX_PEER_CTR_LAST, everything_described);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020052
Harald Welte560bdb32020-12-04 22:24:47 +010053static const struct rate_ctr_group_desc bvc_ctrg_desc = {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020054 .group_name_prefix = "gbproxy:peer",
55 .group_description = "GBProxy Peer Statistics",
Harald Welte560bdb32020-12-04 22:24:47 +010056 .num_ctr = ARRAY_SIZE(bvc_ctr_description),
57 .ctr_desc = bvc_ctr_description,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020058 .class_id = OSMO_STATS_CLASS_PEER,
59};
60
61
Harald Welte560bdb32020-12-04 22:24:47 +010062/* Find the gbproxy_bvc by its BVCI. There can only be one match */
Harald Weltee5209642020-12-05 19:59:45 +010063struct gbproxy_bvc *gbproxy_bvc_by_bvci(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020064{
Harald Welte8b4c7942020-12-05 10:14:49 +010065 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +010066 hash_for_each_possible(nse->bvcs, bvc, list, bvci) {
Daniel Willmanndc763fd2021-09-24 16:45:38 +020067 if (bvc->bvci == bvci && bvc->inactive == false)
68 return bvc;
69 }
70 return NULL;
71}
72
73/* Find the gbproxy_bvc by its BVCI. There can only be one match */
74struct gbproxy_bvc *gbproxy_bvc_by_bvci_inactive(struct gbproxy_nse *nse, uint16_t bvci)
75{
76 struct gbproxy_bvc *bvc;
77 hash_for_each_possible(nse->bvcs, bvc, list, bvci) {
78 if (bvc->bvci == bvci && bvc->inactive == true)
Harald Weltee5209642020-12-05 19:59:45 +010079 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020080 }
81 return NULL;
82}
83
Harald Welte560bdb32020-12-04 22:24:47 +010084struct gbproxy_bvc *gbproxy_bvc_alloc(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020085{
Daniel Willmann990b1512021-07-09 13:58:58 +020086 char idbuf[64];
Harald Welte560bdb32020-12-04 22:24:47 +010087 struct gbproxy_bvc *bvc;
Daniel Willmanne50550e2020-11-26 18:19:21 +010088 OSMO_ASSERT(nse);
89 struct gbproxy_config *cfg = nse->cfg;
90 OSMO_ASSERT(cfg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020091
Harald Welte560bdb32020-12-04 22:24:47 +010092 bvc = talloc_zero(tall_sgsn_ctx, struct gbproxy_bvc);
93 if (!bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020094 return NULL;
95
Daniel Willmann990b1512021-07-09 13:58:58 +020096 snprintf(idbuf, sizeof(idbuf), "BVC%05u-NSE%05u", bvci,
97 nse->nsei);
98 osmo_identifier_sanitize_buf(idbuf, NULL, '_');
Harald Welte560bdb32020-12-04 22:24:47 +010099 bvc->bvci = bvci;
Daniel Willmanndc763fd2021-09-24 16:45:38 +0200100 bvc->inactive = false;
Harald Welte4bd3e492020-12-07 12:06:52 +0100101 bvc->ctrg = rate_ctr_group_alloc(bvc, &bvc_ctrg_desc, (nse->nsei << 16) | bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100102 if (!bvc->ctrg) {
103 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200104 return NULL;
105 }
Daniel Willmann990b1512021-07-09 13:58:58 +0200106 rate_ctr_group_set_name(bvc->ctrg, idbuf);
Harald Welte560bdb32020-12-04 22:24:47 +0100107 bvc->nse = nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200108
Harald Welte8b4c7942020-12-05 10:14:49 +0100109 hash_add(nse->bvcs, &bvc->list, bvc->bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200110
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100111 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Created\n");
112
113 /* We leave allocating the bvc->fi to the caller, as the FSM details depend
114 * on the type of BVC (SIG/PTP) and role (SGSN/BSS) */
Daniel Willmann3ea37932021-02-10 13:41:14 +0100115 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200116}
117
Harald Welte560bdb32020-12-04 22:24:47 +0100118void gbproxy_bvc_free(struct gbproxy_bvc *bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200119{
Harald Welte560bdb32020-12-04 22:24:47 +0100120 if (!bvc)
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100121 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100122
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100123 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n");
124
Harald Welte8b4c7942020-12-05 10:14:49 +0100125 hash_del(&bvc->list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200126
Harald Welte560bdb32020-12-04 22:24:47 +0100127 rate_ctr_group_free(bvc->ctrg);
128 bvc->ctrg = NULL;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200129
Harald Weltee5209642020-12-05 19:59:45 +0100130 osmo_fsm_inst_free(bvc->fi);
131
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200132 if (bvc->cell) {
133 gbproxy_cell_cleanup_bvc(bvc->cell, bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100134 bvc->cell = NULL;
135 }
Harald Welte560bdb32020-12-04 22:24:47 +0100136 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200137}
138
Harald Weltee5209642020-12-05 19:59:45 +0100139/*! remove BVCs on NSE specified by NSEI.
Harald Welte560bdb32020-12-04 22:24:47 +0100140 * \param[in] cfg proxy in which we operate
141 * \param[in] nsei NS entity in which we should clean up
Harald Weltee5209642020-12-05 19:59:45 +0100142 * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */
143int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200144{
Harald Weltee5209642020-12-05 19:59:45 +0100145 struct hlist_node *btmp;
146 struct gbproxy_bvc *bvc;
147 int j, counter = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200148
Harald Weltee5209642020-12-05 19:59:45 +0100149 if (!nse)
150 return 0;
151
152 hash_for_each_safe(nse->bvcs, j, btmp, bvc, list) {
153 if (bvci && bvc->bvci != bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200154 continue;
Harald Weltee5209642020-12-05 19:59:45 +0100155 if (bvci == 0 && bvc->bvci == 0)
156 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200157
Harald Weltee5209642020-12-05 19:59:45 +0100158 gbproxy_bvc_free(bvc);
159 counter += 1;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200160 }
161
162 return counter;
163}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100164
Harald Weltee5209642020-12-05 19:59:45 +0100165
166/***********************************************************************
167 * CELL
168 ***********************************************************************/
169
170/* Allocate a new 'cell' object */
Philipp Maiere4597ec2021-02-09 16:02:00 +0100171struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci,
172 const struct gprs_ra_id *raid, uint16_t cid)
Harald Weltee5209642020-12-05 19:59:45 +0100173{
174 struct gbproxy_cell *cell;
175 OSMO_ASSERT(cfg);
176
177 cell = talloc_zero(cfg, struct gbproxy_cell);
178 if (!cell)
179 return NULL;
180
181 cell->cfg = cfg;
182 cell->bvci = bvci;
Philipp Maiere4597ec2021-02-09 16:02:00 +0100183 cell->id.cid = cid;
184 memcpy(&cell->id.raid, raid, sizeof(cell->id.raid));
Harald Weltee5209642020-12-05 19:59:45 +0100185
186 hash_add(cfg->cells, &cell->list, cell->bvci);
187
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100188 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n");
189
Harald Weltee5209642020-12-05 19:59:45 +0100190 return cell;
191}
192
193/* Find cell by BVCI */
194struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
195{
196 struct gbproxy_cell *cell;
197
198 hash_for_each_possible(cfg->cells, cell, list, bvci) {
199 if (cell->bvci == bvci)
200 return cell;
201 }
202 return NULL;
203}
204
Daniel Willmannfccbef02021-01-19 17:59:19 +0100205struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid)
206{
207 int i;
208 struct gbproxy_cell *cell;
209
210 hash_for_each(cfg->cells, i, cell, list) {
211 if (cell->id.cid == cid && gsm48_ra_equal(&cell->id.raid, raid)) {
212 return cell;
213 }
214 }
215 return NULL;
216}
217
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200218void gbproxy_cell_cleanup_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
219{
220 int i;
221
Daniel Willmann28ec0e32021-12-06 16:47:22 +0100222 if (cell->bss_bvc == bvc) {
223 /* Remove the whole cell including all BVCs */
224 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
225 if (cell->sgsn_bvc[i]) {
226 gbproxy_bvc_free(cell->sgsn_bvc[i]);
227 cell->sgsn_bvc[i] = NULL;
228 }
229 }
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200230 return gbproxy_cell_free(cell);
Daniel Willmann28ec0e32021-12-06 16:47:22 +0100231 }
Daniel Willmanna631a3a2021-07-14 18:10:16 +0200232
233 /* we could also be a SGSN-side BVC */
234 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
235 if (cell->sgsn_bvc[i] == bvc)
236 cell->sgsn_bvc[i] = NULL;
237 }
238
239}
240
Harald Weltee5209642020-12-05 19:59:45 +0100241void gbproxy_cell_free(struct gbproxy_cell *cell)
242{
243 unsigned int i;
244
245 if (!cell)
246 return;
247
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100248 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
249
Harald Weltee5209642020-12-05 19:59:45 +0100250 /* remove from cfg.cells */
251 hash_del(&cell->list);
252
253 /* remove back-pointers from the BSS side */
254 if (cell->bss_bvc && cell->bss_bvc->cell)
255 cell->bss_bvc->cell = NULL;
256
257 /* remove back-pointers from the SGSN side */
258 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
259 if (!cell->sgsn_bvc[i])
260 continue;
261 if (cell->sgsn_bvc[i]->cell)
262 cell->sgsn_bvc[i]->cell = NULL;
263 }
264
265 talloc_free(cell);
266}
267
268bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
269{
270 unsigned int i;
271 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
272 if (!cell->sgsn_bvc[i]) {
273 cell->sgsn_bvc[i] = bvc;
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100274 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
275 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100276 return true;
277 }
278 }
279 return false;
280}
281
Daniel Willmann77493b12020-12-29 21:13:31 +0100282
283/***********************************************************************
284 * TLLI cache
285 ***********************************************************************/
286
Daniel Willmann9170c342021-01-17 13:12:46 +0100287static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli)
288{
289 struct gbproxy_tlli_cache_entry *cache_entry;
290
291 hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) {
292 if (cache_entry->tlli == tlli)
293 return cache_entry;
294 }
295 return NULL;
296}
297
Daniel Willmann77493b12020-12-29 21:13:31 +0100298void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli)
299{
300 struct gbproxy_config *cfg = nse->cfg;
301 struct timespec now;
302 struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli);
303
304 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
305
306 if (cache_entry) {
307 /* Update the entry if it already exists */
308 cache_entry->nse = nse;
309 cache_entry->tstamp = now.tv_sec;
310 return;
311 }
312
313 cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry);
314 cache_entry->tlli = tlli;
315 cache_entry->nse = nse;
316 cache_entry->tstamp = now.tv_sec;
317 hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli);
318}
319
320static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) {
321 uint i;
322 struct gbproxy_config *cfg = nse->cfg;
323 struct gbproxy_tlli_cache_entry *tlli_cache;
324 struct hlist_node *tmp;
325
326 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
327 if (tlli_cache->nse == nse) {
328 hash_del(&tlli_cache->list);
329 talloc_free(tlli_cache);
330 }
331 }
332}
333
334void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli)
335{
336 struct gbproxy_tlli_cache_entry *tlli_cache;
337 struct hlist_node *tmp;
338
339 hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) {
340 if (tlli_cache->tlli == tlli) {
341 hash_del(&tlli_cache->list);
342 talloc_free(tlli_cache);
343 return;
344 }
345 }
346}
347
348int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg)
349{
350 int i, count = 0;
351 struct gbproxy_tlli_cache_entry *tlli_cache;
352 struct hlist_node *tmp;
353 struct timespec now;
354 time_t expiry;
355
356 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
357 expiry = now.tv_sec - cfg->tlli_cache.timeout;
358
359 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
360 if (tlli_cache->tstamp < expiry) {
361 count++;
362 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli);
363 hash_del(&tlli_cache->list);
364 talloc_free(tlli_cache);
365 }
366 }
367 return count;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100368
369}
370/***********************************************************************
371 * IMSI cache
372 ***********************************************************************/
373static inline uint16_t _checksum_imsi(const char *imsi)
374{
375 size_t len = strlen(imsi);
376 return osmo_crc16(0, (const uint8_t *)imsi, len);
377}
378
Daniel Willmann361d0b52021-07-09 17:44:30 +0200379static 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 +0100380{
381 struct gbproxy_imsi_cache_entry *cache_entry;
382 uint16_t imsi_hash = _checksum_imsi(imsi);
383
384 hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200385 if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi)) && cache_entry->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100386 return cache_entry;
387 }
388 return NULL;
389}
390
Daniel Willmann361d0b52021-07-09 17:44:30 +0200391void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100392{
393 struct gbproxy_config *cfg = nse->cfg;
394 struct timespec now;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200395 struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi, usage);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100396 uint16_t imsi_hash = _checksum_imsi(imsi);
397
398 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
399
400 if (cache_entry) {
401 /* Update the entry if it already exists */
402 cache_entry->nse = nse;
403 cache_entry->tstamp = now.tv_sec;
404 return;
405 }
406
407 cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry);
408 OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi);
409 cache_entry->nse = nse;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200410 cache_entry->usage = usage;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100411 cache_entry->tstamp = now.tv_sec;
412 hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash);
413}
414
415static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) {
416 uint i;
417 struct gbproxy_config *cfg = nse->cfg;
418 struct gbproxy_imsi_cache_entry *imsi_cache;
419 struct hlist_node *tmp;
420
421 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
422 if (imsi_cache->nse == nse) {
423 hash_del(&imsi_cache->list);
424 talloc_free(imsi_cache);
425 }
426 }
427}
428
Daniel Willmann361d0b52021-07-09 17:44:30 +0200429void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100430{
431 struct gbproxy_imsi_cache_entry *imsi_cache;
432 struct hlist_node *tmp;
433 uint16_t imsi_hash = _checksum_imsi(imsi);
434
435 hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200436 if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi))) && imsi_cache->usage == usage) {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100437 hash_del(&imsi_cache->list);
438 talloc_free(imsi_cache);
439 return;
440 }
441 }
442}
443
444int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg)
445{
446 int i, count = 0;
447 struct gbproxy_imsi_cache_entry *imsi_cache;
448 struct hlist_node *tmp;
449 struct timespec now;
450 time_t expiry;
451
452 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
453 expiry = now.tv_sec - cfg->imsi_cache.timeout;
454
455 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
456 if (imsi_cache->tstamp < expiry) {
457 count++;
458 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi);
459 hash_del(&imsi_cache->list);
460 talloc_free(imsi_cache);
461 }
462 }
463 return count;
Daniel Willmann77493b12020-12-29 21:13:31 +0100464}
465
Harald Weltee5209642020-12-05 19:59:45 +0100466/***********************************************************************
467 * NSE - NS Entity
468 ***********************************************************************/
469
470struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100471{
472 struct gbproxy_nse *nse;
473 OSMO_ASSERT(cfg);
474
475 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
476 if (!nse)
477 return NULL;
478
479 nse->nsei = nsei;
Daniel Willmanna8b61652021-02-12 05:05:14 +0100480 nse->max_sdu_len = DEFAULT_NSE_SDU;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100481 nse->cfg = cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100482 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100483
Harald Weltee5209642020-12-05 19:59:45 +0100484 if (sgsn_facing)
485 hash_add(cfg->sgsn_nses, &nse->list, nsei);
486 else
487 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100488
Harald Welte8b4c7942020-12-05 10:14:49 +0100489 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100490
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100491 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
492
Daniel Willmanne50550e2020-11-26 18:19:21 +0100493 return nse;
494}
495
Daniel Willmannee834af2020-12-14 16:22:39 +0100496static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100497{
Harald Welte8b4c7942020-12-05 10:14:49 +0100498 struct gbproxy_bvc *bvc;
499 struct hlist_node *tmp;
500 int i;
501
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100502 if (!nse)
503 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100504
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100505 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
506
Harald Welted2fef952020-12-05 00:31:07 +0100507 hash_del(&nse->list);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100508 /* Clear the cache entries of this NSE */
Daniel Willmann77493b12020-12-29 21:13:31 +0100509 _tlli_cache_remove_nse(nse);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100510 _imsi_cache_remove_nse(nse);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100511
Harald Welte8b4c7942020-12-05 10:14:49 +0100512 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100513 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100514
515 talloc_free(nse);
516}
Daniel Willmannee834af2020-12-14 16:22:39 +0100517static void _sgsn_free(struct gbproxy_sgsn *sgsn);
518
519void gbproxy_nse_free(struct gbproxy_nse *nse)
520{
521 if (!nse)
522 return;
523 OSMO_ASSERT(nse->cfg);
524
525 if (nse->sgsn_facing) {
526 struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
527 OSMO_ASSERT(sgsn);
528 _sgsn_free(sgsn);
529 }
530
531 _nse_free(nse);
532}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100533
Harald Weltee5209642020-12-05 19:59:45 +0100534struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100535{
536 struct gbproxy_nse *nse;
537 OSMO_ASSERT(cfg);
538
Harald Weltee5209642020-12-05 19:59:45 +0100539 if (flags & NSE_F_SGSN) {
540 hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) {
541 if (nse->nsei == nsei)
542 return nse;
543 }
544 }
545
546 if (flags & NSE_F_BSS) {
547 hash_for_each_possible(cfg->bss_nses, nse, list, nsei) {
548 if (nse->nsei == nsei)
549 return nse;
550 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100551 }
552
553 return NULL;
554}
555
Harald Weltee5209642020-12-05 19:59:45 +0100556struct 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 +0100557{
558 struct gbproxy_nse *nse;
559 OSMO_ASSERT(cfg);
560
Harald Weltee5209642020-12-05 19:59:45 +0100561 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100562 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100563 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100564
565 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100566}
Daniel Willmannee834af2020-12-14 16:22:39 +0100567
Daniel Willmann77493b12020-12-29 21:13:31 +0100568struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli)
569{
570 struct gbproxy_tlli_cache_entry *tlli_cache;
571
572 hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) {
573 if (tlli_cache->tlli == tlli)
574 return tlli_cache->nse;
575 }
576 return NULL;
577}
578
Daniel Willmann361d0b52021-07-09 17:44:30 +0200579struct 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 +0100580{
581 struct gbproxy_imsi_cache_entry *imsi_cache;
582 uint16_t imsi_hash = _checksum_imsi(imsi);
583
584 hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200585 if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)) && imsi_cache->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100586 return imsi_cache->nse;
587 }
588 return NULL;
589}
Daniel Willmann77493b12020-12-29 21:13:31 +0100590
Daniel Willmann216dff82020-12-29 16:07:17 +0100591/***********************************************************************
592 * SGSN - Serving GPRS Support Node
593 ***********************************************************************/
594
595/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
596 * \param[in] cfg The gbproxy configuration
597 * \param[in] nsei The nsei where the SGSN can be reached
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100598 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100599 * \return The SGSN, NULL if it couldn't be allocated
600 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100601struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100602{
603 struct gbproxy_sgsn *sgsn;
604 OSMO_ASSERT(cfg);
605
606 sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn);
607 if (!sgsn)
608 return NULL;
609
610 sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
611 if (!sgsn->nse) {
Vadim Yanitskiy6964e882021-01-05 14:42:46 +0100612 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
613 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100614 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100615 }
616
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100617 if (name)
618 sgsn->name = talloc_strdup(sgsn, name);
619 else
620 sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
621 if (!sgsn->name)
622 goto free_sgsn;
623
Daniel Willmannee834af2020-12-14 16:22:39 +0100624 sgsn->pool.allow_attach = true;
625 sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
626
627 llist_add_tail(&sgsn->list, &cfg->sgsns);
628 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
629 return sgsn;
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100630
631free_sgsn:
632 talloc_free(sgsn);
633 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100634}
635
636/* Only free gbproxy_sgsn, sgsn can't be NULL */
637static void _sgsn_free(struct gbproxy_sgsn *sgsn) {
638 struct gbproxy_config *cfg;
639
640 OSMO_ASSERT(sgsn->nse);
641 cfg = sgsn->nse->cfg;
642 OSMO_ASSERT(cfg);
643
644 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
645 llist_del(&sgsn->list);
Daniel Willmann5193f222021-01-11 05:00:46 +0100646 /* talloc will free ->name and ->pool.nri_ranges */
Daniel Willmannee834af2020-12-14 16:22:39 +0100647 talloc_free(sgsn);
648}
649
Daniel Willmann216dff82020-12-29 16:07:17 +0100650/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
651 * \param[in] sgsn The SGSN
652 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100653void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)
654{
655 if (!sgsn)
656 return;
657
Daniel Willmann37518b32021-05-27 18:13:36 +0200658 OSMO_ASSERT(sgsn->nse);
Daniel Willmannee834af2020-12-14 16:22:39 +0100659
660 _nse_free(sgsn->nse);
661 _sgsn_free(sgsn);
662}
663
Daniel Willmann216dff82020-12-29 16:07:17 +0100664/*! Return the SGSN for a given NSEI
665 * \param[in] cfg The gbproxy configuration
666 * \param[in] nsei The nsei where the SGSN can be reached
667 * \return Returns the matching SGSN or NULL if it couldn't be found
668 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100669struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
670{
671 struct gbproxy_sgsn *sgsn;
672 OSMO_ASSERT(cfg);
673
674 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
675 if (!strcmp(sgsn->name, name))
676 return sgsn;
677 }
678
679 return NULL;
680}
681
682/*! Return the SGSN for a given NSEI
683 * \param[in] cfg The gbproxy configuration
684 * \param[in] nsei The nsei where the SGSN can be reached
685 * \return Returns the matching SGSN or NULL if it couldn't be found
686 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100687struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
688{
689 struct gbproxy_sgsn *sgsn;
690 OSMO_ASSERT(cfg);
691
692 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
693 if (sgsn->nse->nsei == nsei)
694 return sgsn;
695 }
696
697 return NULL;
698}
699
Daniel Willmann216dff82020-12-29 16:07:17 +0100700/*! Return the SGSN for a given NSEI, creating a new one if none exists
701 * \param[in] cfg The gbproxy configuration
702 * \param[in] nsei The nsei where the SGSN can be reached
703 * \return Returns the SGSN
704 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100705struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
706{
707 struct gbproxy_sgsn *sgsn;
708 OSMO_ASSERT(cfg);
709
710 sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
711 if (!sgsn)
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100712 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100713
714 return sgsn;
715}
716
717/*! Return the gbproxy_sgsn matching that NRI
718 * \param[in] cfg proxy in which we operate
719 * \param[in] nri NRI to look for
720 * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI
721 * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found
722 */
723struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri)
724{
725 struct gbproxy_sgsn *sgsn;
726 OSMO_ASSERT(cfg);
727
728 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200729 if (!sgsn->nse->alive)
730 continue;
731
Daniel Willmannee834af2020-12-14 16:22:39 +0100732 if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) {
733 /* Also check if the NRI we're looking for is a NULL NRI */
Vadim Yanitskiy4dba67a2021-01-05 14:36:52 +0100734 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100735 if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges))
736 *null_nri = true;
737 else
738 *null_nri = false;
739 }
740 return sgsn;
741 }
742 }
743
744 return NULL;
745}
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100746
Daniel Willmann37518b32021-05-27 18:13:36 +0200747/*! 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 +0100748 * \param[in] cfg The gbproxy configuration
749 * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution
750 * \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
751 * added/removed or allow_attach changes.
752 * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found
753 */
754struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
755 uint32_t tlli)
756{
757 uint32_t i = 0;
758 uint32_t index, num_sgsns;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100759 OSMO_ASSERT(cfg);
760
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100761 struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override;
762
763 if (sgsn) {
764 LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n");
765 return sgsn;
766 }
767
Daniel Willmann5193f222021-01-11 05:00:46 +0100768 /* TODO: We should keep track of count in cfg */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100769 num_sgsns = llist_count(&cfg->sgsns);
770
771 if (num_sgsns == 0)
772 return NULL;
773
Daniel Willmann5193f222021-01-11 05:00:46 +0100774 /* FIXME: 256 SGSNs ought to be enough for everyone */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100775 index = hash_32(tlli, 8) % num_sgsns;
776
Daniel Willmann5193f222021-01-11 05:00:46 +0100777 /* Get the first enabled SGSN after index */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100778 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200779 if (i >= index && sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100780 return sgsn;
781 }
782 i++;
783 }
Daniel Willmann5193f222021-01-11 05:00:46 +0100784 /* Start again from the beginning */
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100785 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100786 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100787 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100788 break;
Daniel Willmann37518b32021-05-27 18:13:36 +0200789 } else if (sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100790 return sgsn;
791 }
792 i++;
793 }
794
795 return NULL;
796}
Daniel Willmann37518b32021-05-27 18:13:36 +0200797
798/*! Return the first available gbproxy_sgsn
799 * \param[in] cfg proxy in which we operate
800 * \return The SGSN, NULL if no matching SGSN could be found
801 */
802struct gbproxy_sgsn *gbproxy_sgsn_by_available(struct gbproxy_config *cfg)
803{
804 struct gbproxy_sgsn *sgsn;
805 OSMO_ASSERT(cfg);
806
807 llist_for_each_entry(sgsn, &cfg->sgsns, list)
808 if (sgsn->nse->alive &&sgsn->pool.allow_attach)
809 return sgsn;
810
811 return NULL;
812}