Fix possible blocker if the bind will be closed earlier then proxy

This commit is contained in:
Zoltán Papp 2025-02-17 22:03:15 +01:00
parent 1963644c99
commit 3d80a25b4d
2 changed files with 21 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package bind package bind
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"net/netip" "net/netip"
@ -38,7 +39,7 @@ func (rc receiverCreator) CreateIPv4ReceiverFn(pc *ipv4.PacketConn, conn *net.UD
// use the port because in the Send function the wgConn.Endpoint the port info is not exported. // use the port because in the Send function the wgConn.Endpoint the port info is not exported.
type ICEBind struct { type ICEBind struct {
*wgConn.StdNetBind *wgConn.StdNetBind
RecvChan chan RecvMessage recvChan chan RecvMessage
transportNet transport.Net transportNet transport.Net
filterFn FilterFn filterFn FilterFn
@ -58,7 +59,7 @@ func NewICEBind(transportNet transport.Net, filterFn FilterFn) *ICEBind {
b, _ := wgConn.NewStdNetBind().(*wgConn.StdNetBind) b, _ := wgConn.NewStdNetBind().(*wgConn.StdNetBind)
ib := &ICEBind{ ib := &ICEBind{
StdNetBind: b, StdNetBind: b,
RecvChan: make(chan RecvMessage, 1), recvChan: make(chan RecvMessage, 1),
transportNet: transportNet, transportNet: transportNet,
filterFn: filterFn, filterFn: filterFn,
endpoints: make(map[netip.Addr]net.Conn), endpoints: make(map[netip.Addr]net.Conn),
@ -155,6 +156,14 @@ func (b *ICEBind) Send(bufs [][]byte, ep wgConn.Endpoint) error {
return nil return nil
} }
func (b *ICEBind) Recv(ctx context.Context, msg RecvMessage) {
select {
case <-ctx.Done():
return
case b.recvChan <- msg:
}
}
func (s *ICEBind) createIPv4ReceiverFn(pc *ipv4.PacketConn, conn *net.UDPConn, rxOffload bool, msgsPool *sync.Pool) wgConn.ReceiveFunc { func (s *ICEBind) createIPv4ReceiverFn(pc *ipv4.PacketConn, conn *net.UDPConn, rxOffload bool, msgsPool *sync.Pool) wgConn.ReceiveFunc {
s.muUDPMux.Lock() s.muUDPMux.Lock()
defer s.muUDPMux.Unlock() defer s.muUDPMux.Unlock()
@ -264,7 +273,7 @@ func (c *ICEBind) receiveRelayed(buffs [][]byte, sizes []int, eps []wgConn.Endpo
select { select {
case <-c.closedChan: case <-c.closedChan:
return 0, net.ErrClosed return 0, net.ErrClosed
case msg, ok := <-c.RecvChan: case msg, ok := <-c.recvChan:
if !ok { if !ok {
return 0, net.ErrClosed return 0, net.ErrClosed
} }

View File

@ -13,8 +13,14 @@ import (
"github.com/netbirdio/netbird/client/iface/bind" "github.com/netbirdio/netbird/client/iface/bind"
) )
type IceBind interface {
SetEndpoint(addr *net.UDPAddr, conn net.Conn) (*net.UDPAddr, error)
RemoveEndpoint(addr *net.UDPAddr)
Recv(ctx context.Context, msg bind.RecvMessage)
}
type ProxyBind struct { type ProxyBind struct {
bind *bind.ICEBind bind IceBind
// wgEndpoint is a fake address that generated by the Bind.SetEndpoint based on the remote NetBird peer address // wgEndpoint is a fake address that generated by the Bind.SetEndpoint based on the remote NetBird peer address
wgRelayedEndpoint *bind.Endpoint wgRelayedEndpoint *bind.Endpoint
@ -30,7 +36,7 @@ type ProxyBind struct {
isStarted bool isStarted bool
} }
func NewProxyBind(bind *bind.ICEBind) *ProxyBind { func NewProxyBind(bind IceBind) *ProxyBind {
return &ProxyBind{ return &ProxyBind{
bind: bind, bind: bind,
pausedCond: sync.NewCond(&sync.Mutex{}), pausedCond: sync.NewCond(&sync.Mutex{}),
@ -172,7 +178,7 @@ func (p *ProxyBind) proxyToLocal(ctx context.Context) {
Endpoint: p.wgCurrentUsed, Endpoint: p.wgCurrentUsed,
Buffer: buf[:n], Buffer: buf[:n],
} }
p.bind.RecvChan <- msg p.bind.Recv(ctx, msg)
p.pausedCond.L.Unlock() p.pausedCond.L.Unlock()
} }
} }