avoid alien packets
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Wed, 7 May 2025 17:11:17 +0000 (19:11 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Wed, 7 May 2025 17:11:17 +0000 (19:11 +0200)
morep.c

diff --git a/morep.c b/morep.c
index f99bc5c5dfb4d0ecef2aebdc77cd61d0cf49963e..a75198f15bd707431e6f63f2c71c072a4a196578 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -64,6 +64,7 @@ typedef struct {
     uint8_t seqnum;
     uint8_t *tx_buffer;
     uint8_t *rx_buffer;
+    uint16_t n_etype;
     int used;
 } MOREP_descriptor_t ;
 
@@ -335,8 +336,8 @@ int MOREP_Connect (char *url)
         MOREP_list[index]->sock = sock;
         memcpy (MOREP_list[index]->tx_buffer, (void *) addr.mac, 6);
         memcpy (MOREP_list[index]->tx_buffer + 6, sock_addr.sll_addr, 6);
-        uint16_t ether_type = htons (addr.ether_type);
-        memcpy (MOREP_list[index]->tx_buffer + 12, (void *) &ether_type, 2);
+        MOREP_list[index]->n_etype = htons (addr.ether_type);
+        memcpy (MOREP_list[index]->tx_buffer + 12, (void *) &(MOREP_list[index]->n_etype), 2);
     }
 
     return index;
@@ -422,6 +423,16 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len)
         rxlen = recvfrom (morep->sock, morep->rx_buffer, ETHER_PREAMBLE + MAX_ETHER_SIZE, 0, NULL, 0);
         VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->rx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':'));
 
+        /* avoid alien packets */
+        uint16_t rx_n_etype;
+        memcpy ((void *)&rx_n_etype, morep->rx_buffer + 12, 2);
+        if ((morep->n_etype != rx_n_etype) ||
+            (memcmp (morep->tx_buffer, morep->rx_buffer + 6, 6) != 0) ||
+            (memcmp (morep->tx_buffer + 6, morep->rx_buffer, 6) != 0)) {
+            VERBOSE (morep, DEBUG, PRINTF ("alien packet\n"));
+            continue;
+        }
+
         /* sequence number */
         int pkgseqnum = morep->rx_buffer[ETHER_PREAMBLE + 1];
         if ((rxseqnum != -1) && (rxseqnum != pkgseqnum)) {