Commit a6093804 authored by Martin Åberg's avatar Martin Åberg Committed by Jukka Rissanen
Browse files

net: ip: enforce alignment in struct cmsghdr



The alignment is needed when using CMSG_DATA() to access a uint64_t
when an object of type struct cmsghdr is allocated on the stack.

Also update the ALIGN_H and ALIGN_D macros to better match the
description in RFC 3542:
   "The macros ALIGN_H() and ALIGN_D(), which are implementation
   dependent, round their arguments up to the next even multiple of
   whatever alignment is required for the start of the cmsghdr structure
   and the data, respectively."

This commit makes the following test pass on qemu_leon3:
- net.socket.udp. It previously generated an unaligned data access
exception when a uint64_t value was accessed via CMSG_DATA().

Signed-off-by: default avatarMartin Åberg <martin.aberg@gaisler.com>
parent e6f4140b
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -238,17 +238,18 @@ struct cmsghdr {
	socklen_t cmsg_len;    /* Number of bytes, including header */
	int       cmsg_level;  /* Originating protocol */
	int       cmsg_type;   /* Protocol-specific type */
	/* Followed by unsigned char cmsg_data[]; */
	/* Flexible array member to force alignment of cmsghdr */
	z_max_align_t cmsg_data[];
};

/* Alignment for headers and data. These are arch specific but define
 * them here atm if not found alredy.
 */
#if !defined(ALIGN_H)
#define ALIGN_H(x) WB_UP(x)
#define ALIGN_H(x) ROUND_UP(x, __alignof__(struct cmsghdr))
#endif
#if !defined(ALIGN_D)
#define ALIGN_D(x) WB_UP(x)
#define ALIGN_D(x) ROUND_UP(x, __alignof__(z_max_align_t))
#endif

#if !defined(CMSG_FIRSTHDR)