github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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 "github.com/SagerNet/gvisor/pkg/tcpip" 20 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 21 "github.com/SagerNet/gvisor/pkg/tcpip/header" 22 "github.com/SagerNet/gvisor/pkg/tcpip/header/parse" 23 "github.com/SagerNet/gvisor/pkg/tcpip/stack" 24 "github.com/SagerNet/gvisor/pkg/tcpip/transport/raw" 25 "github.com/SagerNet/gvisor/pkg/waiter" 26 ) 27 28 const ( 29 // ProtocolNumber is the udp protocol number. 30 ProtocolNumber = header.UDPProtocolNumber 31 32 // MinBufferSize is the smallest size of a receive or send buffer. 33 MinBufferSize = 4 << 10 // 4KiB bytes. 34 35 // DefaultSendBufferSize is the default size of the send buffer for 36 // an endpoint. 37 DefaultSendBufferSize = 32 << 10 // 32KiB 38 39 // DefaultReceiveBufferSize is the default size of the receive buffer 40 // for an endpoint. 41 DefaultReceiveBufferSize = 32 << 10 // 32KiB 42 43 // MaxBufferSize is the largest size a receive/send buffer can grow to. 44 MaxBufferSize = 4 << 20 // 4MiB 45 ) 46 47 type protocol struct { 48 stack *stack.Stack 49 } 50 51 // Number returns the udp protocol number. 52 func (*protocol) Number() tcpip.TransportProtocolNumber { 53 return ProtocolNumber 54 } 55 56 // NewEndpoint creates a new udp endpoint. 57 func (p *protocol) NewEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { 58 return newEndpoint(p.stack, netProto, waiterQueue), nil 59 } 60 61 // NewRawEndpoint creates a new raw UDP endpoint. It implements 62 // stack.TransportProtocol.NewRawEndpoint. 63 func (p *protocol) NewRawEndpoint(netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { 64 return raw.NewEndpoint(p.stack, netProto, header.UDPProtocolNumber, waiterQueue) 65 } 66 67 // MinimumPacketSize returns the minimum valid udp packet size. 68 func (*protocol) MinimumPacketSize() int { 69 return header.UDPMinimumSize 70 } 71 72 // ParsePorts returns the source and destination ports stored in the given udp 73 // packet. 74 func (*protocol) ParsePorts(v buffer.View) (src, dst uint16, err tcpip.Error) { 75 h := header.UDP(v) 76 return h.SourcePort(), h.DestinationPort(), nil 77 } 78 79 // HandleUnknownDestinationPacket handles packets that are targeted at this 80 // protocol but don't match any existing endpoint. 81 func (p *protocol) HandleUnknownDestinationPacket(id stack.TransportEndpointID, pkt *stack.PacketBuffer) stack.UnknownDestinationPacketDisposition { 82 hdr := header.UDP(pkt.TransportHeader().View()) 83 if int(hdr.Length()) > pkt.Data().Size()+header.UDPMinimumSize { 84 p.stack.Stats().UDP.MalformedPacketsReceived.Increment() 85 return stack.UnknownDestinationPacketMalformed 86 } 87 88 if !verifyChecksum(hdr, pkt) { 89 p.stack.Stats().UDP.ChecksumErrors.Increment() 90 return stack.UnknownDestinationPacketMalformed 91 } 92 93 return stack.UnknownDestinationPacketUnhandled 94 } 95 96 // SetOption implements stack.TransportProtocol.SetOption. 97 func (*protocol) SetOption(tcpip.SettableTransportProtocolOption) tcpip.Error { 98 return &tcpip.ErrUnknownProtocolOption{} 99 } 100 101 // Option implements stack.TransportProtocol.Option. 102 func (*protocol) Option(tcpip.GettableTransportProtocolOption) tcpip.Error { 103 return &tcpip.ErrUnknownProtocolOption{} 104 } 105 106 // Close implements stack.TransportProtocol.Close. 107 func (*protocol) Close() {} 108 109 // Wait implements stack.TransportProtocol.Wait. 110 func (*protocol) Wait() {} 111 112 // Parse implements stack.TransportProtocol.Parse. 113 func (*protocol) Parse(pkt *stack.PacketBuffer) bool { 114 return parse.UDP(pkt) 115 } 116 117 // NewProtocol returns a UDP transport protocol. 118 func NewProtocol(s *stack.Stack) stack.TransportProtocol { 119 return &protocol{stack: s} 120 }