Skip to content

Commit 87a5a38

Browse files
committed
move nft pkg to netbase
- remove nft pkgs from both network/netlight pkgs - move lansecurity rules template from cmds
1 parent 6e42ba2 commit 87a5a38

9 files changed

Lines changed: 186 additions & 158 deletions

File tree

pkg/netbase/nft/lansecurity.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
flush chain inet filter forward;
2+
3+
table inet filter {
4+
chain forward {
5+
type filter hook forward priority filter; policy accept;
6+
7+
# @th,16,16 is raw expression for sport/dport in transport header
8+
# used due to limitation on the installed nft v0.9.1
9+
meta l4proto { tcp, udp } @th,16,16 { 9650, 9651 } accept;
10+
11+
# accept traffic to only default gateway
12+
ip daddr {{.GatewayIP}} accept;
13+
14+
# drop traffic to all other ips on the subnet
15+
ip daddr {{.SubnetIP}} drop;
16+
}
17+
}

pkg/netbase/nft/nft.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package nft
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os/exec"
7+
8+
_ "embed"
9+
10+
"github.com/containernetworking/plugins/pkg/ns"
11+
"github.com/pkg/errors"
12+
"github.com/rs/zerolog/log"
13+
"github.com/threefoldtech/zosbase/pkg/netlight/namespace"
14+
"github.com/vishvananda/netlink"
15+
)
16+
17+
//go:embed lansecurity.tmpl
18+
var lanSecurityTmpl string
19+
20+
// Apply applies the ntf configuration contained in the reader r
21+
// if ns is specified, the nft command is execute in the network namespace names ns
22+
func Apply(r io.Reader, ns string) error {
23+
var cmd *exec.Cmd
24+
25+
if ns != "" {
26+
cmd = exec.Command("ip", "netns", "exec", ns, "nft", "-f", "-")
27+
} else {
28+
cmd = exec.Command("nft", "-f", "-")
29+
}
30+
31+
cmd.Stdin = r
32+
33+
out, err := cmd.CombinedOutput()
34+
if err != nil {
35+
log.Error().Err(err).Str("output", string(out)).Msg("error during nft")
36+
if eerr, ok := err.(*exec.ExitError); ok {
37+
return errors.Wrapf(err, "failed to execute nft: %v", string(eerr.Stderr))
38+
}
39+
return errors.Wrap(err, "failed to execute nft")
40+
}
41+
return nil
42+
}
43+
44+
// DropTrafficToLAN drops all the outgoing traffic to any peers on
45+
// the same lan network, but allow dicovery port for ygg/myc by accepting
46+
// traffic to/from dest/src ports.
47+
func DropTrafficToLAN(netns string) error {
48+
var dgw netlink.Neigh
49+
50+
toRun := func(_ ns.NetNS) error {
51+
var err error
52+
dgw, err = getDefaultGW()
53+
return err
54+
}
55+
56+
if netns != "" {
57+
nss, err := namespace.GetByName(netns)
58+
if err != nil {
59+
return fmt.Errorf("failed to get namespace %q", netns)
60+
}
61+
defer nss.Close()
62+
63+
if err := nss.Do(toRun); err != nil {
64+
return fmt.Errorf("failed to execute in namespace %q: %w", netns, err)
65+
}
66+
} else {
67+
if err := toRun(nil); err != nil {
68+
return fmt.Errorf("failed to get default gateway: %w", err)
69+
}
70+
}
71+
72+
if !dgw.IP.IsPrivate() {
73+
log.Warn().Msg("skip LAN security. default gateway is public")
74+
return nil
75+
}
76+
77+
rules, err := renderRulesTemplate(lanSecurityTmpl, dgw)
78+
if err != nil {
79+
return fmt.Errorf("failed to render nft rules template: %w", err)
80+
}
81+
82+
return Apply(rules, netns)
83+
}

pkg/netbase/nft/utils.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package nft
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"text/template"
8+
9+
"github.com/pkg/errors"
10+
"github.com/rs/zerolog/log"
11+
"github.com/vishvananda/netlink"
12+
)
13+
14+
func getDefaultGW() (netlink.Neigh, error) {
15+
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
16+
if err != nil {
17+
return netlink.Neigh{}, fmt.Errorf("failed to list routes: %v", err)
18+
}
19+
20+
var defaultRoute *netlink.Route
21+
for _, route := range routes {
22+
if route.Dst == nil {
23+
defaultRoute = &route
24+
break
25+
}
26+
}
27+
28+
if defaultRoute == nil {
29+
return netlink.Neigh{}, fmt.Errorf("default route not found")
30+
}
31+
32+
if defaultRoute.Gw == nil {
33+
return netlink.Neigh{}, fmt.Errorf("default route has no gateway")
34+
}
35+
36+
neighs, err := netlink.NeighList(0, netlink.FAMILY_V4)
37+
if err != nil {
38+
return netlink.Neigh{}, fmt.Errorf("failed to list neighbors: %v", err)
39+
}
40+
41+
for _, neigh := range neighs {
42+
if neigh.IP.Equal(defaultRoute.Gw) {
43+
return neigh, nil
44+
}
45+
}
46+
47+
return netlink.Neigh{}, errors.New("failed to get default gw")
48+
}
49+
50+
func getNetworkRange(ip netlink.Neigh) string {
51+
mask := ip.IP.DefaultMask()
52+
network := ip.IP.Mask(mask)
53+
ones, _ := mask.Size()
54+
networkRange := fmt.Sprintf("%s/%d", network.String(), ones)
55+
56+
return networkRange
57+
}
58+
59+
func renderRulesTemplate(tmpl string, gateway netlink.Neigh) (io.Reader, error) {
60+
GatewayIP := gateway.IP.String()
61+
SubnetIP := getNetworkRange(gateway)
62+
63+
log.Debug().
64+
Str("GatewayIP", GatewayIP).
65+
Str("SubnetIP", SubnetIP).
66+
Msg("drop traffic to lan with the default gateway")
67+
68+
templ, err := template.New("lanSecurityRules").Parse(tmpl)
69+
if err != nil {
70+
return nil, fmt.Errorf("failed to create template: %w", err)
71+
}
72+
73+
var buf bytes.Buffer
74+
if err := templ.Execute(&buf, map[string]string{
75+
"GatewayIP": GatewayIP,
76+
"SubnetIP": SubnetIP,
77+
}); err != nil {
78+
return nil, fmt.Errorf("failed to execute template: %w", err)
79+
}
80+
81+
return &buf, nil
82+
}

pkg/netlight/nft/nft.go

Lines changed: 0 additions & 120 deletions
This file was deleted.

pkg/netlight/resource/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/threefoldtech/zosbase/pkg/netlight/bridge"
1515
"github.com/threefoldtech/zosbase/pkg/netlight/ifaceutil"
1616
"github.com/threefoldtech/zosbase/pkg/netlight/namespace"
17-
"github.com/threefoldtech/zosbase/pkg/netlight/nft"
1817
"github.com/threefoldtech/zosbase/pkg/netlight/options"
1918
"github.com/threefoldtech/zosbase/pkg/netlight/tuntap"
19+
"github.com/threefoldtech/zosbase/pkg/netbase/nft"
2020
"github.com/threefoldtech/zosbase/pkg/zinit"
2121
"github.com/vishvananda/netlink"
2222
)

pkg/network/ndmz/dualstack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"github.com/cenkalti/backoff/v3"
1414
"github.com/threefoldtech/zosbase/pkg/gridtypes"
1515
"github.com/threefoldtech/zosbase/pkg/kernel"
16+
"github.com/threefoldtech/zosbase/pkg/netbase/nft"
1617
"github.com/threefoldtech/zosbase/pkg/network/bridge"
1718
"github.com/threefoldtech/zosbase/pkg/network/dhcp"
1819
"github.com/threefoldtech/zosbase/pkg/network/ifaceutil"
19-
"github.com/threefoldtech/zosbase/pkg/network/nft"
2020
"github.com/threefoldtech/zosbase/pkg/network/options"
2121
"github.com/threefoldtech/zosbase/pkg/network/types"
2222
"github.com/threefoldtech/zosbase/pkg/network/yggdrasil"

pkg/network/nft/nft.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

pkg/network/nr/net_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"github.com/containernetworking/plugins/pkg/ns"
2929
"github.com/rs/zerolog/log"
3030
"github.com/threefoldtech/zosbase/pkg"
31+
"github.com/threefoldtech/zosbase/pkg/netbase/nft"
3132
"github.com/threefoldtech/zosbase/pkg/network/bridge"
3233
"github.com/threefoldtech/zosbase/pkg/network/namespace"
33-
"github.com/threefoldtech/zosbase/pkg/network/nft"
3434
"github.com/threefoldtech/zosbase/pkg/network/wireguard"
3535
"github.com/vishvananda/netlink"
3636
)

pkg/network/qsfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
"github.com/pkg/errors"
88
"github.com/rs/zerolog/log"
9+
"github.com/threefoldtech/zosbase/pkg/netbase/nft"
910
"github.com/threefoldtech/zosbase/pkg/network/ifaceutil"
1011
"github.com/threefoldtech/zosbase/pkg/network/namespace"
11-
"github.com/threefoldtech/zosbase/pkg/network/nft"
1212
)
1313

1414
var _nft = `

0 commit comments

Comments
 (0)