net: arp: Remove in_addr/in6_addr from packed net_arp_hdr struct

Replace unpacked in_addr/in6_addr structures with raw buffers in
net_arp_hdr struct, to prevent compiler warnings about unaligned
access.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2021-10-04 12:56:46 +02:00 committed by Anas Nashif
parent 0da228c57c
commit bbdeef4ac4
5 changed files with 69 additions and 41 deletions

View file

@ -739,6 +739,18 @@ static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)
#define net_ipaddr_copy(dest, src) \
UNALIGNED_PUT(UNALIGNED_GET(src), dest)
/**
* @brief Copy an IPv4 address raw buffer
*
* @param dest Destination IP address.
* @param src Source IP address.
*/
static inline void net_ipv4_addr_copy_raw(uint8_t *dest,
const uint8_t *src)
{
net_ipaddr_copy((struct in_addr *)dest, (const struct in_addr *)src);
}
/**
* @brief Compare two IPv4 addresses
*
@ -753,6 +765,21 @@ static inline bool net_ipv4_addr_cmp(const struct in_addr *addr1,
return UNALIGNED_GET(&addr1->s_addr) == UNALIGNED_GET(&addr2->s_addr);
}
/**
* @brief Compare two raw IPv4 address buffers
*
* @param addr1 Pointer to IPv4 address buffer.
* @param addr2 Pointer to IPv4 address buffer.
*
* @return True if the addresses are the same, false otherwise.
*/
static inline bool net_ipv4_addr_cmp_raw(const uint8_t *addr1,
const uint8_t *addr2)
{
return net_ipv4_addr_cmp((const struct in_addr *)addr1,
(const struct in_addr *)addr2);
}
/**
* @brief Compare two IPv6 addresses
*

View file

@ -112,14 +112,14 @@ enum net_verdict net_ipv4_autoconf_input(struct net_if *iface,
arp_hdr = NET_ARP_HDR(pkt);
if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr,
&cfg->ipv4auto.requested_ip)) {
if (!net_ipv4_addr_cmp_raw(arp_hdr->dst_ipaddr,
(uint8_t *)&cfg->ipv4auto.requested_ip)) {
/* No conflict */
return NET_CONTINUE;
}
if (!net_ipv4_addr_cmp(&arp_hdr->src_ipaddr,
&cfg->ipv4auto.requested_ip)) {
if (!net_ipv4_addr_cmp_raw(arp_hdr->src_ipaddr,
(uint8_t *)&cfg->ipv4auto.requested_ip)) {
/* No need to defend */
return NET_CONTINUE;
}

View file

@ -298,7 +298,7 @@ static inline struct net_pkt *arp_prepare(struct net_if *iface,
(void)memset(&hdr->dst_hwaddr.addr, 0x00, sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->dst_ipaddr, next_addr);
net_ipv4_addr_copy_raw(hdr->dst_ipaddr, (uint8_t *)next_addr);
memcpy(hdr->src_hwaddr.addr, net_pkt_lladdr_src(pkt)->addr,
sizeof(struct net_eth_addr));
@ -306,13 +306,13 @@ static inline struct net_pkt *arp_prepare(struct net_if *iface,
if (net_pkt_ipv4_auto(pkt)) {
my_addr = current_ip;
} else if (!entry) {
my_addr = &NET_IPV4_HDR(pending)->src;
my_addr = (struct in_addr *)NET_IPV4_HDR(pending)->src;
} else {
my_addr = if_get_addr(entry->iface, current_ip);
}
if (my_addr) {
net_ipaddr_copy(&hdr->src_ipaddr, my_addr);
net_ipv4_addr_copy_raw(hdr->src_ipaddr, (uint8_t *)my_addr);
} else {
(void)memset(&hdr->src_ipaddr, 0, sizeof(struct in_addr));
}
@ -523,8 +523,8 @@ static inline struct net_pkt *arp_prepare_reply(struct net_if *iface,
memcpy(&hdr->src_hwaddr.addr, net_if_get_link_addr(iface)->addr,
sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->dst_ipaddr, &query->src_ipaddr);
net_ipaddr_copy(&hdr->src_ipaddr, &query->dst_ipaddr);
net_ipv4_addr_copy_raw(hdr->dst_ipaddr, query->src_ipaddr);
net_ipv4_addr_copy_raw(hdr->src_ipaddr, query->dst_ipaddr);
net_pkt_lladdr_src(pkt)->addr = net_if_get_link_addr(iface)->addr;
net_pkt_lladdr_src(pkt)->len = sizeof(struct net_eth_addr);
@ -541,7 +541,7 @@ static bool arp_hdr_check(struct net_arp_hdr *arp_hdr)
ntohs(arp_hdr->protocol) != NET_ETH_PTYPE_IP ||
arp_hdr->hwlen != sizeof(struct net_eth_addr) ||
arp_hdr->protolen != NET_ARP_IPV4_PTYPE_SIZE ||
net_ipv4_is_addr_loopback(&arp_hdr->src_ipaddr)) {
net_ipv4_is_addr_loopback((struct in_addr *)arp_hdr->src_ipaddr)) {
NET_DBG("DROP: Invalid ARP header");
return false;
}
@ -594,7 +594,7 @@ enum net_verdict net_arp_input(struct net_pkt *pkt,
* then update it here.
*/
arp_update(net_pkt_iface(pkt),
&arp_hdr->src_ipaddr,
(struct in_addr *)arp_hdr->src_ipaddr,
&arp_hdr->src_hwaddr,
true, false);
break;
@ -606,13 +606,14 @@ enum net_verdict net_arp_input(struct net_pkt *pkt,
*/
if (memcmp(&eth_hdr->dst, net_eth_broadcast_addr(),
sizeof(struct net_eth_addr)) == 0 &&
net_ipv4_is_addr_mcast(&arp_hdr->src_ipaddr)) {
net_ipv4_is_addr_mcast((struct in_addr *)arp_hdr->src_ipaddr)) {
NET_DBG("DROP: eth addr is bcast, src addr is mcast");
return NET_DROP;
}
/* Someone wants to know our ll address */
addr = if_get_addr(net_pkt_iface(pkt), &arp_hdr->dst_ipaddr);
addr = if_get_addr(net_pkt_iface(pkt),
(struct in_addr *)arp_hdr->dst_ipaddr);
if (!addr) {
/* Not for us so drop the packet silently */
return NET_DROP;
@ -639,7 +640,7 @@ enum net_verdict net_arp_input(struct net_pkt *pkt,
arp_hdr->hwlen)));
arp_update(net_pkt_iface(pkt),
&arp_hdr->src_ipaddr,
(struct in_addr *)arp_hdr->src_ipaddr,
&arp_hdr->src_hwaddr,
false, true);
@ -659,9 +660,9 @@ enum net_verdict net_arp_input(struct net_pkt *pkt,
break;
case NET_ARP_REPLY:
if (net_ipv4_is_my_addr(&arp_hdr->dst_ipaddr)) {
if (net_ipv4_is_my_addr((struct in_addr *)arp_hdr->dst_ipaddr)) {
arp_update(net_pkt_iface(pkt),
&arp_hdr->src_ipaddr,
(struct in_addr *)arp_hdr->src_ipaddr,
&arp_hdr->src_hwaddr,
false, false);
}

View file

@ -26,16 +26,16 @@ extern "C" {
#define NET_ARP_HDR(pkt) ((struct net_arp_hdr *)net_pkt_data(pkt))
struct net_arp_hdr {
uint16_t hwtype; /* HTYPE */
uint16_t protocol; /* PTYPE */
uint8_t hwlen; /* HLEN */
uint8_t protolen; /* PLEN */
uint16_t hwtype; /* HTYPE */
uint16_t protocol; /* PTYPE */
uint8_t hwlen; /* HLEN */
uint8_t protolen; /* PLEN */
uint16_t opcode;
struct net_eth_addr src_hwaddr; /* SHA */
struct in_addr src_ipaddr; /* SPA */
struct net_eth_addr dst_hwaddr; /* THA */
struct in_addr dst_ipaddr; /* TPA */
} __packed;
struct net_eth_addr src_hwaddr; /* SHA */
uint8_t src_ipaddr[NET_IPV4_ADDR_SIZE]; /* SPA */
struct net_eth_addr dst_hwaddr; /* THA */
uint8_t dst_ipaddr[NET_IPV4_ADDR_SIZE]; /* TPA */
} __packed;
#define NET_ARP_HTYPE_ETH 1
#define NET_ARP_IPV4_PTYPE_SIZE 4

View file

@ -210,8 +210,8 @@ static inline struct net_pkt *prepare_arp_reply(struct net_if *iface,
memcpy(&hdr->src_hwaddr.addr, addr,
sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->dst_ipaddr, &NET_ARP_HDR(req)->src_ipaddr);
net_ipaddr_copy(&hdr->src_ipaddr, &NET_ARP_HDR(req)->dst_ipaddr);
net_ipv4_addr_copy_raw(hdr->dst_ipaddr, NET_ARP_HDR(req)->src_ipaddr);
net_ipv4_addr_copy_raw(hdr->src_ipaddr, NET_ARP_HDR(req)->dst_ipaddr);
net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));
@ -260,8 +260,8 @@ static inline struct net_pkt *prepare_arp_request(struct net_if *iface,
(void)memset(&hdr->dst_hwaddr.addr, 0x00, sizeof(struct net_eth_addr));
memcpy(&hdr->src_hwaddr.addr, addr, sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->src_ipaddr, &req_hdr->src_ipaddr);
net_ipaddr_copy(&hdr->dst_ipaddr, &req_hdr->dst_ipaddr);
net_ipv4_addr_copy_raw(hdr->src_ipaddr, req_hdr->src_ipaddr);
net_ipv4_addr_copy_raw(hdr->dst_ipaddr, req_hdr->dst_ipaddr);
net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));
@ -430,16 +430,16 @@ void test_arp(void)
zassert_true(0, "exiting");
}
if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr,
&NET_IPV4_HDR(pkt)->dst)) {
if (!net_ipv4_addr_cmp_raw(arp_hdr->dst_ipaddr,
(uint8_t *)&NET_IPV4_HDR(pkt)->dst)) {
printk("ARP IP dest invalid %s, should be %s",
net_sprint_ipv4_addr(&arp_hdr->dst_ipaddr),
net_sprint_ipv4_addr(&NET_IPV4_HDR(pkt)->dst));
zassert_true(0, "exiting");
}
if (!net_ipv4_addr_cmp(&arp_hdr->src_ipaddr,
&NET_IPV4_HDR(pkt)->src)) {
if (!net_ipv4_addr_cmp_raw(arp_hdr->src_ipaddr,
(uint8_t *)&NET_IPV4_HDR(pkt)->src)) {
printk("ARP IP src invalid %s, should be %s",
net_sprint_ipv4_addr(&arp_hdr->src_ipaddr),
net_sprint_ipv4_addr(&NET_IPV4_HDR(pkt)->src));
@ -468,8 +468,8 @@ void test_arp(void)
arp_hdr = NET_ARP_HDR(pkt2);
if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr,
&iface->config.ip.ipv4->gw)) {
if (!net_ipv4_addr_cmp_raw(arp_hdr->dst_ipaddr,
(uint8_t *)&iface->config.ip.ipv4->gw)) {
printk("ARP IP dst invalid %s, should be %s\n",
net_sprint_ipv4_addr(&arp_hdr->dst_ipaddr),
net_sprint_ipv4_addr(&iface->config.ip.ipv4->gw));
@ -526,8 +526,8 @@ void test_arp(void)
arp_hdr = NET_ARP_HDR(pkt);
net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));
net_ipaddr_copy(&arp_hdr->dst_ipaddr, &dst);
net_ipaddr_copy(&arp_hdr->src_ipaddr, &src);
net_ipv4_addr_copy_raw(arp_hdr->dst_ipaddr, (uint8_t *)&dst);
net_ipv4_addr_copy_raw(arp_hdr->src_ipaddr, (uint8_t *)&src);
pkt2 = prepare_arp_reply(iface, pkt, &hwaddr, &eth_hdr);
@ -567,8 +567,8 @@ void test_arp(void)
(sizeof(struct net_eth_hdr)));
net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));
net_ipaddr_copy(&arp_hdr->dst_ipaddr, &src);
net_ipaddr_copy(&arp_hdr->src_ipaddr, &dst);
net_ipv4_addr_copy_raw(arp_hdr->dst_ipaddr, (uint8_t *)&src);
net_ipv4_addr_copy_raw(arp_hdr->src_ipaddr, (uint8_t *)&dst);
pkt2 = prepare_arp_request(iface, pkt, &hwaddr, &eth_hdr);
@ -627,8 +627,8 @@ void test_arp(void)
arp_hdr->opcode = htons(NET_ARP_REQUEST);
memcpy(&arp_hdr->src_hwaddr, &new_hwaddr, 6);
memcpy(&arp_hdr->dst_hwaddr, net_eth_broadcast_addr(), 6);
net_ipaddr_copy(&arp_hdr->dst_ipaddr, &dst);
net_ipaddr_copy(&arp_hdr->src_ipaddr, &dst);
net_ipv4_addr_copy_raw(arp_hdr->dst_ipaddr, (uint8_t *)&dst);
net_ipv4_addr_copy_raw(arp_hdr->src_ipaddr, (uint8_t *)&dst);
net_buf_add(pkt->buffer, sizeof(struct net_arp_hdr));