Fill Last Used E-UTRAN PLMN Id when in CSFB

Since recently, osmo-bsc behaves strictly as per specs, meaning it will
only send the "Cell selection indicator after release of all TCH and SDCCH IE"
in RR Channel Release iff:
* "Last Used E-UTRAN PLMN Id" was received in the CommonID sent MSC->BSC
* "Last Used E-UTRAN PLMN Id" was received insider "old BSS to new BSS Information"
  in the HandoverRequest sent MSC->BSC.
On the other hand, CSFB_Indicator from ClearCommand MSC->BSC is nw
ignored and not taken into account.

Hence, let's update osmo-msc to also behave correctly by sending the
Last Used E-UTRAN PLMN ID at CommonID tx time to avoid regressions in
CSFB support when running against newer osmo-bsc.

Let's keep sending the CSFB Indicator in ClearCommand as we used too, in
order to keep compatibility with older BSCs (as per spec).

Related: SYS#5337
Change-Id: Ic5f175b179973d0a50d94f00e15f5a3e332605fc
diff --git a/src/libvlr/vlr.c b/src/libvlr/vlr.c
index 33d6331..02aceef 100644
--- a/src/libvlr/vlr.c
+++ b/src/libvlr/vlr.c
@@ -476,6 +476,23 @@
 	       vsub->imsi, vsub->msisdn);
 }
 
+void vlr_subscr_set_last_used_eutran_plmn_id(struct vlr_subscr *vsub,
+					     const struct osmo_plmn_id *last_eutran_plmn)
+{
+	if (!vsub)
+		return;
+	if (last_eutran_plmn) {
+		vsub->sgs.last_eutran_plmn_present = true;
+		memcpy(&vsub->sgs.last_eutran_plmn, last_eutran_plmn, sizeof(*last_eutran_plmn));
+	} else {
+		vsub->sgs.last_eutran_plmn_present = false;
+	}
+	DEBUGP(DVLR, "set Last E-UTRAN PLMN ID on subscriber: %s\n",
+	       vsub->sgs.last_eutran_plmn_present ?
+	         osmo_plmn_name(&vsub->sgs.last_eutran_plmn) :
+		 "(none)");
+}
+
 bool vlr_subscr_matches_imsi(struct vlr_subscr *vsub, const char *imsi)
 {
 	return vsub && imsi && vsub->imsi[0] && !strcmp(vsub->imsi, imsi);
diff --git a/src/libvlr/vlr_sgs.c b/src/libvlr/vlr_sgs.c
index 269dda6..5659886 100644
--- a/src/libvlr/vlr_sgs.c
+++ b/src/libvlr/vlr_sgs.c
@@ -68,11 +68,12 @@
  * \param[in] type location update type (normal or IMSI attach).
  * \param[in] imsi mobile identity (IMSI).
  * \param[in] new_lai identifier of the new location area.
+ * \param[in] last_eutran_plnm_id Last E-UTRAN PLMN ID (can be NULL).
  * \returns 0 in case of success, -EINVAL in case of error. */
 int vlr_sgs_loc_update(struct vlr_instance *vlr, struct vlr_sgs_cfg *cfg,
 		       vlr_sgs_lu_response_cb_t response_cb, vlr_sgs_lu_paging_cb_t paging_cb,
 		       vlr_sgs_lu_mminfo_cb_t mminfo_cb, char *mme_name, enum vlr_lu_type type, const char *imsi,
-		       struct osmo_location_area_id *new_lai)
+		       struct osmo_location_area_id *new_lai, struct osmo_plmn_id *last_eutran_plmn)
 {
 	struct vlr_subscr *vsub = NULL;
 
@@ -93,6 +94,7 @@
 	vsub->sgs.paging_cb = paging_cb;
 	vsub->sgs.mminfo_cb = mminfo_cb;
 	vlr_subscr_set_imsi(vsub, imsi);
+	vlr_subscr_set_last_used_eutran_plmn_id(vsub, last_eutran_plmn);
 	osmo_strlcpy(vsub->sgs.mme_name, mme_name, sizeof(vsub->sgs.mme_name));
 
 	osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_MME, NULL);