[gprs] gb_proxy: Fix message looping bug

We don't want to loop back the BSS messages to the BSS itself.
diff --git a/openbsc/src/gb_proxy.c b/openbsc/src/gb_proxy.c
index a4b432e..fe5ad70 100644
--- a/openbsc/src/gb_proxy.c
+++ b/openbsc/src/gb_proxy.c
@@ -169,7 +169,7 @@
 static int gbprox_relay2peer(struct msgb *msg, struct gbprox_peer *peer,
 			  uint16_t ns_bvci)
 {
-	DEBUGP(DGPRS, "NSEI=%u proxying to to BSS (NS_BVCI=%u, NSEI=%u)\n",
+	DEBUGP(DGPRS, "NSEI=%u proxying to BSS (NS_BVCI=%u, NSEI=%u)\n",
 		msgb_nsei(msg), ns_bvci, peer->nsvc->nsei);
 
 	msgb_bvci(msg) = ns_bvci;
@@ -188,8 +188,11 @@
 	struct gbprox_peer *peer;
 
 	peer = peer_by_bvci(ptp_bvci);
-	if (!peer)
+	if (!peer) {
+		LOGP(DGPRS, LOGL_ERROR, "Cannot find BSS for BVCI %u\n",
+			ptp_bvci);
 		return -ENOENT;
+	}
 
 	return gbprox_relay2peer(msg, peer, ns_bvci);
 }
@@ -255,6 +258,14 @@
 					"BVC RESET ACK of BVCI=0\n", nsvc->nsei);
 				return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
 							    nsvc->nsei, 0, ns_bvci);
+			} else if (!peer_by_bvci(bvci)) {
+				/* if a PTP-BVC is reset, and we don't know that
+				 * PTP-BVCI yet, we should allocate a new peer */
+				LOGP(DGPRS, LOGL_INFO, "Allocationg new peer for "
+				     "BVCI=%u via NSVCI=%u/NSEI=%u\n", bvci,
+				     nsvc->nsvci, nsvc->nsei);
+				from_peer = peer_alloc(bvci);
+				from_peer->nsvc = nsvc;
 			}
 		}
 		break;
@@ -362,10 +373,15 @@
 
 	return rc;
 err_mand_ie:
-	; /* FIXME: this would pull gprs_bssgp.c in, which in turn has dependencies */
+	LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) missing mandatory IE\n",
+		nsvc->nsei);
+	/* FIXME: this would pull gprs_bssgp.c in, which in turn has dependencies */
 	//return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
+	return;
 err_no_peer:
-	; /* FIXME */
+	LOGP(DGPRS, LOGL_ERROR, "NSEI=%u(SGSN) cannot find peer based on RAC\n");
+	/* FIXME */
+	return;
 }
 
 /* Main input function for Gb proxy */
@@ -381,7 +397,7 @@
 			rc = gbprox_rx_sig_from_bss(msg, nsvc, ns_bvci);
 	} else {
 		/* All other BVCI are PTP and thus can be simply forwarded */
-		if (nsvc->remote_end_is_sgsn) {
+		if (!nsvc->remote_end_is_sgsn) {
 			rc = gbprox_relay2sgsn(msg, ns_bvci);
 		} else {
 			struct gbprox_peer *peer = peer_by_bvci(ns_bvci);