| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package fragmentation |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"time" |
| 24 |
|
| 25 |
"inet.af/netstack/sync" |
| 26 |
"inet.af/netstack/tcpip" |
| 27 |
"inet.af/netstack/tcpip/buffer" |
| 28 |
"inet.af/netstack/tcpip/stack" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
HighFragThreshold = 4 << 20 |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
LowFragThreshold = 3 << 20 |
| 43 |
|
| 44 |
|
| 45 |
minBlockSize = 1 |
| 46 |
) |
| 47 |
|
| 48 |
var ( |
| 49 |
|
| 50 |
|
| 51 |
ErrInvalidArgs = errors.New("invalid args") |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ErrFragmentOverlap = errors.New("overlapping fragments") |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
ErrFragmentConflict = errors.New("conflicting fragments") |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
type FragmentID struct { |
| 64 |
|
| 65 |
Source tcpip.Address |
| 66 |
|
| 67 |
|
| 68 |
Destination tcpip.Address |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ID uint32 |
| 74 |
|
| 75 |
|
| 76 |
Protocol uint8 |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
type Fragmentation struct { |
| 82 |
mu sync.Mutex |
| 83 |
highLimit int |
| 84 |
lowLimit int |
| 85 |
reassemblers map[FragmentID]*reassembler |
| 86 |
rList reassemblerList |
| 87 |
memSize int |
| 88 |
timeout time.Duration |
| 89 |
blockSize uint16 |
| 90 |
clock tcpip.Clock |
| 91 |
releaseJob *tcpip.Job |
| 92 |
timeoutHandler TimeoutHandler |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
type TimeoutHandler interface { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
OnReassemblyTimeout(pkt *stack.PacketBuffer) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func NewFragmentation(blockSize uint16, highMemoryLimit, lowMemoryLimit int, reassemblingTimeout time.Duration, clock tcpip.Clock, timeoutHandler TimeoutHandler) *Fragmentation { |
| 118 |
if lowMemoryLimit >= highMemoryLimit { |
| 119 |
lowMemoryLimit = highMemoryLimit |
| 120 |
} |
| 121 |
|
| 122 |
if lowMemoryLimit < 0 { |
| 123 |
lowMemoryLimit = 0 |
| 124 |
} |
| 125 |
|
| 126 |
if blockSize < minBlockSize { |
| 127 |
blockSize = minBlockSize |
| 128 |
} |
| 129 |
|
| 130 |
f := &Fragmentation{ |
| 131 |
reassemblers: make(map[FragmentID]*reassembler), |
| 132 |
highLimit: highMemoryLimit, |
| 133 |
lowLimit: lowMemoryLimit, |
| 134 |
timeout: reassemblingTimeout, |
| 135 |
blockSize: blockSize, |
| 136 |
clock: clock, |
| 137 |
timeoutHandler: timeoutHandler, |
| 138 |
} |
| 139 |
f.releaseJob = tcpip.NewJob(f.clock, &f.mu, f.releaseReassemblersLocked) |
| 140 |
|
| 141 |
return f |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
func (f *Fragmentation) Process( |
| 158 |
id FragmentID, first, last uint16, more bool, proto uint8, pkt *stack.PacketBuffer) ( |
| 159 |
*stack.PacketBuffer, uint8, bool, error) { |
| 160 |
if first > last { |
| 161 |
return nil, 0, false, fmt.Errorf("first=%d is greater than last=%d: %w", first, last, ErrInvalidArgs) |
| 162 |
} |
| 163 |
|
| 164 |
if first%f.blockSize != 0 { |
| 165 |
return nil, 0, false, fmt.Errorf("first=%d is not a multiple of block size=%d: %w", first, f.blockSize, ErrInvalidArgs) |
| 166 |
} |
| 167 |
|
| 168 |
fragmentSize := last - first + 1 |
| 169 |
if more && fragmentSize%f.blockSize != 0 { |
| 170 |
return nil, 0, false, fmt.Errorf("fragment size=%d bytes is not a multiple of block size=%d on non-final fragment: %w", fragmentSize, f.blockSize, ErrInvalidArgs) |
| 171 |
} |
| 172 |
|
| 173 |
if l := pkt.Data().Size(); l != int(fragmentSize) { |
| 174 |
return nil, 0, false, fmt.Errorf("got fragment size=%d bytes not equal to the expected fragment size=%d bytes (first=%d last=%d): %w", l, fragmentSize, first, last, ErrInvalidArgs) |
| 175 |
} |
| 176 |
|
| 177 |
f.mu.Lock() |
| 178 |
r, ok := f.reassemblers[id] |
| 179 |
if !ok { |
| 180 |
r = newReassembler(id, f.clock) |
| 181 |
f.reassemblers[id] = r |
| 182 |
wasEmpty := f.rList.Empty() |
| 183 |
f.rList.PushFront(r) |
| 184 |
if wasEmpty { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
f.releaseReassemblersLocked() |
| 189 |
} |
| 190 |
} |
| 191 |
f.mu.Unlock() |
| 192 |
|
| 193 |
resPkt, firstFragmentProto, done, memConsumed, err := r.process(first, last, more, proto, pkt) |
| 194 |
if err != nil { |
| 195 |
|
| 196 |
|
| 197 |
f.mu.Lock() |
| 198 |
f.release(r, false ) |
| 199 |
f.mu.Unlock() |
| 200 |
return nil, 0, false, fmt.Errorf("fragmentation processing error: %w", err) |
| 201 |
} |
| 202 |
f.mu.Lock() |
| 203 |
f.memSize += memConsumed |
| 204 |
if done { |
| 205 |
f.release(r, false ) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if f.memSize > f.highLimit { |
| 210 |
for f.memSize > f.lowLimit { |
| 211 |
tail := f.rList.Back() |
| 212 |
if tail == nil { |
| 213 |
break |
| 214 |
} |
| 215 |
f.release(tail, false ) |
| 216 |
} |
| 217 |
} |
| 218 |
f.mu.Unlock() |
| 219 |
return resPkt, firstFragmentProto, done, nil |
| 220 |
} |
| 221 |
|
| 222 |
func (f *Fragmentation) release(r *reassembler, timedOut bool) { |
| 223 |
|
| 224 |
|
| 225 |
if r.checkDoneOrMark() { |
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
delete(f.reassemblers, r.id) |
| 230 |
f.rList.Remove(r) |
| 231 |
f.memSize -= r.memSize |
| 232 |
if f.memSize < 0 { |
| 233 |
log.Printf("memory counter < 0 (%d), this is an accounting bug that requires investigation", f.memSize) |
| 234 |
f.memSize = 0 |
| 235 |
} |
| 236 |
|
| 237 |
if h := f.timeoutHandler; timedOut && h != nil { |
| 238 |
h.OnReassemblyTimeout(r.pkt) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
func (f *Fragmentation) releaseReassemblersLocked() { |
| 246 |
now := f.clock.NowMonotonic() |
| 247 |
for { |
| 248 |
|
| 249 |
r := f.rList.Back() |
| 250 |
if r == nil { |
| 251 |
|
| 252 |
break |
| 253 |
} |
| 254 |
elapsed := now.Sub(r.createdAt) |
| 255 |
if f.timeout > elapsed { |
| 256 |
|
| 257 |
|
| 258 |
f.releaseJob.Schedule(f.timeout - elapsed) |
| 259 |
break |
| 260 |
} |
| 261 |
|
| 262 |
f.release(r, true ) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
type PacketFragmenter struct { |
| 268 |
transportHeader buffer.View |
| 269 |
data buffer.VectorisedView |
| 270 |
reserve int |
| 271 |
fragmentPayloadLen int |
| 272 |
fragmentCount int |
| 273 |
currentFragment int |
| 274 |
fragmentOffset int |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func MakePacketFragmenter(pkt *stack.PacketBuffer, fragmentPayloadLen uint32, reserve int) PacketFragmenter { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
var fragmentableData buffer.VectorisedView |
| 295 |
fragmentableData.AppendView(pkt.TransportHeader().View()) |
| 296 |
fragmentableData.Append(pkt.Data().ExtractVV()) |
| 297 |
fragmentCount := (uint32(fragmentableData.Size()) + fragmentPayloadLen - 1) / fragmentPayloadLen |
| 298 |
|
| 299 |
return PacketFragmenter{ |
| 300 |
data: fragmentableData, |
| 301 |
reserve: reserve, |
| 302 |
fragmentPayloadLen: int(fragmentPayloadLen), |
| 303 |
fragmentCount: int(fragmentCount), |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
func (pf *PacketFragmenter) BuildNextFragment() (*stack.PacketBuffer, int, int, bool) { |
| 317 |
if pf.currentFragment >= pf.fragmentCount { |
| 318 |
panic("BuildNextFragment should not be called again after the last fragment was returned") |
| 319 |
} |
| 320 |
|
| 321 |
fragPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 322 |
ReserveHeaderBytes: pf.reserve, |
| 323 |
}) |
| 324 |
|
| 325 |
|
| 326 |
copied := fragPkt.Data().ReadFromVV(&pf.data, pf.fragmentPayloadLen) |
| 327 |
|
| 328 |
offset := pf.fragmentOffset |
| 329 |
pf.fragmentOffset += copied |
| 330 |
pf.currentFragment++ |
| 331 |
more := pf.currentFragment != pf.fragmentCount |
| 332 |
|
| 333 |
return fragPkt, offset, copied, more |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
func (pf *PacketFragmenter) RemainingFragmentCount() int { |
| 338 |
return pf.fragmentCount - pf.currentFragment |
| 339 |
} |