mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Use tcpip.Subnet in tcpip.Route
This is the first step in replacing some of the redundant types with the standard library equivalents. PiperOrigin-RevId: 264706552
This commit is contained in:
committed by
gVisor bot
parent
7e79ca0225
commit
573e6e4bba
@@ -20,7 +20,6 @@ import (
|
||||
"gvisor.dev/gvisor/pkg/sentry/inet"
|
||||
"gvisor.dev/gvisor/pkg/sentry/socket/netfilter"
|
||||
"gvisor.dev/gvisor/pkg/syserr"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/iptables"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
|
||||
@@ -154,7 +153,7 @@ func (s *Stack) RouteTable() []inet.Route {
|
||||
|
||||
for _, rt := range s.Stack.GetRouteTable() {
|
||||
var family uint8
|
||||
switch len(rt.Destination) {
|
||||
switch len(rt.Destination.ID()) {
|
||||
case header.IPv4AddressSize:
|
||||
family = linux.AF_INET
|
||||
case header.IPv6AddressSize:
|
||||
@@ -164,14 +163,9 @@ func (s *Stack) RouteTable() []inet.Route {
|
||||
continue
|
||||
}
|
||||
|
||||
dstSubnet, err := tcpip.NewSubnet(rt.Destination, rt.Mask)
|
||||
if err != nil {
|
||||
log.Warningf("Invalid destination & mask in route: %s(%s): %v", rt.Destination, rt.Mask, err)
|
||||
continue
|
||||
}
|
||||
routeTable = append(routeTable, inet.Route{
|
||||
Family: family,
|
||||
DstLen: uint8(dstSubnet.Prefix()), // The CIDR prefix for the destination.
|
||||
DstLen: uint8(rt.Destination.Prefix()), // The CIDR prefix for the destination.
|
||||
|
||||
// Always return unspecified protocol since we have no notion of
|
||||
// protocol for routes.
|
||||
@@ -182,7 +176,7 @@ func (s *Stack) RouteTable() []inet.Route {
|
||||
Scope: linux.RT_SCOPE_LINK,
|
||||
Type: linux.RTN_UNICAST,
|
||||
|
||||
DstAddr: []byte(rt.Destination),
|
||||
DstAddr: []byte(rt.Destination.ID()),
|
||||
OutputInterface: int32(rt.NIC),
|
||||
GatewayAddr: []byte(rt.Gateway),
|
||||
})
|
||||
|
||||
@@ -24,6 +24,7 @@ go_test(
|
||||
embed = [":gonet"],
|
||||
deps = [
|
||||
"//pkg/tcpip",
|
||||
"//pkg/tcpip/header",
|
||||
"//pkg/tcpip/link/loopback",
|
||||
"//pkg/tcpip/network/ipv4",
|
||||
"//pkg/tcpip/network/ipv6",
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
"golang.org/x/net/nettest"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/loopback"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
|
||||
@@ -69,17 +70,13 @@ func newLoopbackStack() (*stack.Stack, *tcpip.Error) {
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
// IPv4
|
||||
{
|
||||
Destination: tcpip.Address(strings.Repeat("\x00", 4)),
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\x00", 4)),
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: NICID,
|
||||
},
|
||||
|
||||
// IPv6
|
||||
{
|
||||
Destination: tcpip.Address(strings.Repeat("\x00", 16)),
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\x00", 16)),
|
||||
Gateway: "",
|
||||
Destination: header.IPv6EmptySubnet,
|
||||
NIC: NICID,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -111,6 +111,15 @@ const (
|
||||
IPv4FlagDontFragment
|
||||
)
|
||||
|
||||
// IPv4EmptySubnet is the empty IPv4 subnet.
|
||||
var IPv4EmptySubnet = func() tcpip.Subnet {
|
||||
subnet, err := tcpip.NewSubnet(IPv4Any, tcpip.AddressMask(IPv4Any))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return subnet
|
||||
}()
|
||||
|
||||
// IPVersion returns the version of IP used in the given packet. It returns -1
|
||||
// if the packet is not large enough to contain the version field.
|
||||
func IPVersion(b []byte) int {
|
||||
|
||||
@@ -82,6 +82,15 @@ const (
|
||||
IPv6Any tcpip.Address = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||
)
|
||||
|
||||
// IPv6EmptySubnet is the empty IPv6 subnet.
|
||||
var IPv6EmptySubnet = func() tcpip.Subnet {
|
||||
subnet, err := tcpip.NewSubnet(IPv6Any, tcpip.AddressMask(IPv6Any))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return subnet
|
||||
}()
|
||||
|
||||
// PayloadLength returns the value of the "payload length" field of the ipv6
|
||||
// header.
|
||||
func (b IPv6) PayloadLength() uint16 {
|
||||
|
||||
@@ -66,9 +66,7 @@ func newTestContext(t *testing.T) *testContext {
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: "\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: 1,
|
||||
}})
|
||||
|
||||
|
||||
@@ -173,8 +173,7 @@ func buildIPv4Route(local, remote tcpip.Address) (stack.Route, *tcpip.Error) {
|
||||
s.CreateNIC(1, loopback.New())
|
||||
s.AddAddress(1, ipv4.ProtocolNumber, local)
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: ipv4SubnetAddr,
|
||||
Mask: ipv4SubnetMask,
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
Gateway: ipv4Gateway,
|
||||
NIC: 1,
|
||||
}})
|
||||
@@ -187,8 +186,7 @@ func buildIPv6Route(local, remote tcpip.Address) (stack.Route, *tcpip.Error) {
|
||||
s.CreateNIC(1, loopback.New())
|
||||
s.AddAddress(1, ipv6.ProtocolNumber, local)
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: ipv6SubnetAddr,
|
||||
Mask: ipv6SubnetMask,
|
||||
Destination: header.IPv6EmptySubnet,
|
||||
Gateway: ipv6Gateway,
|
||||
NIC: 1,
|
||||
}})
|
||||
|
||||
@@ -52,9 +52,7 @@ func TestExcludeBroadcast(t *testing.T) {
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: "\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: 1,
|
||||
}})
|
||||
|
||||
@@ -247,14 +245,22 @@ func buildContext(t *testing.T, packetCollectorErrors []*tcpip.Error, mtu uint32
|
||||
_, linkEP := newErrorChannel(100 /* Enough for all tests. */, mtu, "", packetCollectorErrors)
|
||||
linkEPId := stack.RegisterLinkEndpoint(linkEP)
|
||||
s.CreateNIC(1, linkEPId)
|
||||
s.AddAddress(1, ipv4.ProtocolNumber, "\x10\x00\x00\x01")
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: "\x10\x00\x00\x02",
|
||||
Mask: "\xff\xff\xff\xff",
|
||||
Gateway: "",
|
||||
NIC: 1,
|
||||
}})
|
||||
r, err := s.FindRoute(0, "\x10\x00\x00\x01", "\x10\x00\x00\x02", ipv4.ProtocolNumber, false /* multicastLoop */)
|
||||
const (
|
||||
src = "\x10\x00\x00\x01"
|
||||
dst = "\x10\x00\x00\x02"
|
||||
)
|
||||
s.AddAddress(1, ipv4.ProtocolNumber, src)
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet(dst, tcpip.AddressMask(header.IPv4Broadcast))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
Destination: subnet,
|
||||
NIC: 1,
|
||||
}})
|
||||
}
|
||||
r, err := s.FindRoute(0, src, dst, ipv4.ProtocolNumber, false /* multicastLoop */)
|
||||
if err != nil {
|
||||
t.Fatalf("s.FindRoute got %v, want %v", err, nil)
|
||||
}
|
||||
|
||||
@@ -91,13 +91,18 @@ func TestICMPCounts(t *testing.T) {
|
||||
t.Fatalf("AddAddress(_, %d, %s) = %s", ProtocolNumber, lladdr0, err)
|
||||
}
|
||||
}
|
||||
s.SetRouteTable(
|
||||
[]tcpip.Route{{
|
||||
Destination: lladdr1,
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\xff", 16)),
|
||||
NIC: 1,
|
||||
}},
|
||||
)
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet(lladdr1, tcpip.AddressMask(strings.Repeat("\xff", len(lladdr1))))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable(
|
||||
[]tcpip.Route{{
|
||||
Destination: subnet,
|
||||
NIC: 1,
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
netProto := s.NetworkProtocolInstance(ProtocolNumber)
|
||||
if netProto == nil {
|
||||
@@ -237,17 +242,23 @@ func newTestContext(t *testing.T) *testContext {
|
||||
t.Fatalf("AddAddress sn lladdr1: %v", err)
|
||||
}
|
||||
|
||||
subnet0, err := tcpip.NewSubnet(lladdr1, tcpip.AddressMask(strings.Repeat("\xff", len(lladdr1))))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.s0.SetRouteTable(
|
||||
[]tcpip.Route{{
|
||||
Destination: lladdr1,
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\xff", 16)),
|
||||
Destination: subnet0,
|
||||
NIC: 1,
|
||||
}},
|
||||
)
|
||||
subnet1, err := tcpip.NewSubnet(lladdr0, tcpip.AddressMask(strings.Repeat("\xff", len(lladdr0))))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.s1.SetRouteTable(
|
||||
[]tcpip.Route{{
|
||||
Destination: lladdr0,
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\xff", 16)),
|
||||
Destination: subnet1,
|
||||
NIC: 1,
|
||||
}},
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@ go_binary(
|
||||
deps = [
|
||||
"//pkg/tcpip",
|
||||
"//pkg/tcpip/buffer",
|
||||
"//pkg/tcpip/header",
|
||||
"//pkg/tcpip/link/fdbased",
|
||||
"//pkg/tcpip/link/rawfile",
|
||||
"//pkg/tcpip/link/sniffer",
|
||||
|
||||
@@ -52,6 +52,7 @@ import (
|
||||
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/buffer"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
|
||||
@@ -152,9 +153,7 @@ func main() {
|
||||
// Add default route.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{
|
||||
Destination: "\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: 1,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -149,12 +149,15 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
subnet, err := tcpip.NewSubnet(tcpip.Address(strings.Repeat("\x00", len(addr))), tcpip.AddressMask(strings.Repeat("\x00", len(addr))))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Add default route.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{
|
||||
Destination: tcpip.Address(strings.Repeat("\x00", len(addr))),
|
||||
Mask: tcpip.AddressMask(strings.Repeat("\x00", len(addr))),
|
||||
Gateway: "",
|
||||
Destination: subnet,
|
||||
NIC: 1,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -895,7 +895,7 @@ func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address, n
|
||||
}
|
||||
} else {
|
||||
for _, route := range s.routeTable {
|
||||
if (id != 0 && id != route.NIC) || (len(remoteAddr) != 0 && !route.Match(remoteAddr)) {
|
||||
if (id != 0 && id != route.NIC) || (len(remoteAddr) != 0 && !isBroadcast && !route.Destination.Contains(remoteAddr)) {
|
||||
continue
|
||||
}
|
||||
if nic, ok := s.nics[route.NIC]; ok {
|
||||
|
||||
@@ -312,7 +312,13 @@ func TestNetworkSend(t *testing.T) {
|
||||
t.Fatal("NewNIC failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{"\x00", "\x00", "\x00", 1}})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
if err := s.AddAddress(1, fakeNetNumber, "\x01"); err != nil {
|
||||
t.Fatal("AddAddress failed:", err)
|
||||
@@ -360,10 +366,20 @@ func TestNetworkSendMultiRoute(t *testing.T) {
|
||||
// Set a route table that sends all packets with odd destination
|
||||
// addresses through the first NIC, and all even destination address
|
||||
// through the second one.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x01", "\x01", "\x00", 1},
|
||||
{"\x00", "\x01", "\x00", 2},
|
||||
})
|
||||
{
|
||||
subnet0, err := tcpip.NewSubnet("\x00", "\x01")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subnet1, err := tcpip.NewSubnet("\x01", "\x01")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{Destination: subnet1, Gateway: "\x00", NIC: 1},
|
||||
{Destination: subnet0, Gateway: "\x00", NIC: 2},
|
||||
})
|
||||
}
|
||||
|
||||
// Send a packet to an odd destination.
|
||||
sendTo(t, s, "\x05", nil)
|
||||
@@ -439,10 +455,20 @@ func TestRoutes(t *testing.T) {
|
||||
// Set a route table that sends all packets with odd destination
|
||||
// addresses through the first NIC, and all even destination address
|
||||
// through the second one.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x01", "\x01", "\x00", 1},
|
||||
{"\x00", "\x01", "\x00", 2},
|
||||
})
|
||||
{
|
||||
subnet0, err := tcpip.NewSubnet("\x00", "\x01")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subnet1, err := tcpip.NewSubnet("\x01", "\x01")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{Destination: subnet1, Gateway: "\x00", NIC: 1},
|
||||
{Destination: subnet0, Gateway: "\x00", NIC: 2},
|
||||
})
|
||||
}
|
||||
|
||||
// Test routes to odd address.
|
||||
testRoute(t, s, 0, "", "\x05", "\x01")
|
||||
@@ -524,9 +550,13 @@ func TestDelayedRemovalDueToRoute(t *testing.T) {
|
||||
t.Fatal("AddAddress failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
fakeNet := s.NetworkProtocolInstance(fakeNetNumber).(*fakeNetworkProtocol)
|
||||
|
||||
@@ -583,9 +613,13 @@ func TestPromiscuousMode(t *testing.T) {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
fakeNet := s.NetworkProtocolInstance(fakeNetNumber).(*fakeNetworkProtocol)
|
||||
|
||||
@@ -643,9 +677,13 @@ func TestAddressSpoofing(t *testing.T) {
|
||||
t.Fatal("AddAddress failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
// With address spoofing disabled, FindRoute does not permit an address
|
||||
// that was not added to the NIC to be used as the source.
|
||||
@@ -806,9 +844,13 @@ func TestSubnetAcceptsMatchingPacket(t *testing.T) {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
fakeNet := s.NetworkProtocolInstance(fakeNetNumber).(*fakeNetworkProtocol)
|
||||
|
||||
@@ -840,9 +882,13 @@ func TestCheckLocalAddressForSubnet(t *testing.T) {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", nicID}, // default route
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: nicID}})
|
||||
}
|
||||
|
||||
subnet, err := tcpip.NewSubnet(tcpip.Address("\xa0"), tcpip.AddressMask("\xf0"))
|
||||
|
||||
@@ -854,7 +900,7 @@ func TestCheckLocalAddressForSubnet(t *testing.T) {
|
||||
}
|
||||
|
||||
// Loop over all subnet addresses and check them.
|
||||
numOfAddresses := (1 << uint((8 - subnet.Prefix())))
|
||||
numOfAddresses := 1 << uint(8-subnet.Prefix())
|
||||
if numOfAddresses < 1 || numOfAddresses > 255 {
|
||||
t.Fatalf("got numOfAddresses = %d, want = [1 .. 255] (subnet=%s)", numOfAddresses, subnet)
|
||||
}
|
||||
@@ -881,9 +927,13 @@ func TestSubnetRejectsNonmatchingPacket(t *testing.T) {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x00", "\x00", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
fakeNet := s.NetworkProtocolInstance(fakeNetNumber).(*fakeNetworkProtocol)
|
||||
|
||||
@@ -1261,9 +1311,13 @@ func TestNICStats(t *testing.T) {
|
||||
t.Fatal("AddAddress failed:", err)
|
||||
}
|
||||
// Route all packets for address \x01 to NIC 1.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x01", "\xff", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x01", "\xff")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
// Send a packet to address 1.
|
||||
buf := buffer.NewView(30)
|
||||
@@ -1312,9 +1366,13 @@ func TestNICForwarding(t *testing.T) {
|
||||
}
|
||||
|
||||
// Route all packets to address 3 to NIC 2.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x03", "\xff", "\x00", 2},
|
||||
})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x03", "\xff")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 2}})
|
||||
}
|
||||
|
||||
// Send a packet to address 3.
|
||||
buf := buffer.NewView(30)
|
||||
|
||||
@@ -284,7 +284,13 @@ func TestTransportReceive(t *testing.T) {
|
||||
t.Fatalf("CreateNIC failed: %v", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{"\x00", "\x00", "\x00", 1}})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
if err := s.AddAddress(1, fakeNetNumber, "\x01"); err != nil {
|
||||
t.Fatalf("AddAddress failed: %v", err)
|
||||
@@ -340,7 +346,13 @@ func TestTransportControlReceive(t *testing.T) {
|
||||
t.Fatalf("CreateNIC failed: %v", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{"\x00", "\x00", "\x00", 1}})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
if err := s.AddAddress(1, fakeNetNumber, "\x01"); err != nil {
|
||||
t.Fatalf("AddAddress failed: %v", err)
|
||||
@@ -406,7 +418,13 @@ func TestTransportSend(t *testing.T) {
|
||||
t.Fatalf("AddAddress failed: %v", err)
|
||||
}
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{{"\x00", "\x00", "\x00", 1}})
|
||||
{
|
||||
subnet, err := tcpip.NewSubnet("\x00", "\x00")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{{Destination: subnet, Gateway: "\x00", NIC: 1}})
|
||||
}
|
||||
|
||||
// Create endpoint and bind it.
|
||||
wq := waiter.Queue{}
|
||||
@@ -497,10 +515,20 @@ func TestTransportForwarding(t *testing.T) {
|
||||
|
||||
// Route all packets to address 3 to NIC 2 and all packets to address
|
||||
// 1 to NIC 1.
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{"\x03", "\xff", "\x00", 2},
|
||||
{"\x01", "\xff", "\x00", 1},
|
||||
})
|
||||
{
|
||||
subnet0, err := tcpip.NewSubnet("\x03", "\xff")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subnet1, err := tcpip.NewSubnet("\x01", "\xff")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{Destination: subnet0, Gateway: "\x00", NIC: 2},
|
||||
{Destination: subnet1, Gateway: "\x00", NIC: 1},
|
||||
})
|
||||
}
|
||||
|
||||
wq := waiter.Queue{}
|
||||
ep, err := s.NewEndpoint(fakeTransNumber, fakeNetNumber, &wq)
|
||||
|
||||
+5
-39
@@ -205,16 +205,8 @@ func (s *Subnet) ID() Address {
|
||||
// Bits returns the number of ones (network bits) and zeros (host bits) in the
|
||||
// subnet mask.
|
||||
func (s *Subnet) Bits() (ones int, zeros int) {
|
||||
for _, b := range []byte(s.mask) {
|
||||
for i := uint(0); i < 8; i++ {
|
||||
if b&(1<<i) == 0 {
|
||||
zeros++
|
||||
} else {
|
||||
ones++
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
ones = s.mask.Prefix()
|
||||
return ones, len(s.mask)*8 - ones
|
||||
}
|
||||
|
||||
// Prefix returns the number of bits before the first host bit.
|
||||
@@ -578,13 +570,8 @@ type BroadcastOption int
|
||||
// gateway) sets of packets should be routed. A row is considered viable if the
|
||||
// masked target address matches the destination address in the row.
|
||||
type Route struct {
|
||||
// Destination is the address that must be matched against the masked
|
||||
// target address to check if this row is viable.
|
||||
Destination Address
|
||||
|
||||
// Mask specifies which bits of the Destination and the target address
|
||||
// must match for this row to be viable.
|
||||
Mask AddressMask
|
||||
// Destination must contain the target address for this row to be viable.
|
||||
Destination Subnet
|
||||
|
||||
// Gateway is the gateway to be used if this row is viable.
|
||||
Gateway Address
|
||||
@@ -596,7 +583,7 @@ type Route struct {
|
||||
// String implements the fmt.Stringer interface.
|
||||
func (r *Route) String() string {
|
||||
var out strings.Builder
|
||||
fmt.Fprintf(&out, "%s/%d", r.Destination, r.Mask.Prefix())
|
||||
fmt.Fprintf(&out, "%s", r.Destination)
|
||||
if len(r.Gateway) > 0 {
|
||||
fmt.Fprintf(&out, " via %s", r.Gateway)
|
||||
}
|
||||
@@ -604,27 +591,6 @@ func (r *Route) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
// Match determines if r is viable for the given destination address.
|
||||
func (r *Route) Match(addr Address) bool {
|
||||
if len(addr) != len(r.Destination) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Using header.Ipv4Broadcast would introduce an import cycle, so
|
||||
// we'll use a literal instead.
|
||||
if addr == "\xff\xff\xff\xff" {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := 0; i < len(r.Destination); i++ {
|
||||
if (addr[i] & r.Mask[i]) != r.Destination[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// LinkEndpointID represents a data link layer endpoint.
|
||||
type LinkEndpointID uint64
|
||||
|
||||
|
||||
+6
-26
@@ -60,12 +60,12 @@ func TestSubnetBits(t *testing.T) {
|
||||
}{
|
||||
{"\x00", 0, 8},
|
||||
{"\x00\x00", 0, 16},
|
||||
{"\x36", 4, 4},
|
||||
{"\x5c", 4, 4},
|
||||
{"\x5c\x5c", 8, 8},
|
||||
{"\x5c\x36", 8, 8},
|
||||
{"\x36\x5c", 8, 8},
|
||||
{"\x36\x36", 8, 8},
|
||||
{"\x36", 0, 8},
|
||||
{"\x5c", 0, 8},
|
||||
{"\x5c\x5c", 0, 16},
|
||||
{"\x5c\x36", 0, 16},
|
||||
{"\x36\x5c", 0, 16},
|
||||
{"\x36\x36", 0, 16},
|
||||
{"\xff", 8, 0},
|
||||
{"\xff\xff", 16, 0},
|
||||
}
|
||||
@@ -122,26 +122,6 @@ func TestSubnetCreation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteMatch(t *testing.T) {
|
||||
tests := []struct {
|
||||
d Address
|
||||
m AddressMask
|
||||
a Address
|
||||
want bool
|
||||
}{
|
||||
{"\xc2\x80", "\xff\xf0", "\xc2\x80", true},
|
||||
{"\xc2\x80", "\xff\xf0", "\xc2\x00", false},
|
||||
{"\xc2\x00", "\xff\xf0", "\xc2\x00", true},
|
||||
{"\xc2\x00", "\xff\xf0", "\xc2\x80", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
r := Route{Destination: tt.d, Mask: tt.m}
|
||||
if got := r.Match(tt.a); got != tt.want {
|
||||
t.Errorf("Route(%v).Match(%v) = %v, want %v", r, tt.a, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddressString(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
// Taken from stdlib.
|
||||
|
||||
@@ -1348,7 +1348,7 @@ func (e *endpoint) checkV4Mapped(addr *tcpip.FullAddress) (tcpip.NetworkProtocol
|
||||
|
||||
netProto = header.IPv4ProtocolNumber
|
||||
addr.Addr = addr.Addr[header.IPv6AddressSize-header.IPv4AddressSize:]
|
||||
if addr.Addr == "\x00\x00\x00\x00" {
|
||||
if addr.Addr == header.IPv4Any {
|
||||
addr.Addr = ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2874,15 +2874,11 @@ func makeStack() (*stack.Stack, *tcpip.Error) {
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{
|
||||
Destination: "\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: 1,
|
||||
},
|
||||
{
|
||||
Destination: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv6EmptySubnet,
|
||||
NIC: 1,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -168,15 +168,11 @@ func New(t *testing.T, mtu uint32) *Context {
|
||||
|
||||
s.SetRouteTable([]tcpip.Route{
|
||||
{
|
||||
Destination: "\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv4EmptySubnet,
|
||||
NIC: 1,
|
||||
},
|
||||
{
|
||||
Destination: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Mask: "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Gateway: "",
|
||||
Destination: header.IPv6EmptySubnet,
|
||||
NIC: 1,
|
||||
},
|
||||
})
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user