Skip to content

Allow not handled packets for UDP Forwarder #11879

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/tcpip/adapters/gonet/gonet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func TestUDPForwarder(t *testing.T) {
}

done := make(chan struct{})
fwd := udp.NewForwarder(s, func(r *udp.ForwarderRequest) {
fwd := udp.NewForwarder(s, func(r *udp.ForwarderRequest) bool {
defer close(done)

var wq waiter.Queue
Expand All @@ -496,11 +496,15 @@ func TestUDPForwarder(t *testing.T) {
n, e := c.Read(buf)
if e != nil {
t.Errorf("c.Read() = %v", e)
return true
}

if _, e := c.Write(buf[:n]); e != nil {
t.Errorf("c.Write() = %v", e)
return true
}

return true
})
s.SetTransportProtocolHandler(udp.ProtocolNumber, fwd.HandlePacket)

Expand Down
13 changes: 8 additions & 5 deletions pkg/tcpip/transport/udp/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ import (
"gvisor.dev/gvisor/pkg/waiter"
)

// ForwarderHandler handles incoming requests. Returning true marks the
// request as handled, returning false marks the request as unhandled.
// Stack may send an ICMP port unreachable message for unhandled requests.
type ForwarderHandler func(*ForwarderRequest) (handled bool)

// Forwarder is a session request forwarder, which allows clients to decide
// what to do with a session request, for example: ignore it, or process it.
//
// The canonical way of using it is to pass the Forwarder.HandlePacket function
// to stack.SetTransportProtocolHandler.
type Forwarder struct {
handler func(*ForwarderRequest)
handler ForwarderHandler

stack *stack.Stack
}

// NewForwarder allocates and initializes a new forwarder.
func NewForwarder(s *stack.Stack, handler func(*ForwarderRequest)) *Forwarder {
func NewForwarder(s *stack.Stack, handler ForwarderHandler) *Forwarder {
return &Forwarder{
stack: s,
handler: handler,
Expand All @@ -44,13 +49,11 @@ func NewForwarder(s *stack.Stack, handler func(*ForwarderRequest)) *Forwarder {
// This function is expected to be passed as an argument to the
// stack.SetTransportProtocolHandler function.
func (f *Forwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.PacketBuffer) bool {
f.handler(&ForwarderRequest{
return f.handler(&ForwarderRequest{
stack: f.stack,
id: id,
pkt: pkt.Clone(),
})

return true
}

// ForwarderRequest represents a session request received by the forwarder and
Expand Down