Add nftables implementation of ipmasq
Signed-off-by: Dan Winship <danwinship@redhat.com>
This commit is contained in:

committed by
Casey Callendrello

parent
729dd23c40
commit
61d078645a
@ -15,111 +15,78 @@
|
||||
package ip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-iptables/iptables"
|
||||
"github.com/containernetworking/cni/pkg/types"
|
||||
"github.com/containernetworking/plugins/pkg/utils"
|
||||
)
|
||||
|
||||
// SetupIPMasq installs iptables rules to masquerade traffic
|
||||
// coming from ip of ipn and going outside of ipn
|
||||
func SetupIPMasq(ipn *net.IPNet, chain string, comment string) error {
|
||||
isV6 := ipn.IP.To4() == nil
|
||||
|
||||
var ipt *iptables.IPTables
|
||||
var err error
|
||||
var multicastNet string
|
||||
|
||||
if isV6 {
|
||||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
|
||||
multicastNet = "ff00::/8"
|
||||
} else {
|
||||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
||||
multicastNet = "224.0.0.0/4"
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to locate iptables: %v", err)
|
||||
}
|
||||
|
||||
// Create chain if doesn't exist
|
||||
exists := false
|
||||
chains, err := ipt.ListChains("nat")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list chains: %v", err)
|
||||
}
|
||||
for _, ch := range chains {
|
||||
if ch == chain {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
if err = ipt.NewChain("nat", chain); err != nil {
|
||||
return err
|
||||
// SetupIPMasqForNetwork installs rules to masquerade traffic coming from ip of ipn and
|
||||
// going outside of ipn, using a chain name based on network, ifname, and containerID. The
|
||||
// backend can be either "iptables" or "nftables"; if it is nil, then a suitable default
|
||||
// implementation will be used.
|
||||
func SetupIPMasqForNetwork(backend *string, ipn *net.IPNet, network, ifname, containerID string) error {
|
||||
if backend == nil {
|
||||
// Prefer iptables, unless only nftables is available
|
||||
defaultBackend := "iptables"
|
||||
if !utils.SupportsIPTables() && utils.SupportsNFTables() {
|
||||
defaultBackend = "nftables"
|
||||
}
|
||||
backend = &defaultBackend
|
||||
}
|
||||
|
||||
// Packets to this network should not be touched
|
||||
if err := ipt.AppendUnique("nat", chain, "-d", ipn.String(), "-j", "ACCEPT", "-m", "comment", "--comment", comment); err != nil {
|
||||
return err
|
||||
switch *backend {
|
||||
case "iptables":
|
||||
return setupIPMasqIPTables(ipn, network, ifname, containerID)
|
||||
case "nftables":
|
||||
return setupIPMasqNFTables(ipn, network, ifname, containerID)
|
||||
default:
|
||||
return fmt.Errorf("unknown ipmasq backend %q", *backend)
|
||||
}
|
||||
|
||||
// Don't masquerade multicast - pods should be able to talk to other pods
|
||||
// on the local network via multicast.
|
||||
if err := ipt.AppendUnique("nat", chain, "!", "-d", multicastNet, "-j", "MASQUERADE", "-m", "comment", "--comment", comment); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Packets from the specific IP of this network will hit the chain
|
||||
return ipt.AppendUnique("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment)
|
||||
}
|
||||
|
||||
// TeardownIPMasq undoes the effects of SetupIPMasq
|
||||
func TeardownIPMasq(ipn *net.IPNet, chain string, comment string) error {
|
||||
isV6 := ipn.IP.To4() == nil
|
||||
// TeardownIPMasqForNetwork undoes the effects of SetupIPMasqForNetwork
|
||||
func TeardownIPMasqForNetwork(ipn *net.IPNet, network, ifname, containerID string) error {
|
||||
var errs []string
|
||||
|
||||
var ipt *iptables.IPTables
|
||||
var err error
|
||||
// Do both the iptables and the nftables cleanup, since the pod may have been
|
||||
// created with a different version of this plugin or a different configuration.
|
||||
|
||||
if isV6 {
|
||||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv6)
|
||||
} else {
|
||||
ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to locate iptables: %v", err)
|
||||
err := teardownIPMasqIPTables(ipn, network, ifname, containerID)
|
||||
if err != nil && utils.SupportsIPTables() {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.IP.String(), "-j", chain, "-m", "comment", "--comment", comment)
|
||||
if err != nil && !isNotExist(err) {
|
||||
return err
|
||||
err = teardownIPMasqNFTables(ipn, network, ifname, containerID)
|
||||
if err != nil && utils.SupportsNFTables() {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
// for downward compatibility
|
||||
err = ipt.Delete("nat", "POSTROUTING", "-s", ipn.String(), "-j", chain, "-m", "comment", "--comment", comment)
|
||||
if err != nil && !isNotExist(err) {
|
||||
return err
|
||||
if errs == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = ipt.ClearChain("nat", chain)
|
||||
if err != nil && !isNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ipt.DeleteChain("nat", chain)
|
||||
if err != nil && !isNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return errors.New(strings.Join(errs, "\n"))
|
||||
}
|
||||
|
||||
// isNotExist returnst true if the error is from iptables indicating
|
||||
// that the target does not exist.
|
||||
func isNotExist(err error) bool {
|
||||
e, ok := err.(*iptables.Error)
|
||||
if !ok {
|
||||
return false
|
||||
// GCIPMasqForNetwork garbage collects stale IPMasq entries for network
|
||||
func GCIPMasqForNetwork(network string, attachments []types.GCAttachment) error {
|
||||
var errs []string
|
||||
|
||||
err := gcIPMasqIPTables(network, attachments)
|
||||
if err != nil && utils.SupportsIPTables() {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
return e.IsNotExist()
|
||||
|
||||
err = gcIPMasqNFTables(network, attachments)
|
||||
if err != nil && utils.SupportsNFTables() {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
|
||||
if errs == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New(strings.Join(errs, "\n"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user