tstest/integration/testcontrol: add ipv6 support (#2394)

Split from https://github.com/tailscale/tailscale/pull/2376.

This adds IPv6 support to testcontrol so each member of the tailscale
network gets an IPv6 address too.

Signed-off-by: Christine Dodrill <xe@tailscale.com>
This commit is contained in:
Christine Dodrill 2021-07-13 13:10:08 -04:00 committed by GitHub
parent 61e8fd4698
commit 1f2a877c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -28,6 +28,7 @@
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"golang.org/x/crypto/nacl/box" "golang.org/x/crypto/nacl/box"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/net/tsaddr"
"tailscale.com/smallzstd" "tailscale.com/smallzstd"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/logger" "tailscale.com/types/logger"
@ -406,8 +407,12 @@ func (s *Server) serveRegister(w http.ResponseWriter, r *http.Request, mkey tail
machineAuthorized := true // TODO: add Server.RequireMachineAuth machineAuthorized := true // TODO: add Server.RequireMachineAuth
v4Prefix := netaddr.IPPrefixFrom(netaddr.IPv4(100, 64, uint8(tailcfg.NodeID(user.ID)>>8), uint8(tailcfg.NodeID(user.ID))), 32)
v6Prefix := netaddr.IPPrefixFrom(tsaddr.Tailscale4To6(v4Prefix.IP()), 128)
allowedIPs := []netaddr.IPPrefix{ allowedIPs := []netaddr.IPPrefix{
netaddr.MustParseIPPrefix(fmt.Sprintf("100.64.%d.%d/32", uint8(tailcfg.NodeID(user.ID)>>8), uint8(tailcfg.NodeID(user.ID)))), v4Prefix,
v6Prefix,
} }
s.nodes[req.NodeKey] = &tailcfg.Node{ s.nodes[req.NodeKey] = &tailcfg.Node{
@ -638,8 +643,12 @@ func (s *Server) MapResponse(req *tailcfg.MapRequest) (res *tailcfg.MapResponse,
} }
} }
v4Prefix := netaddr.IPPrefixFrom(netaddr.IPv4(100, 64, uint8(tailcfg.NodeID(user.ID)>>8), uint8(tailcfg.NodeID(user.ID))), 32)
v6Prefix := netaddr.IPPrefixFrom(tsaddr.Tailscale4To6(v4Prefix.IP()), 128)
res.Node.Addresses = []netaddr.IPPrefix{ res.Node.Addresses = []netaddr.IPPrefix{
netaddr.MustParseIPPrefix(fmt.Sprintf("100.64.%d.%d/32", uint8(node.ID>>8), uint8(node.ID))), v4Prefix,
v6Prefix,
} }
res.Node.AllowedIPs = res.Node.Addresses res.Node.AllowedIPs = res.Node.Addresses

View File

@ -966,6 +966,26 @@ func (h Harness) testDistro(t *testing.T, d Distro, ipm ipMapping) {
t.Fatalf("wanted %q from vm, got: %q", securePassword, msg) t.Fatalf("wanted %q from vm, got: %q", securePassword, msg)
} }
}) })
t.Run("ipv6-addr", func(t *testing.T) {
sess, err := cli.NewSession()
if err != nil {
t.Fatalf("can't make incoming session: %v", err)
}
defer sess.Close()
outp, err := sess.CombinedOutput("tailscale ip -6")
if err != nil {
t.Log(string(outp))
t.Fatalf("can't get ipv6 address: %v", err)
}
_, err = netaddr.ParseIP(string(bytes.TrimSpace(outp)))
if err != nil {
t.Log(string(outp))
t.Fatalf("can't parse IP: %v", err)
}
})
} }
func runTestCommands(t *testing.T, timeout time.Duration, cli *ssh.Client, batch []expect.Batcher) { func runTestCommands(t *testing.T, timeout time.Duration, cli *ssh.Client, batch []expect.Batcher) {