-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[cllient] Don't track ebpf traffic in conntrack #5166
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "github.com/google/nftables/binaryutil" | ||
| "github.com/google/nftables/expr" | ||
| log "github.com/sirupsen/logrus" | ||
| "golang.org/x/sys/unix" | ||
|
|
||
| firewall "github.com/netbirdio/netbird/client/firewall/manager" | ||
| "github.com/netbirdio/netbird/client/iface/wgaddr" | ||
|
|
@@ -48,8 +49,10 @@ type Manager struct { | |
| rConn *nftables.Conn | ||
| wgIface iFaceMapper | ||
|
|
||
| router *router | ||
| aclManager *AclManager | ||
| router *router | ||
| aclManager *AclManager | ||
| notrackOutputChain *nftables.Chain | ||
| notrackPreroutingChain *nftables.Chain | ||
| } | ||
|
|
||
| // Create nftables firewall manager | ||
|
|
@@ -91,6 +94,10 @@ func (m *Manager) Init(stateManager *statemanager.Manager) error { | |
| return fmt.Errorf("acl manager init: %w", err) | ||
| } | ||
|
|
||
| if err := m.initNoTrackChains(workTable); err != nil { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same question. What is the impact of this failing? Maybe we should do a soft failure with log only?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caller will log it only |
||
| return fmt.Errorf("init notrack chains: %w", err) | ||
| } | ||
|
|
||
| stateManager.RegisterState(&ShutdownState{}) | ||
|
|
||
| // We only need to record minimal interface state for potential recreation. | ||
|
|
@@ -288,7 +295,15 @@ func (m *Manager) Flush() error { | |
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| return m.aclManager.Flush() | ||
| if err := m.aclManager.Flush(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := m.refreshNoTrackChains(); err != nil { | ||
| log.Errorf("failed to refresh notrack chains: %v", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // AddDNATRule adds a DNAT rule | ||
|
|
@@ -331,6 +346,176 @@ func (m *Manager) RemoveInboundDNAT(localAddr netip.Addr, protocol firewall.Prot | |
| return m.router.RemoveInboundDNAT(localAddr, protocol, sourcePort, targetPort) | ||
| } | ||
|
|
||
| const ( | ||
| chainNameRawOutput = "netbird-raw-out" | ||
| chainNameRawPrerouting = "netbird-raw-pre" | ||
| ) | ||
|
|
||
| // SetupEBPFProxyNoTrack creates notrack rules for eBPF proxy loopback traffic. | ||
| // This prevents conntrack from tracking WireGuard proxy traffic on loopback, which | ||
| // can interfere with MASQUERADE rules (e.g., from container runtimes like Podman/netavark). | ||
| // | ||
| // Traffic flows that need NOTRACK: | ||
| // | ||
| // 1. Egress: WireGuard -> fake endpoint (before eBPF rewrite) | ||
| // src=127.0.0.1:wgPort -> dst=127.0.0.1:fakePort | ||
| // Matched by: sport=wgPort | ||
| // | ||
| // 2. Egress: Proxy -> WireGuard (via raw socket) | ||
| // src=127.0.0.1:fakePort -> dst=127.0.0.1:wgPort | ||
| // Matched by: dport=wgPort | ||
| // | ||
| // 3. Ingress: Packets to WireGuard | ||
| // dst=127.0.0.1:wgPort | ||
| // Matched by: dport=wgPort | ||
| // | ||
| // 4. Ingress: Packets to proxy (after eBPF rewrite) | ||
| // dst=127.0.0.1:proxyPort | ||
| // Matched by: dport=proxyPort | ||
| // | ||
| // Rules are cleaned up when the firewall manager is closed. | ||
| func (m *Manager) SetupEBPFProxyNoTrack(proxyPort, wgPort uint16) error { | ||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| if m.notrackOutputChain == nil || m.notrackPreroutingChain == nil { | ||
| return fmt.Errorf("notrack chains not initialized") | ||
| } | ||
|
|
||
| proxyPortBytes := binaryutil.BigEndian.PutUint16(proxyPort) | ||
| wgPortBytes := binaryutil.BigEndian.PutUint16(wgPort) | ||
| loopback := []byte{127, 0, 0, 1} | ||
|
|
||
| // Egress rules: match outgoing loopback UDP packets | ||
| m.rConn.AddRule(&nftables.Rule{ | ||
| Table: m.notrackOutputChain.Table, | ||
| Chain: m.notrackOutputChain, | ||
| Exprs: []expr.Any{ | ||
| &expr.Meta{Key: expr.MetaKeyOIFNAME, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: ifname("lo")}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 12, Len: 4}, // saddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 16, Len: 4}, // daddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{unix.IPPROTO_UDP}}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 0, Len: 2}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: wgPortBytes}, // sport=wgPort | ||
| &expr.Counter{}, | ||
| &expr.Notrack{}, | ||
| }, | ||
| }) | ||
| m.rConn.AddRule(&nftables.Rule{ | ||
| Table: m.notrackOutputChain.Table, | ||
| Chain: m.notrackOutputChain, | ||
| Exprs: []expr.Any{ | ||
| &expr.Meta{Key: expr.MetaKeyOIFNAME, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: ifname("lo")}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 12, Len: 4}, // saddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 16, Len: 4}, // daddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{unix.IPPROTO_UDP}}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 2, Len: 2}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: wgPortBytes}, // dport=wgPort | ||
| &expr.Counter{}, | ||
| &expr.Notrack{}, | ||
| }, | ||
| }) | ||
|
|
||
| // Ingress rules: match incoming loopback UDP packets | ||
| m.rConn.AddRule(&nftables.Rule{ | ||
| Table: m.notrackPreroutingChain.Table, | ||
| Chain: m.notrackPreroutingChain, | ||
| Exprs: []expr.Any{ | ||
| &expr.Meta{Key: expr.MetaKeyIIFNAME, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: ifname("lo")}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 12, Len: 4}, // saddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 16, Len: 4}, // daddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{unix.IPPROTO_UDP}}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 2, Len: 2}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: wgPortBytes}, // dport=wgPort | ||
| &expr.Counter{}, | ||
| &expr.Notrack{}, | ||
| }, | ||
| }) | ||
| m.rConn.AddRule(&nftables.Rule{ | ||
| Table: m.notrackPreroutingChain.Table, | ||
| Chain: m.notrackPreroutingChain, | ||
| Exprs: []expr.Any{ | ||
| &expr.Meta{Key: expr.MetaKeyIIFNAME, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: ifname("lo")}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 12, Len: 4}, // saddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseNetworkHeader, Offset: 16, Len: 4}, // daddr | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: loopback}, | ||
| &expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: []byte{unix.IPPROTO_UDP}}, | ||
| &expr.Payload{DestRegister: 1, Base: expr.PayloadBaseTransportHeader, Offset: 2, Len: 2}, | ||
| &expr.Cmp{Op: expr.CmpOpEq, Register: 1, Data: proxyPortBytes}, // dport=proxyPort | ||
| &expr.Counter{}, | ||
| &expr.Notrack{}, | ||
| }, | ||
| }) | ||
|
|
||
| if err := m.rConn.Flush(); err != nil { | ||
| return fmt.Errorf("flush notrack rules: %w", err) | ||
| } | ||
|
|
||
| log.Debugf("set up ebpf proxy notrack rules for ports %d,%d", proxyPort, wgPort) | ||
| return nil | ||
| } | ||
|
|
||
| func (m *Manager) initNoTrackChains(table *nftables.Table) error { | ||
| m.notrackOutputChain = m.rConn.AddChain(&nftables.Chain{ | ||
| Name: chainNameRawOutput, | ||
| Table: table, | ||
| Type: nftables.ChainTypeFilter, | ||
| Hooknum: nftables.ChainHookOutput, | ||
| Priority: nftables.ChainPriorityRaw, | ||
| }) | ||
|
|
||
| m.notrackPreroutingChain = m.rConn.AddChain(&nftables.Chain{ | ||
| Name: chainNameRawPrerouting, | ||
| Table: table, | ||
| Type: nftables.ChainTypeFilter, | ||
| Hooknum: nftables.ChainHookPrerouting, | ||
| Priority: nftables.ChainPriorityRaw, | ||
| }) | ||
|
|
||
| if err := m.rConn.Flush(); err != nil { | ||
| return fmt.Errorf("flush chain creation: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (m *Manager) refreshNoTrackChains() error { | ||
| chains, err := m.rConn.ListChainsOfTableFamily(nftables.TableFamilyIPv4) | ||
| if err != nil { | ||
| return fmt.Errorf("list chains: %w", err) | ||
| } | ||
|
|
||
| tableName := getTableName() | ||
| for _, c := range chains { | ||
| if c.Table.Name != tableName { | ||
| continue | ||
| } | ||
| switch c.Name { | ||
| case chainNameRawOutput: | ||
| m.notrackOutputChain = c | ||
| case chainNameRawPrerouting: | ||
| m.notrackPreroutingChain = c | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (m *Manager) createWorkTable() (*nftables.Table, error) { | ||
| tables, err := m.rConn.ListTablesOfFamily(nftables.TableFamilyIPv4) | ||
| if err != nil { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the impact of this failing? Maybe we should do a soft failure with log only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller will log it only