core: Fix sequencing number issues

Sequencing in MCTP allows SOM to declare a completely new sequence
number. Also add an implementation that handles messages that are
neither SOM or EOM.

Minor changes from Jeremy Kerr <jk@ozlabs.org>

Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/core.c b/core.c
index ebad5f8..08e5d05 100644
--- a/core.c
+++ b/core.c
@@ -247,9 +247,9 @@
 {
 	struct mctp_bus *bus = binding->bus;
 	struct mctp *mctp = binding->mctp;
+	uint8_t flags, exp_seq, seq, tag;
 	struct mctp_msg_ctx *ctx;
 	struct mctp_hdr *hdr;
-	uint8_t flags, seq, tag;
 	size_t len;
 	void *p;
 	int rc;
@@ -284,10 +284,6 @@
 			mctp_msg_ctx_reset(ctx);
 		} else {
 			ctx = mctp_msg_ctx_create(mctp, hdr->src, tag);
-			if (((ctx->last_seq + 1) % 4) != seq) {
-				mctp_msg_ctx_drop(ctx);
-				return;
-			}
 		}
 
 		rc = mctp_msg_ctx_add_pkt(ctx, pkt);
@@ -304,7 +300,12 @@
 		if (!ctx)
 			return;
 
-		if (((ctx->last_seq + 1) % 4) != seq) {
+		exp_seq = (ctx->last_seq + 1) % 4;
+
+		if (exp_seq != seq) {
+			mctp_prdebug(
+				"Sequence number %d does not match expected %d",
+				seq, exp_seq);
 			mctp_msg_ctx_drop(ctx);
 			return;
 		}
@@ -317,6 +318,30 @@
 
 		mctp_msg_ctx_drop(ctx);
 		break;
+
+	case 0:
+		/* Neither SOM nor EOM */
+		ctx = mctp_msg_ctx_lookup(mctp, hdr->src, tag);
+		if (!ctx)
+			return;
+
+		exp_seq = (ctx->last_seq + 1) % 4;
+		if (exp_seq != seq) {
+			mctp_prdebug(
+				"Sequence number %d does not match expected %d",
+				seq, exp_seq);
+			mctp_msg_ctx_drop(ctx);
+			return;
+		}
+
+		rc = mctp_msg_ctx_add_pkt(ctx, pkt);
+		if (rc) {
+			mctp_msg_ctx_drop(ctx);
+			return;
+		}
+		ctx->last_seq = seq;
+
+		break;
 	}
 }