diff --git a/include/net/yaip/net_if.h b/include/net/yaip/net_if.h index bbe0230b0ad..ea88ee44bdb 100644 --- a/include/net/yaip/net_if.h +++ b/include/net/yaip/net_if.h @@ -410,10 +410,12 @@ struct net_if *net_if_get_default(void); * @brief Check if this IPv6 address belongs to one of the interfaces. * * @param addr IPv6 address + * @param iface Pointer to interface is returned * * @return Pointer to interface address, NULL if not found. */ -struct net_if_addr *net_if_ipv6_addr_lookup(struct in6_addr *addr); +struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr, + struct net_if **iface); /** * @brief Check if this IPv6 address belongs to this specific interfaces. @@ -496,10 +498,12 @@ bool net_if_ipv6_maddr_rm(struct net_if *iface, struct in6_addr *addr); * @brief Check if this IPv6 multicast address belongs to one of the interfaces. * * @param addr IPv6 address + * @param iface Pointer to interface is returned * * @return Pointer to interface multicast address, NULL if not found. */ -struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(struct in6_addr *addr); +struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *addr, + struct net_if **iface); /** * @brief Check if this IPv6 prefix belongs to this interface @@ -723,10 +727,12 @@ static inline uint8_t net_if_ipv4_get_ttl(struct net_if *iface) * @brief Check if this IPv4 address belongs to one of the interfaces. * * @param addr IPv4 address + * @param iface Interface is returned * * @return Pointer to interface address, NULL if not found. */ -struct net_if_addr *net_if_ipv4_addr_lookup(struct in_addr *addr); +struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr, + struct net_if **iface); /** * @brief Add a IPv4 address to an interface diff --git a/include/net/yaip/net_ip.h b/include/net/yaip/net_ip.h index e5cdebf643e..799195b83f1 100644 --- a/include/net/yaip/net_ip.h +++ b/include/net/yaip/net_ip.h @@ -244,7 +244,10 @@ static inline bool net_is_ipv6_addr_mcast(struct in6_addr *addr) return addr->s6_addr[0] == 0xFF; } -extern struct net_if_addr *net_if_ipv6_addr_lookup(struct in6_addr *addr); +struct net_if; + +extern struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr, + struct net_if **iface); /** * @brief Check if IPv6 address is found in one of the network interfaces. @@ -255,10 +258,11 @@ extern struct net_if_addr *net_if_ipv6_addr_lookup(struct in6_addr *addr); */ static inline bool net_is_my_ipv6_addr(struct in6_addr *addr) { - return net_if_ipv6_addr_lookup(addr) != NULL; + return net_if_ipv6_addr_lookup(addr, NULL) != NULL; } -extern struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(struct in6_addr *addr); +extern struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *addr, + struct net_if **iface); /** * @brief Check if IPv6 multicast address is found in one of the @@ -270,7 +274,7 @@ extern struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(struct in6_addr *addr) */ static inline bool net_is_my_ipv6_maddr(struct in6_addr *maddr) { - return net_if_ipv6_maddr_lookup(maddr) != NULL; + return net_if_ipv6_maddr_lookup(maddr, NULL) != NULL; } /** @@ -302,7 +306,8 @@ static inline bool net_is_ipv6_prefix(uint8_t *addr1, uint8_t *addr2, (addr2[16 - bytes] & ((8 - remain) << 8))); } -extern struct net_if_addr *net_if_ipv4_addr_lookup(struct in_addr *addr); +extern struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr, + struct net_if **iface); /** * @brief Check if the IPv4 address is assigned to any network interface @@ -315,7 +320,7 @@ extern struct net_if_addr *net_if_ipv4_addr_lookup(struct in_addr *addr); */ static inline bool net_is_my_ipv4_addr(struct in_addr *addr) { - return net_if_ipv4_addr_lookup(addr) != NULL; + return net_if_ipv4_addr_lookup(addr, NULL) != NULL; } /** diff --git a/net/yaip/ipv6.c b/net/yaip/ipv6.c index afe932b4ab8..c01fc1907f4 100644 --- a/net/yaip/ipv6.c +++ b/net/yaip/ipv6.c @@ -1085,7 +1085,7 @@ static inline void handle_prefix_autonomous(struct net_buf *buf, net_ipv6_addr_create_iid(&addr, net_if_get_link_addr(net_nbuf_iface(buf))); - ifaddr = net_if_ipv6_addr_lookup(&addr); + ifaddr = net_if_ipv6_addr_lookup(&addr, NULL); if (ifaddr && ifaddr->addr_type == NET_ADDR_AUTOCONF) { if (prefix_info->valid_lifetime == NET_IPV6_ND_INFINITE_LIFETIME) { diff --git a/net/yaip/net_if.c b/net/yaip/net_if.c index 0c9a87fcbf2..ddfa6476033 100644 --- a/net/yaip/net_if.c +++ b/net/yaip/net_if.c @@ -190,7 +190,8 @@ void net_if_start_rs(struct net_if *iface) } #endif /* CONFIG_NET_IPV6_ND */ -struct net_if_addr *net_if_ipv6_addr_lookup(struct in6_addr *addr) +struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr, + struct net_if **ret) { struct net_if *iface; @@ -206,6 +207,11 @@ struct net_if_addr *net_if_ipv6_addr_lookup(struct in6_addr *addr) if (net_is_ipv6_prefix(addr->s6_addr, iface->ipv6.unicast[i].address.in6_addr.s6_addr, 128)) { + + if (ret) { + *ret = iface; + } + return &iface->ipv6.unicast[i]; } } @@ -339,7 +345,8 @@ bool net_if_ipv6_maddr_rm(struct net_if *iface, struct in6_addr *addr) return false; } -struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(struct in6_addr *maddr) +struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *maddr, + struct net_if **ret) { struct net_if *iface; @@ -355,6 +362,11 @@ struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(struct in6_addr *maddr) if (net_is_ipv6_prefix(maddr->s6_addr, iface->ipv6.mcast[i].address.in6_addr.s6_addr, 128)) { + + if (ret) { + *ret = iface; + } + return &iface->ipv6.mcast[i]; } } @@ -733,7 +745,8 @@ bool net_if_ipv4_addr_mask_cmp(struct net_if *iface, return false; } -struct net_if_addr *net_if_ipv4_addr_lookup(struct in_addr *addr) +struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr, + struct net_if **ret) { struct net_if *iface; @@ -748,6 +761,11 @@ struct net_if_addr *net_if_ipv4_addr_lookup(struct in_addr *addr) if (addr->s4_addr32[0] == iface->ipv4.unicast[i].address.in_addr.s_addr[0]) { + + if (ret) { + *ret = iface; + } + return &iface->ipv4.unicast[i]; } } diff --git a/tests/net/ip-addr/src/main.c b/tests/net/ip-addr/src/main.c index df232207183..52048b6cb7d 100644 --- a/tests/net/ip-addr/src/main.c +++ b/tests/net/ip-addr/src/main.c @@ -252,7 +252,7 @@ void main(void) return; } - ifaddr2 = net_if_ipv6_addr_lookup(&addr6); + ifaddr2 = net_if_ipv6_addr_lookup(&addr6, NULL); if (ifaddr1 != ifaddr2) { printk("IPv6 interface address mismatch\n"); return;