Skip to contentSkip to conversation

hw/net: ftgmac100: copy eth_hdr for alignment

PATCH In review Patrick Venture 4 participants 5 messages Feb 27, 2025 – Mar 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net: ftgmac100: copy eth_hdr for alignment

eth_hdr requires 2 byte alignment

---
 hw/net/ftgmac100.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
hw/net/ftgmac100.c +12 -3
index 1f524d7a01..a33aaa01ee 100644
@@ -989,12 +989,16 @@ static void ftgmac100_high_write(void *opaque, hwaddr addr,
 static int ftgmac100_filter(FTGMAC100State *s, const uint8_t *buf, size_t len)
 {
     unsigned mcast_idx;
+    struct eth_header eth_hdr = {};
 
     if (s->maccr & FTGMAC100_MACCR_RX_ALL) {
         return 1;
     }
 
-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+
+    switch (get_eth_packet_type(&eth_hdr)) {
     case ETH_PKT_BCAST:
         if (!(s->maccr & FTGMAC100_MACCR_RX_BROADPKT)) {
             return 0;
@@ -1028,6 +1032,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
 {
     FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
     FTGMAC100Desc bd;
+    struct eth_header eth_hdr = {};
     uint32_t flags = 0;
     uint64_t addr;
     uint32_t crc;
@@ -1036,7 +1041,11 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
     uint32_t buf_len;
     size_t size = len;
     uint32_t first = FTGMAC100_RXDES0_FRS;
-    uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);
+    uint16_t proto;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    proto = be16_to_cpu(eth_hdr.h_proto);
     int max_frame_size = ftgmac100_max_frame_size(s, proto);
 
     if ((s->maccr & (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN))
@@ -1061,7 +1070,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
         flags |= FTGMAC100_RXDES0_FTL;
     }
 
-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
+    switch (get_eth_packet_type(&eth_hdr)) {
     case ETH_PKT_BCAST:
         flags |= FTGMAC100_RXDES0_BROADCAST;
         break;
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#1 lore raw

Hi Patrick,

Quoted 16 lines · Patrick Venture

eth_hdr requires 2 byte alignment

---
 hw/net/ftgmac100.c | 15 ++++++++++++---

 1 file changed, 12 insertions(+), 3 deletions(-)
hw/net/ftgmac100.c +1 -0
index 1f524d7a01..a33aaa01ee 100644
@@ -989,12 +989,16 @@ static void ftgmac100_high_write(void *opaque, hwaddr addr,
  static int ftgmac100_filter(FTGMAC100State *s, const uint8_t *buf, size_t len)
  {
      unsigned mcast_idx;
+    struct eth_header eth_hdr = {};
Quoted 8 lines


     if (s->maccr & FTGMAC100_MACCR_RX_ALL) { return 1;
     }

-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) { +    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));

I don't think truncating the memcpy() in this way is what we want? The switched value may not be meaningful for small values of len.

Perhaps return an error?

Quoted 14 lines

+
+    switch (get_eth_packet_type(&eth_hdr)) { case ETH_PKT_BCAST:
         if (!(s->maccr & FTGMAC100_MACCR_RX_BROADPKT)) { return 0;

hw/net/ftgmac100.c +1 -0
@@ -1028,6 +1032,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
  {
      FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
      FTGMAC100Desc bd;
+    struct eth_header eth_hdr = {};
      uint32_t flags = 0;
      uint64_t addr;
      uint32_t crc;

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf, · line 1047

Quoted 8 lines
hw/net/ftgmac100.c +4 -1
@@ -1036,7 +1041,11 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
      uint32_t buf_len;
      size_t size = len;
      uint32_t first = FTGMAC100_RXDES0_FRS;
-    uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);
+    uint16_t proto;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));

Again here.

Quoted 13 lines
hw/net/ftgmac100.c +1 -0
+    proto = be16_to_cpu(eth_hdr.h_proto);
      int max_frame_size = ftgmac100_max_frame_size(s, proto);


     if ((s->maccr & (FTGMAC100_MACCR_RXDMA_EN | FTGMAC100_MACCR_RXMAC_EN))

hw/net/ftgmac100.c +0 -0
@@ -1061,7 +1070,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
          flags |= FTGMAC100_RXDES0_FTL;
      }


-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) { +    switch (get_eth_packet_type(&eth_hdr)) { case ETH_PKT_BCAST:
         flags |= FTGMAC100_RXDES0_BROADCAST; break;

#2 lore raw
Quoted 62 lines · Andrew Jeffery

Hi Patrick,

hw/net/ftgmac100.c static void ftgmac100_high_write(void *opaque,

Quoted 12 lines · Patrick Venture

eth_hdr requires 2 byte alignment

---
 hw/net/ftgmac100.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
hw/net/ftgmac100.c +0 -0
index 1f524d7a01..a33aaa01ee 100644
@@ -989,12 +989,16 @@ static void ftgmac100_high_write(void *opaque,

hwaddr addr,

hw/net/ftgmac100.c static void ftgmac100_high_write(void *opaque, · line 989

Quoted 1 line
hw/net/ftgmac100.c +0 -0
 static int ftgmac100_filter(FTGMAC100State *s, const uint8_t *buf,

size_t len)

hw/net/ftgmac100.c static void ftgmac100_high_write(void *opaque, hwaddr addr, · line 999

Quoted 11 lines
hw/net/ftgmac100.c +3 -1
 {
     unsigned mcast_idx;
+    struct eth_header eth_hdr = {};
 
     if (s->maccr & FTGMAC100_MACCR_RX_ALL) {
         return 1;
     }
 
-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));

I don't think truncating the memcpy() in this way is what we want? The switched value may not be meaningful for small values of len.

Perhaps return an error?

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState

Quoted 6 lines
hw/net/ftgmac100.c +2 -0
+
+    switch (get_eth_packet_type(&eth_hdr)) {
     case ETH_PKT_BCAST:
         if (!(s->maccr & FTGMAC100_MACCR_RX_BROADPKT)) {
             return 0;
@@ -1028,6 +1032,7 @@ static ssize_t ftgmac100_receive(NetClientState

*nc, const uint8_t *buf,

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState

Quoted 8 lines
hw/net/ftgmac100.c +1 -0
 {
     FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
     FTGMAC100Desc bd;
+    struct eth_header eth_hdr = {};
     uint32_t flags = 0;
     uint64_t addr;
     uint32_t crc;
@@ -1036,7 +1041,11 @@ static ssize_t ftgmac100_receive(NetClientState

*nc, const uint8_t *buf,

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf, · line 1047

Quoted 8 lines
hw/net/ftgmac100.c +4 -1
     uint32_t buf_len;
     size_t size = len;
     uint32_t first = FTGMAC100_RXDES0_FRS;
-    uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);
+    uint16_t proto;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));

Again here.

Quoted 17 lines

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,

Quoted 4 lines
hw/net/ftgmac100.c +1 -0
+    proto = be16_to_cpu(eth_hdr.h_proto);
     int max_frame_size = ftgmac100_max_frame_size(s, proto);
 
     if ((s->maccr & (FTGMAC100_MACCR_RXDMA_EN |

FTGMAC100_MACCR_RXMAC_EN))

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState

Quoted 1 line
hw/net/ftgmac100.c +0 -0
@@ -1061,7 +1070,7 @@ static ssize_t ftgmac100_receive(NetClientState

*nc, const uint8_t *buf,

Quoted 8 lines
hw/net/ftgmac100.c +1 -1
         flags |= FTGMAC100_RXDES0_FTL;
     }
 
-    switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf))) {
+    switch (get_eth_packet_type(&eth_hdr)) {
     case ETH_PKT_BCAST:
         flags |= FTGMAC100_RXDES0_BROADCAST;
         break;

Thanks, I've been asked to fix eth_header to be correctly packed instead and I'm still fixing the implications of that.

#3 lore raw
In reply to Patrick Venture

Hi Patrick,

Quoted 17 lines · Patrick Venture

eth_hdr requires 2 byte alignment

---
  hw/net/ftgmac100.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
hw/net/ftgmac100.c +1 -0
@@ -1028,6 +1032,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
  {
      FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
      FTGMAC100Desc bd;
+    struct eth_header eth_hdr = {};
      uint32_t flags = 0;
      uint64_t addr;
      uint32_t crc;

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf, · line 1039

Quoted 4 lines
hw/net/ftgmac100.c +0 -1
@@ -1036,7 +1041,11 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
      uint32_t buf_len;
      size_t size = len;
      uint32_t first = FTGMAC100_RXDES0_FRS;
-    uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);

The LD/ST API deals with unaligned fields, would that help?

    uint16_t proto = lduw_be_p(&PKT_GET_ETH_HDR(buf)->h_proto);
Quoted 6 lines
hw/net/ftgmac100.c +5 -0
+    uint16_t proto;
+
+    memcpy(&eth_hdr, PKT_GET_ETH_HDR(buf),
+           (sizeof(eth_hdr) > len) ? len : sizeof(eth_hdr));
+    proto = be16_to_cpu(eth_hdr.h_proto);
      int max_frame_size = ftgmac100_max_frame_size(s, proto);
#4 lore raw
Quoted 28 lines · Philippe Mathieu-Daudé

Hi Patrick,

Quoted 17 lines · Patrick Venture

eth_hdr requires 2 byte alignment

---
  hw/net/ftgmac100.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
hw/net/ftgmac100.c +1 -0
@@ -1028,6 +1032,7 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
  {
      FTGMAC100State *s = FTGMAC100(qemu_get_nic_opaque(nc));
      FTGMAC100Desc bd;
+    struct eth_header eth_hdr = {};
      uint32_t flags = 0;
      uint64_t addr;
      uint32_t crc;

hw/net/ftgmac100.c static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf, · line 1039

Quoted 4 lines
hw/net/ftgmac100.c +0 -1
@@ -1036,7 +1041,11 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
      uint32_t buf_len;
      size_t size = len;
      uint32_t first = FTGMAC100_RXDES0_FRS;
-    uint16_t proto = be16_to_cpu(PKT_GET_ETH_HDR(buf)->h_proto);

The LD/ST API deals with unaligned fields, would that help?

Quoted 1 line
     uint16_t proto = lduw_be_p(&PKT_GET_ETH_HDR(buf)->h_proto);

No, it doesn't, unfortunately -- I forget the details, but if the struct is unaligned but its definition says it is aligned then you get UB even with our accessor functions. This is why in commit f8b94b4c520126 I had to fix this for struct ip_header by marking it as QEMU_PACKED, even though the actual access there was a uint8_t*.

(See also the other thread where I suggested to Patrick that the best approach here is to mark eth_hdr as QEMU_PACKED, and fix up anything we need to do to make that change.)

thanks
-- PMM