make gsup ipa name configurable in osmo-sgsn.cfg

Add a 'gsup ipa-name' VTY command which overrides the default
IPA name used by the SGSN on the GSUP link towards the HLR.
This is required for GSUP routing in multi-SGSN networks.

The 'gsup ipa-name' option can only be set via the config file
because changing the IPA name at run-time conflicts with active
GSUP connections and routes configured in the HLR. The osmo-sgsn
program must be restarted if its IPA name needs to change.

Related: OS#3356

Change-Id: Ib2f65fed9f56b9718e8a9647e3f01dce69870c1f
diff --git a/include/osmocom/sgsn/sgsn.h b/include/osmocom/sgsn/sgsn.h
index 3a34ff9..c80355d 100644
--- a/include/osmocom/sgsn/sgsn.h
+++ b/include/osmocom/sgsn/sgsn.h
@@ -124,6 +124,12 @@
 		enum ranap_nsap_addr_enc rab_assign_addr_enc;
 	} iu;
 #endif
+
+	/* This is transmitted as IPA Serial Number tag, which is used for GSUP routing (e.g. in OsmoHLR).
+	 * This name must be set in a multi-SGSN network, and it must be unique to each SGSN.
+	 * If no name is set, the IPA Serial Number will be the same as the Unit Name,
+	 * and will be of the form 'SGSN-00-00-00-00-00-00' */
+	char *sgsn_ipa_name;
 };
 
 struct sgsn_instance {
diff --git a/src/gprs/gprs_subscriber.c b/src/gprs/gprs_subscriber.c
index 4ab45c2..484c7ef 100644
--- a/src/gprs/gprs_subscriber.c
+++ b/src/gprs/gprs_subscriber.c
@@ -23,6 +23,7 @@
 #include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
 #include <osmocom/gsm/gsup.h>
 #include <osmocom/gsm/apn.h>
+#include <osmocom/gsm/ipa.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/core/logging.h>
 #include <osmocom/sgsn/gprs_subscriber.h>
@@ -63,15 +64,20 @@
 int gprs_subscr_init(struct sgsn_instance *sgi)
 {
 	const char *addr_str;
+	struct ipaccess_unit *ipa_dev;
 
 	if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr)
 		return 0;
 
 	addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr);
 
-	sgi->gsup_client = osmo_gsup_client_create(
+	ipa_dev = talloc_zero(sgi, struct ipaccess_unit);
+	ipa_dev->unit_name = "SGSN";
+	ipa_dev->serno = sgi->cfg.sgsn_ipa_name; /* NULL unless configured via VTY */
+
+	sgi->gsup_client = osmo_gsup_client_create2(
 		sgi,
-		"SGSN",
+		ipa_dev,
 		addr_str, sgi->cfg.gsup_server_port,
 		&gsup_read_cb,
 		&sgi->cfg.oap);
diff --git a/src/gprs/sgsn_vty.c b/src/gprs/sgsn_vty.c
index 601b3c5..3757c07 100644
--- a/src/gprs/sgsn_vty.c
+++ b/src/gprs/sgsn_vty.c
@@ -203,6 +203,8 @@
 		vty_out(vty, " encryption %s%s",
 			get_value_string(gprs_cipher_names, g_cfg->cipher),
 			VTY_NEWLINE);
+	if (g_cfg->sgsn_ipa_name)
+		vty_out(vty, " gsup ipa-name %s%s", g_cfg->sgsn_ipa_name, VTY_NEWLINE);
 	if (g_cfg->gsup_server_addr.sin_addr.s_addr)
 		vty_out(vty, " gsup remote-ip %s%s",
 			inet_ntoa(g_cfg->gsup_server_addr.sin_addr), VTY_NEWLINE);
@@ -1075,6 +1077,25 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(cfg_gsup_ipa_name,
+	cfg_gsup_ipa_name_cmd,
+	"gsup ipa-name NAME",
+	"GSUP Parameters\n"
+	"Set the IPA name of this SGSN\n"
+	"A unique name for this SGSN. For example: PLMN + redundancy server number: SGSN-901-70-0. "
+	"This name is used for GSUP routing and must be set if more than one SGSN is connected to the network. "
+	"The default is 'SGSN-00-00-00-00-00-00'.\n")
+{
+	if (vty->type != VTY_FILE) {
+		vty_out(vty, "The IPA name cannot be changed at run-time; "
+			"It can only be set in the configuraton file.%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	g_cfg->sgsn_ipa_name = talloc_strdup(tall_vty_ctx, argv[0]);
+	return CMD_SUCCESS;
+}
+
 DEFUN(cfg_gsup_remote_ip, cfg_gsup_remote_ip_cmd,
 	"gsup remote-ip A.B.C.D",
 	"GSUP Parameters\n"
@@ -1365,6 +1386,7 @@
 	install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
 	install_element(SGSN_NODE, &cfg_auth_policy_cmd);
 	install_element(SGSN_NODE, &cfg_encrypt_cmd);
+	install_element(SGSN_NODE, &cfg_gsup_ipa_name_cmd);
 	install_element(SGSN_NODE, &cfg_gsup_remote_ip_cmd);
 	install_element(SGSN_NODE, &cfg_gsup_remote_port_cmd);
 	install_element(SGSN_NODE, &cfg_gsup_oap_id_cmd);