#ipv6 #ipv4 #ipv4-address #address-translation #ipv4-embedded-ipv6

rfc6052

Rust functions for interacting with RFC6052 IPv4-Embedded IPv6 Addresses

1 stable release

1.0.0 Feb 12, 2024

#11 in #ipv4-address


Used in protomask

GPL-3.0 license

16KB
208 lines

RFC6052 for Rust

Crates.io Docs.rs

RFC6052 defines "the algorithmic translation of an IPv6 address to a corresponding IPv4 address, and vice versa, using only statically configured information". In simpler terms, this means embedding IPv4 address into IPv6 addresses. The primary use case of which being NAT64 translators.

The RFC defines the following scheme for embedding IPv4 addresses into IPv6 addresses:

+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|PL| 0-------------32--40--48--56--64--72--80--88--96--104---------|
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|32|     prefix    |v4(32)         | u | suffix                    |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|40|     prefix        |v4(24)     | u |(8)| suffix                |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|48|     prefix            |v4(16) | u | (16)  | suffix            |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|56|     prefix                |(8)| u |  v4(24)   | suffix        |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|64|     prefix                    | u |   v4(32)      | suffix    |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|96|     prefix                                    |    v4(32)     |
+--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  • PL is the prefix length
  • u is a reserved byte that must be set to 0

Safe vs. Unsafe

This library provides both a "regular" and "unchecked" version of the functions for embedding and extracting IPv4 addresses from IPv6 addresses.

The "regular" functions enforce the restricted set of IPv6 prefix lengths allowed by the RFC (32, 40, 48, 56, 64, and 96 bits long). The "unchecked" functions do not enforce this restriction, and will happily accept any prefix length at the cost of non-compliance with the RFC.

Dependencies

~0.4–0.8MB
~19K SLoC