changeset 5193:658eb5d94c0b draft

DHCP: Use correct buffer for receiving UDP Big wup! While here, ensure buffer is aligned to the structure we want to read.
author Roy Marples <roy@marples.name>
date Tue, 05 May 2020 19:03:49 +0000
parents 06fe088b1953
children c37ba778f32b b58248b5703a
files src/dhcp.c src/dhcp6.c src/ipv6nd.c
diffstat 3 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/dhcp.c	Tue May 05 17:55:10 2020 +0100
+++ b/src/dhcp.c	Tue May 05 19:03:49 2020 +0000
@@ -3599,7 +3599,7 @@
 	}
 #endif
 
-	dhcp_handlebootp(ifp, (struct bootp *)iov->iov_base, iov->iov_len,
+	dhcp_handlebootp(ifp, iov->iov_base, iov->iov_len,
 	    &from->sin_addr);
 }
 
@@ -3608,10 +3608,13 @@
 {
 	const struct dhcp_state *state;
 	struct sockaddr_in from;
-	unsigned char buf[10 * 1024]; /* Maximum MTU */
+	union {
+		struct bootp bootp;
+		uint8_t buf[10 * 1024]; /* Maximum MTU */
+	} iovbuf;
 	struct iovec iov = {
-		.iov_base = buf,
-		.iov_len = sizeof(buf),
+		.iov_base = iovbuf.buf,
+		.iov_len = sizeof(iovbuf.buf),
 	};
 	union {
 		struct cmsghdr hdr;
@@ -3624,7 +3627,7 @@
 	struct msghdr msg = {
 	    .msg_name = &from, .msg_namelen = sizeof(from),
 	    .msg_iov = &iov, .msg_iovlen = 1,
-	    .msg_control = buf, .msg_controllen = sizeof(cmsgbuf.buf),
+	    .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf),
 	};
 	int s;
 	ssize_t bytes;
--- a/src/dhcp6.c	Tue May 05 17:55:10 2020 +0100
+++ b/src/dhcp6.c	Tue May 05 19:03:49 2020 +0000
@@ -3721,10 +3721,12 @@
 dhcp6_recv(struct dhcpcd_ctx *ctx, struct ipv6_addr *ia)
 {
 	struct sockaddr_in6 from;
-	unsigned char buf[64 * 1024]; /* Maximum UDP message size */
+	union {
+		struct dhcp6_message dhcp6;
+		uint8_t buf[64 * 1024]; /* Maximum UDP message size */
+	} iovbuf;
 	struct iovec iov = {
-		.iov_base = buf,
-		.iov_len = sizeof(buf),
+		.iov_base = iovbuf.buf, .iov_len = sizeof(iovbuf.buf),
 	};
 	union {
 		struct cmsghdr hdr;
--- a/src/ipv6nd.c	Tue May 05 17:55:10 2020 +0100
+++ b/src/ipv6nd.c	Tue May 05 19:03:49 2020 +0000
@@ -1937,10 +1937,12 @@
 	struct dhcpcd_ctx *ctx;
 	int fd;
 	struct sockaddr_in6 from;
-	unsigned char buf[64 * 1024]; /* Maximum ICMPv6 size */
+	union {
+		struct icmp6_hdr hdr;
+		uint8_t buf[64 * 1024]; /* Maximum ICMPv6 size */
+	} iovbuf;
 	struct iovec iov = {
-		.iov_base = buf,
-		.iov_len = sizeof(buf),
+		.iov_base = iovbuf.buf, .iov_len = sizeof(iovbuf.buf),
 	};
 	union {
 		struct cmsghdr hdr;