gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/tcpip/transport/udp/protocol.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package udp contains the implementation of the UDP transport protocol.
    16  package udp
    17  
    18  import (
    19  	"gvisor.dev/gvisor/pkg/tcpip"
    20  	"gvisor.dev/gvisor/pkg/tcpip/header"
    21  	"gvisor.dev/gvisor/pkg/tcpip/header/parse"
    22  	"gvisor.dev/gvisor/pkg/tcpip/stack"
    23  	"gvisor.dev/gvisor/pkg/tcpip/transport/raw"
    24  	"gvisor.dev/gvisor/pkg/waiter"
    25  )
    26  
    27  const (
    28  	// ProtocolNumber is the udp protocol number.
    29  	ProtocolNumber = header.UDPProtocolNumber
    30  
    31  	// MinBufferSize is the smallest size of a receive or send buffer.
    32  	MinBufferSize = 4 << 10 // 4KiB bytes.
    33  
    34  	// DefaultSendBufferSize is the default size of the send buffer for
    35  	// an endpoint.
    36  	DefaultSendBufferSize = 32 << 10 // 32KiB
    37  
    38  	// DefaultReceiveBufferSize is the default size of the receive buffer
    39  	// for an endpoint.
    40  	DefaultReceiveBufferSize = 32 << 10 // 32KiB
    41  
    42  	// MaxBufferSize is the largest size a receive/send buffer can grow to.
    43  	MaxBufferSize = 4 << 20 // 4MiB
    44  )
    45  
    46  type protocol struct {
    47  	stack *stack.Stack
    48  }
    49  
    50  // Number returns the udp protocol number.
    51  func (*protocol) Number() tcpip.TransportProtocolNumber {
    52  	return ProtocolNumber
    53  }
    54  
    55  // NewEndpoint creates a new udp endpoint.
    56  func (p *protocol) NewEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) {
    57  	return newEndpoint(p.stack, netProto, waiterQueue), nil
    58  }
    59  
    60  // NewRawEndpoint creates a new raw UDP endpoint. It implements
    61  // stack.TransportProtocol.NewRawEndpoint.
    62  func (p *protocol) NewRawEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) {
    63  	return raw.NewEndpoint(p.stack, netProto, header.UDPProtocolNumber, waiterQueue)
    64  }
    65  
    66  // MinimumPacketSize returns the minimum valid udp packet size.
    67  func (*protocol) MinimumPacketSize() int {
    68  	return header.UDPMinimumSize
    69  }
    70  
    71  // ParsePorts returns the source and destination ports stored in the given udp
    72  // packet.
    73  func (*protocol) ParsePorts(v []byte) (src, dst uint16, err tcpip.Error) {
    74  	h := header.UDP(v)
    75  	return h.SourcePort(), h.DestinationPort(), nil
    76  }
    77  
    78  // HandleUnknownDestinationPacket handles packets that are targeted at this
    79  // protocol but don't match any existing endpoint.
    80  func (p *protocol) HandleUnknownDestinationPacket(id stack.TransportEndpointID, pkt *stack.PacketBuffer) stack.UnknownDestinationPacketDisposition {
    81  	hdr := header.UDP(pkt.TransportHeader().Slice())
    82  	netHdr := pkt.Network()
    83  	lengthValid, csumValid := header.UDPValid(
    84  		hdr,
    85  		func() uint16 { return pkt.Data().Checksum() },
    86  		uint16(pkt.Data().Size()),
    87  		pkt.NetworkProtocolNumber,
    88  		netHdr.SourceAddress(),
    89  		netHdr.DestinationAddress(),
    90  		pkt.RXChecksumValidated)
    91  	if !lengthValid {
    92  		p.stack.Stats().UDP.MalformedPacketsReceived.Increment()
    93  		return stack.UnknownDestinationPacketMalformed
    94  	}
    95  
    96  	if !csumValid {
    97  		p.stack.Stats().UDP.ChecksumErrors.Increment()
    98  		return stack.UnknownDestinationPacketMalformed
    99  	}
   100  
   101  	return stack.UnknownDestinationPacketUnhandled
   102  }
   103  
   104  // SetOption implements stack.TransportProtocol.SetOption.
   105  func (*protocol) SetOption(tcpip.SettableTransportProtocolOption) tcpip.Error {
   106  	return &tcpip.ErrUnknownProtocolOption{}
   107  }
   108  
   109  // Option implements stack.TransportProtocol.Option.
   110  func (*protocol) Option(tcpip.GettableTransportProtocolOption) tcpip.Error {
   111  	return &tcpip.ErrUnknownProtocolOption{}
   112  }
   113  
   114  // Close implements stack.TransportProtocol.Close.
   115  func (*protocol) Close() {}
   116  
   117  // Wait implements stack.TransportProtocol.Wait.
   118  func (*protocol) Wait() {}
   119  
   120  // Pause implements stack.TransportProtocol.Pause.
   121  func (*protocol) Pause() {}
   122  
   123  // Resume implements stack.TransportProtocol.Resume.
   124  func (*protocol) Resume() {}
   125  
   126  // Parse implements stack.TransportProtocol.Parse.
   127  func (*protocol) Parse(pkt *stack.PacketBuffer) bool {
   128  	return parse.UDP(pkt)
   129  }
   130  
   131  // NewProtocol returns a UDP transport protocol.
   132  func NewProtocol(s *stack.Stack) stack.TransportProtocol {
   133  	return &protocol{stack: s}
   134  }