Skip to content

Commit

Permalink
Make ip addresses/prefixes mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszWojciechO committed Dec 17, 2024
1 parent 5cf68be commit 32987c5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion daemon/vpn/nordlynx/kernel_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (k *KernelSpace) Start(
interfaceIps = append(interfaceIps, ipv6)
}

tun := tunnel.New(*iface, interfaceIps, nil)
tun := tunnel.New(*iface, interfaceIps, netip.Prefix{})
k.tun = tun
if err := pushConfig(tun.Interface(), conf); err != nil {
if err := k.stop(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions daemon/vpn/nordlynx/libtelio/libtelio.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (l *Libtelio) openTunnel(ip netip.Addr, privateKey string) (err error) {
return fmt.Errorf("retrieving the interface: %w", err)
}

tun := tunnel.New(*iface, []netip.Addr{ip}, nil)
tun := tunnel.New(*iface, []netip.Addr{ip}, netip.Prefix{})

err = tun.AddAddrs()
if err != nil {
Expand Down Expand Up @@ -628,7 +628,7 @@ func (l *Libtelio) updateTunnel(privateKey string, ip netip.Addr) error {
if err := l.tun.DelAddrs(); err != nil {
return fmt.Errorf("deleting interface addrs: %w", err)
}
tun := tunnel.New(l.tun.Interface(), []netip.Addr{ip}, nil)
tun := tunnel.New(l.tun.Interface(), []netip.Addr{ip}, netip.Prefix{})
if err := tun.AddAddrs(); err != nil {
return fmt.Errorf("adding interface addrs: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/vpn/nordlynx/user_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (u *UserSpace) Start(

u.conn = conn

tun := tunnel.New(*iface, interfaceIps, nil)
tun := tunnel.New(*iface, interfaceIps, netip.Prefix{})
u.tun = tun
if err := tun.AddAddrs(); err != nil {
if err := u.stop(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion daemon/vpn/quench/libquench.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (q *Quench) Start(ctx context.Context, creds vpn.Credentials, server vpn.Se
}

ip := netip.MustParsePrefix(quenchInterfaceAddr)
tun := tunnel.New(*iface, []netip.Addr{}, []netip.Prefix{ip})
tun := tunnel.New(*iface, []netip.Addr{}, ip)

if err := tun.AddAddrs(); err != nil {
return fmt.Errorf("setting up vinc: %w", err)
Expand Down
37 changes: 17 additions & 20 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type Tunnel struct {
// might be a good idea to change this to a pointer now
// so that we could see changes to the interface at real time
// but this would need testing first to check if it actually works
iface net.Interface
ips []netip.Addr
prefixes []netip.Prefix
iface net.Interface
ips []netip.Addr
prefix netip.Prefix
}

func New(iface net.Interface, ips []netip.Addr, prefixes []netip.Prefix) *Tunnel {
return &Tunnel{iface: iface, ips: ips, prefixes: prefixes}
func New(iface net.Interface, ips []netip.Addr, prefix netip.Prefix) *Tunnel {
return &Tunnel{iface: iface, ips: ips, prefix: prefix}
}

// Interface returns the underlying network interface.
Expand Down Expand Up @@ -106,22 +106,19 @@ func addDelAddr(cmd string, ifaceName string, addr string) ([]byte, error) {
}

func (t *Tunnel) cmdAddrs(cmd string) error {
for _, ip := range t.ips {
mask := 10 // unify with other platforms
if ip.BitLen() > 32 {
mask = ip.BitLen() // ipv6
}
out, err := addDelAddr(cmd, t.iface.Name, fmt.Sprintf("%s/%d", ip.String(), mask))
if err != nil {
return fmt.Errorf("%s IP address to interface: %s : %w", cmd, string(out), err)
}
}

for _, prefix := range t.prefixes {
out, err := addDelAddr(cmd, t.iface.Name, prefix.String())
if err != nil {
return fmt.Errorf("%s IP address to interface: %s : %w", cmd, string(out), err)
if len(t.ips) > 0 {
for _, ip := range t.ips {
mask := 10 // unify with other platforms
if ip.BitLen() > 32 {
mask = ip.BitLen() // ipv6
}
out, err := addDelAddr(cmd, t.iface.Name, fmt.Sprintf("%s/%d", ip.String(), mask))
if err != nil {
return fmt.Errorf("%s IP address to interface: %s : %w", cmd, string(out), err)
}
}
} else {
addDelAddr(cmd, t.iface.Name, t.prefix.String())
}
return nil
}
Expand Down

0 comments on commit 32987c5

Please sign in to comment.