github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/transport/raw/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 raw 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 *rawPacket) saveReceivedAt() int64 { 27 return p.receivedAt.UnixNano() 28 } 29 30 // loadReceivedAt is invoked by stateify. 31 func (p *rawPacket) loadReceivedAt(nsec int64) { 32 p.receivedAt = time.Unix(0, nsec) 33 } 34 35 // saveData saves rawPacket.data field. 36 func (p *rawPacket) 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 rawPacket.data field. 43 func (p *rawPacket) 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 the endpoint is connected, re-connect. 68 if e.connected { 69 var err tcpip.Error 70 // TODO(github.com/SagerNet/issue/4906): Properly restore the route with the right 71 // remote address. We used to pass e.remote.RemoteAddress which was 72 // effectively the empty address but since moving e.route to hold a pointer 73 // to a route instead of the route by value, we pass the empty address 74 // directly. Obviously this was always wrong since we should provide the 75 // remote address we were connected to, to properly restore the route. 76 e.route, err = e.stack.FindRoute(e.RegisterNICID, e.BindAddr, "", e.NetProto, false) 77 if err != nil { 78 panic(err) 79 } 80 } 81 82 // If the endpoint is bound, re-bind. 83 if e.bound { 84 if e.stack.CheckLocalAddress(e.RegisterNICID, e.NetProto, e.BindAddr) == 0 { 85 panic(&tcpip.ErrBadLocalAddress{}) 86 } 87 } 88 89 if e.associated { 90 if err := e.stack.RegisterRawTransportEndpoint(e.NetProto, e.TransProto, e); err != nil { 91 panic(err) 92 } 93 } 94 }