From d72294b966dfe1ebff2eeb58f702d40aa1d75b77 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 2 Feb 2017 17:10:59 +0100
Subject: [PATCH] fix gen_emm_data

It was not setting emm_data->eplmn.plmn[X], only emm_data->eplmn.n_plmns,
leading to the UE to dig for PLMN 00000.

(It's not the end of the story, seems like the PLMN selection
does not work, the UE only tries one from what I've seen, but
I didn't dig much, may be wrong.)
---
 openair3/NAS/TOOLS/conf_emm.c    | 95 +++++++++++++++++---------------
 openair3/NAS/TOOLS/conf_emm.h    |  9 ++-
 openair3/NAS/TOOLS/conf_parser.c |  2 +-
 3 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/openair3/NAS/TOOLS/conf_emm.c b/openair3/NAS/TOOLS/conf_emm.c
index 48627cdc0e..668b90a0ae 100644
--- a/openair3/NAS/TOOLS/conf_emm.c
+++ b/openair3/NAS/TOOLS/conf_emm.c
@@ -4,56 +4,65 @@
 #include "conf_emm.h"
 #include "fs.h"
 
-void gen_emm_data(emm_nvdata_t *emm_data, const char *hplmn, const char *msin, int ehplmn_count, const networks_t networks) {
-	memset(emm_data, 0, sizeof(emm_nvdata_t));
-	int hplmn_index = get_plmn_index(hplmn, networks);
-	plmn_conf_param_t *conf = &networks.items[hplmn_index].conf;
-	emm_data->imsi.length = 8;
-	emm_data->imsi.u.num.parity = get_msin_parity(msin, conf->mcc, conf->mnc);
-	emm_data->imsi.u.num.digit1 = conf->mcc[0];
-	emm_data->imsi.u.num.digit2 = conf->mcc[1];
-	emm_data->imsi.u.num.digit3 = conf->mcc[2];
+void gen_emm_data(
+    emm_nvdata_t     *emm_data,
+    const char       *hplmn,
+    const char       *msin,
+    const plmns_list *eplmn,
+    const networks_t networks)
+{
+  memset(emm_data, 0, sizeof(emm_nvdata_t));
+  int hplmn_index = get_plmn_index(hplmn, networks);
+  plmn_conf_param_t *conf = &networks.items[hplmn_index].conf;
+  int i;
 
-	emm_data->imsi.u.num.digit4 = conf->mnc[0];
-	emm_data->imsi.u.num.digit5 = conf->mnc[1];
+  emm_data->imsi.length = 8;
+  emm_data->imsi.u.num.parity = get_msin_parity(msin, conf->mcc, conf->mnc);
+  emm_data->imsi.u.num.digit1 = conf->mcc[0];
+  emm_data->imsi.u.num.digit2 = conf->mcc[1];
+  emm_data->imsi.u.num.digit3 = conf->mcc[2];
 
-	if (strlen(conf->mnc) == 3) {
-		emm_data->rplmn.MNCdigit3 = conf->mnc[2];
+  emm_data->imsi.u.num.digit4 = conf->mnc[0];
+  emm_data->imsi.u.num.digit5 = conf->mnc[1];
 
-		emm_data->imsi.u.num.digit6 = conf->mnc[2];
-		emm_data->imsi.u.num.digit7 = msin[0];
-		emm_data->imsi.u.num.digit8 = msin[1];
-		emm_data->imsi.u.num.digit9 = msin[2];
-		emm_data->imsi.u.num.digit10 = msin[3];
-		emm_data->imsi.u.num.digit11 = msin[4];
-		emm_data->imsi.u.num.digit12 = msin[5];
-		emm_data->imsi.u.num.digit13 = msin[6];
-		emm_data->imsi.u.num.digit14 = msin[7];
-		emm_data->imsi.u.num.digit15 = msin[8];
+  if (strlen(conf->mnc) == 3) {
+    emm_data->rplmn.MNCdigit3 = conf->mnc[2];
 
-	} else {
-		emm_data->rplmn.MNCdigit3 = 0xf;
+    emm_data->imsi.u.num.digit6 = conf->mnc[2];
+    emm_data->imsi.u.num.digit7 = msin[0];
+    emm_data->imsi.u.num.digit8 = msin[1];
+    emm_data->imsi.u.num.digit9 = msin[2];
+    emm_data->imsi.u.num.digit10 = msin[3];
+    emm_data->imsi.u.num.digit11 = msin[4];
+    emm_data->imsi.u.num.digit12 = msin[5];
+    emm_data->imsi.u.num.digit13 = msin[6];
+    emm_data->imsi.u.num.digit14 = msin[7];
+    emm_data->imsi.u.num.digit15 = msin[8];
+  } else {
+    emm_data->rplmn.MNCdigit3 = 0xf;
 
-		emm_data->imsi.u.num.digit6 = msin[0];
-		emm_data->imsi.u.num.digit7 = msin[1];
-		emm_data->imsi.u.num.digit8 = msin[2];
-		emm_data->imsi.u.num.digit9 = msin[3];
-		emm_data->imsi.u.num.digit10 = msin[4];
-		emm_data->imsi.u.num.digit11 = msin[5];
-		emm_data->imsi.u.num.digit12 = msin[6];
-		emm_data->imsi.u.num.digit13 = msin[7];
-		emm_data->imsi.u.num.digit14 = msin[8];
-		emm_data->imsi.u.num.digit15 = msin[9];
+    emm_data->imsi.u.num.digit6 = msin[0];
+    emm_data->imsi.u.num.digit7 = msin[1];
+    emm_data->imsi.u.num.digit8 = msin[2];
+    emm_data->imsi.u.num.digit9 = msin[3];
+    emm_data->imsi.u.num.digit10 = msin[4];
+    emm_data->imsi.u.num.digit11 = msin[5];
+    emm_data->imsi.u.num.digit12 = msin[6];
+    emm_data->imsi.u.num.digit13 = msin[7];
+    emm_data->imsi.u.num.digit14 = msin[8];
+    emm_data->imsi.u.num.digit15 = msin[9];
+  }
 
-	}
-
-	emm_data->rplmn.MCCdigit1 = conf->mcc[0];
-	emm_data->rplmn.MCCdigit2 = conf->mcc[1];
-	emm_data->rplmn.MCCdigit3 = conf->mcc[2];
-	emm_data->rplmn.MNCdigit1 = conf->mnc[0];
-	emm_data->rplmn.MNCdigit2 = conf->mnc[1];
+  emm_data->rplmn.MCCdigit1 = conf->mcc[0];
+  emm_data->rplmn.MCCdigit2 = conf->mcc[1];
+  emm_data->rplmn.MCCdigit3 = conf->mcc[2];
+  emm_data->rplmn.MNCdigit1 = conf->mnc[0];
+  emm_data->rplmn.MNCdigit2 = conf->mnc[1];
 
-	emm_data->eplmn.n_plmns = ehplmn_count;
+  emm_data->eplmn.n_plmns = eplmn->size;
+  for (i = 0; i < eplmn->size; i++) {
+    emm_data->eplmn.plmn[i] = networks.items[eplmn->items[i]].plmn;
+  }
 }
 
 bool write_emm_data(const char *directory, int user_id, emm_nvdata_t *emm_data) {
diff --git a/openair3/NAS/TOOLS/conf_emm.h b/openair3/NAS/TOOLS/conf_emm.h
index a7b97c5da2..f9c30d31c9 100644
--- a/openair3/NAS/TOOLS/conf_emm.h
+++ b/openair3/NAS/TOOLS/conf_emm.h
@@ -3,8 +3,15 @@
 
 #include "emmData.h"
 #include "conf_network.h"
+#include "conf_user_plmn.h"
+
+void gen_emm_data(
+    emm_nvdata_t     *emm_data,
+    const char       *hplmn,
+    const char       *msin,
+    const plmns_list *eplmn,
+    const networks_t networks);
 
-void gen_emm_data(emm_nvdata_t *emm_data, const char *hplmn, const char *msin, int ehplmn_count, const networks_t networks);
 bool write_emm_data(const char *directory, int user_id, emm_nvdata_t *emm_data);
 int get_msin_parity(const char * msin, const char *mcc, const char *mnc);
 
diff --git a/openair3/NAS/TOOLS/conf_parser.c b/openair3/NAS/TOOLS/conf_parser.c
index e7d1240fad..d46142c461 100644
--- a/openair3/NAS/TOOLS/conf_parser.c
+++ b/openair3/NAS/TOOLS/conf_parser.c
@@ -74,7 +74,7 @@ bool parse_config_file(const char *output_dir, const char *conf_filename, int ou
         gen_usim_data(&usim_data_conf, &usim_data, &user_plmns, networks);
 
         gen_emm_data(&emm_data, usim_data_conf.hplmn, usim_data_conf.msin,
-                     user_plmns.equivalents_home.size, networks);
+                     &user_plmns.equivalents_home, networks);
 
         if ( output_flags & OUTPUT_UEDATA ) {
             write_user_data(output_dir, i, &user_data);
-- 
GitLab