Skip to content

Commit 579e017

Browse files
committed
feat(linux): add IPv6 support for VPN gateway
1 parent 848960d commit 579e017

22 files changed

Lines changed: 611 additions & 179 deletions

application.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type NetManager interface {
108108
EnableClientRoutes(tunIfName string) error
109109
DisableClientRoutes() error
110110
ClientRoutesActive() bool
111-
EnableServerNAT(awlSubnet, tunIfName string) error
111+
EnableServerNAT(awlSubnet, awlSubnet6, tunIfName string) error
112112
DisableServerNAT() error
113113
ServerNATActive() bool
114114
}
@@ -145,12 +145,16 @@ func (a *Application) Init(ctx context.Context, tunDevice tun.Device) error {
145145
a.logger.Info("VPN interface is disabled from config")
146146
} else {
147147
localIP, netMask := a.Conf.VPNLocalIPMask()
148+
localIPv6, netMaskv6 := a.Conf.VPNLocalIPMaskV6()
148149
interfaceName := a.Conf.VPNConfig.InterfaceName
149-
a.vpnDevice, err = vpn.NewDevice(tunDevice, interfaceName, localIP, netMask)
150+
a.vpnDevice, err = vpn.NewDevice(tunDevice, interfaceName, localIP, netMask, localIPv6, netMaskv6)
150151
if err != nil {
151152
return fmt.Errorf("failed to init vpn: %v", err)
152153
}
153154
a.logger.Infof("VPN interface created. Name: %s CIDR: %s", interfaceName, &net.IPNet{IP: localIP, Mask: netMask})
155+
if localIPv6 != nil {
156+
a.logger.Infof("VPN interface IPv6: %s", &net.IPNet{IP: localIPv6, Mask: netMaskv6})
157+
}
154158

155159
a.Tunnel = service.NewTunnel(a.P2p, a.vpnDevice, a.Conf, a.Eventbus)
156160
go a.vpnDevice.ReadTUNPackets(a.Tunnel.HandleReadPackets)

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type (
7878
DisableVPNInterface bool `json:"disableVPNInterface"`
7979
InterfaceName string `json:"interfaceName"`
8080
IPNet string `json:"ipNet"`
81+
IPNetV6 string `json:"ipNetV6"`
8182
}
8283
// VPNGatewayConfig configures full-tunnel VPN gateway mode.
8384
//

config/network_addr.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
const (
1111
DefaultVPNInterfaceName = "awl0"
1212
// TODO: generate subnets if this has already taken
13-
DefaultVPNNetworkSubnet = "10.66.0.1/16"
13+
DefaultVPNNetworkSubnet = "10.66.0.1/16"
14+
DefaultVPNNetworkSubnet6 = "fd00:66:0::1/48"
1415
)
1516

1617
func (c *Config) VPNLocalIPMask() (net.IP, net.IPMask) {
@@ -29,6 +30,25 @@ func (c *Config) VPNLocalIPMaskUnlocked() (net.IP, net.IPMask) {
2930
return localIP.To4(), ipNet.Mask
3031
}
3132

33+
func (c *Config) VPNLocalIPMaskV6() (net.IP, net.IPMask) {
34+
c.RLock()
35+
defer c.RUnlock()
36+
37+
return c.VPNLocalIPMaskV6Unlocked()
38+
}
39+
40+
func (c *Config) VPNLocalIPMaskV6Unlocked() (net.IP, net.IPMask) {
41+
if c.VPNConfig.IPNetV6 == "" {
42+
return nil, nil
43+
}
44+
localIP, ipNet, err := net.ParseCIDR(c.VPNConfig.IPNetV6)
45+
if err != nil {
46+
logger.Errorf("parse CIDR %s: %v", c.VPNConfig.IPNetV6, err)
47+
return nil, nil
48+
}
49+
return localIP.To16(), ipNet.Mask
50+
}
51+
3252
// GenerateNextIpAddr is not thread safe.
3353
func (c *Config) GenerateNextIpAddr() string {
3454
return c.GenerateNextIpAddrExcept(nil)

config/other.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,15 @@ func setDefaults(conf *Config, bus awlevent.Bus) {
174174
if conf.VPNConfig.IPNet == "" {
175175
conf.VPNConfig.IPNet = DefaultVPNNetworkSubnet
176176
}
177+
if conf.VPNConfig.IPNetV6 == "" {
178+
conf.VPNConfig.IPNetV6 = DefaultVPNNetworkSubnet6
179+
}
177180
if ip, _ := conf.VPNLocalIPMask(); ip == nil {
178181
conf.VPNConfig.IPNet = DefaultVPNNetworkSubnet
179182
}
183+
if ip, _ := conf.VPNLocalIPMaskV6(); ip == nil {
184+
conf.VPNConfig.IPNetV6 = DefaultVPNNetworkSubnet6
185+
}
180186
if conf.VPNConfig.InterfaceName == "" {
181187
if runtime.GOOS == "darwin" {
182188
conf.VPNConfig.InterfaceName = "utun"

0 commit comments

Comments
 (0)