gtp_{ggsn,mme}: Allocate contexts under struct sgsn_instance

This way apns are managed by the lifcycle of the main global struct
sgsn_instance automatically.

Change-Id: Ie65d59632a368c6957c33dca64e856ace792b2c6
diff --git a/src/sgsn/gprs_sgsn.c b/src/sgsn/gprs_sgsn.c
index 42d1ece..8c2734b 100644
--- a/src/sgsn/gprs_sgsn.c
+++ b/src/sgsn/gprs_sgsn.c
@@ -713,11 +713,11 @@
 		ggsn = apn_ctx->ggsn;
 	} else if (llist_empty(&sgsn->apn_list)) {
 		/* No configuration -> use GGSN 0 */
-		ggsn = sgsn_ggsn_ctx_by_id(0);
+		ggsn = sgsn_ggsn_ctx_by_id(sgsn, 0);
 	} else if (allow_any_apn &&
 		   (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
 		/* No APN given and no default configuration -> Use GGSN 0 */
-		ggsn = sgsn_ggsn_ctx_by_id(0);
+		ggsn = sgsn_ggsn_ctx_by_id(sgsn, 0);
 	} else {
 		/* No matching configuration found */
 		LOGMMCTXP(LOGL_NOTICE, mmctx,
diff --git a/src/sgsn/gprs_sm.c b/src/sgsn/gprs_sm.c
index 157e279..184350d 100644
--- a/src/sgsn/gprs_sm.c
+++ b/src/sgsn/gprs_sm.c
@@ -378,7 +378,7 @@
 		goto reject_due_failure;
 	}
 
-	ggsn = sgsn_ggsn_ctx_alloc(UINT32_MAX);
+	ggsn = sgsn_ggsn_ctx_alloc(sgsn, UINT32_MAX);
 	if (!ggsn) {
 		LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "Failed to create ggsn.\n");
 		goto reject_due_failure;
diff --git a/src/sgsn/gtp_ggsn.c b/src/sgsn/gtp_ggsn.c
index be07d13..b43fb25 100644
--- a/src/sgsn/gtp_ggsn.c
+++ b/src/sgsn/gtp_ggsn.c
@@ -35,8 +35,6 @@
 #include <osmocom/sgsn/gprs_gmm_fsm.h>
 #include <osmocom/sgsn/gprs_sm.h>
 
-extern void *tall_sgsn_ctx;
-
 void sgsn_ggsn_ctx_check_echo_timer(struct sgsn_ggsn_ctx *ggc)
 {
 	bool pending = osmo_timer_pending(&ggc->echo_timer);
@@ -59,11 +57,11 @@
 	osmo_timer_schedule(&ggc->echo_timer, ggc->echo_interval, 0);
 }
 
-struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
+struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(struct sgsn_instance *sgsn, uint32_t id)
 {
 	struct sgsn_ggsn_ctx *ggc;
 
-	ggc = talloc_zero(tall_sgsn_ctx, struct sgsn_ggsn_ctx);
+	ggc = talloc_zero(sgsn, struct sgsn_ggsn_ctx);
 	if (!ggc)
 		return NULL;
 
@@ -86,7 +84,7 @@
 	talloc_free(ggc);
 }
 
-struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
+struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(struct sgsn_instance *sgsn, uint32_t id)
 {
 	struct sgsn_ggsn_ctx *ggc;
 
@@ -97,7 +95,7 @@
 	return NULL;
 }
 
-struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
+struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct sgsn_instance *sgsn, struct in_addr *addr)
 {
 	struct sgsn_ggsn_ctx *ggc;
 
@@ -109,13 +107,13 @@
 }
 
 
-struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
+struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(struct sgsn_instance *sgsn, uint32_t id)
 {
 	struct sgsn_ggsn_ctx *ggc;
 
-	ggc = sgsn_ggsn_ctx_by_id(id);
+	ggc = sgsn_ggsn_ctx_by_id(sgsn, id);
 	if (!ggc)
-		ggc = sgsn_ggsn_ctx_alloc(id);
+		ggc = sgsn_ggsn_ctx_alloc(sgsn, id);
 	return ggc;
 }
 
diff --git a/src/sgsn/gtp_mme.c b/src/sgsn/gtp_mme.c
index 4fe804d..948e8e8 100644
--- a/src/sgsn/gtp_mme.c
+++ b/src/sgsn/gtp_mme.c
@@ -39,7 +39,7 @@
 struct sgsn_mme_ctx *sgsn_mme_ctx_alloc(struct sgsn_instance *sgsn, const char *name)
 {
 	struct sgsn_mme_ctx *mme;
-	mme = talloc_zero(tall_sgsn_ctx, struct sgsn_mme_ctx);
+	mme = talloc_zero(sgsn, struct sgsn_mme_ctx);
 	if (!mme)
 		return NULL;
 
diff --git a/src/sgsn/sgsn_libgtp.c b/src/sgsn/sgsn_libgtp.c
index f848e57..dc3f916 100644
--- a/src/sgsn/sgsn_libgtp.c
+++ b/src/sgsn/sgsn_libgtp.c
@@ -600,7 +600,7 @@
 	struct sgsn_ggsn_ctx *ggsn;
 	struct sgsn_pdp_ctx *pctx = NULL;
 
-	ggsn = sgsn_ggsn_ctx_by_addr(&peer->sin_addr);
+	ggsn = sgsn_ggsn_ctx_by_addr(sgsn, &peer->sin_addr);
 	if (!ggsn) {
 		LOGP(DGPRS, LOGL_NOTICE, "Received Recovery IE for unknown GGSN\n");
 		return -EINVAL;
diff --git a/src/sgsn/sgsn_vty.c b/src/sgsn/sgsn_vty.c
index b6d03cf..3d3e8cd 100644
--- a/src/sgsn/sgsn_vty.c
+++ b/src/sgsn/sgsn_vty.c
@@ -422,7 +422,7 @@
 	"IPv4 Address\n")
 {
 	uint32_t id = atoi(argv[0]);
-	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
+	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(sgsn, id);
 
 	inet_aton(argv[1], &ggc->remote_addr);
 
@@ -447,7 +447,7 @@
 	"Version 0\n" "Version 1\n")
 {
 	uint32_t id = atoi(argv[0]);
-	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
+	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(sgsn, id);
 
 	if (atoi(argv[1]))
 		ggc->gtp_version = 1;
@@ -465,7 +465,7 @@
 	"Interval between echo requests in seconds.\n")
 {
 	uint32_t id = atoi(argv[0]);
-	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
+	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(sgsn, id);
 
 	ggc->echo_interval = atoi(argv[1]);
 
@@ -484,7 +484,7 @@
 	NO_STR "Send an echo request to this static GGSN every interval.\n")
 {
 	uint32_t id = atoi(argv[0]);
-	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
+	struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(sgsn, id);
 
 	ggc->echo_interval = 0;
 	sgsn_ggsn_ctx_check_echo_timer(ggc);
@@ -525,7 +525,7 @@
 	struct apn_ctx *actx;
 	struct sgsn_ggsn_ctx *ggsn;
 
-	ggsn = sgsn_ggsn_ctx_by_id(ggsn_id);
+	ggsn = sgsn_ggsn_ctx_by_id(sgsn, ggsn_id);
 	if (ggsn == NULL) {
 		vty_out(vty, "%% a GGSN with id %d has not been defined%s",
 			ggsn_id, VTY_NEWLINE);