net: tcp: Fix SYN handling after connection is established

According to RFC 793, ch 3.9 Event Processing, receving SYN flag after
the connection has been established is an error codition:

    If the SYN is in the window it is an error, send a reset, any
    outstanding RECEIVEs and SEND should receive "reset" responses,
    all segment queues should be flushed, the user should also
    receive an unsolicited general "connection reset" signal, enter
    the CLOSED state, delete the TCB, and return."

Currently TCP stack ignored such event, causing interoperability test
failures. Fix this, by verifying if the SYN flag is set in a packet in
any state other than TCP_LISTEN and TCP_SYN_SENT.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
diff --git a/subsys/net/ip/tcp.c b/subsys/net/ip/tcp.c
index 2f620a3..7be0e03 100644
--- a/subsys/net/ip/tcp.c
+++ b/subsys/net/ip/tcp.c
@@ -1962,6 +1962,21 @@
 		goto next_state;
 	}
 
+	if (th && (conn->state != TCP_LISTEN) && (conn->state != TCP_SYN_SENT) &&
+	    tcp_validate_seq(conn, th) && FL(&fl, &, SYN)) {
+		/* According to RFC 793, ch 3.9 Event Processing, receiving SYN
+		 * once the connection has been established is an error
+		 * condition, reset should be sent and connection closed.
+		 */
+		NET_DBG("conn: %p, SYN received in %s state, dropping connection",
+			conn, tcp_state_to_str(conn->state, false));
+		net_stats_update_tcp_seg_drop(conn->iface);
+		tcp_out(conn, RST);
+		conn_state(conn, TCP_CLOSED);
+		close_status = -ECONNRESET;
+		goto next_state;
+	}
+
 	if (th) {
 		size_t max_win;