github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/transport/udp/endpoint_state.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
    16  
    17  import (
    18  	"time"
    19  
    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  // saveReceivedAt is invoked by stateify.
    27  func (p *udpPacket) saveReceivedAt() int64 {
    28  	return p.receivedAt.UnixNano()
    29  }
    30  
    31  // loadReceivedAt is invoked by stateify.
    32  func (p *udpPacket) loadReceivedAt(nsec int64) {
    33  	p.receivedAt = time.Unix(0, nsec)
    34  }
    35  
    36  // saveData saves udpPacket.data field.
    37  func (p *udpPacket) saveData() buffer.VectorisedView {
    38  	// We cannot save p.data directly as p.data.views may alias to p.views,
    39  	// which is not allowed by state framework (in-struct pointer).
    40  	return p.data.Clone(nil)
    41  }
    42  
    43  // loadData loads udpPacket.data field.
    44  func (p *udpPacket) loadData(data buffer.VectorisedView) {
    45  	// NOTE: We cannot do the p.data = data.Clone(p.views[:]) optimization
    46  	// here because data.views is not guaranteed to be loaded by now. Plus,
    47  	// data.views will be allocated anyway so there really is little point
    48  	// of utilizing p.views for data.views.
    49  	p.data = data
    50  }
    51  
    52  // afterLoad is invoked by stateify.
    53  func (e *endpoint) afterLoad() {
    54  	stack.StackFromEnv.RegisterRestoredEndpoint(e)
    55  }
    56  
    57  // beforeSave is invoked by stateify.
    58  func (e *endpoint) beforeSave() {
    59  	e.freeze()
    60  }
    61  
    62  // Resume implements tcpip.ResumableEndpoint.Resume.
    63  func (e *endpoint) Resume(s *stack.Stack) {
    64  	e.thaw()
    65  
    66  	e.mu.Lock()
    67  	defer e.mu.Unlock()
    68  
    69  	e.stack = s
    70  	e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    71  
    72  	for m := range e.multicastMemberships {
    73  		if err := e.stack.JoinGroup(e.NetProto, m.nicID, m.multicastAddr); err != nil {
    74  			panic(err)
    75  		}
    76  	}
    77  
    78  	state := e.EndpointState()
    79  	if state != StateBound && state != StateConnected {
    80  		return
    81  	}
    82  
    83  	netProto := e.effectiveNetProtos[0]
    84  	// Connect() and bindLocked() both assert
    85  	//
    86  	//     netProto == header.IPv6ProtocolNumber
    87  	//
    88  	// before creating a multi-entry effectiveNetProtos.
    89  	if len(e.effectiveNetProtos) > 1 {
    90  		netProto = header.IPv6ProtocolNumber
    91  	}
    92  
    93  	var err tcpip.Error
    94  	if state == StateConnected {
    95  		e.route, err = e.stack.FindRoute(e.RegisterNICID, e.ID.LocalAddress, e.ID.RemoteAddress, netProto, e.ops.GetMulticastLoop())
    96  		if err != nil {
    97  			panic(err)
    98  		}
    99  	} else if len(e.ID.LocalAddress) != 0 && !e.isBroadcastOrMulticast(e.RegisterNICID, netProto, e.ID.LocalAddress) { // stateBound
   100  		// A local unicast address is specified, verify that it's valid.
   101  		if e.stack.CheckLocalAddress(e.RegisterNICID, netProto, e.ID.LocalAddress) == 0 {
   102  			panic(&tcpip.ErrBadLocalAddress{})
   103  		}
   104  	}
   105  
   106  	// Our saved state had a port, but we don't actually have a
   107  	// reservation. We need to remove the port from our state, but still
   108  	// pass it to the reservation machinery.
   109  	id := e.ID
   110  	e.ID.LocalPort = 0
   111  	e.ID, e.boundBindToDevice, err = e.registerWithStack(e.effectiveNetProtos, id)
   112  	if err != nil {
   113  		panic(err)
   114  	}
   115  }