summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/tcpip')
-rw-r--r--pkg/tcpip/config/config.go68
-rw-r--r--pkg/tcpip/sample/wg_tunnel/main.go38
2 files changed, 55 insertions, 51 deletions
diff --git a/pkg/tcpip/config/config.go b/pkg/tcpip/config/config.go
index 3c1f91ecc..19e8711db 100644
--- a/pkg/tcpip/config/config.go
+++ b/pkg/tcpip/config/config.go
@@ -9,6 +9,7 @@ import (
"net"
"os"
"runtime"
+ "sort"
"strconv"
"strings"
@@ -64,7 +65,9 @@ type Tuntap struct {
type WireguardKey []byte
-type Routes []tcpip.Route
+type Config struct {
+ routes []tcpip.Route
+}
func (wgKey *WireguardKey) UnmarshalYAML(value *yaml.Node) error{
key, err := base64.StdEncoding.DecodeString(value.Value)
@@ -326,7 +329,7 @@ func ParseSubnet(subnetName string) (tcpip.Address, tcpip.Subnet, tcpip.NetworkP
return addr, subnet, proto
}
-func (routes *Routes) AddAddress(s *stack.Stack, nic tcpip.NICID, addrName string) tcpip.NetworkProtocolNumber {
+func (config *Config) AddAddress(s *stack.Stack, nic tcpip.NICID, addrName string) tcpip.NetworkProtocolNumber {
// // Parse the IP address. Support both ipv4 and ipv6.
addr, subnet, proto := ParseSubnet(addrName)
@@ -353,7 +356,7 @@ func (routes *Routes) AddAddress(s *stack.Stack, nic tcpip.NICID, addrName strin
NIC: nic,
}
- *routes = append(*routes, route)
+ config.routes = append(config.routes, route)
}
// subnet, err := tcpip.NewSubnet(tcpip.Address(parsedNet.IP),
@@ -365,7 +368,7 @@ func (routes *Routes) AddAddress(s *stack.Stack, nic tcpip.NICID, addrName strin
return proto
}
-func (routes *Routes) addRoute(nic tcpip.NICID, routeCfg Route){
+func (config *Config) addRoute(nic tcpip.NICID, routeCfg Route){
_, dest, _ := ParseSubnet(routeCfg.To)
via, _ := parseAddress(routeCfg.Via)
@@ -377,20 +380,30 @@ func (routes *Routes) addRoute(nic tcpip.NICID, routeCfg Route){
Markmask: routeCfg.Markmask,
}
- *routes = append(*routes, route)
+ config.routes = append(config.routes, route)
}
-func (routes *Routes) setupCommon(s *stack.Stack, nic tcpip.NICID, id string, cfg Common) {
+func (config *Config) AddRoute(nic tcpip.NICID, prefix string) {
+ _, dest, _ := ParseSubnet(prefix)
+ route := tcpip.Route{
+ Destination: dest,
+ NIC: nic,
+ }
+
+ config.routes = append(config.routes, route)
+}
+
+func (config *Config) setupCommon(s *stack.Stack, nic tcpip.NICID, id string, cfg Common) {
for _, addr := range cfg.Addresses {
- routes.AddAddress(s, nic, addr)
+ config.AddAddress(s, nic, addr)
}
for _, route := range cfg.Routes {
fmt.Println("Add Route:", route)
- routes.addRoute(nic, route)
+ config.addRoute(nic, route)
}
- for _, route := range *routes {
+ for _, route := range config.routes {
fmt.Println("Added Route:", route)
}
@@ -400,7 +413,7 @@ func (routes *Routes) setupCommon(s *stack.Stack, nic tcpip.NICID, id string, cf
// }
}
-func (routes *Routes) setupLoopback(s *stack.Stack, nic tcpip.NICID, id string, eth *Ethernet) {
+func (config *Config) setupLoopback(s *stack.Stack, nic tcpip.NICID, id string, eth *Ethernet) {
fmt.Println("Ethernet", id, nic, eth)
linkEP := loopback.New()
@@ -408,10 +421,10 @@ func (routes *Routes) setupLoopback(s *stack.Stack, nic tcpip.NICID, id string,
log.Fatal("CreateNIC", err)
}
- routes.setupCommon(s, nic, id, eth.Common)
+ config.setupCommon(s, nic, id, eth.Common)
}
-func (routes *Routes) setupTunnel(s *stack.Stack, nic tcpip.NICID, id string, tun *Tunnel) {
+func (config *Config) setupTunnel(s *stack.Stack, nic tcpip.NICID, id string, tun *Tunnel) {
fmt.Println("TUN", id, nic, tun)
maddr, err := net.ParseMAC(tun.Macaddress)
@@ -421,11 +434,11 @@ func (routes *Routes) setupTunnel(s *stack.Stack, nic tcpip.NICID, id string, tu
addRouterLink(s, nic, id, tcpip.LinkAddress(maddr), tun)
fmt.Println("Tunnel 20", tun.Conn)
- routes.setupCommon(s, nic, id, tun.Common)
+ config.setupCommon(s, nic, id, tun.Common)
fmt.Println("Tunnel 21", tun.Conn)
}
-func (routes *Routes) setupTuntap(s *stack.Stack, nic tcpip.NICID, id string, tun *Tuntap) {
+func (config *Config) setupTuntap(s *stack.Stack, nic tcpip.NICID, id string, tun *Tuntap) {
fmt.Println("Tuntap", id, nic, tun)
maddr, err := net.ParseMAC(tun.Macaddress)
@@ -444,10 +457,10 @@ func (routes *Routes) setupTuntap(s *stack.Stack, nic tcpip.NICID, id string, tu
}
addTunLink(s, nic, id, tap, tcpip.LinkAddress(maddr), tun)
- routes.setupCommon(s, nic, id, tun.Common)
+ config.setupCommon(s, nic, id, tun.Common)
}
-func (routes *Routes) setupWG(s *stack.Stack, nic tcpip.NICID, id string, wg *Wireguard) {
+func (config *Config) setupWG(s *stack.Stack, nic tcpip.NICID, id string, wg *Wireguard) {
fmt.Println("WG", id, nic, wg.ListenPort, wg)
fmt.Printf("Peers %v\n", wg.Peers)
@@ -474,7 +487,7 @@ func (routes *Routes) setupWG(s *stack.Stack, nic tcpip.NICID, id string, wg *Wi
fmt.Println("IpcSetOperation", str)
device.IpcSetOperation(bufio.NewReader(strings.NewReader(str)))
- routes.setupCommon(s, nic, id, wg.Common)
+ config.setupCommon(s, nic, id, wg.Common)
go func() {
fmt.Println("Starting ", nic)
@@ -489,7 +502,16 @@ func (routes *Routes) setupWG(s *stack.Stack, nic tcpip.NICID, id string, wg *Wi
}()
}
-func (routes *Routes) Setup(s *stack.Stack, np *Netplan) {
+func (config *Config) SetRouteTable(s *stack.Stack) {
+ // Sort route table for longest prefix match
+ sort.Slice(config.routes, func(i, j int) bool {
+ return config.routes[i].Destination.Prefix() > config.routes[j].Destination.Prefix()
+ })
+
+ s.SetRouteTable(config.routes)
+}
+
+func (config *Config) Setup(s *stack.Stack, np *Netplan) {
s.SetForwarding(true)
var nic tcpip.NICID = -1
@@ -497,12 +519,12 @@ func (routes *Routes) Setup(s *stack.Stack, np *Netplan) {
for id, tun := range np.Network.Ethernets {
nic = nic + 1
- routes.setupLoopback(s, nic, id, tun)
+ config.setupLoopback(s, nic, id, tun)
}
for id, tun := range np.Network.Tuntaps {
nic = nic + 1
- routes.setupTuntap(s, nic, id, tun)
+ config.setupTuntap(s, nic, id, tun)
}
for id, wg := range np.Network.Wireguards {
@@ -510,12 +532,12 @@ func (routes *Routes) Setup(s *stack.Stack, np *Netplan) {
// if id == "wg2" {
// wg2Nic = nic
// }
- routes.setupWG(s, nic, id, wg)
+ config.setupWG(s, nic, id, wg)
}
for id, tun := range np.Network.Tunnels {
nic = nic + 1
- routes.setupTunnel(s, nic, id, tun)
+ config.setupTunnel(s, nic, id, tun)
}
nicCount := nic
@@ -523,4 +545,6 @@ func (routes *Routes) Setup(s *stack.Stack, np *Netplan) {
for nic = 0; nic < nicCount; nic++ {
s.EnableNIC(nic)
}
+
+ config.SetRouteTable(s)
}
diff --git a/pkg/tcpip/sample/wg_tunnel/main.go b/pkg/tcpip/sample/wg_tunnel/main.go
index 066216dfb..47d0b40dc 100644
--- a/pkg/tcpip/sample/wg_tunnel/main.go
+++ b/pkg/tcpip/sample/wg_tunnel/main.go
@@ -29,7 +29,6 @@ import (
"net"
"os"
"runtime"
- "sort"
"strconv"
"time"
@@ -248,7 +247,7 @@ func NewDHCPv4Query(flags uint32, modifiers ...dhcpv6.Modifier) (*dhcpv6.Message
return msg, nil
}
-func doClient(s *stack.Stack, routes *config.Routes, nic tcpip.NICID) {
+func doClient(s *stack.Stack, cfg *config.Config, nic tcpip.NICID) {
fmt.Println("doClient start")
// TODO use link local address
@@ -361,20 +360,13 @@ func doClient(s *stack.Stack, routes *config.Routes, nic tcpip.NICID) {
// client.Close()
fmt.Println("doClient end", ack.YourIPAddr, ack.SubnetMask())
ip := net.IPNet{IP: ack.YourIPAddr, Mask:ack.SubnetMask()}
- routes.AddAddress(s, nic, ip.String())
+ cfg.AddAddress(s, nic, ip.String())
iana := msg.GetOneOption(dhcpv6.OptionIANA).(*dhcpv6.OptIANA)
for _, addr := range iana.Options.Get(dhcpv6.OptionIAAddr) {
str := addr.(*dhcpv6.OptIAAddress).IPv6Addr.String() + "/128"
- routes.AddAddress(s, nic, str)
-
- _, dest, _ := config.ParseSubnet(addr.(*dhcpv6.OptIAAddress).IPv6Addr.String() + "/64")
- route := tcpip.Route{
- Destination: dest,
- NIC: nic,
- }
-
- *routes = append(*routes, route)
+ cfg.AddAddress(s, nic, str)
+ cfg.AddRoute(nic, addr.(*dhcpv6.OptIAAddress).IPv6Addr.String() + "/64")
}
var loNic tcpip.NICID = 0
@@ -382,7 +374,7 @@ func doClient(s *stack.Stack, routes *config.Routes, nic tcpip.NICID) {
for _, opt := range iapd.Options.Get(dhcpv6.OptionIAPrefix) {
prefix := opt.(*dhcpv6.OptIAPrefix)
str := prefix.Prefix.IP.String()+"1"+"/128"
- routes.AddAddress(s, loNic, str)
+ cfg.AddAddress(s, loNic, str)
}
dumpAddresses(s)
@@ -455,7 +447,7 @@ func main() {
log.Fatalf("Unable to convert port %v: %v", portName, err)
}
- routes := config.Routes{}
+ cfg := config.Config{}
// Create the stack with ip and tcp protocols, then add a tun-based
// NIC and address.
@@ -471,30 +463,18 @@ func main() {
})
// FIXME enable
- routes.Setup(s, &np)
+ cfg.Setup(s, &np)
KeepAliveTunnel(&np)
- // Sort route table for longest prefix match
- sort.Slice(routes, func(i, j int) bool {
- return routes[i].Destination.Prefix() > routes[j].Destination.Prefix()
- })
-
- s.SetRouteTable(routes)
-
// FIXME disabled for now, to test startSolicitingRouters
if false {
// FIXME
var wg2Nic tcpip.NICID = -1
- doClient(s, &routes, wg2Nic)
+ doClient(s, &cfg, wg2Nic)
}
- // Sort route table for longest prefix match
- sort.Slice(routes, func(i, j int) bool {
- return routes[i].Destination.Prefix() > routes[j].Destination.Prefix()
- })
-
- s.SetRouteTable(routes)
+ cfg.SetRouteTable(s)
dumpAddresses(s)
dumpRoutes(s)