Skip to content

Commit

Permalink
Make is_global/is_unicast_global special address handling complete
Browse files Browse the repository at this point in the history
IANA explicitly documents 192.0.0.9/32, 192.0.0.9/32 and 2001:30::/28 as
globally reachable[1][2] and the is_global implementations declare
following IANA so let's make this happen.

In case of 2002::/16 IANA says N/A so I think it's safe to say we
shouldn't return true from is_global and is_unicast_global either for
addresses in that block.

[1] https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
[2] https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
  • Loading branch information
jstasiak committed Dec 16, 2023
1 parent a96d57b commit d845f61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 13 additions & 2 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,11 @@ impl Ipv4Addr {
|| self.is_loopback()
|| self.is_link_local()
// addresses reserved for future protocols (`192.0.0.0/24`)
||(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
// .9 and .10 are documented as globally reachable so they're excluded
|| (
self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
&& self.octets()[3] != 9 && self.octets()[3] != 10
)
|| self.is_documentation()
|| self.is_benchmarking()
|| self.is_reserved()
Expand Down Expand Up @@ -1515,8 +1519,12 @@ impl Ipv6Addr {
// AS112-v6 (`2001:4:112::/48`)
|| matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
// ORCHIDv2 (`2001:20::/28`)
|| matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x2F)
// Drone Remote ID Protocol Entity Tags (DETs) Prefix (`2001:30::/28`)`
|| matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F)
))
// 6to4 (`2002::/16`) – it's not explicitly documented as globally reachable,
// IANA says N/A.
|| matches!(self.segments(), [0x2002, _, _, _, _, _, _, _])
|| self.is_documentation()
|| self.is_unique_local()
|| self.is_unicast_link_local())
Expand Down Expand Up @@ -1684,6 +1692,7 @@ impl Ipv6Addr {
/// - unique local addresses
/// - the unspecified address
/// - the address range reserved for documentation
/// - 6to4 addresses
///
/// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7]
///
Expand Down Expand Up @@ -1717,6 +1726,8 @@ impl Ipv6Addr {
&& !self.is_unspecified()
&& !self.is_documentation()
&& !self.is_benchmarking()
// 6to4 (`2002::/16`), not documented as globally reachable by IANA
&& self.segments()[0] != 0x2002
}

/// Returns the address's multicast scope if the address is multicast.
Expand Down
11 changes: 10 additions & 1 deletion library/core/tests/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ fn ipv4_properties() {
check!("198.18.54.2", benchmarking);
check!("198.19.255.255", benchmarking);
check!("192.0.0.0");
check!("192.0.0.8");
check!("192.0.0.9", global);
check!("192.0.0.10", global);
check!("192.0.0.255");
check!("192.0.0.100");
check!("240.0.0.0", reserved);
Expand All @@ -480,6 +483,10 @@ fn ipv6_properties() {
}

macro_rules! check {
($s:expr, &[$($octet:expr),*]) => {
check!($s, &[$($octet),*], 0);
};

($s:expr, &[$($octet:expr),*], $mask:expr) => {
assert_eq!($s, ip!($s).to_string());
let octets = &[$($octet),*];
Expand Down Expand Up @@ -657,14 +664,16 @@ fn ipv6_properties() {
global | unicast_global
);

check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unicast_global);
check!("2001:30::", &[0x20, 1, 0, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], global | unicast_global);

check!(
"2001:200::",
&[0x20, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
global | unicast_global
);

check!("2002::", &[0x20, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

check!("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], unique_local);

check!(
Expand Down

0 comments on commit d845f61

Please sign in to comment.