From bc19aeca5f6c695ad3db0196057d155e4f64584e Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 6 Sep 2022 13:02:17 +0100 Subject: [ipv6] Fix mask calculation when prefix length is not a multiple of 8 Signed-off-by: Michael Brown --- src/net/ipv6.c | 2 +- src/tests/ipv6_test.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/net/ipv6.c b/src/net/ipv6.c index 4b2c33e..901203c 100644 --- a/src/net/ipv6.c +++ b/src/net/ipv6.c @@ -251,7 +251,7 @@ int ipv6_add_miniroute ( struct net_device *netdev, struct in6_addr *address, *prefix_mask = 0xff; } if ( remaining ) - *prefix_mask <<= ( 8 - remaining ); + *prefix_mask = ( 0xff << ( 8 - remaining ) ); } /* Add to start of routing table */ diff --git a/src/tests/ipv6_test.c b/src/tests/ipv6_test.c index 0a8467d..de8edc8 100644 --- a/src/tests/ipv6_test.c +++ b/src/tests/ipv6_test.c @@ -131,9 +131,21 @@ static struct net_device ipv6_test_netdev = { .state = NETDEV_OPEN, }; +/** /48 prefix */ +PREFIX ( prefix48, 48, "ffff:ffff:ffff::" ); + /** /64 prefix */ PREFIX ( prefix64, 64, "ffff:ffff:ffff:ffff::" ); +/** /126 prefix */ +PREFIX ( prefix126, 126, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc" ); + +/** /127 prefix */ +PREFIX ( prefix127, 127, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe" ); + +/** /128 prefix */ +PREFIX ( prefix128, 128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" ); + /** Routing table with only a link-local address */ TABLE ( table_link_local, { "fe80::69ff:fe50:5845", &prefix64, NULL } ); @@ -152,6 +164,13 @@ TABLE ( table_multi, { "fd44:9112:6442::69ff:fe50:5845", &prefix64, "fe80::1" }, { "fd70:6ba9:50ae::69ff:fe50:5845", &prefix64, "fe80::3" } ); +/** Routing table with unusual prefix lengths */ +TABLE ( table_unusual, + { "2001:db8:1::1", &prefix48, "fe80::1" }, + { "2001:db8:2::1", &prefix126, NULL }, + { "2001:db8:3::1", &prefix127, NULL }, + { "2001:db8:4::1", &prefix128, NULL } ); + /** * Report an inet6_ntoa() test result * @@ -502,6 +521,7 @@ static void ipv6_test_exec ( void ) { ipv6_table_ok ( &table_link_local ); ipv6_table_ok ( &table_normal ); ipv6_table_ok ( &table_multi ); + ipv6_table_ok ( &table_unusual ); /* Routing table with only a link-local address */ ipv6_route_ok ( &table_link_local, "fe80::1", @@ -545,10 +565,27 @@ static void ipv6_test_exec ( void ) { ipv6_route_ok ( &table_multi, "ff02::1", "fe80::69ff:fe50:5845", NULL ); + /* Routing table with unusual prefix lengths */ + ipv6_route_ok ( &table_unusual, "2001:db8:2::1", + "2001:db8:2::1", NULL ); + ipv6_route_ok ( &table_unusual, "2001:db8:2::3", + "2001:db8:2::1", NULL ); + ipv6_route_ok ( &table_unusual, "2001:db8:3::1", + "2001:db8:3::1", NULL ); + ipv6_route_ok ( &table_unusual, "2001:db8:3::2", + "2001:db8:1::1", "fe80::1" ); + ipv6_route_ok ( &table_unusual, "2001:db8:4::1", + "2001:db8:4::1", NULL ); + ipv6_route_ok ( &table_unusual, "2001:db8:4::0", + "2001:db8:1::1", "fe80::1" ); + ipv6_route_ok ( &table_unusual, "2001:db8:4::2", + "2001:db8:1::1", "fe80::1" ); + /* Destroy test routing tables */ ipv6_table_del ( &table_link_local ); ipv6_table_del ( &table_normal ); ipv6_table_del ( &table_multi ); + ipv6_table_del ( &table_unusual ); } /** IPv6 self-test */ -- cgit v1.1