Kaynağa Gözat

misc/ipv4_proto.h: fix aliasing problems with casting to ipv4_short. This manifested itself as incorrectly computed IP checksums of sent
packets in BDHCPClient, when compiled with gcc at -O3. Also fix misc/udp_proto.h.

ambrop7 13 yıl önce
ebeveyn
işleme
0f07c8a237
2 değiştirilmiş dosya ile 4 ekleme ve 12 silme
  1. 2 9
      misc/ipv4_proto.h
  2. 2 3
      misc/udp_proto.h

+ 2 - 9
misc/ipv4_proto.h

@@ -39,6 +39,7 @@
 #include <misc/debug.h>
 #include <misc/byteorder.h>
 #include <misc/packed.h>
+#include <misc/read_write_int.h>
 
 #define IPV4_PROTOCOL_IGMP 2
 #define IPV4_PROTOCOL_UDP 17
@@ -67,22 +68,14 @@ B_END_PACKED
 
 #define IPV4_MAKE_VERSION_IHL(size) (((size)/4) + (4 << 4))
 
-B_START_PACKED
-struct ipv4_short {
-    uint16_t v;
-} B_PACKED;
-B_END_PACKED
-
 static uint16_t ipv4_checksum (uint8_t *ip_hdr, uint16_t len)
 {
     ASSERT(len % 2 == 0)
     
-    struct ipv4_short *s = (struct ipv4_short *)ip_hdr;
-    
     uint32_t t = 0;
     
     for (uint16_t i = 0; i < len / 2; i++) {
-        t += ntoh16(s[i].v);
+        t += badvpn_read_be16((const char *)ip_hdr + 2 * i);
     }
     
     while (t >> 16) {

+ 2 - 3
misc/udp_proto.h

@@ -39,6 +39,7 @@
 #include <misc/debug.h>
 #include <misc/byteorder.h>
 #include <misc/ipv4_proto.h>
+#include <misc/read_write_int.h>
 
 B_START_PACKED
 struct udp_header {
@@ -53,12 +54,10 @@ static uint32_t udp_checksum_summer (uint8_t *data, uint16_t len)
 {
     ASSERT(len % 2 == 0)
     
-    struct ipv4_short *s = (struct ipv4_short *)data;
-    
     uint32_t t = 0;
     
     for (uint16_t i = 0; i < len / 2; i++) {
-        t += ntoh16(s[i].v);
+        t += badvpn_read_be16((const char *)data + 2 * i);
     }
     
     return t;