[client] Refactor/optimise raw socket headers#5174
Conversation
Add IPv6 packet header support in UDP raw socket proxy to handle both IPv4 and IPv6 source addresses. Refactor error handling in proxy bind implementations to validate endpoints before acquiring locks.
The sendPkg function now detects IP version and creates appropriate IPv4 or IPv6 headers, preventing IPv6 addresses from being placed into IPv4 packet headers.
…ient packet handling
📝 WalkthroughWalkthroughRefactors eBPF WireGuard proxy packet path: Changes
Sequence Diagram(s)sequenceDiagram
participant EBPF as eBPF program
participant Proxy as ProxyWrapper
participant Headers as PacketHeaders
participant Raw as rawConn (net.PacketConn)
participant Net as Network
EBPF->>Proxy: hand off UDP payload
Proxy->>Headers: select / create headers (headerCurrentUsed)
Proxy->>Raw: selectRawConn(headerCurrentUsed)
Proxy->>Raw: sendPkg(payload + headers)
Raw->>Net: write packet to network
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@client/iface/wgproxy/ebpf/wrapper.go`:
- Around line 244-262: The sendPkg method in ProxyWrapper should defensively
validate that the packetSender argument is not nil before dereferencing its
fields; add a nil check at the start of sendPkg (returning a descriptive error
like "nil packetSender") and ensure the deferred cleanup that calls
packetSender.layerBuffer.Clear is only executed when packetSender and
packetSender.layerBuffer are non-nil (or move the defer after the nil check) to
avoid a nil pointer dereference when accessing packetSender.layerBuffer,
packetSender.ipH, packetSender.udpH, and packetSender.localHostAddr.
🧹 Nitpick comments (1)
client/iface/wgproxy/ebpf/wrapper.go (1)
21-76: Code duplication with rawsocket.go'sprepareHeaders.The logic in
NewPacketHeadersclosely mirrorsprepareHeadersinrawsocket.go. Consider extracting the shared IPv4/IPv6 header construction into a common utility to reduce duplication and ensure consistency.
# Conflicts: # client/iface/wgproxy/ebpf/proxy.go # client/iface/wgproxy/ebpf/wrapper.go # client/iface/wgproxy/udp/rawsocket.go
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
client/iface/wgproxy/ebpf/wrapper.go (1)
159-175: KeeprawConnaligned withheaderCurrentUsedon resume.
Work()resetsheaderCurrentUsedtop.headersbut leavesrawConnunchanged. If a priorRedirectAs()switched to IPv6, this can serialize IPv4 headers while writing to the IPv6 raw socket (or vice‑versa), causing drops/EINVAL. Either preserve the redirected header or updaterawConnafter the assignment.🐛 Suggested fix (keeps redirected header if present and syncs rawConn)
- p.headerCurrentUsed = p.headers + if p.headerCurrentUsed == nil { + p.headerCurrentUsed = p.headers + } + p.rawConn = p.selectRawConn(p.headerCurrentUsed)
Update rawConn when resetting to original headers in Work() to maintain consistency between packet headers and the selected raw socket connection.
|



Describe your changes
Pre-create and reuse packet headers to eliminate per-packet allocations.
Issue ticket number and link
Stack
Checklist
Documentation
Select exactly one:
Docs PR URL (required if "docs added" is checked)
Paste the PR link from https://github.com/netbirdio/docs here:
https://github.com/netbirdio/docs/pull/__
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.