github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/link/pipe/pipe.go (about) 1 // Copyright 2020 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 pipe provides the implementation of pipe-like data-link layer 16 // endpoints. Such endpoints allow packets to be sent between two interfaces. 17 package pipe 18 19 import ( 20 "github.com/SagerNet/gvisor/pkg/tcpip" 21 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 22 "github.com/SagerNet/gvisor/pkg/tcpip/header" 23 "github.com/SagerNet/gvisor/pkg/tcpip/stack" 24 ) 25 26 var _ stack.LinkEndpoint = (*Endpoint)(nil) 27 28 // New returns both ends of a new pipe. 29 func New(linkAddr1, linkAddr2 tcpip.LinkAddress) (*Endpoint, *Endpoint) { 30 ep1 := &Endpoint{ 31 linkAddr: linkAddr1, 32 } 33 ep2 := &Endpoint{ 34 linkAddr: linkAddr2, 35 } 36 ep1.linked = ep2 37 ep2.linked = ep1 38 return ep1, ep2 39 } 40 41 // Endpoint is one end of a pipe. 42 type Endpoint struct { 43 dispatcher stack.NetworkDispatcher 44 linked *Endpoint 45 linkAddr tcpip.LinkAddress 46 } 47 48 func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkts stack.PacketBufferList) { 49 if !e.linked.IsAttached() { 50 return 51 } 52 53 // Note that the local address from the perspective of this endpoint is the 54 // remote address from the perspective of the other end of the pipe 55 // (e.linked). Similarly, the remote address from the perspective of this 56 // endpoint is the local address on the other end. 57 // 58 // Deliver the packet in a new goroutine to escape this goroutine's stack and 59 // avoid a deadlock when a packet triggers a response which leads the stack to 60 // try and take a lock it already holds. 61 for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { 62 e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, proto, stack.NewPacketBuffer(stack.PacketBufferOptions{ 63 Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), 64 })) 65 } 66 } 67 68 // WritePacket implements stack.LinkEndpoint. 69 func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { 70 var pkts stack.PacketBufferList 71 pkts.PushBack(pkt) 72 e.deliverPackets(r, proto, pkts) 73 return nil 74 } 75 76 // WritePackets implements stack.LinkEndpoint. 77 func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) { 78 n := pkts.Len() 79 e.deliverPackets(r, proto, pkts) 80 return n, nil 81 } 82 83 // Attach implements stack.LinkEndpoint. 84 func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) { 85 e.dispatcher = dispatcher 86 } 87 88 // IsAttached implements stack.LinkEndpoint. 89 func (e *Endpoint) IsAttached() bool { 90 return e.dispatcher != nil 91 } 92 93 // Wait implements stack.LinkEndpoint. 94 func (*Endpoint) Wait() {} 95 96 // MTU implements stack.LinkEndpoint. 97 func (*Endpoint) MTU() uint32 { 98 return header.IPv6MinimumMTU 99 } 100 101 // Capabilities implements stack.LinkEndpoint. 102 func (*Endpoint) Capabilities() stack.LinkEndpointCapabilities { 103 return 0 104 } 105 106 // MaxHeaderLength implements stack.LinkEndpoint. 107 func (*Endpoint) MaxHeaderLength() uint16 { 108 return 0 109 } 110 111 // LinkAddress implements stack.LinkEndpoint. 112 func (e *Endpoint) LinkAddress() tcpip.LinkAddress { 113 return e.linkAddr 114 } 115 116 // ARPHardwareType implements stack.LinkEndpoint. 117 func (*Endpoint) ARPHardwareType() header.ARPHardwareType { 118 return header.ARPHardwareNone 119 } 120 121 // AddHeader implements stack.LinkEndpoint. 122 func (*Endpoint) AddHeader(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) { 123 }