gbproxy: Remove gbproxy_register_tlli

This function is a remainder of the initial implemenation that was
not meant for TLLI patching and can be used for the BSS side only.
The SGSN side is already using a composition of more flexible
single purpose functions.

This patch changes the implementation to use a similar approach. The
function is moved to gbproxy_test.c and renamed to register_tlli to
keep the tests intact.

Sponsored-by: On-Waves ehf
diff --git a/openbsc/src/gprs/gb_proxy_tlli.c b/openbsc/src/gprs/gb_proxy_tlli.c
index c3dc7ba..78459b7 100644
--- a/openbsc/src/gprs/gb_proxy_tlli.c
+++ b/openbsc/src/gprs/gb_proxy_tlli.c
@@ -135,8 +135,8 @@
 	OSMO_ASSERT(llist_empty(&state->enabled_tllis));
 }
 
-static void gbproxy_attach_tlli_info(struct gbproxy_peer *peer, time_t now,
-				     struct gbproxy_tlli_info *tlli_info)
+void gbproxy_attach_tlli_info(struct gbproxy_peer *peer, time_t now,
+			      struct gbproxy_tlli_info *tlli_info)
 {
 	struct gbproxy_patch_state *state = &peer->patch_state;
 
@@ -201,8 +201,7 @@
 	return deleted_count;
 }
 
-static struct gbproxy_tlli_info *gbproxy_tlli_info_alloc(
-	struct gbproxy_peer *peer)
+struct gbproxy_tlli_info *gbproxy_tlli_info_alloc( struct gbproxy_peer *peer)
 {
 	struct gbproxy_tlli_info *tlli_info;
 
@@ -215,7 +214,7 @@
 	return tlli_info;
 }
 
-static void gbproxy_detach_tlli_info(
+void gbproxy_detach_tlli_info(
 	struct gbproxy_peer *peer,
 	struct gbproxy_tlli_info *tlli_info)
 {
@@ -229,8 +228,8 @@
 		state->enabled_tllis_count;
 }
 
-static void gbproxy_update_tlli_info(struct gbproxy_tlli_info *tlli_info,
-				     const uint8_t *imsi, size_t imsi_len)
+void gbproxy_update_tlli_info(struct gbproxy_tlli_info *tlli_info,
+			      const uint8_t *imsi, size_t imsi_len)
 {
 	if (!gprs_is_mi_imsi(imsi, imsi_len))
 		return;
@@ -317,57 +316,6 @@
 	gbproxy_attach_tlli_info(peer, now, tlli_info);
 }
 
-struct gbproxy_tlli_info *gbproxy_register_tlli(
-	struct gbproxy_peer *peer, uint32_t tlli,
-	const uint8_t *imsi, size_t imsi_len, time_t now)
-{
-	struct gbproxy_tlli_info *tlli_info;
-	int enable_patching = -1;
-	int tlli_already_known = 0;
-
-	/* Check, whether the IMSI matches */
-	if (gprs_is_mi_imsi(imsi, imsi_len)) {
-		enable_patching = gbproxy_check_imsi(peer, imsi, imsi_len);
-		if (enable_patching < 0)
-			return NULL;
-	}
-
-	tlli_info = gbproxy_find_tlli(peer, tlli);
-
-	if (!tlli_info) {
-		tlli_info = gbproxy_find_tlli_by_imsi(peer, imsi, imsi_len);
-
-		if (tlli_info) {
-			/* TLLI has changed somehow, adjust it */
-			LOGP(DGPRS, LOGL_INFO,
-			     "The TLLI has changed from %08x to %08x\n",
-			     tlli_info->tlli.current, tlli);
-			tlli_info->tlli.current = tlli;
-		}
-	}
-
-	if (!tlli_info) {
-		tlli_info = gbproxy_tlli_info_alloc(peer);
-		tlli_info->tlli.current = tlli;
-	} else {
-		gbproxy_detach_tlli_info(peer, tlli_info);
-		tlli_already_known = 1;
-	}
-
-	OSMO_ASSERT(tlli_info != NULL);
-
-	if (!tlli_already_known)
-		LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n", tlli);
-
-	gbproxy_attach_tlli_info(peer, now, tlli_info);
-	gbproxy_update_tlli_info(tlli_info, imsi, imsi_len);
-
-	if (enable_patching >= 0)
-		tlli_info->enable_patching = enable_patching;
-
-	return tlli_info;
-}
-
 static void gbproxy_unregister_tlli(struct gbproxy_peer *peer, uint32_t tlli)
 {
 	struct gbproxy_tlli_info *tlli_info;
@@ -397,20 +345,27 @@
 {
 	struct gbproxy_tlli_info *tlli_info = NULL;
 
-	if (parse_ctx->tlli_enc)
+	if (parse_ctx->tlli_enc) {
 		tlli_info = gbproxy_find_tlli(peer, parse_ctx->tlli);
 
+		if (!tlli_info && parse_ctx->imsi)
+			tlli_info = gbproxy_find_tlli_by_imsi(
+				peer, parse_ctx->imsi, parse_ctx->imsi_len);
+	}
+
 	if (parse_ctx->tlli_enc && parse_ctx->llc) {
 		uint32_t sgsn_tlli;
 		if (!tlli_info) {
-			tlli_info =
-				gbproxy_register_tlli(peer, parse_ctx->tlli,
-						      parse_ctx->imsi,
-						      parse_ctx->imsi_len, now);
+			LOGP(DGPRS, LOGL_INFO, "Adding TLLI %08x to list\n",
+			    parse_ctx->tlli);
+			tlli_info = gbproxy_tlli_info_alloc(peer);
+			gbproxy_attach_tlli_info(peer, now, tlli_info);
+
 			/* Setup TLLIs */
 			sgsn_tlli = gbproxy_make_sgsn_tlli(peer, tlli_info,
 							   parse_ctx->tlli);
 			tlli_info->sgsn_tlli.current = sgsn_tlli;
+			tlli_info->tlli.current = parse_ctx->tlli;;
 		} else {
 			sgsn_tlli = gbproxy_map_tlli(parse_ctx->tlli, tlli_info, 0);
 			if (!sgsn_tlli)
@@ -476,6 +431,7 @@
 		   !peer->cfg->patch_ptmsi) {
 		/* A new P-TMSI has been signalled in the message with an unknown
 		 * TLLI, create a new tlli_info */
+		/* TODO: Add a test case for this branch */
 		uint32_t new_ptmsi;
 		if (!gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN,
 					&new_ptmsi)) {
@@ -494,6 +450,7 @@
 		tlli_info->tlli.current = parse_ctx->tlli;;
 		tlli_info->sgsn_tlli.ptmsi = new_ptmsi;
 		tlli_info->tlli.ptmsi = new_ptmsi;
+		gbproxy_attach_tlli_info(peer, now, tlli_info);
 	} else if (parse_ctx->tlli_enc && parse_ctx->llc && !tlli_info &&
 		   !peer->cfg->patch_ptmsi) {
 		/* Unknown SGSN TLLI, create a new tlli_info */