mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-20 01:47:33 +00:00
net/netaddr: start migrating to net/netip via new netaddr adapter package
Updates #5162 Change-Id: Id7bdec303b25471f69d542f8ce43805328d56c12 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
7b1a91dfd3
commit
7eaf5e509f
@@ -10,8 +10,9 @@ import (
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
"go4.org/netipx"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
"inet.af/netaddr"
|
||||
"tailscale.com/net/netaddr"
|
||||
"tailscale.com/types/logger"
|
||||
"tailscale.com/wgengine/monitor"
|
||||
)
|
||||
@@ -59,7 +60,7 @@ func (r *openbsdRouter) Up() error {
|
||||
}
|
||||
|
||||
func inet(p netaddr.IPPrefix) string {
|
||||
if p.IP().Is6() {
|
||||
if p.Addr().Is6() {
|
||||
return "inet6"
|
||||
}
|
||||
return "inet"
|
||||
@@ -79,11 +80,11 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
localAddr4 := netaddr.IPPrefix{}
|
||||
localAddr6 := netaddr.IPPrefix{}
|
||||
for _, addr := range cfg.LocalAddrs {
|
||||
if addr.IP().Is4() {
|
||||
if addr.Addr().Is4() {
|
||||
numIPv4++
|
||||
localAddr4 = addr
|
||||
}
|
||||
if addr.IP().Is6() {
|
||||
if addr.Addr().Is6() {
|
||||
numIPv6++
|
||||
localAddr6 = addr
|
||||
}
|
||||
@@ -95,7 +96,7 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
var errq error
|
||||
|
||||
if localAddr4 != r.local4 {
|
||||
if !r.local4.IsZero() {
|
||||
if r.local4.IsValid() {
|
||||
addrdel := []string{"ifconfig", r.tunname,
|
||||
"inet", r.local4.String(), "-alias"}
|
||||
out, err := cmd(addrdel...).CombinedOutput()
|
||||
@@ -108,7 +109,7 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
|
||||
routedel := []string{"route", "-q", "-n",
|
||||
"del", "-inet", r.local4.String(),
|
||||
"-iface", r.local4.IP().String()}
|
||||
"-iface", r.local4.Addr().String()}
|
||||
if out, err := cmd(routedel...).CombinedOutput(); err != nil {
|
||||
r.logf("route del failed: %v: %v\n%s", routedel, err, out)
|
||||
if errq == nil {
|
||||
@@ -117,7 +118,7 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !localAddr4.IsZero() {
|
||||
if localAddr4.IsValid() {
|
||||
addradd := []string{"ifconfig", r.tunname,
|
||||
"inet", localAddr4.String(), "alias"}
|
||||
out, err := cmd(addradd...).CombinedOutput()
|
||||
@@ -130,7 +131,7 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
|
||||
routeadd := []string{"route", "-q", "-n",
|
||||
"add", "-inet", localAddr4.String(),
|
||||
"-iface", localAddr4.IP().String()}
|
||||
"-iface", localAddr4.Addr().String()}
|
||||
if out, err := cmd(routeadd...).CombinedOutput(); err != nil {
|
||||
r.logf("route add failed: %v: %v\n%s", routeadd, err, out)
|
||||
if errq == nil {
|
||||
@@ -140,15 +141,15 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !localAddr6.IsZero() {
|
||||
if localAddr6.IsValid() {
|
||||
// in https://github.com/tailscale/tailscale/issues/1307 we made
|
||||
// FreeBSD use a /48 for IPv6 addresses, which is nice because we
|
||||
// don't need to additionally add routing entries. Do that here too.
|
||||
localAddr6 = netaddr.IPPrefixFrom(localAddr6.IP(), 48)
|
||||
localAddr6 = netaddr.IPPrefixFrom(localAddr6.Addr(), 48)
|
||||
}
|
||||
|
||||
if localAddr6 != r.local6 {
|
||||
if !r.local6.IsZero() {
|
||||
if r.local6.IsValid() {
|
||||
addrdel := []string{"ifconfig", r.tunname,
|
||||
"inet6", r.local6.String(), "delete"}
|
||||
out, err := cmd(addrdel...).CombinedOutput()
|
||||
@@ -160,7 +161,7 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
}
|
||||
}
|
||||
|
||||
if !localAddr6.IsZero() {
|
||||
if localAddr6.IsValid() {
|
||||
addradd := []string{"ifconfig", r.tunname,
|
||||
"inet6", localAddr6.String()}
|
||||
out, err := cmd(addradd...).CombinedOutput()
|
||||
@@ -179,12 +180,12 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
}
|
||||
for route := range r.routes {
|
||||
if _, keep := newRoutes[route]; !keep {
|
||||
net := route.IPNet()
|
||||
net := netipx.PrefixIPNet(route)
|
||||
nip := net.IP.Mask(net.Mask)
|
||||
nstr := fmt.Sprintf("%v/%d", nip, route.Bits())
|
||||
dst := localAddr4.IP().String()
|
||||
if route.IP().Is6() {
|
||||
dst = localAddr6.IP().String()
|
||||
dst := localAddr4.Addr().String()
|
||||
if route.Addr().Is6() {
|
||||
dst = localAddr6.Addr().String()
|
||||
}
|
||||
routedel := []string{"route", "-q", "-n",
|
||||
"del", "-" + inet(route), nstr,
|
||||
@@ -200,12 +201,12 @@ func (r *openbsdRouter) Set(cfg *Config) error {
|
||||
}
|
||||
for route := range newRoutes {
|
||||
if _, exists := r.routes[route]; !exists {
|
||||
net := route.IPNet()
|
||||
net := netipx.PrefixIPNet(route)
|
||||
nip := net.IP.Mask(net.Mask)
|
||||
nstr := fmt.Sprintf("%v/%d", nip, route.Bits())
|
||||
dst := localAddr4.IP().String()
|
||||
if route.IP().Is6() {
|
||||
dst = localAddr6.IP().String()
|
||||
dst := localAddr4.Addr().String()
|
||||
if route.Addr().Is6() {
|
||||
dst = localAddr6.Addr().String()
|
||||
}
|
||||
routeadd := []string{"route", "-q", "-n",
|
||||
"add", "-" + inet(route), nstr,
|
||||
|
Reference in New Issue
Block a user