gb: Fix NS RESET/RESET_ACK abnormal cases

This changes the implementations for the reception of RESET and
RESET_ACK to be compatible with 3GPP TS 08.16, 7.3.1:

- Just send a RESET_ACK with correct values back to the SGSN when a
  RESET with an invalid NSVCI or NSEI has been received.
- Check RESET_ACK for matching NSEI and NSVCI.
- Ignore unexpected RESET_ACKs.

In addition, use RESET_ACK from a BSS to update the BSS source
address based on the NSVCI to be tolerant with changing UDP source
addresses/ports.

Sponsored-by: On-Waves ehf
diff --git a/tests/gb/gprs_ns_test.c b/tests/gb/gprs_ns_test.c
index 3d0293e..546c20a 100644
--- a/tests/gb/gprs_ns_test.c
+++ b/tests/gb/gprs_ns_test.c
@@ -357,6 +357,12 @@
 		       gprs_ns_ll_str(nssd->old_nsvc));
 		break;
 
+	case S_NS_MISMATCH:
+		printf("==> got signal NS_MISMATCH: 0x%04x/%s pdu=%d, ie=%d\n",
+		       nssd->nsvc->nsvci, gprs_ns_ll_str(nssd->nsvc),
+		       nssd->pdu_type, nssd->ie_type);
+		break;
+
 	default:
 		printf("==> got signal %d, NS-VC 0x%04x/%s\n", signal,
 		       nssd->nsvc->nsvci,
diff --git a/tests/gb/gprs_ns_test.ok b/tests/gb/gprs_ns_test.ok
index 96fbd38..a882488 100644
--- a/tests/gb/gprs_ns_test.ok
+++ b/tests/gb/gprs_ns_test.ok
@@ -196,7 +196,7 @@
 ==> got signal NS_REPLACED: 0x1122/1.2.3.4:4444 -> 0x3344/1.2.3.4:3333
 ==> got signal NS_RESET, NS-VC 0x1122/1.2.3.4:3333
 MESSAGE to BSS, msg length 9
-03 01 82 11 22 04 82 11 22 
+03 01 82 11 22 04 82 33 44 
 
 MESSAGE to BSS, msg length 1
 0a 
@@ -205,9 +205,9 @@
 
 Current NS-VCIs:
     VCI 0x3344, NSEI 0x1122, peer 0x00000000:0, blocked
-    VCI 0x1122, NSEI 0x1122, peer 0x01020304:3333, blocked
+    VCI 0x1122, NSEI 0x3344, peer 0x01020304:3333, blocked
          NS-VC replaced other count: 1
-         NS-VC changed NSEI        : 2
+         NS-VC changed NSEI        : 1
 
 --- Peer port 4444, RESET, NSEI is changed back ---
 
@@ -340,35 +340,21 @@
 PROCESSING RESET_ACK from 0x01020304:2222
 03 01 82 10 01 04 82 10 00 
 
+==> got signal NS_REPLACED: 0x1001/1.2.3.4:1111 -> 0x2001/1.2.3.4:2222
 MESSAGE to BSS, msg length 1
 0a 
 
 result (RESET_ACK) = 1
 
 Current NS-VCIs:
-    VCI 0x2001, NSEI 0x2000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 1
-    VCI 0x1001, NSEI 0x1000, peer 0x01020304:1111, blocked
-         NS-VC Block count         : 1
+    VCI 0x2001, NSEI 0x2000, peer 0x00000000:0
+    VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
+         NS-VC Block count         : 2
+         NS-VC replaced other count: 1
 
 --- Setup VC 2 SGSN -> BSS (hits NSEI 2) ---
 
-MESSAGE to BSS, msg length 12
-02 00 81 01 01 82 20 01 04 82 20 00 
-
-PROCESSING RESET_ACK from 0x01020304:1111
-03 01 82 20 01 04 82 20 00 
-
-MESSAGE to BSS, msg length 1
-0a 
-
-result (RESET_ACK) = 1
-
-Current NS-VCIs:
-    VCI 0x2001, NSEI 0x2000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 1
-    VCI 0x1001, NSEI 0x1000, peer 0x01020304:1111, blocked
-         NS-VC Block count         : 2
+Failed to send RESET to 0.0.0.0:0
 
 --- Setup VC 1 SGSN -> BSS (hits NSEI 1) ---
 
@@ -384,10 +370,10 @@
 result (RESET_ACK) = 1
 
 Current NS-VCIs:
-    VCI 0x2001, NSEI 0x2000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 2
-    VCI 0x1001, NSEI 0x1000, peer 0x01020304:1111, blocked
-         NS-VC Block count         : 2
+    VCI 0x2001, NSEI 0x2000, peer 0x00000000:0
+    VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
+         NS-VC Block count         : 3
+         NS-VC replaced other count: 1
 
 --- Setup VC 2 BSS -> SGSN ---
 
@@ -396,7 +382,6 @@
 PROCESSING RESET from 0x01020304:1111
 02 00 81 01 01 82 20 01 04 82 20 00 
 
-==> got signal NS_REPLACED: 0x2001/1.2.3.4:2222 -> 0x1001/1.2.3.4:1111
 ==> got signal NS_RESET, NS-VC 0x2001/1.2.3.4:1111
 MESSAGE to BSS, msg length 9
 03 01 82 20 01 04 82 20 00 
@@ -430,32 +415,27 @@
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
+    VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
+         NS-VC Block count         : 3
          NS-VC replaced other count: 1
-    VCI 0x1001, NSEI 0x1000, peer 0x00000000:0, blocked
-         NS-VC Block count         : 2
 
 --- RESET with invalid NSEI, BSS -> SGSN ---
 
 PROCESSING RESET from 0x01020304:2222
 02 00 81 01 01 82 10 01 04 82 f0 00 
 
-==> got signal NS_RESET, NS-VC 0x1001/1.2.3.4:2222
+==> got signal NS_MISMATCH: 0x1001/1.2.3.4:2222 pdu=2, ie=4
 MESSAGE to BSS, msg length 9
-03 01 82 10 01 04 82 f0 00 
+03 01 82 10 01 04 82 10 00 
 
-MESSAGE to BSS, msg length 1
-0a 
-
-result (RESET) = 9
+result (RESET) = 0
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
+    VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
+         NS-VC Block count         : 3
          NS-VC replaced other count: 1
-    VCI 0x1001, NSEI 0xf000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 2
-         NS-VC changed NSEI        : 1
+         NSEI was invalid count    : 1
 
 --- RESET with invalid NSVCI, BSS -> SGSN ---
 
@@ -473,11 +453,10 @@
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
-         NS-VC replaced other count: 1
     VCI 0xf001, NSEI 0x1000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 2
-         NS-VC changed NSEI        : 1
+         NS-VC Block count         : 3
+         NS-VC replaced other count: 1
+         NSEI was invalid count    : 1
 
 --- RESET with old NSEI, NSVCI, BSS -> SGSN ---
 
@@ -495,29 +474,24 @@
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
-         NS-VC replaced other count: 1
     VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 2
-         NS-VC changed NSEI        : 1
+         NS-VC Block count         : 3
+         NS-VC replaced other count: 1
+         NSEI was invalid count    : 1
 
 --- Unexpected RESET_ACK VC 1, BSS -> SGSN ---
 
 PROCESSING RESET_ACK from 0x01020304:2222
 03 01 82 10 01 04 82 10 00 
 
-MESSAGE to BSS, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+result (RESET_ACK) = 0
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
-         NS-VC replaced other count: 1
     VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
          NS-VC Block count         : 3
-         NS-VC changed NSEI        : 1
+         NS-VC replaced other count: 1
+         NSEI was invalid count    : 1
 
 ---  RESET_ACK with invalid NSEI, BSS -> SGSN ---
 
@@ -527,18 +501,15 @@
 PROCESSING RESET_ACK from 0x01020304:2222
 03 01 82 10 01 04 82 f0 00 
 
-MESSAGE to BSS, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+==> got signal NS_MISMATCH: 0x1001/1.2.3.4:2222 pdu=3, ie=4
+result (RESET_ACK) = -22
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
-         NS-VC replaced other count: 1
     VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 4
-         NS-VC changed NSEI        : 1
+         NS-VC Block count         : 3
+         NS-VC replaced other count: 1
+         NSEI was invalid count    : 2
 
 ---  RESET_ACK with invalid NSVCI, BSS -> SGSN ---
 
@@ -548,18 +519,16 @@
 PROCESSING RESET_ACK from 0x01020304:2222
 03 01 82 f0 01 04 82 10 00 
 
-MESSAGE to BSS, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+==> got signal NS_MISMATCH: 0x1001/1.2.3.4:2222 pdu=3, ie=1
+result (RESET_ACK) = -22
 
 Current NS-VCIs:
     VCI 0x2001, NSEI 0x2000, peer 0x01020304:1111
-         NS-VC Block count         : 2
-         NS-VC replaced other count: 1
     VCI 0x1001, NSEI 0x1000, peer 0x01020304:2222, blocked
-         NS-VC Block count         : 5
-         NS-VC changed NSEI        : 1
+         NS-VC Block count         : 3
+         NS-VC replaced other count: 1
+         NS-VCI was invalid count  : 1
+         NSEI was invalid count    : 2
 
 Current NS-VCIs:
 
@@ -617,36 +586,33 @@
 PROCESSING RESET from 0x05060708:32000
 02 00 81 01 01 82 01 01 04 82 f0 00 
 
-==> got signal NS_RESET, NS-VC 0x0101/5.6.7.8:32000
+==> got signal NS_MISMATCH: 0x0101/5.6.7.8:32000 pdu=2, ie=4
 MESSAGE to SGSN, msg length 9
-03 01 82 01 01 04 82 f0 00 
+03 01 82 01 01 04 82 01 00 
 
-MESSAGE to SGSN, msg length 1
-0a 
-
-result (RESET) = 9
+result (RESET) = 0
 
 Current NS-VCIs:
-    VCI 0x0101, NSEI 0xf000, peer 0x05060708:32000, blocked
+    VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
          NS-VC Block count         : 1
+         NSEI was invalid count    : 1
 
 --- RESET with invalid NSVCI, SGSN -> BSS ---
 
 PROCESSING RESET from 0x05060708:32000
 02 00 81 01 01 82 f0 01 04 82 01 00 
 
-==> got signal NS_RESET, NS-VC 0xf001/5.6.7.8:32000
+==> got signal NS_MISMATCH: 0x0101/5.6.7.8:32000 pdu=2, ie=1
 MESSAGE to SGSN, msg length 9
-03 01 82 f0 01 04 82 01 00 
+03 01 82 01 01 04 82 01 00 
 
-MESSAGE to SGSN, msg length 1
-0a 
-
-result (RESET) = 9
+result (RESET) = 0
 
 Current NS-VCIs:
-    VCI 0xf001, NSEI 0x0100, peer 0x05060708:32000, blocked
+    VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
          NS-VC Block count         : 1
+         NS-VCI was invalid count  : 1
+         NSEI was invalid count    : 1
 
 --- RESET, SGSN -> BSS ---
 
@@ -665,20 +631,21 @@
 Current NS-VCIs:
     VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
          NS-VC Block count         : 1
+         NS-VCI was invalid count  : 1
+         NSEI was invalid count    : 1
 
 --- Unexpected RESET_ACK VC 1, BSS -> SGSN ---
 
 PROCESSING RESET_ACK from 0x05060708:32000
 03 01 82 01 01 04 82 01 00 
 
-MESSAGE to SGSN, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+result (RESET_ACK) = 0
 
 Current NS-VCIs:
     VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
-         NS-VC Block count         : 2
+         NS-VC Block count         : 1
+         NS-VCI was invalid count  : 1
+         NSEI was invalid count    : 1
 
 ---  RESET_ACK with invalid NSEI, BSS -> SGSN ---
 
@@ -688,14 +655,14 @@
 PROCESSING RESET_ACK from 0x05060708:32000
 03 01 82 01 01 04 82 e0 00 
 
-MESSAGE to SGSN, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+==> got signal NS_MISMATCH: 0x0101/5.6.7.8:32000 pdu=3, ie=4
+result (RESET_ACK) = -22
 
 Current NS-VCIs:
     VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
-         NS-VC Block count         : 3
+         NS-VC Block count         : 1
+         NS-VCI was invalid count  : 1
+         NSEI was invalid count    : 2
 
 ---  RESET_ACK with invalid NSVCI, BSS -> SGSN ---
 
@@ -705,14 +672,14 @@
 PROCESSING RESET_ACK from 0x05060708:32000
 03 01 82 e0 01 04 82 01 00 
 
-MESSAGE to SGSN, msg length 1
-0a 
-
-result (RESET_ACK) = 1
+==> got signal NS_MISMATCH: 0x0101/5.6.7.8:32000 pdu=3, ie=1
+result (RESET_ACK) = -22
 
 Current NS-VCIs:
     VCI 0x0101, NSEI 0x0100, peer 0x05060708:32000, blocked
-         NS-VC Block count         : 4
+         NS-VC Block count         : 1
+         NS-VCI was invalid count  : 2
+         NSEI was invalid count    : 2
 
 Current NS-VCIs: