ensure unique CellIDs in HNB-GW

If we receive a HNB-REGISTER-REQ with a cell ID which is already used
by another registered NNB, log an error and send HNB-REGISTER-REJECT.

Tested manually by running two 'hnb-test' programs concurrently (they
need to listen on different telnet ports; this port is hard-coded so
I compiled two different hnb-test binaries).
Then I issued the 'hnbap hnb register' command on the telnet interface
of each, and verified that the correct action is logged by osmo-hnbgw.
Both hnb-test programs can connect, but only one of them can register
at a time. Killing a registered 'hnb-test' program terminates its
connection and allows the previously rejected one to register.

The new rejection log message looks like this:
 hnbgw_hnbap.c:429 rejecting HNB-REGISTER-REQ with duplicate cell
 identity MCC=901,MNC=99,LAC=49406,RAC=66,SAC=43947,CID=182250155
 from (r=127.0.0.1:42828<->l=127.0.0.1:29169)

This change depends on a new API in libosmo-netif, which is added in
https://gerrit.osmocom.org/#/c/6844/

Change-Id: Iffd441eb2b6b75dfbe001b49b01bea015ca6e11c
Depends: I8ed78fe39c463e9018756700d13ee5ebe003b57f
Related: OS#2789
diff --git a/src/hnbgw.c b/src/hnbgw.c
index cd492bb..94d8fb9 100644
--- a/src/hnbgw.c
+++ b/src/hnbgw.c
@@ -204,6 +204,12 @@
 }
 static int hnb_close_cb(struct osmo_stream_srv *conn)
 {
+	struct hnb_context *hnb = osmo_stream_srv_get_data(conn);
+
+	/* This connection is about to be closed. Destroy the HNB context now. */
+	if (hnb)
+		hnb_context_release(hnb, false);
+
 	return 0;
 }
 
@@ -228,10 +234,10 @@
 	} else if (rc < 0) {
 		LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
 		/* FIXME: clean up after disappeared HNB */
-		hnb_context_release(hnb);
+		hnb_context_release(hnb, true);
 		goto out;
 	} else if (rc == 0) {
-		hnb_context_release(hnb);
+		hnb_context_release(hnb, true);
 		rc = -1;
 
 		goto out;
@@ -288,7 +294,7 @@
 	return ctx;
 }
 
-void hnb_context_release(struct hnb_context *ctx)
+void hnb_context_release(struct hnb_context *ctx, bool destroy_conn)
 {
 	struct hnbgw_context_map *map, *map2;
 
@@ -305,7 +311,9 @@
 		context_map_deactivate(map);
 	}
 	ue_context_free_by_hnb(ctx->gw, ctx);
-	osmo_stream_srv_destroy(ctx->conn);
+
+	if (destroy_conn)
+		osmo_stream_srv_destroy(ctx->conn);
 
 	talloc_free(ctx);
 }