tstest/natlab: first network attached becomes the default route.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/518/head
David Anderson 4 years ago
parent f2e5da916a
commit 771eb05bcb

@ -37,10 +37,6 @@ func NewInternet() *Network {
Name: "internet",
Prefix4: mustPrefix("203.0.113.0/24"), // documentation netblock that looks Internet-y
Prefix6: mustPrefix("fc00:52::/64"),
pushRoutes: []netaddr.IPPrefix{
mustPrefix("0.0.0.0/0"),
mustPrefix("::/0"),
},
}
}
@ -49,8 +45,6 @@ type Network struct {
Prefix4 netaddr.IPPrefix
Prefix6 netaddr.IPPrefix
pushRoutes []netaddr.IPPrefix
mu sync.Mutex
machine map[netaddr.IP]*Machine
lastV4 netaddr.IP
@ -225,6 +219,9 @@ func unspecOf(ip netaddr.IP) netaddr.IP {
}
// Attach adds an interface to a machine.
//
// The first interface added to a Machine becomes that machine's
// default route.
func (m *Machine) Attach(interfaceName string, n *Network) *Interface {
f := &Interface{
net: n,
@ -241,22 +238,30 @@ func (m *Machine) Attach(interfaceName string, n *Network) *Interface {
defer m.mu.Unlock()
m.interfaces = append(m.interfaces, f)
if n.pushRoutes == nil {
if len(m.interfaces) == 1 {
m.routes = append(m.routes,
routeEntry{
prefix: mustPrefix("0.0.0.0/0"),
iface: f,
},
routeEntry{
prefix: mustPrefix("::/0"),
iface: f,
})
} else {
if !n.Prefix4.IsZero() {
n.pushRoutes = append(n.pushRoutes, n.Prefix4)
m.routes = append(m.routes, routeEntry{
prefix: n.Prefix4,
iface: f,
})
}
if !n.Prefix6.IsZero() {
n.pushRoutes = append(n.pushRoutes, n.Prefix6)
}
}
for _, pfx := range n.pushRoutes {
m.routes = append(m.routes, routeEntry{
prefix: pfx,
prefix: n.Prefix6,
iface: f,
})
}
}
sort.Slice(m.routes, func(i, j int) bool {
return m.routes[i].prefix.Bits > m.routes[j].prefix.Bits
})

Loading…
Cancel
Save