gprs: Support cancellation type

The cancellation type that is part of the UpdateCancellation message
is currently ignored.

This patch adds the missing glue between the existing GSUP and GMM
support. If the type is not present or has the value updateProcedure
the subcriber and MM context are siliently removed. Otherwise, a
message with cause 'implicitly detached' is sent to the MS. Since the
real cause is not known (the specification neither added a cause IE
nor defined a static cause value), the MS may get the real cause in
the following AttachRej.

Added VTY commands:

- update-subscriber imsi IMSI cancel update-procedure
- update-subscriber imsi IMSI cancel subscription-withdraw

the old form without the cause is no longer supported.

Sponsored-by: On-Waves ehf
diff --git a/openbsc/src/gprs/gprs_subscriber.c b/openbsc/src/gprs/gprs_subscriber.c
index 54a6161..e4dda9c 100644
--- a/openbsc/src/gprs/gprs_subscriber.c
+++ b/openbsc/src/gprs/gprs_subscriber.c
@@ -439,13 +439,24 @@
 					     struct gprs_gsup_message *gsup_msg)
 {
 	struct gprs_gsup_message gsup_reply = {0};
+	int is_update_procedure = !gsup_msg->cancel_type || gsup_msg->cancel_type;
 
-	LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
+	LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n",
+		    is_update_procedure ?
+		    "update procedure" : "subscription withdraw");
 
 	gsup_reply.message_type = GPRS_GSUP_MSGT_LOCATION_CANCEL_RESULT;
 	gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
 
-	subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
+	if (is_update_procedure)
+		subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
+	else
+		/* Since a withdraw cause is not specified, just abort the
+		 * current attachment. The following re-attachment should then
+		 * be rejected with a proper cause value.
+		 */
+		subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
+
 	gprs_subscr_cancel(subscr);
 
 	return 0;
diff --git a/openbsc/src/gprs/sgsn_vty.c b/openbsc/src/gprs/sgsn_vty.c
index 1ecb2ea..ba71555 100644
--- a/openbsc/src/gprs/sgsn_vty.c
+++ b/openbsc/src/gprs/sgsn_vty.c
@@ -545,11 +545,14 @@
 }
 
 DEFUN(update_subscr_cancel, update_subscr_cancel_cmd,
-	UPDATE_SUBSCR_STR "cancel",
+	UPDATE_SUBSCR_STR "cancel (update-procedure|subscription-withdraw)",
 	UPDATE_SUBSCR_HELP
-	"Cancel (remove) subscriber record\n")
+	"Cancel (remove) subscriber record\n"
+	"The MS moved to another SGSN\n"
+	"The subscription is no longer valid\n")
 {
 	const char *imsi = argv[0];
+	const char *cancel_type = argv[1];
 
 	struct gsm_subscriber *subscr;
 
@@ -560,6 +563,11 @@
 		return CMD_WARNING;
 	}
 
+	if (strcmp(cancel_type, "update-procedure") == 0)
+		subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
+	else
+		subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
+
 	gprs_subscr_cancel(subscr);
 	subscr_put(subscr);
 
@@ -606,6 +614,7 @@
 	}
 
 	subscr->keep_in_ram = 0;
+	subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
 	gprs_subscr_cancel(subscr);
 	if (subscr->use_count > 1)
 		vty_out(vty, "%% subscriber is still in use%s",
diff --git a/openbsc/tests/vty_test_runner.py b/openbsc/tests/vty_test_runner.py
index de6a708..4adc8e8 100644
--- a/openbsc/tests/vty_test_runner.py
+++ b/openbsc/tests/vty_test_runner.py
@@ -768,7 +768,7 @@
         res = self.vty.command('show subscriber cache')
         self.assert_(res.find('1234567890') >= 0)
         self.assert_(res.find('Authorized: 1') >= 0)
-        self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 cancel', ['']))
+        self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 cancel update-procedure', ['']))
         res = self.vty.command('show subscriber cache')
         self.assert_(res.find('1234567890') >= 0)
         self.assertTrue(self.vty.verify('update-subscriber imsi 1234567890 destroy', ['']))