Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions include/zephyr/net/net_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct in6_addr {
uint16_t s6_addr16[8]; /**< In big endian */
uint32_t s6_addr32[4]; /**< In big endian */
};
};
} __packed;

/** Binary size of the IPv6 address */
#define NET_IPV6_ADDR_SIZE 16
Expand All @@ -159,7 +159,7 @@ struct in_addr {
uint32_t s4_addr32[1]; /**< In big endian */
uint32_t s_addr; /**< In big endian, for POSIX compatibility. */
};
};
} __packed;

/** Binary size of the IPv4 address */
#define NET_IPV4_ADDR_SIZE 4
Expand Down Expand Up @@ -202,7 +202,7 @@ struct sockaddr_ll {
uint8_t sll_pkttype; /**< Packet type */
uint8_t sll_halen; /**< Length of address */
uint8_t sll_addr[8]; /**< Physical-layer address, big endian */
};
} __packed __aligned(2);

/** @cond INTERNAL_HIDDEN */

Expand Down Expand Up @@ -682,10 +682,10 @@ union net_proto_header {
*/
static inline bool net_ipv6_is_addr_loopback(struct in6_addr *addr)
{
return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
ntohl(UNALIGNED_GET(&addr->s6_addr32[3])) == 1;
return addr->s6_addr32[0] == 0 &&
addr->s6_addr32[1] == 0 &&
addr->s6_addr32[2] == 0 &&
ntohl(addr->s6_addr32[3]) == 1;
}

/**
Expand Down Expand Up @@ -826,7 +826,7 @@ static inline bool net_ipv4_is_addr_loopback(struct in_addr *addr)
*/
static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
{
return UNALIGNED_GET(&addr->s_addr) == 0;
return addr->s_addr == 0;
}

/**
Expand All @@ -838,7 +838,7 @@ static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)
*/
static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
{
return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xF0000000) == 0xE0000000;
return (ntohl(addr->s_addr) & 0xF0000000) == 0xE0000000;
}

/**
Expand All @@ -850,7 +850,7 @@ static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)
*/
static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)
{
return (ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFF0000) == 0xA9FE0000;
return (ntohl(addr->s_addr) & 0xFFFF0000) == 0xA9FE0000;
}

/**
Expand All @@ -866,7 +866,7 @@ static inline bool net_ipv4_is_private_addr(const struct in_addr *addr)
{
uint32_t masked_24, masked_16, masked_12, masked_10, masked_8;

masked_24 = ntohl(UNALIGNED_GET(&addr->s_addr)) & 0xFFFFFF00;
masked_24 = ntohl(addr->s_addr) & 0xFFFFFF00;
masked_16 = masked_24 & 0xFFFF0000;
masked_12 = masked_24 & 0xFFF00000;
masked_10 = masked_24 & 0xFFC00000;
Expand All @@ -890,7 +890,7 @@ static inline bool net_ipv4_is_private_addr(const struct in_addr *addr)
* @return Destination address.
*/
#define net_ipaddr_copy(dest, src) \
UNALIGNED_PUT(UNALIGNED_GET(src), dest)
*(dest) = *(src)

/**
* @brief Copy an IPv4 address raw buffer
Expand Down Expand Up @@ -927,7 +927,7 @@ static inline void net_ipv6_addr_copy_raw(uint8_t *dest,
static inline bool net_ipv4_addr_cmp(const struct in_addr *addr1,
const struct in_addr *addr2)
{
return UNALIGNED_GET(&addr1->s_addr) == UNALIGNED_GET(&addr2->s_addr);
return addr1->s_addr == addr2->s_addr;
}

/**
Expand Down Expand Up @@ -983,7 +983,7 @@ static inline bool net_ipv6_addr_cmp_raw(const uint8_t *addr1,
*/
static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
{
return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFE80);
return addr->s6_addr16[0] == htons(0xFE80);
}

/**
Expand All @@ -995,7 +995,7 @@ static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)
*/
static inline bool net_ipv6_is_sl_addr(const struct in6_addr *addr)
{
return UNALIGNED_GET(&addr->s6_addr16[0]) == htons(0xFEC0);
return addr->s6_addr16[0] == htons(0xFEC0);
}


Expand Down Expand Up @@ -1036,7 +1036,7 @@ static inline bool net_ipv6_is_private_addr(const struct in6_addr *addr)
{
uint32_t masked_32, masked_7;

masked_32 = ntohl(UNALIGNED_GET(&addr->s6_addr32[0]));
masked_32 = ntohl(addr->s6_addr32[0]);
masked_7 = masked_32 & 0xfc000000;

return masked_32 == 0x20010db8 || /* 2001:db8::/32 */
Expand Down Expand Up @@ -1148,10 +1148,10 @@ static inline bool net_ipv4_is_my_addr(const struct in_addr *addr)
*/
static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
{
return UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[2]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[3]) == 0;
return addr->s6_addr32[0] == 0 &&
addr->s6_addr32[1] == 0 &&
addr->s6_addr32[2] == 0 &&
addr->s6_addr32[3] == 0;
}

/**
Expand All @@ -1164,10 +1164,10 @@ static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)
*/
static inline bool net_ipv6_is_addr_solicited_node(const struct in6_addr *addr)
{
return UNALIGNED_GET(&addr->s6_addr32[0]) == htonl(0xff020000) &&
UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00000000 &&
UNALIGNED_GET(&addr->s6_addr32[2]) == htonl(0x00000001) &&
((UNALIGNED_GET(&addr->s6_addr32[3]) & htonl(0xff000000)) ==
return addr->s6_addr32[0] == htonl(0xff020000) &&
addr->s6_addr32[1] == 0x00000000 &&
addr->s6_addr32[2] == htonl(0x00000001) &&
((addr->s6_addr32[3] & htonl(0xff000000)) ==
htonl(0xff000000));
}

Expand Down Expand Up @@ -1298,12 +1298,12 @@ static inline bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)
static inline bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr,
const struct in6_addr *group)
{
return UNALIGNED_GET(&addr->s6_addr16[1]) == group->s6_addr16[1] &&
UNALIGNED_GET(&addr->s6_addr16[2]) == group->s6_addr16[2] &&
UNALIGNED_GET(&addr->s6_addr16[3]) == group->s6_addr16[3] &&
UNALIGNED_GET(&addr->s6_addr32[1]) == group->s6_addr32[1] &&
UNALIGNED_GET(&addr->s6_addr32[2]) == group->s6_addr32[1] &&
UNALIGNED_GET(&addr->s6_addr32[3]) == group->s6_addr32[3];
return addr->s6_addr16[1] == group->s6_addr16[1] &&
addr->s6_addr16[2] == group->s6_addr16[2] &&
addr->s6_addr16[3] == group->s6_addr16[3] &&
addr->s6_addr32[1] == group->s6_addr32[1] &&
addr->s6_addr32[2] == group->s6_addr32[1] &&
addr->s6_addr32[3] == group->s6_addr32[3];
}

/**
Expand Down Expand Up @@ -1370,15 +1370,15 @@ void net_ipv6_addr_create_solicited_node(const struct in6_addr *src,
{
dst->s6_addr[0] = 0xFF;
dst->s6_addr[1] = 0x02;
UNALIGNED_PUT(0, &dst->s6_addr16[1]);
UNALIGNED_PUT(0, &dst->s6_addr16[2]);
UNALIGNED_PUT(0, &dst->s6_addr16[3]);
UNALIGNED_PUT(0, &dst->s6_addr16[4]);
dst->s6_addr16[1] = 0;
dst->s6_addr16[2] = 0;
dst->s6_addr16[3] = 0;
dst->s6_addr16[4] = 0;
dst->s6_addr[10] = 0U;
dst->s6_addr[11] = 0x01;
dst->s6_addr[12] = 0xFF;
dst->s6_addr[13] = src->s6_addr[13];
UNALIGNED_PUT(UNALIGNED_GET(&src->s6_addr16[7]), &dst->s6_addr16[7]);
dst->s6_addr16[7] = src->s6_addr16[7];
}

/** @brief Construct an IPv6 address from eight 16-bit words.
Expand All @@ -1399,14 +1399,14 @@ static inline void net_ipv6_addr_create(struct in6_addr *addr,
uint16_t addr4, uint16_t addr5,
uint16_t addr6, uint16_t addr7)
{
UNALIGNED_PUT(htons(addr0), &addr->s6_addr16[0]);
UNALIGNED_PUT(htons(addr1), &addr->s6_addr16[1]);
UNALIGNED_PUT(htons(addr2), &addr->s6_addr16[2]);
UNALIGNED_PUT(htons(addr3), &addr->s6_addr16[3]);
UNALIGNED_PUT(htons(addr4), &addr->s6_addr16[4]);
UNALIGNED_PUT(htons(addr5), &addr->s6_addr16[5]);
UNALIGNED_PUT(htons(addr6), &addr->s6_addr16[6]);
UNALIGNED_PUT(htons(addr7), &addr->s6_addr16[7]);
addr->s6_addr16[0] = htons(addr0);
addr->s6_addr16[1] = htons(addr1);
addr->s6_addr16[2] = htons(addr2);
addr->s6_addr16[3] = htons(addr3);
addr->s6_addr16[4] = htons(addr4);
addr->s6_addr16[5] = htons(addr5);
addr->s6_addr16[6] = htons(addr6);
addr->s6_addr16[7] = htons(addr7);
}

/**
Expand Down Expand Up @@ -1453,9 +1453,9 @@ static inline void net_ipv6_addr_create_v4_mapped(const struct in_addr *addr4,
*/
static inline bool net_ipv6_addr_is_v4_mapped(const struct in6_addr *addr)
{
if (UNALIGNED_GET(&addr->s6_addr32[0]) == 0 &&
UNALIGNED_GET(&addr->s6_addr32[1]) == 0 &&
UNALIGNED_GET(&addr->s6_addr16[5]) == 0xffff) {
if (addr->s6_addr32[0] == 0 &&
addr->s6_addr32[1] == 0 &&
addr->s6_addr16[5] == 0xffff) {
return true;
}

Expand Down
22 changes: 11 additions & 11 deletions subsys/net/ip/6lo.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,36 +122,36 @@ static int get_ihpc_inlined_size(uint16_t iphc)
static inline bool net_6lo_ll_prefix_padded_with_zeros(struct in6_addr *addr)
{
return (net_ipv6_is_ll_addr(addr) &&
(UNALIGNED_GET(&addr->s6_addr16[1]) == 0x00) &&
(UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00));
(addr->s6_addr16[1] == 0x00) &&
(addr->s6_addr32[1] == 0x00));
}

static inline bool net_6lo_addr_16_bit_compressible(struct in6_addr *addr)
{
return ((UNALIGNED_GET(&addr->s6_addr32[2]) == htonl(0xFFu)) &&
(UNALIGNED_GET(&addr->s6_addr16[6]) == htons(0xFE00u)));
return ((addr->s6_addr32[2] == htonl(0xFFu)) &&
(addr->s6_addr16[6] == htons(0xFE00u)));
}

static inline bool net_6lo_maddr_8_bit_compressible(struct in6_addr *addr)
{
return ((addr->s6_addr[1] == 0x02) &&
(UNALIGNED_GET(&addr->s6_addr16[1]) == 0x00) &&
(UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
(UNALIGNED_GET(&addr->s6_addr32[2]) == 0x00) &&
(addr->s6_addr16[1] == 0x00) &&
(addr->s6_addr32[1] == 0x00) &&
(addr->s6_addr32[2] == 0x00) &&
(addr->s6_addr[14] == 0x00));
}

static inline bool net_6lo_maddr_32_bit_compressible(struct in6_addr *addr)
{
return ((UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
(UNALIGNED_GET(&addr->s6_addr32[2]) == 0x00) &&
return ((addr->s6_addr32[1] == 0x00) &&
(addr->s6_addr32[2] == 0x00) &&
(addr->s6_addr[12] == 0x00));
}

static inline bool net_6lo_maddr_48_bit_compressible(struct in6_addr *addr)
{
return ((UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
(UNALIGNED_GET(&addr->s6_addr16[4]) == 0x00) &&
return ((addr->s6_addr32[1] == 0x00) &&
(addr->s6_addr16[4] == 0x00) &&
(addr->s6_addr[10] == 0x00));
}

Expand Down
18 changes: 8 additions & 10 deletions subsys/net/ip/ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,10 @@ int net_ipv6_addr_generate_iid(struct net_if *iface,
int ret;

if (prefix == NULL) {
UNALIGNED_PUT(htonl(0xfe800000), &tmp_prefix.s6_addr32[0]);
tmp_prefix.s6_addr32[0] = htonl(0xfe800000);
} else {
UNALIGNED_PUT(UNALIGNED_GET(&prefix->s6_addr32[0]),
&tmp_prefix.s6_addr32[0]);
UNALIGNED_PUT(UNALIGNED_GET(&prefix->s6_addr32[1]),
&tmp_prefix.s6_addr32[1]);
tmp_prefix.s6_addr32[0] = prefix->s6_addr32[0];
tmp_prefix.s6_addr32[1] = prefix->s6_addr32[1];
}

ret = gen_stable_iid(if_index, &tmp_prefix, network_id, network_id_len,
Expand All @@ -992,11 +990,11 @@ int net_ipv6_addr_generate_iid(struct net_if *iface,
}

if (prefix == NULL) {
UNALIGNED_PUT(htonl(0xfe800000), &tmp_addr.s6_addr32[0]);
UNALIGNED_PUT(0, &tmp_addr.s6_addr32[1]);
tmp_addr.s6_addr32[0] = htonl(0xfe800000);
tmp_addr.s6_addr32[1] = 0;
} else {
UNALIGNED_PUT(UNALIGNED_GET(&prefix->s6_addr32[0]), &tmp_addr.s6_addr32[0]);
UNALIGNED_PUT(UNALIGNED_GET(&prefix->s6_addr32[1]), &tmp_addr.s6_addr32[1]);
tmp_addr.s6_addr32[0] = prefix->s6_addr32[0];
tmp_addr.s6_addr32[1] = prefix->s6_addr32[1];
}

if (IS_ENABLED(CONFIG_NET_IPV6_IID_EUI_64)) {
Expand All @@ -1006,7 +1004,7 @@ int net_ipv6_addr_generate_iid(struct net_if *iface,
* Universal/Local bit. RFC 6282 ch 3.2.2
*/
if (lladdr->type == NET_LINK_IEEE802154) {
UNALIGNED_PUT(0, &tmp_addr.s6_addr32[2]);
tmp_addr.s6_addr32[2] = 0;
tmp_addr.s6_addr[11] = 0xff;
tmp_addr.s6_addr[12] = 0xfe;
tmp_addr.s6_addr[13] = 0U;
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/ip/net_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ int net_context_bind(struct net_context *context, const struct sockaddr *addr,

ptr = &maddr->address.in_addr;

} else if (UNALIGNED_GET(&addr4->sin_addr.s_addr) == INADDR_ANY) {
} else if (addr4->sin_addr.s_addr == INADDR_ANY) {
if (iface == NULL) {
iface = net_if_ipv4_select_src_iface(
&net_sin(&context->remote)->sin_addr);
Expand Down
10 changes: 5 additions & 5 deletions subsys/net/ip/net_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -3623,7 +3623,7 @@ bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
continue;
}

subnet = UNALIGNED_GET(&addr->s_addr) &
subnet = addr->s_addr &
ipv4->unicast[i].netmask.s_addr;

if ((ipv4->unicast[i].ipv4.address.in_addr.s_addr &
Expand Down Expand Up @@ -3663,7 +3663,7 @@ static bool ipv4_is_broadcast_address(struct net_if *iface,
bcast.s_addr = ipv4->unicast[i].ipv4.address.in_addr.s_addr |
~ipv4->unicast[i].netmask.s_addr;

if (bcast.s_addr == UNALIGNED_GET(&addr->s_addr)) {
if (bcast.s_addr == addr->s_addr) {
ret = true;
goto out;
}
Expand Down Expand Up @@ -3991,7 +3991,7 @@ struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
continue;
}

if (UNALIGNED_GET(&addr->s4_addr32[0]) ==
if (addr->s4_addr32[0] ==
ipv4->unicast[i].ipv4.address.in_addr.s_addr) {

if (ret) {
Expand Down Expand Up @@ -4061,7 +4061,7 @@ struct in_addr net_if_ipv4_get_netmask_by_addr(struct net_if *iface,
continue;
}

subnet = UNALIGNED_GET(&addr->s_addr) &
subnet = addr->s_addr &
ipv4->unicast[i].netmask.s_addr;

if ((ipv4->unicast[i].ipv4.address.in_addr.s_addr &
Expand Down Expand Up @@ -4102,7 +4102,7 @@ bool net_if_ipv4_set_netmask_by_addr(struct net_if *iface,
continue;
}

subnet = UNALIGNED_GET(&addr->s_addr) &
subnet = addr->s_addr &
ipv4->unicast[i].netmask.s_addr;

if ((ipv4->unicast[i].ipv4.address.in_addr.s_addr &
Expand Down
Loading