gprs: Refactor gbprox_register_tlli()

Currently gbprox_register_tlli() is a rather complex function.

This patch splits it into several smaller functions to ease reviewing
and maintaining it.

Sponsored-by: On-Waves ehf
This commit is contained in:
Jacob Erlbeck 2014-07-03 15:52:49 +02:00 committed by Holger Hans Peter Freyther
parent 0196c9936c
commit 58cf664d28
1 changed files with 54 additions and 32 deletions

View File

@ -495,6 +495,16 @@ static int gbprox_check_imsi(struct gbproxy_peer *peer,
return 1; return 1;
} }
static void gbprox_attach_tlli_info(struct gbproxy_peer *peer, time_t now,
struct gbproxy_tlli_info *tlli_info)
{
struct gbproxy_patch_state *state = &peer->patch_state;
tlli_info->timestamp = now;
llist_add(&tlli_info->list, &state->enabled_tllis);
state->enabled_tllis_count += 1;
}
int gbprox_remove_stale_tllis(struct gbproxy_peer *peer, time_t now) int gbprox_remove_stale_tllis(struct gbproxy_peer *peer, time_t now)
{ {
struct gbproxy_patch_state *state = &peer->patch_state; struct gbproxy_patch_state *state = &peer->patch_state;
@ -530,6 +540,35 @@ int gbprox_remove_stale_tllis(struct gbproxy_peer *peer, time_t now)
return deleted_count; return deleted_count;
} }
static struct gbproxy_tlli_info *gbprox_get_detached_tlli_info(
struct gbproxy_peer *peer,
struct gbproxy_tlli_info *tlli_info,
uint32_t tlli)
{
struct gbproxy_patch_state *state = &peer->patch_state;
if (!tlli_info) {
tlli_info = talloc_zero(peer, struct gbproxy_tlli_info);
tlli_info->tlli = tlli;
} else {
llist_del(&tlli_info->list);
OSMO_ASSERT(state->enabled_tllis_count > 0);
state->enabled_tllis_count -= 1;
}
return tlli_info;
}
static void gbprox_update_tlli_info(struct gbproxy_tlli_info *tlli_info,
const uint8_t *imsi, size_t imsi_len)
{
tlli_info->mi_data_len = imsi_len;
tlli_info->mi_data =
talloc_realloc_size(tlli_info, tlli_info->mi_data, imsi_len);
OSMO_ASSERT(tlli_info->mi_data != NULL);
memcpy(tlli_info->mi_data, imsi, imsi_len);
}
void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli, void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli,
const uint8_t *imsi, size_t imsi_len) const uint8_t *imsi, size_t imsi_len)
{ {
@ -537,6 +576,7 @@ void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli,
struct gbproxy_tlli_info *tlli_info; struct gbproxy_tlli_info *tlli_info;
int enable_patching; int enable_patching;
time_t now = 0; time_t now = 0;
int tlli_already_known;
if (gprs_tlli_type(tlli) != TLLI_LOCAL) if (gprs_tlli_type(tlli) != TLLI_LOCAL)
return; return;
@ -547,14 +587,14 @@ void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli,
if (!peer->cfg->check_imsi) if (!peer->cfg->check_imsi)
return; return;
tlli_info = gbprox_find_tlli(peer, tlli);
/* Check, whether the IMSI matches */ /* Check, whether the IMSI matches */
enable_patching = gbprox_check_imsi(peer, imsi, imsi_len); enable_patching = gbprox_check_imsi(peer, imsi, imsi_len);
if (enable_patching < 0) if (enable_patching < 0)
return; return;
tlli_info = gbprox_find_tlli(peer, tlli);
if (!tlli_info) { if (!tlli_info) {
tlli_info = gbprox_find_tlli_by_mi(peer, imsi, imsi_len); tlli_info = gbprox_find_tlli_by_mi(peer, imsi, imsi_len);
@ -567,37 +607,27 @@ void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli,
} }
} }
if (!tlli_info) { tlli_already_known = tlli_info != NULL;
if (!enable_patching)
return;
LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", tlli); if (!tlli_already_known && !enable_patching)
tlli_info = talloc_zero(peer, struct gbproxy_tlli_info); return;
tlli_info->tlli = tlli;
} else {
llist_del(&tlli_info->list);
OSMO_ASSERT(state->enabled_tllis_count > 0);
state->enabled_tllis_count -= 1;
}
tlli_info = gbprox_get_detached_tlli_info(peer, tlli_info, tlli);
OSMO_ASSERT(tlli_info != NULL); OSMO_ASSERT(tlli_info != NULL);
if (enable_patching) { if (enable_patching) {
if (!tlli_already_known)
LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", tlli);
now = time(NULL); now = time(NULL);
tlli_info->timestamp = now; gbprox_attach_tlli_info(peer, now, tlli_info);
llist_add(&tlli_info->list, &state->enabled_tllis); gbprox_update_tlli_info(tlli_info, imsi, imsi_len);
state->enabled_tllis_count += 1;
gbprox_remove_stale_tllis(peer, now); gbprox_remove_stale_tllis(peer, now);
/* Be on the safe side, currently the new tlli_info won't be
if (tlli_info != llist_entry(state->enabled_tllis.next, * removed, but this not enforced explicitely */
struct gbproxy_tlli_info, list)) { tlli_info = NULL;
LOGP(DGPRS, LOGL_ERROR,
"Unexpectedly removed new TLLI entry as stale, "
"TLLI %08x\n", tlli);
tlli_info = NULL;
}
} else { } else {
LOGP(DGPRS, LOGL_INFO, LOGP(DGPRS, LOGL_INFO,
"Removing TLLI %08x from list (patching no longer enabled)\n", "Removing TLLI %08x from list (patching no longer enabled)\n",
@ -606,14 +636,6 @@ void gbprox_register_tlli(struct gbproxy_peer *peer, uint32_t tlli,
tlli_info = NULL; tlli_info = NULL;
} }
if (tlli_info) {
tlli_info->mi_data_len = imsi_len;
tlli_info->mi_data =
talloc_realloc_size(tlli_info, tlli_info->mi_data, imsi_len);
OSMO_ASSERT(tlli_info->mi_data != NULL);
memcpy(tlli_info->mi_data, imsi, imsi_len);
}
peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current = peer->ctrg->ctr[GBPROX_PEER_CTR_TLLI_CACHE_SIZE].current =
state->enabled_tllis_count; state->enabled_tllis_count;
} }