Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/acl for forward #1305

Merged
merged 96 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
7ac3f98
Replace input, output to pre-post-routing
pappz Nov 3, 2023
393bc37
Set chain type to nat
pappz Nov 6, 2023
2700f7e
Revert change and remove IPv6
pappz Nov 9, 2023
d2efff1
Refactor input/output logic
pappz Nov 10, 2023
57019dd
Rename tables and chains
pappz Nov 10, 2023
a1864dd
Fix interface names in fwd chain
pappz Nov 13, 2023
820c72d
Simplify the table check
pappz Nov 13, 2023
227017b
Error handling
pappz Nov 13, 2023
2202237
Restriction for NetBird address in fwd chain
pappz Nov 13, 2023
12c8de7
Try to fix fwd policy
pappz Nov 13, 2023
201a5c2
Try to fix forward-forward pair
pappz Nov 13, 2023
4cd0aad
Remove ipv6 related codes from fw, acl, route mgr
pappz Nov 15, 2023
d4014da
Move fw check to firewall pkg
pappz Nov 15, 2023
156e84e
Refactor acl and routemanager packages
pappz Nov 16, 2023
aa09658
Implement clean up for nftable
pappz Nov 17, 2023
a5a1de3
Handle allow list for route rules
pappz Nov 17, 2023
736f37d
Delete garbage forward rules
pappz Nov 17, 2023
5d53ea2
Fix acl test
pappz Nov 17, 2023
e1b420e
Remove comment
pappz Nov 17, 2023
7d2fc4e
Apply router chains in acl accept chain
pappz Nov 17, 2023
e5b8f9c
Simplify jump rules
pappz Nov 17, 2023
dbdd7ec
Refactor manager_linux.go
pappz Nov 17, 2023
a76c46f
Fix port flush
pappz Nov 18, 2023
ab2bfe9
Fix route delete operation
pappz Nov 18, 2023
50f13d0
Remove ipv6 calculation
pappz Nov 20, 2023
192cd69
Fix ipset usage
pappz Nov 20, 2023
7f76e3e
Add prerouting, mangle filter
pappz Nov 21, 2023
337aeb3
Fix icmp handling
pappz Nov 21, 2023
2f60d94
Add fwd allow
pappz Nov 22, 2023
7f03ca6
Fix filter table cleanup
pappz Nov 22, 2023
ede1884
Fix nft route test
pappz Nov 22, 2023
1ab39bf
Fix usp testing
pappz Nov 22, 2023
ea5a882
Fix iptables router tests
pappz Nov 22, 2023
b6c281a
Fix iptables manager test
pappz Nov 22, 2023
a9441f8
Extend prerouting
pappz Nov 22, 2023
3135c24
Fix ipset handling
pappz Nov 22, 2023
8ce1704
fix test
pappz Nov 22, 2023
0a200e7
fix test
pappz Nov 22, 2023
f3f4977
Code cleaning
pappz Nov 23, 2023
65d0ac8
Code cleaning
pappz Nov 23, 2023
265aeb0
Fix Android build
pappz Nov 23, 2023
ca5d629
Fix fmt format
pappz Nov 23, 2023
f2e02e8
Prevent ipv6 usage
pappz Nov 23, 2023
e84fb09
Remove unused logic from ruleset manager
pappz Nov 23, 2023
f96e40f
Fix error handling for Android build
pappz Nov 23, 2023
34b7454
Fix android build
pappz Nov 23, 2023
1885226
Lint fix
pappz Nov 23, 2023
aa06835
Lint fix
pappz Nov 23, 2023
3152b14
Fix ip check
pappz Nov 23, 2023
96c9612
Fix android build
pappz Nov 23, 2023
96ad2ca
Allowed to work without table filter
pappz Nov 23, 2023
8261ecc
Fix iptables initalization
pappz Nov 23, 2023
5007480
Build acl for linux only
pappz Nov 23, 2023
9423845
Remove unused variable
pappz Nov 23, 2023
2a7ce73
Write better debug msg
pappz Nov 23, 2023
53368dc
Remove ipv6 field from rule struct
pappz Nov 23, 2023
de843d7
Code cleaning
pappz Nov 27, 2023
f135372
Update in iptables the cleanup, restore procedures
pappz Nov 27, 2023
b984a98
Add mangle.prerouting
pappz Nov 28, 2023
04480b8
Fix router cleanup
pappz Nov 28, 2023
dfc583a
Fix some parts of the tests
pappz Nov 28, 2023
6fb6e70
fix ipset list handling
pappz Nov 28, 2023
d0b6d75
code cleaning
pappz Nov 28, 2023
4117b89
code cleaning
pappz Nov 28, 2023
a4d29e7
Fix typo
pappz Nov 29, 2023
0834b23
Add prerouting rules
pappz Nov 29, 2023
96ac03c
Cleanup mangle table
pappz Nov 29, 2023
a74578d
Fix rule deletion in iptables
pappz Nov 29, 2023
95adf23
Fix iptables router tests
pappz Nov 30, 2023
2021557
Fix tests
pappz Nov 30, 2023
2819bb3
Remove unused code
pappz Nov 30, 2023
7ff83e1
Fix nil ipset handling
pappz Nov 30, 2023
77a50a6
Fix reset tables in route
pappz Nov 30, 2023
39f8a01
Fix ipset expression in mangle table
pappz Nov 30, 2023
542361f
add debug lines
pappz Nov 30, 2023
b33666c
Fix route cleanup
pappz Nov 30, 2023
8c9cd25
Fix test
pappz Nov 30, 2023
a5d72cc
Fix delete rule
pappz Dec 1, 2023
2f00ae9
Fix fw creation error handling
pappz Dec 1, 2023
a9d62db
Fix prerouting filter rules
pappz Dec 1, 2023
6ff81fc
Fix firewall creation in engine for Darwin
pappz Dec 2, 2023
8b4229c
Print dns manager type with strings
pappz Dec 4, 2023
a686513
Fix routeManager initialization for Android
pappz Dec 4, 2023
956d1b8
Fix test
pappz Dec 4, 2023
bbafad7
Fix usp firewall error handling
pappz Dec 4, 2023
6d1ca75
Change debug level on some error line
pappz Dec 4, 2023
7c6150d
remove debug logs
pappz Dec 4, 2023
83ba9d7
Remove debug line from dns
pappz Dec 5, 2023
c74ed35
Fix reference to nft's handler
pappz Dec 5, 2023
3861c9a
Add default route allow rule
pappz Dec 5, 2023
c5c3eff
Fix add prerouting check
pappz Dec 5, 2023
3a844dd
Fix userdata id
pappz Dec 5, 2023
5983ef1
Add routeing fix for iptables
pappz Dec 5, 2023
cdcf6e1
Remove unnecessary allow routing
pappz Dec 5, 2023
383fa7b
Merge branch 'main' into fix/acl-for-forward
pappz Dec 6, 2023
5efc565
Fix
pappz Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions client/firewall/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !linux || android

package firewall

import (
"context"
"fmt"
"runtime"

log "github.com/sirupsen/logrus"

firewall "github.com/netbirdio/netbird/client/firewall/manager"
"github.com/netbirdio/netbird/client/firewall/uspfilter"
)

// NewFirewall creates a firewall manager instance
func NewFirewall(context context.Context, iface IFaceMapper) (firewall.Manager, error) {
if !iface.IsUserspaceBind() {
return nil, fmt.Errorf("not implemented for this OS: %s", runtime.GOOS)
}

// use userspace packet filtering firewall
fm, err := uspfilter.Create(iface)
if err != nil {
return nil, err
}
err = fm.AllowNetbird()
if err != nil {
log.Warnf("failed to allow netbird interface traffic: %v", err)
}
return fm, nil
}
107 changes: 107 additions & 0 deletions client/firewall/create_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//go:build !android

package firewall

import (
"context"
"fmt"
"os"

"github.com/coreos/go-iptables/iptables"
"github.com/google/nftables"
log "github.com/sirupsen/logrus"

nbiptables "github.com/netbirdio/netbird/client/firewall/iptables"
firewall "github.com/netbirdio/netbird/client/firewall/manager"
nbnftables "github.com/netbirdio/netbird/client/firewall/nftables"
"github.com/netbirdio/netbird/client/firewall/uspfilter"
)

const (
// UNKNOWN is the default value for the firewall type for unknown firewall type
UNKNOWN FWType = iota
// IPTABLES is the value for the iptables firewall type
IPTABLES
// NFTABLES is the value for the nftables firewall type
NFTABLES
)

// SKIP_NFTABLES_ENV is the environment variable to skip nftables check
const SKIP_NFTABLES_ENV = "NB_SKIP_NFTABLES_CHECK"

// FWType is the type for the firewall type
type FWType int

func NewFirewall(context context.Context, iface IFaceMapper) (firewall.Manager, error) {
// on the linux system we try to user nftables or iptables
// in any case, because we need to allow netbird interface traffic
// so we use AllowNetbird traffic from these firewall managers
// for the userspace packet filtering firewall
var fm firewall.Manager
var errFw error

switch check() {
case IPTABLES:
log.Debug("creating an iptables firewall manager")
fm, errFw = nbiptables.Create(context, iface)
if errFw != nil {
log.Errorf("failed to create iptables manager: %s", errFw)
}
case NFTABLES:
log.Debug("creating an nftables firewall manager")
fm, errFw = nbnftables.Create(context, iface)
if errFw != nil {
log.Errorf("failed to create nftables manager: %s", errFw)
}
default:
errFw = fmt.Errorf("no firewall manager found")
log.Debug("no firewall manager found, try to use userspace packet filtering firewall")
}

if iface.IsUserspaceBind() {
var errUsp error
if errFw == nil {
fm, errUsp = uspfilter.CreateWithNativeFirewall(iface, fm)
} else {
fm, errUsp = uspfilter.Create(iface)
}
if errUsp != nil {
log.Debugf("failed to create userspace filtering firewall: %s", errUsp)
return nil, errUsp
}

if err := fm.AllowNetbird(); err != nil {
log.Errorf("failed to allow netbird interface traffic: %v", err)
}
return fm, nil
}

if errFw != nil {
return nil, errFw
}

return fm, nil
}

// check returns the firewall type based on common lib checks. It returns UNKNOWN if no firewall is found.
func check() FWType {
nf := nftables.Conn{}
if _, err := nf.ListChains(); err == nil && os.Getenv(SKIP_NFTABLES_ENV) != "true" {
return NFTABLES
}

ip, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
if err != nil {
return UNKNOWN
}
if isIptablesClientAvailable(ip) {
return IPTABLES
}

return UNKNOWN
}

func isIptablesClientAvailable(client *iptables.IPTables) bool {
_, err := client.ListChains("filter")
return err == nil
}
11 changes: 11 additions & 0 deletions client/firewall/iface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package firewall

import "github.com/netbirdio/netbird/iface"

// IFaceMapper defines subset methods of interface required for manager
type IFaceMapper interface {
Name() string
Address() iface.WGAddress
IsUserspaceBind() bool
SetFilter(iface.PacketFilter) error
}
Loading
Loading