Skip to content

Commit

Permalink
Added Nftables compatibility to containerboot, create the ts-prerouti…
Browse files Browse the repository at this point in the history
…ng chain & use nft to add rules
  • Loading branch information
joesankey committed Aug 11, 2023
1 parent 10acc06 commit b852f9d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.base
Expand Up @@ -2,4 +2,4 @@
# SPDX-License-Identifier: BSD-3-Clause

FROM alpine:3.16
RUN apk add --no-cache ca-certificates iptables iproute2 ip6tables iputils
RUN apk add --no-cache ca-certificates iptables iproute2 ip6tables iputils nftables
7 changes: 2 additions & 5 deletions cmd/containerboot/main.go
Expand Up @@ -497,10 +497,7 @@ func installIPTablesRule(ctx context.Context, dstStr string, tsIPs []netip.Prefi
if err != nil {
return err
}
argv0 := "iptables"
if dst.Is6() {
argv0 = "ip6tables"
}

var local string
for _, pfx := range tsIPs {
if !pfx.IsSingleIP() {
Expand All @@ -518,7 +515,7 @@ func installIPTablesRule(ctx context.Context, dstStr string, tsIPs []netip.Prefi
// Technically, if the control server ever changes the IPs assigned to this
// node, we'll slowly accumulate iptables rules. This shouldn't happen, so
// for now we'll live with it.
cmd := exec.CommandContext(ctx, argv0, "-t", "nat", "-I", "PREROUTING", "1", "-d", local, "-j", "DNAT", "--to-destination", dstStr)
cmd := exec.CommandContext(ctx, "nft", "add", "rule", "ip", "ts-nat", "ts-prerouting", "ip", "daddr", local, "dnat", dstStr)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions util/linuxfw/nftables_runner.go
Expand Up @@ -24,6 +24,7 @@ const (
chainNameForward = "ts-forward"
chainNameInput = "ts-input"
chainNamePostrouting = "ts-postrouting"
chainNamePrerouting = "ts-prerouting"
)

type chainInfo struct {
Expand Down Expand Up @@ -411,6 +412,9 @@ func (n *nftablesRunner) AddChains() error {
if err = createChainIfNotExist(n.conn, chainInfo{nat, chainNamePostrouting, nftables.ChainTypeNAT, nftables.ChainHookPostrouting, nftables.ChainPriorityNATDest}); err != nil {
return fmt.Errorf("create postrouting chain: %w", err)
}
if err = createChainIfNotExist(n.conn, chainInfo{nat, chainNamePrerouting, nftables.ChainTypeNAT, nftables.ChainHookPrerouting, nftables.ChainPriorityNATDest}); err != nil {
return fmt.Errorf("create prerouting chain: %w", err)
}
}

return n.conn.Flush()
Expand Down

0 comments on commit b852f9d

Please sign in to comment.