mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Export ipv6 address helpers
This is useful for Fuchsia. PiperOrigin-RevId: 214619681 Change-Id: If5a60dd82365c2eae51a12bbc819e5aae8c76ee9
This commit is contained in:
committed by
Shentubot
parent
d489336784
commit
bee264f0c5
@@ -202,3 +202,35 @@ func IsV6MulticastAddress(addr tcpip.Address) bool {
|
||||
}
|
||||
return addr[0] == 0xff
|
||||
}
|
||||
|
||||
// SolicitedNodeAddr computes the solicited-node multicast address. This is
|
||||
// used for NDP. Described in RFC 4291. The argument must be a full-length IPv6
|
||||
// address.
|
||||
func SolicitedNodeAddr(addr tcpip.Address) tcpip.Address {
|
||||
const solicitedNodeMulticastPrefix = "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff"
|
||||
return solicitedNodeMulticastPrefix + addr[len(addr)-3:]
|
||||
}
|
||||
|
||||
// LinkLocalAddr computes the default IPv6 link-local address from a link-layer
|
||||
// (MAC) address.
|
||||
func LinkLocalAddr(linkAddr tcpip.LinkAddress) tcpip.Address {
|
||||
// Convert a 48-bit MAC to an EUI-64 and then prepend the link-local
|
||||
// header, FE80::.
|
||||
//
|
||||
// The conversion is very nearly:
|
||||
// aa:bb:cc:dd:ee:ff => FE80::Aabb:ccFF:FEdd:eeff
|
||||
// Note the capital A. The conversion aa->Aa involves a bit flip.
|
||||
lladdrb := [16]byte{
|
||||
0: 0xFE,
|
||||
1: 0x80,
|
||||
8: linkAddr[0] ^ 2,
|
||||
9: linkAddr[1],
|
||||
10: linkAddr[2],
|
||||
11: 0xFF,
|
||||
12: 0xFE,
|
||||
13: linkAddr[3],
|
||||
14: linkAddr[4],
|
||||
15: linkAddr[5],
|
||||
}
|
||||
return tcpip.Address(lladdrb[:])
|
||||
}
|
||||
|
||||
@@ -153,13 +153,6 @@ const (
|
||||
icmpV6LengthOffset = 25
|
||||
)
|
||||
|
||||
// solicitedNodeAddr computes the solicited-node multicast address.
|
||||
// This is used for NDP. Described in RFC 4291.
|
||||
func solicitedNodeAddr(addr tcpip.Address) tcpip.Address {
|
||||
const solicitedNodeMulticastPrefix = "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff"
|
||||
return solicitedNodeMulticastPrefix + addr[len(addr)-3:]
|
||||
}
|
||||
|
||||
var broadcastMAC = tcpip.LinkAddress([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
|
||||
|
||||
var _ stack.LinkAddressResolver = (*protocol)(nil)
|
||||
@@ -171,7 +164,7 @@ func (*protocol) LinkAddressProtocol() tcpip.NetworkProtocolNumber {
|
||||
|
||||
// LinkAddressRequest implements stack.LinkAddressResolver.
|
||||
func (*protocol) LinkAddressRequest(addr, localAddr tcpip.Address, linkEP stack.LinkEndpoint) *tcpip.Error {
|
||||
snaddr := solicitedNodeAddr(addr)
|
||||
snaddr := header.SolicitedNodeAddr(addr)
|
||||
r := &stack.Route{
|
||||
LocalAddress: localAddr,
|
||||
RemoteAddress: snaddr,
|
||||
|
||||
@@ -36,33 +36,9 @@ const (
|
||||
linkAddr1 = tcpip.LinkAddress("\x0a\x0b\x0c\x0d\x0e\x0f")
|
||||
)
|
||||
|
||||
// linkLocalAddr computes the default IPv6 link-local address from
|
||||
// a link-layer (MAC) address.
|
||||
func linkLocalAddr(linkAddr tcpip.LinkAddress) tcpip.Address {
|
||||
// Convert a 48-bit MAC to an EUI-64 and then prepend the
|
||||
// link-local header, FE80::.
|
||||
//
|
||||
// The conversion is very nearly:
|
||||
// aa:bb:cc:dd:ee:ff => FE80::Aabb:ccFF:FEdd:eeff
|
||||
// Note the capital A. The conversion aa->Aa involves a bit flip.
|
||||
lladdrb := [16]byte{
|
||||
0: 0xFE,
|
||||
1: 0x80,
|
||||
8: linkAddr[0] ^ 2,
|
||||
9: linkAddr[1],
|
||||
10: linkAddr[2],
|
||||
11: 0xFF,
|
||||
12: 0xFE,
|
||||
13: linkAddr[3],
|
||||
14: linkAddr[4],
|
||||
15: linkAddr[5],
|
||||
}
|
||||
return tcpip.Address(lladdrb[:])
|
||||
}
|
||||
|
||||
var (
|
||||
lladdr0 = linkLocalAddr(linkAddr0)
|
||||
lladdr1 = linkLocalAddr(linkAddr1)
|
||||
lladdr0 = header.LinkLocalAddr(linkAddr0)
|
||||
lladdr1 = header.LinkLocalAddr(linkAddr1)
|
||||
)
|
||||
|
||||
type testContext struct {
|
||||
@@ -106,7 +82,7 @@ func newTestContext(t *testing.T) *testContext {
|
||||
if err := c.s0.AddAddress(1, ProtocolNumber, lladdr0); err != nil {
|
||||
t.Fatalf("AddAddress lladdr0: %v", err)
|
||||
}
|
||||
if err := c.s0.AddAddress(1, ProtocolNumber, solicitedNodeAddr(lladdr0)); err != nil {
|
||||
if err := c.s0.AddAddress(1, ProtocolNumber, header.SolicitedNodeAddr(lladdr0)); err != nil {
|
||||
t.Fatalf("AddAddress sn lladdr0: %v", err)
|
||||
}
|
||||
|
||||
@@ -120,7 +96,7 @@ func newTestContext(t *testing.T) *testContext {
|
||||
if err := c.s1.AddAddress(1, ProtocolNumber, lladdr1); err != nil {
|
||||
t.Fatalf("AddAddress lladdr1: %v", err)
|
||||
}
|
||||
if err := c.s1.AddAddress(1, ProtocolNumber, solicitedNodeAddr(lladdr1)); err != nil {
|
||||
if err := c.s1.AddAddress(1, ProtocolNumber, header.SolicitedNodeAddr(lladdr1)); err != nil {
|
||||
t.Fatalf("AddAddress sn lladdr1: %v", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user