sgsn: Cleanup GMM state transitions

Currently the GMM state is set to GMM-REGISTERED when an Attach
Accept or a RA Update Accept message is sent, even if a new P-TMSI is
included. In this case 04.08 requires (see 4.7.3.1.3 and 4.7.5.1.3),
that the state is set to GMM-COMMON-PROCEDURE-INITIATED when the
Accept is sent. When the Complete is received, the SGSN shall set
the state to GMM-REGISTERED.

This patch modifies the state updates accordingly.

Sponsored-by: On-Waves ehf
diff --git a/openbsc/src/gprs/gprs_gmm.c b/openbsc/src/gprs/gprs_gmm.c
index e661b48..9f34b4d 100644
--- a/openbsc/src/gprs/gprs_gmm.c
+++ b/openbsc/src/gprs/gprs_gmm.c
@@ -629,13 +629,11 @@
 {
 	/* Request IMSI and IMEI from the MS if they are unknown */
 	if (!strlen(ctx->imei)) {
-		ctx->mm_state = GMM_COMMON_PROC_INIT;
 		ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
 		mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
 		return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
 	}
 	if (!strlen(ctx->imsi)) {
-		ctx->mm_state = GMM_COMMON_PROC_INIT;
 		ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
 		mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
 		return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
@@ -655,9 +653,10 @@
 		/* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
 		mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
 		ctx->t3350_mode = GMM_T3350_MODE_ATT;
+#else
+		ctx->mm_state = GMM_REGISTERED_NORMAL;
 #endif
 
-		ctx->mm_state = GMM_REGISTERED_NORMAL;
 		return gsm48_tx_gmm_att_ack(ctx);
 	default:
 		LOGMMCTXP(LOGL_ERROR, ctx,
@@ -860,6 +859,7 @@
 	/* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
 	ctx->p_tmsi_old = ctx->p_tmsi;
 	ctx->p_tmsi = sgsn_alloc_ptmsi();
+	ctx->mm_state = GMM_COMMON_PROC_INIT;
 #endif
 	/* Even if there is no P-TMSI allocated, the MS will switch from
 	 * foreign TLLI to local TLLI */
@@ -1078,6 +1078,11 @@
 	/* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
 	mmctx->t3350_mode = GMM_T3350_MODE_RAU;
 	mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
+
+	mmctx->mm_state = GMM_COMMON_PROC_INIT;
+#else
+	/* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
+	mmctx->mm_state = GMM_REGISTERED_NORMAL;
 #endif
 	/* Even if there is no P-TMSI allocated, the MS will switch from
 	 * foreign TLLI to local TLLI */
@@ -1094,9 +1099,6 @@
 		process_ms_ctx_status(mmctx, pdp_status);
 	}
 
-	/* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
-	mmctx->mm_state = GMM_REGISTERED_NORMAL;
-
 	/* Send RA UPDATE ACCEPT */
 	return gsm48_tx_gmm_ra_upd_ack(mmctx);
 }
@@ -1189,6 +1191,7 @@
 		mmctx->tlli = mmctx->tlli_new;
 		gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
 				  GPRS_ALGO_GEA0, NULL);
+		mmctx->mm_state = GMM_REGISTERED_NORMAL;
 		rc = 0;
 		break;
 	case GSM48_MT_GMM_RA_UPD_COMPL:
@@ -1202,6 +1205,7 @@
 		mmctx->tlli = mmctx->tlli_new;
 		gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
 				  GPRS_ALGO_GEA0, NULL);
+		mmctx->mm_state = GMM_REGISTERED_NORMAL;
 		rc = 0;
 		break;
 	case GSM48_MT_GMM_PTMSI_REALL_COMPL: