[libvirt PATCH 4/5] nwfilter: Avoid memory alignment issues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The returned packet can have less strict alignment (u_char) than the struct
(ether_header) we are casting it to, so to avoid alignment issues just copy the
header into the struct on the stack.

Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx>
---
 src/nwfilter/nwfilter_learnipaddr.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c
index fb552bd1e60e..99bffdc4fbdb 100644
--- a/src/nwfilter/nwfilter_learnipaddr.c
+++ b/src/nwfilter/nwfilter_learnipaddr.c
@@ -384,7 +384,7 @@ learnIPAddressThread(void *arg)
     struct bpf_program fp;
     struct pcap_pkthdr header;
     const u_char *packet;
-    struct ether_header *ether_hdr;
+    struct ether_header ether_hdr;
     struct ether_vlan_header *vlan_hdr;
     virNWFilterIPAddrLearnReq *req = arg;
     uint32_t vmaddr = 0, bcastaddr = 0;
@@ -506,13 +506,14 @@ learnIPAddressThread(void *arg)
         }
 
         if (header.len >= sizeof(struct ether_header)) {
-            ether_hdr = (struct ether_header*)packet;
+            /* Avoid alignment issues */
+            memcpy(&ether_hdr, packet, sizeof(struct ether_header));
 
-            switch (ntohs(ether_hdr->ether_type)) {
+            switch (ntohs(ether_hdr.ether_type)) {
 
             case ETHERTYPE_IP:
                 ethHdrSize = sizeof(struct ether_header);
-                etherType = ntohs(ether_hdr->ether_type);
+                etherType = ntohs(ether_hdr.ether_type);
                 break;
 
             case ETHERTYPE_VLAN:
@@ -528,7 +529,7 @@ learnIPAddressThread(void *arg)
                 continue;
             }
 
-            if (virMacAddrCmpRaw(&req->binding->mac, ether_hdr->ether_shost) == 0) {
+            if (virMacAddrCmpRaw(&req->binding->mac, ether_hdr.ether_shost) == 0) {
                 /* packets from the VM */
 
                 if (etherType == ETHERTYPE_IP &&
@@ -568,9 +569,9 @@ learnIPAddressThread(void *arg)
                     }
                 }
             } else if (virMacAddrCmpRaw(&req->binding->mac,
-                                        ether_hdr->ether_dhost) == 0 ||
+                                        ether_hdr.ether_dhost) == 0 ||
                        /* allow Broadcast replies from DHCP server */
-                       virMacAddrIsBroadcastRaw(ether_hdr->ether_dhost)) {
+                       virMacAddrIsBroadcastRaw(ether_hdr.ether_dhost)) {
                 /* packets to the VM */
                 if (etherType == ETHERTYPE_IP &&
                     (header.len >= ethHdrSize +
-- 
2.35.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux