Return proper GSUP error in case of too short IMSI

This fixes HLR_Tests.TC_gsup_sai_err_invalid_imsi

Change-Id: I4f51abdf44dfc62d7e8792341aad6dafe58923da
Closes: OS#3028
diff --git a/src/hlr.c b/src/hlr.c
index 4da7b9b..ee19795 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -338,6 +338,29 @@
 	return osmo_gsup_conn_send(conn, msg_out);
 }
 
+static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
+				enum osmo_gsup_message_type type_in, uint8_t err_cause)
+{
+	int type_err = osmo_gsup_get_err_msg_type(type_in);
+	struct osmo_gsup_message gsup_reply = {0};
+	struct msgb *msg_out;
+
+	if (type_err < 0) {
+		LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
+			osmo_gsup_message_type_name(type_in));
+		return type_err;
+	}
+
+	OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
+	gsup_reply.message_type = type_err;
+	gsup_reply.cause = err_cause;
+	msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
+	OSMO_ASSERT(msg_out);
+	osmo_gsup_encode(msg_out, &gsup_reply);
+	LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
+	return osmo_gsup_conn_send(conn, msg_out);
+}
+
 static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
 {
 	static struct osmo_gsup_message gsup;
@@ -349,6 +372,11 @@
 		return rc;
 	}
 
+	/* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
+	 * digits is impossible.  Even 5 digits is a highly theoretical case */
+	if (strlen(gsup.imsi) < 5)
+		return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
+
 	switch (gsup.message_type) {
 	/* requests sent to us */
 	case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: