mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-18 20:51:45 +00:00
tstest/natlab: first network attached becomes the default route.
Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
parent
f2e5da916a
commit
771eb05bcb
@ -37,10 +37,6 @@ func NewInternet() *Network {
|
|||||||
Name: "internet",
|
Name: "internet",
|
||||||
Prefix4: mustPrefix("203.0.113.0/24"), // documentation netblock that looks Internet-y
|
Prefix4: mustPrefix("203.0.113.0/24"), // documentation netblock that looks Internet-y
|
||||||
Prefix6: mustPrefix("fc00:52::/64"),
|
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
|
Prefix4 netaddr.IPPrefix
|
||||||
Prefix6 netaddr.IPPrefix
|
Prefix6 netaddr.IPPrefix
|
||||||
|
|
||||||
pushRoutes []netaddr.IPPrefix
|
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
machine map[netaddr.IP]*Machine
|
machine map[netaddr.IP]*Machine
|
||||||
lastV4 netaddr.IP
|
lastV4 netaddr.IP
|
||||||
@ -225,6 +219,9 @@ func unspecOf(ip netaddr.IP) netaddr.IP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach adds an interface to a machine.
|
// 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 {
|
func (m *Machine) Attach(interfaceName string, n *Network) *Interface {
|
||||||
f := &Interface{
|
f := &Interface{
|
||||||
net: n,
|
net: n,
|
||||||
@ -241,22 +238,30 @@ func (m *Machine) Attach(interfaceName string, n *Network) *Interface {
|
|||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
m.interfaces = append(m.interfaces, f)
|
m.interfaces = append(m.interfaces, f)
|
||||||
|
if len(m.interfaces) == 1 {
|
||||||
if n.pushRoutes == nil {
|
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() {
|
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() {
|
if !n.Prefix6.IsZero() {
|
||||||
n.pushRoutes = append(n.pushRoutes, n.Prefix6)
|
m.routes = append(m.routes, routeEntry{
|
||||||
|
prefix: n.Prefix6,
|
||||||
|
iface: f,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pfx := range n.pushRoutes {
|
|
||||||
m.routes = append(m.routes, routeEntry{
|
|
||||||
prefix: pfx,
|
|
||||||
iface: f,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
sort.Slice(m.routes, func(i, j int) bool {
|
sort.Slice(m.routes, func(i, j int) bool {
|
||||||
return m.routes[i].prefix.Bits > m.routes[j].prefix.Bits
|
return m.routes[i].prefix.Bits > m.routes[j].prefix.Bits
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user