blob: f5ad4456990a99d837ec54ccf23dedc210d3a1fd [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) {
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) */
Daniel Willmann3ea37932021-02-10 13:41:14 +010096 return bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020097}
98
Harald Welte560bdb32020-12-04 22:24:47 +010099void gbproxy_bvc_free(struct gbproxy_bvc *bvc)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200100{
Harald Weltee5209642020-12-05 19:59:45 +0100101 struct gbproxy_cell *cell;
102
Harald Welte560bdb32020-12-04 22:24:47 +0100103 if (!bvc)
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100104 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100105
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100106 LOGPBVC_CAT(bvc, DOBJ, LOGL_INFO, "BVC Destroying\n");
107
Harald Welte8b4c7942020-12-05 10:14:49 +0100108 hash_del(&bvc->list);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200109
Harald Welte560bdb32020-12-04 22:24:47 +0100110 rate_ctr_group_free(bvc->ctrg);
111 bvc->ctrg = NULL;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200112
Harald Weltee5209642020-12-05 19:59:45 +0100113 osmo_fsm_inst_free(bvc->fi);
114
115 cell = bvc->cell;
116 if (cell) {
117 int i;
118
119 if (cell->bss_bvc == bvc)
120 cell->bss_bvc = NULL;
121
122 /* we could also be a SGSN-side BVC */
123 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
124 if (cell->sgsn_bvc[i] == bvc)
125 cell->sgsn_bvc[i] = NULL;
126 }
127 bvc->cell = NULL;
128 }
129
Harald Welte560bdb32020-12-04 22:24:47 +0100130 talloc_free(bvc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200131}
132
Harald Weltee5209642020-12-05 19:59:45 +0100133/*! remove BVCs on NSE specified by NSEI.
Harald Welte560bdb32020-12-04 22:24:47 +0100134 * \param[in] cfg proxy in which we operate
135 * \param[in] nsei NS entity in which we should clean up
Harald Weltee5209642020-12-05 19:59:45 +0100136 * \param[in] bvci if 0: remove all PTP BVCs; if != 0: BVCI of the single BVC to clean up */
137int gbproxy_cleanup_bvcs(struct gbproxy_nse *nse, uint16_t bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200138{
Harald Weltee5209642020-12-05 19:59:45 +0100139 struct hlist_node *btmp;
140 struct gbproxy_bvc *bvc;
141 int j, counter = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200142
Harald Weltee5209642020-12-05 19:59:45 +0100143 if (!nse)
144 return 0;
145
146 hash_for_each_safe(nse->bvcs, j, btmp, bvc, list) {
147 if (bvci && bvc->bvci != bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200148 continue;
Harald Weltee5209642020-12-05 19:59:45 +0100149 if (bvci == 0 && bvc->bvci == 0)
150 continue;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200151
Harald Weltee5209642020-12-05 19:59:45 +0100152 gbproxy_bvc_free(bvc);
153 counter += 1;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200154 }
155
156 return counter;
157}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100158
Harald Weltee5209642020-12-05 19:59:45 +0100159
160/***********************************************************************
161 * CELL
162 ***********************************************************************/
163
164/* Allocate a new 'cell' object */
Philipp Maiere4597ec2021-02-09 16:02:00 +0100165struct gbproxy_cell *gbproxy_cell_alloc(struct gbproxy_config *cfg, uint16_t bvci,
166 const struct gprs_ra_id *raid, uint16_t cid)
Harald Weltee5209642020-12-05 19:59:45 +0100167{
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;
Philipp Maiere4597ec2021-02-09 16:02:00 +0100177 cell->id.cid = cid;
178 memcpy(&cell->id.raid, raid, sizeof(cell->id.raid));
Harald Weltee5209642020-12-05 19:59:45 +0100179
180 hash_add(cfg->cells, &cell->list, cell->bvci);
181
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100182 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Created\n");
183
Harald Weltee5209642020-12-05 19:59:45 +0100184 return cell;
185}
186
187/* Find cell by BVCI */
188struct gbproxy_cell *gbproxy_cell_by_bvci(struct gbproxy_config *cfg, uint16_t bvci)
189{
190 struct gbproxy_cell *cell;
191
192 hash_for_each_possible(cfg->cells, cell, list, bvci) {
193 if (cell->bvci == bvci)
194 return cell;
195 }
196 return NULL;
197}
198
Daniel Willmannfccbef02021-01-19 17:59:19 +0100199struct gbproxy_cell *gbproxy_cell_by_cellid(struct gbproxy_config *cfg, const struct gprs_ra_id *raid, uint16_t cid)
200{
201 int i;
202 struct gbproxy_cell *cell;
203
204 hash_for_each(cfg->cells, i, cell, list) {
205 if (cell->id.cid == cid && gsm48_ra_equal(&cell->id.raid, raid)) {
206 return cell;
207 }
208 }
209 return NULL;
210}
211
Harald Weltee5209642020-12-05 19:59:45 +0100212void gbproxy_cell_free(struct gbproxy_cell *cell)
213{
214 unsigned int i;
215
216 if (!cell)
217 return;
218
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100219 LOGPCELL_CAT(cell, DOBJ, LOGL_INFO, "CELL Destroying\n");
220
Harald Weltee5209642020-12-05 19:59:45 +0100221 /* remove from cfg.cells */
222 hash_del(&cell->list);
223
224 /* remove back-pointers from the BSS side */
225 if (cell->bss_bvc && cell->bss_bvc->cell)
226 cell->bss_bvc->cell = NULL;
227
228 /* remove back-pointers from the SGSN side */
229 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
230 if (!cell->sgsn_bvc[i])
231 continue;
232 if (cell->sgsn_bvc[i]->cell)
233 cell->sgsn_bvc[i]->cell = NULL;
234 }
235
236 talloc_free(cell);
237}
238
239bool gbproxy_cell_add_sgsn_bvc(struct gbproxy_cell *cell, struct gbproxy_bvc *bvc)
240{
241 unsigned int i;
242 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
243 if (!cell->sgsn_bvc[i]) {
244 cell->sgsn_bvc[i] = bvc;
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100245 LOGPCELL_CAT(cell, DOBJ, LOGL_DEBUG, "CELL linked to SGSN\n");
246 LOGPBVC_CAT(bvc, DOBJ, LOGL_DEBUG, "BVC linked to CELL\n");
Harald Weltee5209642020-12-05 19:59:45 +0100247 return true;
248 }
249 }
250 return false;
251}
252
Daniel Willmann77493b12020-12-29 21:13:31 +0100253
254/***********************************************************************
255 * TLLI cache
256 ***********************************************************************/
257
Daniel Willmann9170c342021-01-17 13:12:46 +0100258static inline struct gbproxy_tlli_cache_entry *_get_tlli_entry(struct gbproxy_config *cfg, uint32_t tlli)
259{
260 struct gbproxy_tlli_cache_entry *cache_entry;
261
262 hash_for_each_possible(cfg->tlli_cache.entries, cache_entry, list, tlli) {
263 if (cache_entry->tlli == tlli)
264 return cache_entry;
265 }
266 return NULL;
267}
268
Daniel Willmann77493b12020-12-29 21:13:31 +0100269void gbproxy_tlli_cache_update(struct gbproxy_nse *nse, uint32_t tlli)
270{
271 struct gbproxy_config *cfg = nse->cfg;
272 struct timespec now;
273 struct gbproxy_tlli_cache_entry *cache_entry = _get_tlli_entry(cfg, tlli);
274
275 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
276
277 if (cache_entry) {
278 /* Update the entry if it already exists */
279 cache_entry->nse = nse;
280 cache_entry->tstamp = now.tv_sec;
281 return;
282 }
283
284 cache_entry = talloc_zero(cfg, struct gbproxy_tlli_cache_entry);
285 cache_entry->tlli = tlli;
286 cache_entry->nse = nse;
287 cache_entry->tstamp = now.tv_sec;
288 hash_add(cfg->tlli_cache.entries, &cache_entry->list, cache_entry->tlli);
289}
290
291static void _tlli_cache_remove_nse(struct gbproxy_nse *nse) {
292 uint i;
293 struct gbproxy_config *cfg = nse->cfg;
294 struct gbproxy_tlli_cache_entry *tlli_cache;
295 struct hlist_node *tmp;
296
297 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
298 if (tlli_cache->nse == nse) {
299 hash_del(&tlli_cache->list);
300 talloc_free(tlli_cache);
301 }
302 }
303}
304
305void gbproxy_tlli_cache_remove(struct gbproxy_config *cfg, uint32_t tlli)
306{
307 struct gbproxy_tlli_cache_entry *tlli_cache;
308 struct hlist_node *tmp;
309
310 hash_for_each_possible_safe(cfg->tlli_cache.entries, tlli_cache, tmp, list, tlli) {
311 if (tlli_cache->tlli == tlli) {
312 hash_del(&tlli_cache->list);
313 talloc_free(tlli_cache);
314 return;
315 }
316 }
317}
318
319int gbproxy_tlli_cache_cleanup(struct gbproxy_config *cfg)
320{
321 int i, count = 0;
322 struct gbproxy_tlli_cache_entry *tlli_cache;
323 struct hlist_node *tmp;
324 struct timespec now;
325 time_t expiry;
326
327 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
328 expiry = now.tv_sec - cfg->tlli_cache.timeout;
329
330 hash_for_each_safe(cfg->tlli_cache.entries, i, tmp, tlli_cache, list) {
331 if (tlli_cache->tstamp < expiry) {
332 count++;
333 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for TLLI %08x expired, removing\n", tlli_cache->tlli);
334 hash_del(&tlli_cache->list);
335 talloc_free(tlli_cache);
336 }
337 }
338 return count;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100339
340}
341/***********************************************************************
342 * IMSI cache
343 ***********************************************************************/
344static inline uint16_t _checksum_imsi(const char *imsi)
345{
346 size_t len = strlen(imsi);
347 return osmo_crc16(0, (const uint8_t *)imsi, len);
348}
349
Daniel Willmann361d0b52021-07-09 17:44:30 +0200350static 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 +0100351{
352 struct gbproxy_imsi_cache_entry *cache_entry;
353 uint16_t imsi_hash = _checksum_imsi(imsi);
354
355 hash_for_each_possible(cfg->imsi_cache.entries, cache_entry, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200356 if (!strncmp(cache_entry->imsi, imsi, sizeof(cache_entry->imsi)) && cache_entry->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100357 return cache_entry;
358 }
359 return NULL;
360}
361
Daniel Willmann361d0b52021-07-09 17:44:30 +0200362void gbproxy_imsi_cache_update(struct gbproxy_nse *nse, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100363{
364 struct gbproxy_config *cfg = nse->cfg;
365 struct timespec now;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200366 struct gbproxy_imsi_cache_entry *cache_entry = _get_imsi_entry(cfg, imsi, usage);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100367 uint16_t imsi_hash = _checksum_imsi(imsi);
368
369 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
370
371 if (cache_entry) {
372 /* Update the entry if it already exists */
373 cache_entry->nse = nse;
374 cache_entry->tstamp = now.tv_sec;
375 return;
376 }
377
378 cache_entry = talloc_zero(cfg, struct gbproxy_imsi_cache_entry);
379 OSMO_STRLCPY_ARRAY(cache_entry->imsi, imsi);
380 cache_entry->nse = nse;
Daniel Willmann361d0b52021-07-09 17:44:30 +0200381 cache_entry->usage = usage;
Daniel Willmannc8a50092021-01-17 13:11:41 +0100382 cache_entry->tstamp = now.tv_sec;
383 hash_add(cfg->imsi_cache.entries, &cache_entry->list, imsi_hash);
384}
385
386static void _imsi_cache_remove_nse(struct gbproxy_nse *nse) {
387 uint i;
388 struct gbproxy_config *cfg = nse->cfg;
389 struct gbproxy_imsi_cache_entry *imsi_cache;
390 struct hlist_node *tmp;
391
392 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
393 if (imsi_cache->nse == nse) {
394 hash_del(&imsi_cache->list);
395 talloc_free(imsi_cache);
396 }
397 }
398}
399
Daniel Willmann361d0b52021-07-09 17:44:30 +0200400void gbproxy_imsi_cache_remove(struct gbproxy_config *cfg, const char *imsi, enum cache_usage_type usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100401{
402 struct gbproxy_imsi_cache_entry *imsi_cache;
403 struct hlist_node *tmp;
404 uint16_t imsi_hash = _checksum_imsi(imsi);
405
406 hash_for_each_possible_safe(cfg->imsi_cache.entries, imsi_cache, tmp, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200407 if (!(strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi))) && imsi_cache->usage == usage) {
Daniel Willmannc8a50092021-01-17 13:11:41 +0100408 hash_del(&imsi_cache->list);
409 talloc_free(imsi_cache);
410 return;
411 }
412 }
413}
414
415int gbproxy_imsi_cache_cleanup(struct gbproxy_config *cfg)
416{
417 int i, count = 0;
418 struct gbproxy_imsi_cache_entry *imsi_cache;
419 struct hlist_node *tmp;
420 struct timespec now;
421 time_t expiry;
422
423 osmo_clock_gettime(CLOCK_MONOTONIC, &now);
424 expiry = now.tv_sec - cfg->imsi_cache.timeout;
425
426 hash_for_each_safe(cfg->imsi_cache.entries, i, tmp, imsi_cache, list) {
427 if (imsi_cache->tstamp < expiry) {
428 count++;
429 LOGP(DGPRS, LOGL_NOTICE, "Cache entry for IMSI %s expired, removing\n", imsi_cache->imsi);
430 hash_del(&imsi_cache->list);
431 talloc_free(imsi_cache);
432 }
433 }
434 return count;
Daniel Willmann77493b12020-12-29 21:13:31 +0100435}
436
Harald Weltee5209642020-12-05 19:59:45 +0100437/***********************************************************************
438 * NSE - NS Entity
439 ***********************************************************************/
440
441struct gbproxy_nse *gbproxy_nse_alloc(struct gbproxy_config *cfg, uint16_t nsei, bool sgsn_facing)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100442{
443 struct gbproxy_nse *nse;
444 OSMO_ASSERT(cfg);
445
446 nse = talloc_zero(tall_sgsn_ctx, struct gbproxy_nse);
447 if (!nse)
448 return NULL;
449
450 nse->nsei = nsei;
Daniel Willmanna8b61652021-02-12 05:05:14 +0100451 nse->max_sdu_len = DEFAULT_NSE_SDU;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100452 nse->cfg = cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100453 nse->sgsn_facing = sgsn_facing;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100454
Harald Weltee5209642020-12-05 19:59:45 +0100455 if (sgsn_facing)
456 hash_add(cfg->sgsn_nses, &nse->list, nsei);
457 else
458 hash_add(cfg->bss_nses, &nse->list, nsei);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100459
Harald Welte8b4c7942020-12-05 10:14:49 +0100460 hash_init(nse->bvcs);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100461
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100462 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Created\n");
463
Daniel Willmanne50550e2020-11-26 18:19:21 +0100464 return nse;
465}
466
Daniel Willmannee834af2020-12-14 16:22:39 +0100467static void _nse_free(struct gbproxy_nse *nse)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100468{
Harald Welte8b4c7942020-12-05 10:14:49 +0100469 struct gbproxy_bvc *bvc;
470 struct hlist_node *tmp;
471 int i;
472
Daniel Willmannf6a36cc2020-12-04 17:38:46 +0100473 if (!nse)
474 return;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100475
Harald Weltecfc7e8e2020-12-07 12:03:10 +0100476 LOGPNSE_CAT(nse, DOBJ, LOGL_INFO, "NSE Destroying\n");
477
Harald Welted2fef952020-12-05 00:31:07 +0100478 hash_del(&nse->list);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100479 /* Clear the cache entries of this NSE */
Daniel Willmann77493b12020-12-29 21:13:31 +0100480 _tlli_cache_remove_nse(nse);
Daniel Willmannc8a50092021-01-17 13:11:41 +0100481 _imsi_cache_remove_nse(nse);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100482
Harald Welte8b4c7942020-12-05 10:14:49 +0100483 hash_for_each_safe(nse->bvcs, i, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +0100484 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100485
486 talloc_free(nse);
487}
Daniel Willmannee834af2020-12-14 16:22:39 +0100488static void _sgsn_free(struct gbproxy_sgsn *sgsn);
489
490void gbproxy_nse_free(struct gbproxy_nse *nse)
491{
492 if (!nse)
493 return;
494 OSMO_ASSERT(nse->cfg);
495
496 if (nse->sgsn_facing) {
497 struct gbproxy_sgsn *sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
498 OSMO_ASSERT(sgsn);
499 _sgsn_free(sgsn);
500 }
501
502 _nse_free(nse);
503}
Daniel Willmanne50550e2020-11-26 18:19:21 +0100504
Harald Weltee5209642020-12-05 19:59:45 +0100505struct gbproxy_nse *gbproxy_nse_by_nsei(struct gbproxy_config *cfg, uint16_t nsei, uint32_t flags)
Daniel Willmanne50550e2020-11-26 18:19:21 +0100506{
507 struct gbproxy_nse *nse;
508 OSMO_ASSERT(cfg);
509
Harald Weltee5209642020-12-05 19:59:45 +0100510 if (flags & NSE_F_SGSN) {
511 hash_for_each_possible(cfg->sgsn_nses, nse, list, nsei) {
512 if (nse->nsei == nsei)
513 return nse;
514 }
515 }
516
517 if (flags & NSE_F_BSS) {
518 hash_for_each_possible(cfg->bss_nses, nse, list, nsei) {
519 if (nse->nsei == nsei)
520 return nse;
521 }
Daniel Willmanne50550e2020-11-26 18:19:21 +0100522 }
523
524 return NULL;
525}
526
Harald Weltee5209642020-12-05 19:59:45 +0100527struct 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 +0100528{
529 struct gbproxy_nse *nse;
530 OSMO_ASSERT(cfg);
531
Harald Weltee5209642020-12-05 19:59:45 +0100532 nse = gbproxy_nse_by_nsei(cfg, nsei, sgsn_facing ? NSE_F_SGSN : NSE_F_BSS);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100533 if (!nse)
Harald Weltee5209642020-12-05 19:59:45 +0100534 nse = gbproxy_nse_alloc(cfg, nsei, sgsn_facing);
Daniel Willmanne50550e2020-11-26 18:19:21 +0100535
536 return nse;
Harald Welte560bdb32020-12-04 22:24:47 +0100537}
Daniel Willmannee834af2020-12-14 16:22:39 +0100538
Daniel Willmann77493b12020-12-29 21:13:31 +0100539struct gbproxy_nse *gbproxy_nse_by_tlli(struct gbproxy_config *cfg, uint32_t tlli)
540{
541 struct gbproxy_tlli_cache_entry *tlli_cache;
542
543 hash_for_each_possible(cfg->tlli_cache.entries, tlli_cache, list, tlli) {
544 if (tlli_cache->tlli == tlli)
545 return tlli_cache->nse;
546 }
547 return NULL;
548}
549
Daniel Willmann361d0b52021-07-09 17:44:30 +0200550struct 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 +0100551{
552 struct gbproxy_imsi_cache_entry *imsi_cache;
553 uint16_t imsi_hash = _checksum_imsi(imsi);
554
555 hash_for_each_possible(cfg->imsi_cache.entries, imsi_cache, list, imsi_hash) {
Daniel Willmann361d0b52021-07-09 17:44:30 +0200556 if (!strncmp(imsi_cache->imsi, imsi, sizeof(imsi_cache->imsi)) && imsi_cache->usage == usage)
Daniel Willmannc8a50092021-01-17 13:11:41 +0100557 return imsi_cache->nse;
558 }
559 return NULL;
560}
Daniel Willmann77493b12020-12-29 21:13:31 +0100561
Daniel Willmann216dff82020-12-29 16:07:17 +0100562/***********************************************************************
563 * SGSN - Serving GPRS Support Node
564 ***********************************************************************/
565
566/*! Allocate a new SGSN. This ensures the corresponding gbproxy_nse is allocated as well
567 * \param[in] cfg The gbproxy configuration
568 * \param[in] nsei The nsei where the SGSN can be reached
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100569 * \param[in] name A name to give the SGSN
Daniel Willmann216dff82020-12-29 16:07:17 +0100570 * \return The SGSN, NULL if it couldn't be allocated
571 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100572struct gbproxy_sgsn *gbproxy_sgsn_alloc(struct gbproxy_config *cfg, uint16_t nsei, const char *name)
Daniel Willmannee834af2020-12-14 16:22:39 +0100573{
574 struct gbproxy_sgsn *sgsn;
575 OSMO_ASSERT(cfg);
576
577 sgsn = talloc_zero(tall_sgsn_ctx, struct gbproxy_sgsn);
578 if (!sgsn)
579 return NULL;
580
581 sgsn->nse = gbproxy_nse_alloc(cfg, nsei, true);
582 if (!sgsn->nse) {
Vadim Yanitskiy6964e882021-01-05 14:42:46 +0100583 LOGP(DOBJ, LOGL_ERROR, "Could not allocate NSE(%05u) for SGSN(%s)\n",
584 nsei, sgsn->name);
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100585 goto free_sgsn;
Daniel Willmannee834af2020-12-14 16:22:39 +0100586 }
587
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100588 if (name)
589 sgsn->name = talloc_strdup(sgsn, name);
590 else
591 sgsn->name = talloc_asprintf(sgsn, "NSE(%05u)", sgsn->nse->nsei);
592 if (!sgsn->name)
593 goto free_sgsn;
594
Daniel Willmannee834af2020-12-14 16:22:39 +0100595 sgsn->pool.allow_attach = true;
596 sgsn->pool.nri_ranges = osmo_nri_ranges_alloc(sgsn);
597
598 llist_add_tail(&sgsn->list, &cfg->sgsns);
599 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Created\n");
600 return sgsn;
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100601
602free_sgsn:
603 talloc_free(sgsn);
604 return NULL;
Daniel Willmannee834af2020-12-14 16:22:39 +0100605}
606
607/* Only free gbproxy_sgsn, sgsn can't be NULL */
608static void _sgsn_free(struct gbproxy_sgsn *sgsn) {
609 struct gbproxy_config *cfg;
610
611 OSMO_ASSERT(sgsn->nse);
612 cfg = sgsn->nse->cfg;
613 OSMO_ASSERT(cfg);
614
615 LOGPSGSN_CAT(sgsn, DOBJ, LOGL_INFO, "SGSN Destroying\n");
616 llist_del(&sgsn->list);
Daniel Willmann5193f222021-01-11 05:00:46 +0100617 /* talloc will free ->name and ->pool.nri_ranges */
Daniel Willmannee834af2020-12-14 16:22:39 +0100618 talloc_free(sgsn);
619}
620
Daniel Willmann216dff82020-12-29 16:07:17 +0100621/*! Free the SGSN. This ensures the corresponding gbproxy_nse is freed as well
622 * \param[in] sgsn The SGSN
623 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100624void gbproxy_sgsn_free(struct gbproxy_sgsn *sgsn)
625{
626 if (!sgsn)
627 return;
628
Daniel Willmann37518b32021-05-27 18:13:36 +0200629 OSMO_ASSERT(sgsn->nse);
Daniel Willmannee834af2020-12-14 16:22:39 +0100630
631 _nse_free(sgsn->nse);
632 _sgsn_free(sgsn);
633}
634
Daniel Willmann216dff82020-12-29 16:07:17 +0100635/*! Return the SGSN for a given NSEI
636 * \param[in] cfg The gbproxy configuration
637 * \param[in] nsei The nsei where the SGSN can be reached
638 * \return Returns the matching SGSN or NULL if it couldn't be found
639 */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100640struct gbproxy_sgsn *gbproxy_sgsn_by_name(struct gbproxy_config *cfg, const char *name)
641{
642 struct gbproxy_sgsn *sgsn;
643 OSMO_ASSERT(cfg);
644
645 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
646 if (!strcmp(sgsn->name, name))
647 return sgsn;
648 }
649
650 return NULL;
651}
652
653/*! Return the SGSN for a given NSEI
654 * \param[in] cfg The gbproxy configuration
655 * \param[in] nsei The nsei where the SGSN can be reached
656 * \return Returns the matching SGSN or NULL if it couldn't be found
657 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100658struct gbproxy_sgsn *gbproxy_sgsn_by_nsei(struct gbproxy_config *cfg, uint16_t nsei)
659{
660 struct gbproxy_sgsn *sgsn;
661 OSMO_ASSERT(cfg);
662
663 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
664 if (sgsn->nse->nsei == nsei)
665 return sgsn;
666 }
667
668 return NULL;
669}
670
Daniel Willmann216dff82020-12-29 16:07:17 +0100671/*! Return the SGSN for a given NSEI, creating a new one if none exists
672 * \param[in] cfg The gbproxy configuration
673 * \param[in] nsei The nsei where the SGSN can be reached
674 * \return Returns the SGSN
675 */
Daniel Willmannee834af2020-12-14 16:22:39 +0100676struct gbproxy_sgsn *gbproxy_sgsn_by_nsei_or_new(struct gbproxy_config *cfg, uint16_t nsei)
677{
678 struct gbproxy_sgsn *sgsn;
679 OSMO_ASSERT(cfg);
680
681 sgsn = gbproxy_sgsn_by_nsei(cfg, nsei);
682 if (!sgsn)
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100683 sgsn = gbproxy_sgsn_alloc(cfg, nsei, NULL);
Daniel Willmannee834af2020-12-14 16:22:39 +0100684
685 return sgsn;
686}
687
688/*! Return the gbproxy_sgsn matching that NRI
689 * \param[in] cfg proxy in which we operate
690 * \param[in] nri NRI to look for
691 * \param[out] null_nri If not NULL this indicates whether the NRI is a null NRI
692 * \return The SGSN this NRI has been added to, NULL if no matching SGSN could be found
693 */
694struct gbproxy_sgsn *gbproxy_sgsn_by_nri(struct gbproxy_config *cfg, uint16_t nri, bool *null_nri)
695{
696 struct gbproxy_sgsn *sgsn;
697 OSMO_ASSERT(cfg);
698
699 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200700 if (!sgsn->nse->alive)
701 continue;
702
Daniel Willmannee834af2020-12-14 16:22:39 +0100703 if (osmo_nri_v_matches_ranges(nri, sgsn->pool.nri_ranges)) {
704 /* Also check if the NRI we're looking for is a NULL NRI */
Vadim Yanitskiy4dba67a2021-01-05 14:36:52 +0100705 if (null_nri) {
Daniel Willmannee834af2020-12-14 16:22:39 +0100706 if (osmo_nri_v_matches_ranges(nri, cfg->pool.null_nri_ranges))
707 *null_nri = true;
708 else
709 *null_nri = false;
710 }
711 return sgsn;
712 }
713 }
714
715 return NULL;
716}
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100717
Daniel Willmann37518b32021-05-27 18:13:36 +0200718/*! 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 +0100719 * \param[in] cfg The gbproxy configuration
720 * \param[in] sgsn_avoid If not NULL then avoid this SGSN when selecting a new one. Use for load redistribution
721 * \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
722 * added/removed or allow_attach changes.
723 * \return Returns the sgsn on success, NULL if no SGSN that allows new connections could be found
724 */
725struct gbproxy_sgsn *gbproxy_sgsn_by_tlli(struct gbproxy_config *cfg, struct gbproxy_sgsn *sgsn_avoid,
726 uint32_t tlli)
727{
728 uint32_t i = 0;
729 uint32_t index, num_sgsns;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100730 OSMO_ASSERT(cfg);
731
Daniel Willmannb387c1e2020-12-27 18:14:39 +0100732 struct gbproxy_sgsn *sgsn = cfg->pool.nsf_override;
733
734 if (sgsn) {
735 LOGPSGSN(sgsn, LOGL_DEBUG, "Node selection function is overridden by config\n");
736 return sgsn;
737 }
738
Daniel Willmann5193f222021-01-11 05:00:46 +0100739 /* TODO: We should keep track of count in cfg */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100740 num_sgsns = llist_count(&cfg->sgsns);
741
742 if (num_sgsns == 0)
743 return NULL;
744
Daniel Willmann5193f222021-01-11 05:00:46 +0100745 /* FIXME: 256 SGSNs ought to be enough for everyone */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100746 index = hash_32(tlli, 8) % num_sgsns;
747
Daniel Willmann5193f222021-01-11 05:00:46 +0100748 /* Get the first enabled SGSN after index */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100749 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200750 if (i >= index && sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100751 return sgsn;
752 }
753 i++;
754 }
Daniel Willmann5193f222021-01-11 05:00:46 +0100755 /* Start again from the beginning */
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100756 i = 0;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100757 llist_for_each_entry(sgsn, &cfg->sgsns, list) {
Daniel Willmann3c56a2a2021-01-05 15:52:05 +0100758 if (i >= index) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100759 break;
Daniel Willmann37518b32021-05-27 18:13:36 +0200760 } else if (sgsn->pool.allow_attach && sgsn->nse->alive) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100761 return sgsn;
762 }
763 i++;
764 }
765
766 return NULL;
767}
Daniel Willmann37518b32021-05-27 18:13:36 +0200768
769/*! Return the first available gbproxy_sgsn
770 * \param[in] cfg proxy in which we operate
771 * \return The SGSN, NULL if no matching SGSN could be found
772 */
773struct gbproxy_sgsn *gbproxy_sgsn_by_available(struct gbproxy_config *cfg)
774{
775 struct gbproxy_sgsn *sgsn;
776 OSMO_ASSERT(cfg);
777
778 llist_for_each_entry(sgsn, &cfg->sgsns, list)
779 if (sgsn->nse->alive &&sgsn->pool.allow_attach)
780 return sgsn;
781
782 return NULL;
783}