-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[client] Fix DNS resolution with userspace WireGuard and kernel firewall #5873
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "net/netip" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| // PacketHook stores a registered hook for a specific IP:port. | ||
| type PacketHook struct { | ||
| IP netip.Addr | ||
| Port uint16 | ||
| Fn func([]byte) bool | ||
| } | ||
|
|
||
| // HookMatches checks if a packet's destination matches the hook and invokes it. | ||
| func HookMatches(h *PacketHook, dstIP netip.Addr, dport uint16, packetData []byte) bool { | ||
| if h == nil { | ||
| return false | ||
| } | ||
| if h.IP == dstIP && h.Port == dport { | ||
| return h.Fn(packetData) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // SetHook atomically stores a hook, handling nil removal. | ||
| func SetHook(ptr *atomic.Pointer[PacketHook], ip netip.Addr, dPort uint16, hook func([]byte) bool) { | ||
| if hook == nil { | ||
| ptr.Store(nil) | ||
| return | ||
| } | ||
| ptr.Store(&PacketHook{ | ||
| IP: ip, | ||
| Port: dPort, | ||
| Fn: hook, | ||
| }) | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package uspfilter | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "net/netip" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/netbirdio/netbird/client/firewall/uspfilter/common" | ||
| "github.com/netbirdio/netbird/client/iface/device" | ||
| ) | ||
|
|
||
| const ( | ||
| ipv4HeaderMinLen = 20 | ||
| ipv4ProtoOffset = 9 | ||
| ipv4FlagsOffset = 6 | ||
| ipv4DstOffset = 16 | ||
| ipProtoUDP = 17 | ||
| ipProtoTCP = 6 | ||
| ipv4FragOffMask = 0x1fff | ||
| // dstPortOffset is the offset of the destination port within a UDP or TCP header. | ||
| dstPortOffset = 2 | ||
| ) | ||
|
|
||
| // HooksFilter is a minimal packet filter that only handles outbound DNS hooks. | ||
| // It is installed on the WireGuard interface when the userspace bind is active | ||
| // but a full firewall filter (Manager) is not needed because a native kernel | ||
| // firewall (nftables/iptables) handles packet filtering. | ||
| type HooksFilter struct { | ||
| udpHook atomic.Pointer[common.PacketHook] | ||
| tcpHook atomic.Pointer[common.PacketHook] | ||
| } | ||
|
|
||
| var _ device.PacketFilter = (*HooksFilter)(nil) | ||
|
|
||
| // FilterOutbound checks outbound packets for DNS hook matches. | ||
| // Only IPv4 packets matching the registered hook IP:port are intercepted. | ||
| // IPv6 and non-IP packets pass through unconditionally. | ||
| func (f *HooksFilter) FilterOutbound(packetData []byte, _ int) bool { | ||
| if len(packetData) < ipv4HeaderMinLen { | ||
| return false | ||
| } | ||
|
|
||
| // Only process IPv4 packets, let everything else pass through. | ||
| if packetData[0]>>4 != 4 { | ||
| return false | ||
| } | ||
|
|
||
| ihl := int(packetData[0]&0x0f) * 4 | ||
| if ihl < ipv4HeaderMinLen || len(packetData) < ihl+4 { | ||
| return false | ||
| } | ||
|
|
||
| // Skip non-first fragments: they don't carry L4 headers. | ||
| flagsAndOffset := binary.BigEndian.Uint16(packetData[ipv4FlagsOffset : ipv4FlagsOffset+2]) | ||
| if flagsAndOffset&ipv4FragOffMask != 0 { | ||
| return false | ||
| } | ||
|
|
||
| dstIP, ok := netip.AddrFromSlice(packetData[ipv4DstOffset : ipv4DstOffset+4]) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| proto := packetData[ipv4ProtoOffset] | ||
| dstPort := binary.BigEndian.Uint16(packetData[ihl+dstPortOffset : ihl+dstPortOffset+2]) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| switch proto { | ||
| case ipProtoUDP: | ||
| return common.HookMatches(f.udpHook.Load(), dstIP, dstPort, packetData) | ||
| case ipProtoTCP: | ||
| return common.HookMatches(f.tcpHook.Load(), dstIP, dstPort, packetData) | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // FilterInbound allows all inbound packets (native firewall handles filtering). | ||
| func (f *HooksFilter) FilterInbound([]byte, int) bool { | ||
| return false | ||
| } | ||
|
|
||
| // SetUDPPacketHook registers the UDP packet hook. | ||
| func (f *HooksFilter) SetUDPPacketHook(ip netip.Addr, dPort uint16, hook func([]byte) bool) { | ||
| common.SetHook(&f.udpHook, ip, dPort, hook) | ||
| } | ||
|
|
||
| // SetTCPPacketHook registers the TCP packet hook. | ||
| func (f *HooksFilter) SetTCPPacketHook(ip netip.Addr, dPort uint16, hook func([]byte) bool) { | ||
| common.SetHook(&f.tcpHook, ip, dPort, hook) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.