diff mbox

[ovs-dev,2/5] ofproto-dpif: Fix unaligned eth_addr pointers.

Message ID 20170523230216.29696-3-joe@ovn.org
State Superseded
Headers show

Commit Message

Joe Stringer May 23, 2017, 11:02 p.m. UTC
Clang 4.0 complains:

../ofproto/ofproto-dpif.c:2291:46: error: taking address of packed member
'eth_src' of class or structure 'eth_header' may result in an unaligned pointer
value
      [-Werror,-Waddress-of-packed-member]
    netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
                                             ^~~~~~~~~~~~
../ofproto/ofproto-dpif.c:2316:50: error: taking address of packed member
'eth_src' of class or structure 'eth_header' may result in an unaligned pointer
value
      [-Werror,-Waddress-of-packed-member]
        netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);

Ethernet source addresses are 48 bits offset into the Ethernet header,
so taking a pointer for this is not guaranteed to be valid on all
architectures. Fix this by retrieving the mac address to the local stack
and copying it into the header.

Signed-off-by: Joe Stringer <joe@ovn.org>
---
 ofproto/ofproto-dpif.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

Ben Pfaff May 25, 2017, 3:39 a.m. UTC | #1
On Tue, May 23, 2017 at 04:02:13PM -0700, Joe Stringer wrote:
> Clang 4.0 complains:
> 
> ../ofproto/ofproto-dpif.c:2291:46: error: taking address of packed member
> 'eth_src' of class or structure 'eth_header' may result in an unaligned pointer
> value
>       [-Werror,-Waddress-of-packed-member]
>     netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
>                                              ^~~~~~~~~~~~
> ../ofproto/ofproto-dpif.c:2316:50: error: taking address of packed member
> 'eth_src' of class or structure 'eth_header' may result in an unaligned pointer
> value
>       [-Werror,-Waddress-of-packed-member]
>         netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
> 
> Ethernet source addresses are 48 bits offset into the Ethernet header,
> so taking a pointer for this is not guaranteed to be valid on all
> architectures. Fix this by retrieving the mac address to the local stack
> and copying it into the header.
> 
> Signed-off-by: Joe Stringer <joe@ovn.org>

Hmm, I still don't understand what's packed here.
diff mbox

Patch

diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c
index dc5f004cd92d..5b866427f406 100644
--- a/ofproto/ofproto-dpif.c
+++ b/ofproto/ofproto-dpif.c
@@ -2287,13 +2287,15 @@  rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
     struct ofproto_dpif *ofproto = ofproto_;
     struct ofport_dpif *ofport = ofport_;
     struct eth_header *eth = dp_packet_eth(pkt);
+    struct eth_addr mac;
 
-    netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
-    if (eth_addr_is_zero(eth->eth_src)) {
+    netdev_get_etheraddr(ofport->up.netdev, &mac);
+    if (eth_addr_is_zero(mac)) {
         VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
                      "does not have a configured source MAC address.",
                      ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
     } else {
+        eth->eth_src = mac;
         ofproto_dpif_send_packet(ofport, false, pkt);
     }
     dp_packet_delete(pkt);
@@ -2312,12 +2314,14 @@  send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
                      ofproto->up.name, port_num);
     } else {
         struct eth_header *eth = dp_packet_eth(pkt);
+        struct eth_addr mac;
 
-        netdev_get_etheraddr(ofport->up.netdev, &eth->eth_src);
-        if (eth_addr_is_zero(eth->eth_src)) {
+        netdev_get_etheraddr(ofport->up.netdev, &mac);
+        if (eth_addr_is_zero(mac)) {
             VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
                          "with unknown MAC", ofproto->up.name, port_num);
         } else {
+            eth->eth_src = mac;
             ofproto_dpif_send_packet(ofport, false, pkt);
         }
     }