mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-18 20:51:45 +00:00
cmd/tailscale: add "debug via" subcommand to do CIDR math for via ranges
$ tailscale debug via 0xb 10.2.0.0/16 fd7a:115c:a1e0:b1a:0🅱️a02:0/112 $ tailscale debug via fd7a:115c:a1e0:b1a:0🅱️a02:0/112 site 11 (0xb), 10.2.0.0/16 Previously: 3ae701f0ebe053a1f7b6a3fa345a56b3132c818f This adds a little debug tool to do CIDR math to make converting between those ranges easier for now. Updates #3616 Change-Id: I98302e95d17765bfaced3ecbb71cbd43e84bff46 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
f74ee80abe
commit
d413850bd7
@ -8,6 +8,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
@ -21,9 +22,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/peterbourgon/ff/v3/ffcli"
|
"github.com/peterbourgon/ff/v3/ffcli"
|
||||||
|
"inet.af/netaddr"
|
||||||
"tailscale.com/client/tailscale"
|
"tailscale.com/client/tailscale"
|
||||||
"tailscale.com/hostinfo"
|
"tailscale.com/hostinfo"
|
||||||
"tailscale.com/ipn"
|
"tailscale.com/ipn"
|
||||||
|
"tailscale.com/net/tsaddr"
|
||||||
"tailscale.com/paths"
|
"tailscale.com/paths"
|
||||||
"tailscale.com/safesocket"
|
"tailscale.com/safesocket"
|
||||||
)
|
)
|
||||||
@ -106,6 +109,11 @@ var debugCmd = &ffcli.Command{
|
|||||||
return fs
|
return fs
|
||||||
})(),
|
})(),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "via",
|
||||||
|
Exec: runVia,
|
||||||
|
ShortHelp: "convert between site-specific IPv4 CIDRs and IPv6 'via' routes",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,3 +356,46 @@ func runDaemonMetrics(ctx context.Context, args []string) error {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runVia(ctx context.Context, args []string) error {
|
||||||
|
switch len(args) {
|
||||||
|
default:
|
||||||
|
return errors.New("expect either <site-id> <v4-cidr> or <v6-route>")
|
||||||
|
case 1:
|
||||||
|
ipp, err := netaddr.ParseIPPrefix(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !ipp.IP().Is6() {
|
||||||
|
return errors.New("with one argument, expect an IPv6 CIDR")
|
||||||
|
}
|
||||||
|
if !tsaddr.TailscaleViaRange().Contains(ipp.IP()) {
|
||||||
|
return errors.New("not a via route")
|
||||||
|
}
|
||||||
|
if ipp.Bits() < 96 {
|
||||||
|
return errors.New("short length, want /96 or more")
|
||||||
|
}
|
||||||
|
v4 := tsaddr.UnmapVia(ipp.IP())
|
||||||
|
a := ipp.IP().As16()
|
||||||
|
siteID := binary.BigEndian.Uint32(a[8:12])
|
||||||
|
fmt.Printf("site %v (0x%x), %v\n", siteID, siteID, netaddr.IPPrefixFrom(v4, ipp.Bits()-96))
|
||||||
|
case 2:
|
||||||
|
siteID, err := strconv.ParseUint(args[0], 0, 32)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid site-id %q; must be decimal or hex with 0x prefix", args[0])
|
||||||
|
}
|
||||||
|
if siteID > 0xff {
|
||||||
|
return fmt.Errorf("site-id values over 255 are currently reserved")
|
||||||
|
}
|
||||||
|
ipp, err := netaddr.ParseIPPrefix(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
via, err := tsaddr.MapVia(uint32(siteID), ipp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(via)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
package tsaddr
|
package tsaddr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
@ -280,3 +282,17 @@ func UnmapVia(ip netaddr.IP) netaddr.IP {
|
|||||||
}
|
}
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapVia returns an IPv6 "via" route for an IPv4 CIDR in a given siteID.
|
||||||
|
func MapVia(siteID uint32, v4 netaddr.IPPrefix) (via netaddr.IPPrefix, err error) {
|
||||||
|
if !v4.IP().Is4() {
|
||||||
|
return via, errors.New("want IPv4 CIDR with a site ID")
|
||||||
|
}
|
||||||
|
viaRange16 := TailscaleViaRange().IP().As16()
|
||||||
|
var a [16]byte
|
||||||
|
copy(a[:], viaRange16[:8])
|
||||||
|
binary.BigEndian.PutUint32(a[8:], siteID)
|
||||||
|
ip4a := v4.IP().As4()
|
||||||
|
copy(a[12:], ip4a[:])
|
||||||
|
return netaddr.IPPrefixFrom(netaddr.IPFrom16(a), v4.Bits()+64+32), nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user