Fix alignment of buffers in ra_process and dhcpv6_request
authorBen Hutchings <ben@decadent.org.uk>
Fri, 22 Jan 2016 20:57:34 +0000 (20:57 +0000)
committerBen Hutchings <ben@decadent.org.uk>
Thu, 28 Jan 2016 15:48:06 +0000 (15:48 +0000)
The packet buffer needs to be 32-bit aligned to ensure that the various
32-bit fields we pick out are naturally aligned.

The control message buffers needs to be naturally aligned for struct
cmsghdr.

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
src/dhcpv6.c
src/odhcp6c.h
src/ra.c

index e27d8995a40102a5446816334fb3e3843819e311..8a1231df1245adc1e057b5589818b93be9a9e99b 100644 (file)
@@ -577,7 +577,9 @@ int dhcpv6_request(enum dhcpv6_msg type)
                // Receive rounds
                for (; len < 0 && (round_start < round_end);
                                round_start = odhcp6c_get_milli_time()) {
-                       uint8_t buf[1536], cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
+                       uint8_t buf[1536];
+                       uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]
+                               __aligned(__alignof__(struct cmsghdr));
                        struct iovec iov = {buf, sizeof(buf)};
                        struct sockaddr_in6 addr;
                        struct msghdr msg = {.msg_name = &addr, .msg_namelen = sizeof(addr),
index 98d91dd1bb415eeb637cef789eea2a277095d510..08a816f4446dd893d89db315bf6f8932200ab2b8 100644 (file)
@@ -18,6 +18,7 @@
 
 #define _unused __attribute__((unused))
 #define _packed __attribute__((packed))
+#define __aligned(n) __attribute__((aligned(n)))
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
index 2d442f6a8bfbab096a2654317bd08fa60f113118..dd5962a29e750dba87bd39c33664e8e531efc7d5 100644 (file)
--- a/src/ra.c
+++ b/src/ra.c
@@ -274,7 +274,8 @@ bool ra_process(void)
 {
        bool found = false;
        bool changed = false;
-       uint8_t buf[1500], cmsg_buf[128];
+       uint8_t buf[1500] __aligned(4);
+       uint8_t cmsg_buf[128] __aligned(__alignof__(struct cmsghdr));
        struct nd_router_advert *adv = (struct nd_router_advert*)buf;
        struct odhcp6c_entry *entry = alloca(sizeof(*entry) + 256);
        const struct in6_addr any = IN6ADDR_ANY_INIT;