summaryrefslogtreecommitdiffhomepage
path: root/tun/netstack
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2021-03-14 22:02:56 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2021-11-09 23:09:50 +0100
commitebef2c3fe37b723843fb84695a25df2d4db40038 (patch)
treeb298a7c5c24e870fdee2b4f45259eccbc6ba162f /tun/netstack
parent94e20c34249558152e75c785fa1e0a44cc1a6561 (diff)
netstack: implement Net.AddAddress
Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
Diffstat (limited to 'tun/netstack')
-rw-r--r--tun/netstack/tun.go55
1 files changed, 29 insertions, 26 deletions
diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go
index 44b5f04..f61cc2a 100644
--- a/tun/netstack/tun.go
+++ b/tun/netstack/tun.go
@@ -94,6 +94,32 @@ func (*endpoint) ARPHardwareType() header.ARPHardwareType {
func (e *endpoint) AddHeader(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
}
+func (net *Net) addAddress(ip net.IP) tcpip.Error {
+ if ip4 := ip.To4(); ip4 != nil {
+ protoAddr := tcpip.ProtocolAddress{
+ Protocol: ipv4.ProtocolNumber,
+ AddressWithPrefix: tcpip.Address(ip4).WithPrefix(),
+ }
+ tcpipErr := net.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
+ if tcpipErr == nil && !net.hasV4 {
+ net.hasV4 = true
+ net.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1})
+ }
+ return tcpipErr
+ } else {
+ protoAddr := tcpip.ProtocolAddress{
+ Protocol: ipv6.ProtocolNumber,
+ AddressWithPrefix: tcpip.Address(ip).WithPrefix(),
+ }
+ tcpipErr := net.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
+ if tcpipErr == nil && !net.hasV6{
+ net.hasV6 = true
+ net.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1})
+ }
+ return tcpipErr
+ }
+}
+
func CreateNetTUN(localAddresses, dnsServers []net.IP, mtu int) (tun.Device, *Net, error) {
opts := stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
@@ -112,34 +138,11 @@ func CreateNetTUN(localAddresses, dnsServers []net.IP, mtu int) (tun.Device, *Ne
return nil, nil, fmt.Errorf("CreateNIC: %v", tcpipErr)
}
for _, ip := range localAddresses {
- if ip4 := ip.To4(); ip4 != nil {
- protoAddr := tcpip.ProtocolAddress{
- Protocol: ipv4.ProtocolNumber,
- AddressWithPrefix: tcpip.Address(ip4).WithPrefix(),
- }
- tcpipErr := dev.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
- if tcpipErr != nil {
- return nil, nil, fmt.Errorf("AddProtocolAddress(%v): %v", ip4, tcpipErr)
- }
- dev.hasV4 = true
- } else {
- protoAddr := tcpip.ProtocolAddress{
- Protocol: ipv6.ProtocolNumber,
- AddressWithPrefix: tcpip.Address(ip).WithPrefix(),
- }
- tcpipErr := dev.stack.AddProtocolAddress(1, protoAddr, stack.AddressProperties{})
- if tcpipErr != nil {
- return nil, nil, fmt.Errorf("AddProtocolAddress(%v): %v", ip, tcpipErr)
- }
- dev.hasV6 = true
+ tcpipErr := tnet.addAddress(ip)
+ if tcpipErr != nil {
+ return nil, fmt.Errorf("addAddress(%v): %w", ip, tcpipErr)
}
}
- if dev.hasV4 {
- dev.stack.AddRoute(tcpip.Route{Destination: header.IPv4EmptySubnet, NIC: 1})
- }
- if dev.hasV6 {
- dev.stack.AddRoute(tcpip.Route{Destination: header.IPv6EmptySubnet, NIC: 1})
- }
dev.events <- tun.EventUp
return dev, (*Net)(dev), nil