diff --git a/subsys/net/ip/ipv6.c b/subsys/net/ip/ipv6.c index 7ef758314060..3283712baf67 100644 --- a/subsys/net/ip/ipv6.c +++ b/subsys/net/ip/ipv6.c @@ -502,6 +502,11 @@ enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback) NET_DBG("DROP: invalid scope multicast packet"); goto drop; } + + if (net_ipv6_is_my_addr((struct in6_addr *)hdr->src)) { + NET_DBG("DROP: src addr is %s", "mine"); + goto drop; + } } /* Reconstruct TC field. */ diff --git a/tests/net/dhcpv6/src/main.c b/tests/net/dhcpv6/src/main.c index 80046c96ec96..02463d20a2bf 100644 --- a/tests/net/dhcpv6/src/main.c +++ b/tests/net/dhcpv6/src/main.c @@ -127,6 +127,7 @@ static struct net_pkt *test_dhcpv6_create_message( test_dhcpv6_options_fn_t set_options_fn) { struct in6_addr *local_addr; + struct in6_addr peer_addr; struct net_pkt *pkt; local_addr = net_if_ipv6_get_ll(iface, NET_ADDR_ANY_STATE); @@ -134,13 +135,20 @@ static struct net_pkt *test_dhcpv6_create_message( return NULL; } + /* Create a peer address from my address but invert the last byte + * so that the address is not the same. This is needed as we drop + * the packet if source address is our own address. + */ + memcpy(&peer_addr, local_addr, sizeof(peer_addr)); + peer_addr.s6_addr[15] = ~peer_addr.s6_addr[15]; + pkt = net_pkt_alloc_with_buffer(iface, TEST_MSG_SIZE, AF_INET6, IPPROTO_UDP, K_FOREVER); if (pkt == NULL) { return NULL; } - if (net_ipv6_create(pkt, local_addr, local_addr) < 0 || + if (net_ipv6_create(pkt, &peer_addr, local_addr) < 0 || net_udp_create(pkt, htons(DHCPV6_SERVER_PORT), htons(DHCPV6_CLIENT_PORT)) < 0) { goto fail; diff --git a/tests/net/ipv6/src/main.c b/tests/net/ipv6/src/main.c index 6d8a1a5deeea..ea734afae3e2 100644 --- a/tests/net/ipv6/src/main.c +++ b/tests/net/ipv6/src/main.c @@ -1293,7 +1293,7 @@ ZTEST(net_ipv6, test_src_localaddr_recv) struct in6_addr localaddr = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&localaddr, &addr); @@ -1306,7 +1306,7 @@ ZTEST(net_ipv6, test_dst_localaddr_recv) struct in6_addr localaddr = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&addr, &localaddr); @@ -1319,7 +1319,7 @@ ZTEST(net_ipv6, test_dst_iface_scope_mcast_recv) struct in6_addr mcast_iface = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&addr, &mcast_iface); @@ -1332,7 +1332,7 @@ ZTEST(net_ipv6, test_dst_zero_scope_mcast_recv) struct in6_addr mcast_zero = { { { 0xff, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&addr, &mcast_zero); @@ -1345,7 +1345,7 @@ ZTEST(net_ipv6, test_dst_site_scope_mcast_recv_drop) struct in6_addr mcast_site = { { { 0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&addr, &mcast_site); @@ -1432,7 +1432,7 @@ ZTEST(net_ipv6, test_dst_site_scope_mcast_recv_ok) struct in6_addr mcast_all_dhcp = { { { 0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0x03 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; struct net_context *ctx; @@ -1461,7 +1461,7 @@ ZTEST(net_ipv6, test_dst_org_scope_mcast_recv) struct in6_addr mcast_org = { { { 0xff, 0x08, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; enum net_verdict verdict; verdict = recv_msg(&addr, &mcast_org); @@ -1474,7 +1474,7 @@ ZTEST(net_ipv6, test_dst_iface_scope_mcast_send) struct in6_addr mcast_iface = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0, 0x10 } } }; struct net_if_mcast_addr *maddr; struct net_context *ctx; int ret; @@ -1516,7 +1516,7 @@ ZTEST(net_ipv6, test_dst_unknown_group_mcast_recv) }; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0x10 } } }; struct net_context *ctx; enum net_verdict verdict; @@ -1546,7 +1546,7 @@ ZTEST(net_ipv6, test_y_dst_unjoined_group_mcast_recv) }; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0x10 } } }; struct net_if_mcast_addr *maddr; struct net_context *ctx; enum net_verdict verdict; @@ -1592,7 +1592,7 @@ ZTEST(net_ipv6, test_dst_is_other_iface_mcast_recv) 0x08 } } }; struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0x1 } } }; + 0, 0, 0, 0, 0, 0x10 } } }; struct net_if *test_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)); struct net_if_mcast_addr *maddr; struct net_context *ctx; @@ -1633,7 +1633,7 @@ ZTEST(net_ipv6, test_no_nd_flag) { bool ret; struct in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0x99, 0x1 } } }; + 0, 0, 0, 0, 0, 0, 0x99, 0x10 } } }; struct net_if *iface = TEST_NET_IF; struct net_if_addr *ifaddr;