summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/config/config.go
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2020-03-18 21:46:40 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2020-03-18 21:46:40 +0100
commit13a2f2ef6cfac923b1382d6939646f8dfd22e3df (patch)
tree5f72638566a1fbdbc67654739d146c4c6e62d9fc /pkg/tcpip/config/config.go
parent1d171254911cfcd62926f43938edde2ad20e64fc (diff)
WIP create Config struct
Diffstat (limited to 'pkg/tcpip/config/config.go')
-rw-r--r--pkg/tcpip/config/config.go68
1 files changed, 46 insertions, 22 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)
}