|
|
@@ -20,6 +20,7 @@ import (
|
|
|
"sort"
|
|
|
|
|
|
"github.com/squat/kilo/pkg/encapsulation"
|
|
|
+ "github.com/squat/kilo/pkg/iptables"
|
|
|
"github.com/squat/kilo/pkg/wireguard"
|
|
|
"github.com/vishvananda/netlink"
|
|
|
"golang.org/x/sys/unix"
|
|
|
@@ -432,6 +433,31 @@ func (t *Topology) PeerConf(name string) *wireguard.Conf {
|
|
|
return c
|
|
|
}
|
|
|
|
|
|
+// Rules returns the iptables rules required by the local node.
|
|
|
+func (t *Topology) Rules(cni bool) []iptables.Rule {
|
|
|
+ var rules []iptables.Rule
|
|
|
+ rules = append(rules, iptables.NewChain("nat", "KILO-NAT"))
|
|
|
+ if cni {
|
|
|
+ rules = append(rules, iptables.NewRule("nat", "POSTROUTING", "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-s", t.subnet.String(), "-j", "KILO-NAT"))
|
|
|
+ }
|
|
|
+ for _, s := range t.segments {
|
|
|
+ rules = append(rules, iptables.NewRule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for WireGuared IPs", "-d", s.wireGuardIP.String(), "-j", "RETURN"))
|
|
|
+ for _, aip := range s.allowedIPs {
|
|
|
+ rules = append(rules, iptables.NewRule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for known IPs", "-d", aip.String(), "-j", "RETURN"))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, p := range t.peers {
|
|
|
+ for _, aip := range p.AllowedIPs {
|
|
|
+ rules = append(rules,
|
|
|
+ iptables.NewRule("nat", "POSTROUTING", "-m", "comment", "--comment", "Kilo: jump to NAT chain", "-s", aip.String(), "-j", "KILO-NAT"),
|
|
|
+ iptables.NewRule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: do not NAT packets destined for peers", "-d", aip.String(), "-j", "RETURN"),
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rules = append(rules, iptables.NewRule("nat", "KILO-NAT", "-m", "comment", "--comment", "Kilo: NAT remaining packets", "-j", "MASQUERADE"))
|
|
|
+ return rules
|
|
|
+}
|
|
|
+
|
|
|
// oneAddressCIDR takes an IP address and returns a CIDR
|
|
|
// that contains only that address.
|
|
|
func oneAddressCIDR(ip net.IP) *net.IPNet {
|