github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/transport/icmp/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 icmp 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/stack" 23 ) 24 25 // saveReceivedAt is invoked by stateify. 26 func (p *icmpPacket) saveReceivedAt() int64 { 27 return p.receivedAt.UnixNano() 28 } 29 30 // loadReceivedAt is invoked by stateify. 31 func (p *icmpPacket) loadReceivedAt(nsec int64) { 32 p.receivedAt = time.Unix(0, nsec) 33 } 34 35 // saveData saves icmpPacket.data field. 36 func (p *icmpPacket) saveData() buffer.VectorisedView { 37 // We cannot save p.data directly as p.data.views may alias to p.views, 38 // which is not allowed by state framework (in-struct pointer). 39 return p.data.Clone(nil) 40 } 41 42 // loadData loads icmpPacket.data field. 43 func (p *icmpPacket) loadData(data buffer.VectorisedView) { 44 // NOTE: We cannot do the p.data = data.Clone(p.views[:]) optimization 45 // here because data.views is not guaranteed to be loaded by now. Plus, 46 // data.views will be allocated anyway so there really is little point 47 // of utilizing p.views for data.views. 48 p.data = data 49 } 50 51 // afterLoad is invoked by stateify. 52 func (e *endpoint) afterLoad() { 53 stack.StackFromEnv.RegisterRestoredEndpoint(e) 54 } 55 56 // beforeSave is invoked by stateify. 57 func (e *endpoint) beforeSave() { 58 e.freeze() 59 } 60 61 // Resume implements tcpip.ResumableEndpoint.Resume. 62 func (e *endpoint) Resume(s *stack.Stack) { 63 e.thaw() 64 e.stack = s 65 e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits) 66 67 if e.state != stateBound && e.state != stateConnected { 68 return 69 } 70 71 var err tcpip.Error 72 if e.state == stateConnected { 73 e.route, err = e.stack.FindRoute(e.RegisterNICID, e.BindAddr, e.ID.RemoteAddress, e.NetProto, false /* multicastLoop */) 74 if err != nil { 75 panic(err) 76 } 77 78 e.ID.LocalAddress = e.route.LocalAddress() 79 } else if len(e.ID.LocalAddress) != 0 { // stateBound 80 if e.stack.CheckLocalAddress(e.RegisterNICID, e.NetProto, e.ID.LocalAddress) == 0 { 81 panic(&tcpip.ErrBadLocalAddress{}) 82 } 83 } 84 85 e.ID, err = e.registerWithStack(e.RegisterNICID, []tcpip.NetworkProtocolNumber{e.NetProto}, e.ID) 86 if err != nil { 87 panic(err) 88 } 89 }