Skip to content

Commit f5455e6

Browse files
rlubosdanieldegrasse
authored andcommitted
net: ipv6: 6lo: Avoid casting unaligned address to struct in6_addr
Refactor local functions to work with byte buffers instead of struct in6_addr and use switch to use raw variants of functions operating on IPv6 addresses. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent 74ccd5f commit f5455e6

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

subsys/net/ip/6lo.c

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,42 @@ static int get_ihpc_inlined_size(uint16_t iphc)
119119
* Mesh header compression
120120
*/
121121

122-
static inline bool net_6lo_ll_prefix_padded_with_zeros(struct in6_addr *addr)
122+
static inline bool net_6lo_ll_prefix_padded_with_zeros(const uint8_t *addr)
123123
{
124-
return (net_ipv6_is_ll_addr(addr) &&
125-
(UNALIGNED_GET(&addr->s6_addr16[1]) == 0x00) &&
126-
(UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00));
124+
return (net_ipv6_is_ll_addr_raw(addr) &&
125+
(UNALIGNED_GET((uint16_t *)addr + 1) == 0x00) &&
126+
(UNALIGNED_GET((uint32_t *)addr + 1) == 0x00));
127127
}
128128

129-
static inline bool net_6lo_addr_16_bit_compressible(struct in6_addr *addr)
129+
static inline bool net_6lo_addr_16_bit_compressible(const uint8_t *addr)
130130
{
131-
return ((UNALIGNED_GET(&addr->s6_addr32[2]) == htonl(0xFFu)) &&
132-
(UNALIGNED_GET(&addr->s6_addr16[6]) == htons(0xFE00u)));
131+
return ((UNALIGNED_GET((uint32_t *)addr + 2) == htonl(0xFFu)) &&
132+
(UNALIGNED_GET((uint16_t *)addr + 6) == htons(0xFE00u)));
133133
}
134134

135-
static inline bool net_6lo_maddr_8_bit_compressible(struct in6_addr *addr)
135+
static inline bool net_6lo_maddr_8_bit_compressible(const uint8_t *addr)
136136
{
137-
return ((addr->s6_addr[1] == 0x02) &&
138-
(UNALIGNED_GET(&addr->s6_addr16[1]) == 0x00) &&
139-
(UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
140-
(UNALIGNED_GET(&addr->s6_addr32[2]) == 0x00) &&
141-
(addr->s6_addr[14] == 0x00));
137+
return ((addr[1] == 0x02) &&
138+
(UNALIGNED_GET((uint16_t *)addr + 1) == 0x00) &&
139+
(UNALIGNED_GET((uint32_t *)addr + 1) == 0x00) &&
140+
(UNALIGNED_GET((uint32_t *)addr + 2) == 0x00) &&
141+
(addr[14] == 0x00));
142+
142143
}
143144

144-
static inline bool net_6lo_maddr_32_bit_compressible(struct in6_addr *addr)
145+
static inline bool net_6lo_maddr_32_bit_compressible(const uint8_t *addr)
145146
{
146-
return ((UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
147-
(UNALIGNED_GET(&addr->s6_addr32[2]) == 0x00) &&
148-
(addr->s6_addr[12] == 0x00));
147+
return ((UNALIGNED_GET((uint32_t *)addr + 1) == 0x00) &&
148+
(UNALIGNED_GET((uint32_t *)addr + 2) == 0x00) &&
149+
(addr[12] == 0x00));
150+
149151
}
150152

151-
static inline bool net_6lo_maddr_48_bit_compressible(struct in6_addr *addr)
153+
static inline bool net_6lo_maddr_48_bit_compressible(const uint8_t *addr)
152154
{
153-
return ((UNALIGNED_GET(&addr->s6_addr32[1]) == 0x00) &&
154-
(UNALIGNED_GET(&addr->s6_addr16[4]) == 0x00) &&
155-
(addr->s6_addr[10] == 0x00));
155+
return ((UNALIGNED_GET((uint32_t *)addr + 1) == 0x00) &&
156+
(UNALIGNED_GET((uint16_t *)addr + 4) == 0x00) &&
157+
(addr[10] == 0x00));
156158
}
157159

158160
#if defined(CONFIG_NET_6LO_CONTEXT)
@@ -366,16 +368,15 @@ static uint8_t *compress_sa(struct net_ipv6_hdr *ipv6, struct net_pkt *pkt,
366368
uint8_t *inline_ptr, uint16_t *iphc)
367369
{
368370
/* Address is fully elided */
369-
if (net_ipv6_addr_based_on_ll((struct in6_addr *)ipv6->src,
370-
net_pkt_lladdr_src(pkt))) {
371+
if (net_ipv6_addr_based_on_ll_raw(ipv6->src, net_pkt_lladdr_src(pkt))) {
371372
NET_DBG("SAM_11 src address is fully elided");
372373

373374
*iphc |= NET_6LO_IPHC_SAM_11;
374375
return inline_ptr;
375376
}
376377

377378
/* Following 64 bits are 0000:00ff:fe00:XXXX */
378-
if (net_6lo_addr_16_bit_compressible((struct in6_addr *)ipv6->src)) {
379+
if (net_6lo_addr_16_bit_compressible(ipv6->src)) {
379380
NET_DBG("SAM_10 src addr 16 bit compressible");
380381
*iphc |= NET_6LO_IPHC_SAM_10;
381382

@@ -412,8 +413,7 @@ static uint8_t *compress_sa_ctx(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr,
412413
NET_DBG("SAC_1 src address context based");
413414
*iphc |= NET_6LO_IPHC_SAC_1;
414415

415-
if (net_ipv6_addr_based_on_ll((struct in6_addr *)ipv6->src,
416-
net_pkt_lladdr_src(pkt))) {
416+
if (net_ipv6_addr_based_on_ll_raw(ipv6->src, net_pkt_lladdr_src(pkt))) {
417417
NET_DBG("SAM_11 src address is fully elided");
418418

419419
/* Address is fully elided */
@@ -422,7 +422,7 @@ static uint8_t *compress_sa_ctx(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr,
422422
}
423423

424424
/* Following 64 bits are 0000:00ff:fe00:XXXX */
425-
if (net_6lo_addr_16_bit_compressible((struct in6_addr *)ipv6->src)) {
425+
if (net_6lo_addr_16_bit_compressible(ipv6->src)) {
426426
NET_DBG("SAM_10 src addr 16 bit compressible");
427427

428428
*iphc |= NET_6LO_IPHC_SAM_10;
@@ -452,7 +452,7 @@ static uint8_t *compress_da_mcast(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr
452452

453453
NET_DBG("M_1 dst is mcast");
454454

455-
if (net_6lo_maddr_8_bit_compressible((struct in6_addr *)ipv6->dst)) {
455+
if (net_6lo_maddr_8_bit_compressible(ipv6->dst)) {
456456
NET_DBG("DAM_11 dst maddr 8 bit compressible");
457457

458458
/* last byte */
@@ -464,7 +464,7 @@ static uint8_t *compress_da_mcast(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr
464464
return inline_ptr;
465465
}
466466

467-
if (net_6lo_maddr_32_bit_compressible((struct in6_addr *)ipv6->dst)) {
467+
if (net_6lo_maddr_32_bit_compressible(ipv6->dst)) {
468468
NET_DBG("DAM_10 4 bytes: 2nd byte + last three bytes");
469469

470470
/* 4 bytes: 2nd byte + last three bytes */
@@ -479,7 +479,7 @@ static uint8_t *compress_da_mcast(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr
479479
return inline_ptr;
480480
}
481481

482-
if (net_6lo_maddr_48_bit_compressible((struct in6_addr *)ipv6->dst)) {
482+
if (net_6lo_maddr_48_bit_compressible(ipv6->dst)) {
483483
NET_DBG("DAM_01 6 bytes: 2nd byte + last five bytes");
484484

485485
/* 6 bytes: 2nd byte + last five bytes */
@@ -507,16 +507,15 @@ static uint8_t *compress_da(struct net_ipv6_hdr *ipv6, struct net_pkt *pkt,
507507
uint8_t *inline_ptr, uint16_t *iphc)
508508
{
509509
/* Address is fully elided */
510-
if (net_ipv6_addr_based_on_ll((struct in6_addr *)ipv6->dst,
511-
net_pkt_lladdr_dst(pkt))) {
510+
if (net_ipv6_addr_based_on_ll_raw(ipv6->dst, net_pkt_lladdr_dst(pkt))) {
512511
NET_DBG("DAM_11 dst addr fully elided");
513512

514513
*iphc |= NET_6LO_IPHC_DAM_11;
515514
return inline_ptr;
516515
}
517516

518517
/* Following 64 bits are 0000:00ff:fe00:XXXX */
519-
if (net_6lo_addr_16_bit_compressible((struct in6_addr *)ipv6->dst)) {
518+
if (net_6lo_addr_16_bit_compressible(ipv6->dst)) {
520519
NET_DBG("DAM_10 dst addr 16 bit compressible");
521520

522521
*iphc |= NET_6LO_IPHC_DAM_10;
@@ -553,16 +552,15 @@ static uint8_t *compress_da_ctx(struct net_ipv6_hdr *ipv6, uint8_t *inline_ptr,
553552
{
554553
*iphc |= NET_6LO_IPHC_DAC_1;
555554

556-
if (net_ipv6_addr_based_on_ll((struct in6_addr *)ipv6->dst,
557-
net_pkt_lladdr_dst(pkt))) {
555+
if (net_ipv6_addr_based_on_ll_raw(ipv6->dst, net_pkt_lladdr_dst(pkt))) {
558556
NET_DBG("DAM_11 dst addr fully elided");
559557

560558
*iphc |= NET_6LO_IPHC_DAM_11;
561559
return inline_ptr;
562560
}
563561

564562
/* Following 64 bits are 0000:00ff:fe00:XXXX */
565-
if (net_6lo_addr_16_bit_compressible((struct in6_addr *)ipv6->dst)) {
563+
if (net_6lo_addr_16_bit_compressible(ipv6->dst)) {
566564
NET_DBG("DAM_10 dst addr 16 bit compressible");
567565

568566
*iphc |= NET_6LO_IPHC_DAM_10;
@@ -753,12 +751,12 @@ static inline int compress_IPHC_header(struct net_pkt *pkt)
753751
inline_pos = compress_nh_udp(udp, inline_pos, false);
754752
}
755753

756-
if (net_6lo_ll_prefix_padded_with_zeros((struct in6_addr *)ipv6->dst)) {
754+
if (net_6lo_ll_prefix_padded_with_zeros(ipv6->dst)) {
757755
inline_pos = compress_da(ipv6, pkt, inline_pos, &iphc);
758756
goto da_end;
759757
}
760758

761-
if (net_ipv6_is_addr_mcast((struct in6_addr *)ipv6->dst)) {
759+
if (net_ipv6_is_addr_mcast_raw(ipv6->dst)) {
762760
inline_pos = compress_da_mcast(ipv6, inline_pos, &iphc);
763761
goto da_end;
764762
}
@@ -775,7 +773,7 @@ static inline int compress_IPHC_header(struct net_pkt *pkt)
775773
inline_pos = set_da_inline(ipv6, inline_pos, &iphc);
776774
da_end:
777775

778-
if (net_6lo_ll_prefix_padded_with_zeros((struct in6_addr *)ipv6->src)) {
776+
if (net_6lo_ll_prefix_padded_with_zeros(ipv6->src)) {
779777
inline_pos = compress_sa(ipv6, pkt, inline_pos, &iphc);
780778
goto sa_end;
781779
}

0 commit comments

Comments
 (0)