fix: utun peer address for darwin
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled

This commit is contained in:
Daniel Ding
2025-04-21 11:21:48 +08:00
parent d8a24744d7
commit e8e67e5465
5 changed files with 43 additions and 44 deletions

View File

@@ -10,8 +10,9 @@ import (
type Access struct {
MixAccess
// private
brName string
addr string
brName string
addr string
gateway string
}
func NewAccess(config *config.Access) *Access {
@@ -32,46 +33,51 @@ func (p *Access) Initialize() {
func (p *Access) routeAdd(prefix string) ([]byte, error) {
network.RouteDel("", prefix, "")
out, err := network.RouteAdd(p.IfName(), prefix, "")
out, err := network.RouteAdd("", prefix, p.gateway)
return out, err
}
func (p *Access) AddAddr(ipStr string) error {
if ipStr == "" {
func (p *Access) AddAddr(addr, gateway string) error {
if addr == "" {
return nil
}
// add Access-to-Access
ips := strings.SplitN(ipStr, "/", 2)
out, err := network.AddrAdd(p.IfName(), ips[0], ips[0])
ips := strings.SplitN(addr, "/", 2)
if gateway == "" {
gateway = ips[0]
}
out, err := network.AddrAdd(p.IfName(), ips[0], gateway)
if err != nil {
p.out.Warn("Access.AddAddr: %s, %s", err, out)
return err
}
p.out.Info("Access.AddAddr: %s", ipStr)
p.out.Info("Access.AddAddr: %s", addr)
p.addr = addr
p.gateway = gateway
// add directly route.
out, err = p.routeAdd(ipStr)
out, err = p.routeAdd(addr)
if err != nil {
p.out.Warn("Access.AddAddr: %s, %s", err, out)
}
p.AddRoute()
p.addr = ipStr
p.AddRoute()
return nil
}
func (p *Access) DelAddr(ipStr string) error {
func (p *Access) DelAddr(addr string) error {
// delete directly route.
out, err := network.RouteDel(p.IfName(), ipStr, "")
out, err := network.RouteDel(p.IfName(), addr, "")
if err != nil {
p.out.Warn("Access.DelAddr: %s, %s", err, out)
}
p.out.Info("Access.DelAddr: route %s via %s", ipStr, p.IfName())
p.out.Info("Access.DelAddr: route %s via %s", addr, p.IfName())
// delete Access-to-Access
ip4 := strings.SplitN(ipStr, "/", 2)[0]
ip4 := strings.SplitN(addr, "/", 2)[0]
out, err = network.AddrDel(p.IfName(), ip4)
if err != nil {
p.out.Warn("Access.DelAddr: %s, %s", err, out)

View File

@@ -42,30 +42,30 @@ func (p *Access) Initialize() {
p.MixAccess.Initialize()
}
func (p *Access) DelAddr(ipStr string) error {
if p.link == nil || ipStr == "" {
func (p *Access) DelAddr(addr string) error {
if p.link == nil || addr == "" {
return nil
}
ipAddr, err := netlink.ParseAddr(ipStr)
ipAddr, err := netlink.ParseAddr(addr)
if err != nil {
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", ipStr, err)
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", addr, err)
return err
}
if err := netlink.AddrDel(p.link, ipAddr); err != nil {
p.out.Warn("Access.DelAddr.UnsetLinkIp: %s", err)
}
p.out.Info("Access.DelAddr: %s", ipStr)
p.out.Info("Access.DelAddr: %s", addr)
p.addr = ""
return nil
}
func (p *Access) AddAddr(ipStr string) error {
if ipStr == "" || p.link == nil {
func (p *Access) AddAddr(addr, gateway string) error {
if addr == "" || p.link == nil {
return nil
}
ipAddr, err := netlink.ParseAddr(ipStr)
ipAddr, err := netlink.ParseAddr(addr)
if err != nil {
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", ipStr, err)
p.out.Error("Access.AddAddr.ParseCIDR %s: %s", addr, err)
return err
}
if err := netlink.AddrAdd(p.link, ipAddr); err != nil {
@@ -73,11 +73,11 @@ func (p *Access) AddAddr(ipStr string) error {
return err
}
p.out.Info("Access.AddAddr: %s", ipStr)
p.addr = addr
p.out.Info("Access.AddAddr: %s", addr)
p.AddRoute()
p.addr = ipStr
return nil
}

View File

@@ -4,17 +4,10 @@ package access
import "github.com/luscis/openlan/pkg/config"
type Point struct {
type Access struct {
MixAccess
}
func NewPoint(config *config.Point) *Point {
func NewAccess(config *config.Access) *Access {
return nil
}
func (p *Point) Initialize() {
}
func (p *Point) Start() {
}

View File

@@ -49,8 +49,8 @@ func (p *Access) Trim(out []byte) string {
return strings.TrimSpace(string(out))
}
func (p *Access) AddAddr(ipStr string) error {
if ipStr == "" {
func (p *Access) AddAddr(addr, gateway string) error {
if addr == "" {
return nil
}
addrExisted := network.AddrShow(p.IfName())
@@ -59,13 +59,13 @@ func (p *Access) AddAddr(ipStr string) error {
_, _ = network.AddrDel(p.IfName(), addr)
}
}
out, err := network.AddrAdd(p.IfName(), ipStr)
out, err := network.AddrAdd(p.IfName(), addr)
if err != nil {
p.out.Warn("Access.AddAddr: %s, %s", err, p.Trim(out))
return err
}
p.out.Info("Access.AddAddr: %s", ipStr)
p.addr = ipStr
p.out.Info("Access.AddAddr: %s", addr)
p.addr = addr
return nil
}

View File

@@ -68,7 +68,7 @@ func NewEvent(newType, reason string) *WorkerEvent {
}
type WorkerListener struct {
AddAddr func(ipStr string) error
AddAddr func(ipStr, gateway string) error
DelAddr func(ipStr string) error
OnTap func(w *TapWorker) error
AddRoutes func(routes []*models.Route) error
@@ -337,7 +337,7 @@ func (w *Worker) OnIpAddr(s *SocketWorker, n *models.Network) error {
ipStr := fmt.Sprintf("%s/%d", n.Address, prefix)
w.tapWorker.OnIpAddr(ipStr)
if w.listener.AddAddr != nil {
_ = w.listener.AddAddr(ipStr)
_ = w.listener.AddAddr(ipStr, n.Gateway)
}
if n.Gateway != "" && runtime.GOOS == "darwin" {
@@ -375,7 +375,7 @@ func (w *Worker) OnSuccess(s *SocketWorker) error {
if !w.cfg.RequestAddr {
w.out.Info("SocketWorker.AddAddr: notAllowed")
} else if w.listener.AddAddr != nil {
_ = w.listener.AddAddr(w.ifAddr)
_ = w.listener.AddAddr(w.ifAddr, "")
}
return nil
}