Fix dispatcher race operation in WintunEndpoint
This commit is contained in:
parent
1e975834ca
commit
23eaeb3198
1 changed files with 9 additions and 0 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/sagernet/gvisor/pkg/buffer"
|
"github.com/sagernet/gvisor/pkg/buffer"
|
||||||
"github.com/sagernet/gvisor/pkg/tcpip"
|
"github.com/sagernet/gvisor/pkg/tcpip"
|
||||||
"github.com/sagernet/gvisor/pkg/tcpip/header"
|
"github.com/sagernet/gvisor/pkg/tcpip/header"
|
||||||
|
|
@ -23,6 +25,7 @@ var _ stack.LinkEndpoint = (*WintunEndpoint)(nil)
|
||||||
|
|
||||||
type WintunEndpoint struct {
|
type WintunEndpoint struct {
|
||||||
tun *NativeTun
|
tun *NativeTun
|
||||||
|
access sync.RWMutex
|
||||||
dispatcher stack.NetworkDispatcher
|
dispatcher stack.NetworkDispatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,6 +52,8 @@ func (e *WintunEndpoint) Capabilities() stack.LinkEndpointCapabilities {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *WintunEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
|
func (e *WintunEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
|
||||||
|
e.access.Lock()
|
||||||
|
defer e.access.Unlock()
|
||||||
if dispatcher == nil && e.dispatcher != nil {
|
if dispatcher == nil && e.dispatcher != nil {
|
||||||
e.dispatcher = nil
|
e.dispatcher = nil
|
||||||
return
|
return
|
||||||
|
|
@ -88,7 +93,9 @@ func (e *WintunEndpoint) dispatchLoop() {
|
||||||
Payload: packetBuffer,
|
Payload: packetBuffer,
|
||||||
IsForwardedPacket: true,
|
IsForwardedPacket: true,
|
||||||
})
|
})
|
||||||
|
e.access.RLock()
|
||||||
dispatcher := e.dispatcher
|
dispatcher := e.dispatcher
|
||||||
|
e.access.RUnlock()
|
||||||
if dispatcher == nil {
|
if dispatcher == nil {
|
||||||
pkt.DecRef()
|
pkt.DecRef()
|
||||||
return
|
return
|
||||||
|
|
@ -99,6 +106,8 @@ func (e *WintunEndpoint) dispatchLoop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *WintunEndpoint) IsAttached() bool {
|
func (e *WintunEndpoint) IsAttached() bool {
|
||||||
|
e.access.RLock()
|
||||||
|
defer e.access.RUnlock()
|
||||||
return e.dispatcher != nil
|
return e.dispatcher != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue