Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v3.5-branch] net: ipv6: Check that received src address is not mine #67177

Merged
merged 3 commits into from
Jan 9, 2024
Merged
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
5 changes: 5 additions & 0 deletions subsys/net/ip/ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
10 changes: 9 additions & 1 deletion tests/net/dhcpv6/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,28 @@ 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);
if (local_addr == NULL) {
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;
Expand Down
24 changes: 12 additions & 12 deletions tests/net/ipv6/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down