| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package ipv4 |
| 17 |
|
| 18 |
import ( |
| 19 |
"fmt" |
| 20 |
"math" |
| 21 |
"reflect" |
| 22 |
"sync/atomic" |
| 23 |
"time" |
| 24 |
|
| 25 |
"inet.af/netstack/sync" |
| 26 |
"inet.af/netstack/tcpip" |
| 27 |
"inet.af/netstack/tcpip/buffer" |
| 28 |
"inet.af/netstack/tcpip/header" |
| 29 |
"inet.af/netstack/tcpip/header/parse" |
| 30 |
"inet.af/netstack/tcpip/network/hash" |
| 31 |
"inet.af/netstack/tcpip/network/internal/fragmentation" |
| 32 |
"inet.af/netstack/tcpip/network/internal/ip" |
| 33 |
"inet.af/netstack/tcpip/stack" |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ReassembleTimeout = 30 * time.Second |
| 47 |
|
| 48 |
|
| 49 |
ProtocolNumber = header.IPv4ProtocolNumber |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
MaxTotalSize = 0xffff |
| 54 |
|
| 55 |
|
| 56 |
DefaultTTL = 64 |
| 57 |
|
| 58 |
|
| 59 |
buckets = 2048 |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
fragmentblockSize = 8 |
| 64 |
) |
| 65 |
|
| 66 |
const ( |
| 67 |
forwardingDisabled = 0 |
| 68 |
forwardingEnabled = 1 |
| 69 |
) |
| 70 |
|
| 71 |
var ipv4BroadcastAddr = header.IPv4Broadcast.WithPrefix() |
| 72 |
|
| 73 |
var _ stack.LinkResolvableNetworkEndpoint = (*endpoint)(nil) |
| 74 |
var _ stack.ForwardingNetworkEndpoint = (*endpoint)(nil) |
| 75 |
var _ stack.GroupAddressableEndpoint = (*endpoint)(nil) |
| 76 |
var _ stack.AddressableEndpoint = (*endpoint)(nil) |
| 77 |
var _ stack.NetworkEndpoint = (*endpoint)(nil) |
| 78 |
|
| 79 |
type endpoint struct { |
| 80 |
nic stack.NetworkInterface |
| 81 |
dispatcher stack.TransportDispatcher |
| 82 |
protocol *protocol |
| 83 |
stats sharedStats |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
enabled uint32 |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
forwarding uint32 |
| 96 |
|
| 97 |
mu struct { |
| 98 |
sync.RWMutex |
| 99 |
|
| 100 |
addressableEndpointState stack.AddressableEndpointState |
| 101 |
igmp igmpState |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func (e *endpoint) HandleLinkResolutionFailure(pkt *stack.PacketBuffer) { |
| 107 |
|
| 108 |
|
| 109 |
if pkt.NetworkPacketInfo.IsForwardedPacket { |
| 110 |
|
| 111 |
|
| 112 |
e.protocol.returnError(&icmpReasonHostUnreachable{}, pkt) |
| 113 |
e.stats.ip.Forwarding.Errors.Increment() |
| 114 |
e.stats.ip.Forwarding.HostUnreachable.Increment() |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
pkt = stack.NewPacketBuffer(stack.PacketBufferOptions{ |
| 120 |
Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), |
| 121 |
}) |
| 122 |
defer pkt.DecRef() |
| 123 |
pkt.NICID = e.nic.ID() |
| 124 |
pkt.NetworkProtocolNumber = ProtocolNumber |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
e.handleControl(&icmpv4DestinationHostUnreachableSockError{}, pkt) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (p *protocol) NewEndpoint(nic stack.NetworkInterface, dispatcher stack.TransportDispatcher) stack.NetworkEndpoint { |
| 133 |
e := &endpoint{ |
| 134 |
nic: nic, |
| 135 |
dispatcher: dispatcher, |
| 136 |
protocol: p, |
| 137 |
} |
| 138 |
e.mu.Lock() |
| 139 |
e.mu.addressableEndpointState.Init(e) |
| 140 |
e.mu.igmp.init(e) |
| 141 |
e.mu.Unlock() |
| 142 |
|
| 143 |
tcpip.InitStatCounters(reflect.ValueOf(&e.stats.localStats).Elem()) |
| 144 |
|
| 145 |
stackStats := p.stack.Stats() |
| 146 |
e.stats.ip.Init(&e.stats.localStats.IP, &stackStats.IP) |
| 147 |
e.stats.icmp.init(&e.stats.localStats.ICMP, &stackStats.ICMP.V4) |
| 148 |
e.stats.igmp.init(&e.stats.localStats.IGMP, &stackStats.IGMP) |
| 149 |
|
| 150 |
p.mu.Lock() |
| 151 |
p.mu.eps[nic.ID()] = e |
| 152 |
p.mu.Unlock() |
| 153 |
|
| 154 |
return e |
| 155 |
} |
| 156 |
|
| 157 |
func (p *protocol) findEndpointWithAddress(addr tcpip.Address) *endpoint { |
| 158 |
p.mu.RLock() |
| 159 |
defer p.mu.RUnlock() |
| 160 |
|
| 161 |
for _, e := range p.mu.eps { |
| 162 |
if addressEndpoint := e.AcquireAssignedAddress(addr, false , stack.NeverPrimaryEndpoint); addressEndpoint != nil { |
| 163 |
addressEndpoint.DecRef() |
| 164 |
return e |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
func (p *protocol) getEndpointForNIC(id tcpip.NICID) (*endpoint, bool) { |
| 172 |
p.mu.RLock() |
| 173 |
defer p.mu.RUnlock() |
| 174 |
ep, ok := p.mu.eps[id] |
| 175 |
return ep, ok |
| 176 |
} |
| 177 |
|
| 178 |
func (p *protocol) forgetEndpoint(nicID tcpip.NICID) { |
| 179 |
p.mu.Lock() |
| 180 |
defer p.mu.Unlock() |
| 181 |
delete(p.mu.eps, nicID) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
func (e *endpoint) Forwarding() bool { |
| 186 |
return atomic.LoadUint32(&e.forwarding) == forwardingEnabled |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func (e *endpoint) setForwarding(v bool) bool { |
| 193 |
forwarding := uint32(forwardingDisabled) |
| 194 |
if v { |
| 195 |
forwarding = forwardingEnabled |
| 196 |
} |
| 197 |
|
| 198 |
return atomic.SwapUint32(&e.forwarding, forwarding) != forwarding |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func (e *endpoint) SetForwarding(forwarding bool) { |
| 203 |
e.mu.Lock() |
| 204 |
defer e.mu.Unlock() |
| 205 |
|
| 206 |
if !e.setForwarding(forwarding) { |
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
if forwarding { |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if err := e.joinGroupLocked(header.IPv4AllRoutersGroup); err != nil { |
| 217 |
|
| 218 |
|
| 219 |
panic(fmt.Sprintf("e.joinGroupLocked(%s): %s", header.IPv4AllRoutersGroup, err)) |
| 220 |
} |
| 221 |
|
| 222 |
return |
| 223 |
} |
| 224 |
|
| 225 |
switch err := e.leaveGroupLocked(header.IPv4AllRoutersGroup).(type) { |
| 226 |
case nil: |
| 227 |
case *tcpip.ErrBadLocalAddress: |
| 228 |
|
| 229 |
default: |
| 230 |
panic(fmt.Sprintf("e.leaveGroupLocked(%s): %s", header.IPv4AllRoutersGroup, err)) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
func (e *endpoint) Enable() tcpip.Error { |
| 236 |
e.mu.Lock() |
| 237 |
defer e.mu.Unlock() |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if !e.nic.Enabled() { |
| 242 |
return &tcpip.ErrNotPermitted{} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if !e.setEnabled(true) { |
| 247 |
return nil |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
ep, err := e.mu.addressableEndpointState.AddAndAcquirePermanentAddress(ipv4BroadcastAddr, stack.AddressProperties{PEB: stack.NeverPrimaryEndpoint}) |
| 252 |
if err != nil { |
| 253 |
return err |
| 254 |
} |
| 255 |
|
| 256 |
ep.DecRef() |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
e.mu.igmp.initializeAll() |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if err := e.joinGroupLocked(header.IPv4AllSystems); err != nil { |
| 268 |
|
| 269 |
|
| 270 |
panic(fmt.Sprintf("e.joinGroupLocked(%s): %s", header.IPv4AllSystems, err)) |
| 271 |
} |
| 272 |
|
| 273 |
return nil |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
func (e *endpoint) Enabled() bool { |
| 278 |
return e.nic.Enabled() && e.isEnabled() |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
func (e *endpoint) isEnabled() bool { |
| 284 |
return atomic.LoadUint32(&e.enabled) == 1 |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
func (e *endpoint) setEnabled(v bool) bool { |
| 291 |
if v { |
| 292 |
return atomic.SwapUint32(&e.enabled, 1) == 0 |
| 293 |
} |
| 294 |
return atomic.SwapUint32(&e.enabled, 0) == 1 |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func (e *endpoint) Disable() { |
| 299 |
e.mu.Lock() |
| 300 |
defer e.mu.Unlock() |
| 301 |
e.disableLocked() |
| 302 |
} |
| 303 |
|
| 304 |
func (e *endpoint) disableLocked() { |
| 305 |
if !e.isEnabled() { |
| 306 |
return |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
switch err := e.leaveGroupLocked(header.IPv4AllSystems).(type) { |
| 311 |
case nil, *tcpip.ErrBadLocalAddress: |
| 312 |
default: |
| 313 |
panic(fmt.Sprintf("unexpected error when leaving group = %s: %s", header.IPv4AllSystems, err)) |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
e.mu.igmp.softLeaveAll() |
| 319 |
|
| 320 |
|
| 321 |
switch err := e.mu.addressableEndpointState.RemovePermanentAddress(ipv4BroadcastAddr.Address); err.(type) { |
| 322 |
case nil, *tcpip.ErrBadLocalAddress: |
| 323 |
default: |
| 324 |
panic(fmt.Sprintf("unexpected error when removing address = %s: %s", ipv4BroadcastAddr.Address, err)) |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
e.mu.igmp.resetV1Present() |
| 332 |
|
| 333 |
if !e.setEnabled(false) { |
| 334 |
panic("should have only done work to disable the endpoint if it was enabled") |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
func (e *endpoint) DefaultTTL() uint8 { |
| 340 |
return e.protocol.DefaultTTL() |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
func (e *endpoint) MTU() uint32 { |
| 346 |
networkMTU, err := calculateNetworkMTU(e.nic.MTU(), header.IPv4MinimumSize) |
| 347 |
if err != nil { |
| 348 |
return 0 |
| 349 |
} |
| 350 |
return networkMTU |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
func (e *endpoint) MaxHeaderLength() uint16 { |
| 356 |
return e.nic.MaxHeaderLength() + header.IPv4MaximumHeaderSize |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
func (e *endpoint) NetworkProtocolNumber() tcpip.NetworkProtocolNumber { |
| 361 |
return e.protocol.Number() |
| 362 |
} |
| 363 |
|
| 364 |
func (e *endpoint) addIPHeader(srcAddr, dstAddr tcpip.Address, pkt *stack.PacketBuffer, params stack.NetworkHeaderParams, options header.IPv4OptionsSerializer) tcpip.Error { |
| 365 |
hdrLen := header.IPv4MinimumSize |
| 366 |
var optLen int |
| 367 |
if options != nil { |
| 368 |
optLen = int(options.Length()) |
| 369 |
} |
| 370 |
hdrLen += optLen |
| 371 |
if hdrLen > header.IPv4MaximumHeaderSize { |
| 372 |
return &tcpip.ErrMessageTooLong{} |
| 373 |
} |
| 374 |
ipH := header.IPv4(pkt.NetworkHeader().Push(hdrLen)) |
| 375 |
length := pkt.Size() |
| 376 |
if length > math.MaxUint16 { |
| 377 |
return &tcpip.ErrMessageTooLong{} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
id := atomic.AddUint32(&e.protocol.ids[hashRoute(srcAddr, dstAddr, params.Protocol, e.protocol.hashIV)%buckets], 1) |
| 383 |
ipH.Encode(&header.IPv4Fields{ |
| 384 |
TotalLength: uint16(length), |
| 385 |
ID: uint16(id), |
| 386 |
TTL: params.TTL, |
| 387 |
TOS: params.TOS, |
| 388 |
Protocol: uint8(params.Protocol), |
| 389 |
SrcAddr: srcAddr, |
| 390 |
DstAddr: dstAddr, |
| 391 |
Options: options, |
| 392 |
}) |
| 393 |
ipH.SetChecksum(^ipH.CalculateChecksum()) |
| 394 |
pkt.NetworkProtocolNumber = ProtocolNumber |
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
func (e *endpoint) handleFragments(_ *stack.Route, networkMTU uint32, pkt *stack.PacketBuffer, handler func(*stack.PacketBuffer) tcpip.Error) (int, int, tcpip.Error) { |
| 403 |
|
| 404 |
fragmentPayloadSize := networkMTU &^ 7 |
| 405 |
networkHeader := header.IPv4(pkt.NetworkHeader().View()) |
| 406 |
pf := fragmentation.MakePacketFragmenter(pkt, fragmentPayloadSize, pkt.AvailableHeaderBytes()+len(networkHeader)) |
| 407 |
|
| 408 |
var n int |
| 409 |
for { |
| 410 |
fragPkt, more := buildNextFragment(&pf, networkHeader) |
| 411 |
if err := handler(fragPkt); err != nil { |
| 412 |
return n, pf.RemainingFragmentCount() + 1, err |
| 413 |
} |
| 414 |
n++ |
| 415 |
if !more { |
| 416 |
return n, pf.RemainingFragmentCount(), nil |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
func (e *endpoint) WritePacket(r *stack.Route, params stack.NetworkHeaderParams, pkt *stack.PacketBuffer) tcpip.Error { |
| 423 |
if err := e.addIPHeader(r.LocalAddress(), r.RemoteAddress(), pkt, params, nil ); err != nil { |
| 424 |
return err |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
outNicName := e.protocol.stack.FindNICNameFromID(e.nic.ID()) |
| 430 |
if ok := e.protocol.stack.IPTables().CheckOutput(pkt, r, outNicName); !ok { |
| 431 |
|
| 432 |
e.stats.ip.IPTablesOutputDropped.Increment() |
| 433 |
return nil |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if pkt.DNATDone { |
| 444 |
netHeader := header.IPv4(pkt.NetworkHeader().View()) |
| 445 |
if ep := e.protocol.findEndpointWithAddress(netHeader.DestinationAddress()); ep != nil { |
| 446 |
|
| 447 |
|
| 448 |
ep.handleLocalPacket(pkt, true ) |
| 449 |
return nil |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
return e.writePacket(r, pkt, false ) |
| 454 |
} |
| 455 |
|
| 456 |
func (e *endpoint) writePacket(r *stack.Route, pkt *stack.PacketBuffer, headerIncluded bool) tcpip.Error { |
| 457 |
if r.Loop()&stack.PacketLoop != 0 { |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
e.handleLocalPacket(pkt, !headerIncluded ) |
| 462 |
} |
| 463 |
if r.Loop()&stack.PacketOut == 0 { |
| 464 |
return nil |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
outNicName := e.protocol.stack.FindNICNameFromID(e.nic.ID()) |
| 470 |
if ok := e.protocol.stack.IPTables().CheckPostrouting(pkt, r, e, outNicName); !ok { |
| 471 |
|
| 472 |
e.stats.ip.IPTablesPostroutingDropped.Increment() |
| 473 |
return nil |
| 474 |
} |
| 475 |
|
| 476 |
stats := e.stats.ip |
| 477 |
|
| 478 |
networkMTU, err := calculateNetworkMTU(e.nic.MTU(), uint32(pkt.NetworkHeader().View().Size())) |
| 479 |
if err != nil { |
| 480 |
stats.OutgoingPacketErrors.Increment() |
| 481 |
return err |
| 482 |
} |
| 483 |
|
| 484 |
if packetMustBeFragmented(pkt, networkMTU) { |
| 485 |
h := header.IPv4(pkt.NetworkHeader().View()) |
| 486 |
if h.Flags()&header.IPv4FlagDontFragment != 0 && pkt.NetworkPacketInfo.IsForwardedPacket { |
| 487 |
|
| 488 |
|
| 489 |
return &tcpip.ErrMessageTooLong{} |
| 490 |
} |
| 491 |
sent, remain, err := e.handleFragments(r, networkMTU, pkt, func(fragPkt *stack.PacketBuffer) tcpip.Error { |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
return e.nic.WritePacket(r, ProtocolNumber, fragPkt) |
| 497 |
}) |
| 498 |
stats.PacketsSent.IncrementBy(uint64(sent)) |
| 499 |
stats.OutgoingPacketErrors.IncrementBy(uint64(remain)) |
| 500 |
return err |
| 501 |
} |
| 502 |
|
| 503 |
if err := e.nic.WritePacket(r, ProtocolNumber, pkt); err != nil { |
| 504 |
stats.OutgoingPacketErrors.Increment() |
| 505 |
return err |
| 506 |
} |
| 507 |
stats.PacketsSent.Increment() |
| 508 |
return nil |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
func (e *endpoint) WritePackets(r *stack.Route, pkts stack.PacketBufferList, params stack.NetworkHeaderParams) (int, tcpip.Error) { |
| 513 |
if r.Loop()&stack.PacketLoop != 0 { |
| 514 |
panic("multiple packets in local loop") |
| 515 |
} |
| 516 |
if r.Loop()&stack.PacketOut == 0 { |
| 517 |
return pkts.Len(), nil |
| 518 |
} |
| 519 |
|
| 520 |
stats := e.stats.ip |
| 521 |
|
| 522 |
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { |
| 523 |
if err := e.addIPHeader(r.LocalAddress(), r.RemoteAddress(), pkt, params, nil ); err != nil { |
| 524 |
return 0, err |
| 525 |
} |
| 526 |
|
| 527 |
networkMTU, err := calculateNetworkMTU(e.nic.MTU(), uint32(pkt.NetworkHeader().View().Size())) |
| 528 |
if err != nil { |
| 529 |
stats.OutgoingPacketErrors.IncrementBy(uint64(pkts.Len())) |
| 530 |
return 0, err |
| 531 |
} |
| 532 |
|
| 533 |
if packetMustBeFragmented(pkt, networkMTU) { |
| 534 |
|
| 535 |
|
| 536 |
originalPkt := pkt |
| 537 |
if _, _, err := e.handleFragments(r, networkMTU, pkt, func(fragPkt *stack.PacketBuffer) tcpip.Error { |
| 538 |
fragPkt.IncRef() |
| 539 |
|
| 540 |
pkts.InsertAfter(pkt, fragPkt) |
| 541 |
pkt = fragPkt |
| 542 |
return nil |
| 543 |
}); err != nil { |
| 544 |
panic(fmt.Sprintf("e.handleFragments(_, _, %d, _, _) = %s", networkMTU, err)) |
| 545 |
} |
| 546 |
|
| 547 |
pkts.Remove(originalPkt) |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
outNicName := e.protocol.stack.FindNICNameFromID(e.nic.ID()) |
| 552 |
|
| 553 |
|
| 554 |
outputDropped, natPkts := e.protocol.stack.IPTables().CheckOutputPackets(pkts, r, outNicName) |
| 555 |
stats.IPTablesOutputDropped.IncrementBy(uint64(len(outputDropped))) |
| 556 |
for pkt := range outputDropped { |
| 557 |
pkts.Remove(pkt) |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
locallyDelivered := 0 |
| 562 |
for pkt := range natPkts { |
| 563 |
ep := e.protocol.findEndpointWithAddress(header.IPv4(pkt.NetworkHeader().View()).DestinationAddress()) |
| 564 |
if ep == nil { |
| 565 |
|
| 566 |
continue |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
pkts.Remove(pkt) |
| 571 |
|
| 572 |
|
| 573 |
ep.handleLocalPacket(pkt, true ) |
| 574 |
locallyDelivered++ |
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
postroutingDropped, _ := e.protocol.stack.IPTables().CheckPostroutingPackets(pkts, r, e, outNicName) |
| 582 |
stats.IPTablesPostroutingDropped.IncrementBy(uint64(len(postroutingDropped))) |
| 583 |
for pkt := range postroutingDropped { |
| 584 |
pkts.Remove(pkt) |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
pktsLen := pkts.Len() |
| 589 |
written, err := e.nic.WritePackets(r, pkts, ProtocolNumber) |
| 590 |
stats.PacketsSent.IncrementBy(uint64(written)) |
| 591 |
stats.OutgoingPacketErrors.IncrementBy(uint64(pktsLen - written)) |
| 592 |
|
| 593 |
|
| 594 |
return locallyDelivered + written + len(outputDropped) + len(postroutingDropped), err |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
func (e *endpoint) WriteHeaderIncludedPacket(r *stack.Route, pkt *stack.PacketBuffer) tcpip.Error { |
| 599 |
|
| 600 |
|
| 601 |
h, ok := pkt.Data().PullUp(header.IPv4MinimumSize) |
| 602 |
if !ok { |
| 603 |
return &tcpip.ErrMalformedHeader{} |
| 604 |
} |
| 605 |
|
| 606 |
hdrLen := header.IPv4(h).HeaderLength() |
| 607 |
if hdrLen < header.IPv4MinimumSize { |
| 608 |
return &tcpip.ErrMalformedHeader{} |
| 609 |
} |
| 610 |
|
| 611 |
h, ok = pkt.Data().PullUp(int(hdrLen)) |
| 612 |
if !ok { |
| 613 |
return &tcpip.ErrMalformedHeader{} |
| 614 |
} |
| 615 |
ipH := header.IPv4(h) |
| 616 |
|
| 617 |
|
| 618 |
pktSize := pkt.Data().Size() |
| 619 |
ipH.SetTotalLength(uint16(pktSize)) |
| 620 |
|
| 621 |
|
| 622 |
if ipH.SourceAddress() == header.IPv4Any { |
| 623 |
ipH.SetSourceAddress(r.LocalAddress()) |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
if ipH.ID() == 0 { |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
if ipH.Flags()&header.IPv4FlagDontFragment == 0 || ipH.Flags()&header.IPv4FlagMoreFragments != 0 || ipH.FragmentOffset() > 0 { |
| 632 |
ipH.SetID(uint16(atomic.AddUint32(&e.protocol.ids[hashRoute(r.LocalAddress(), r.RemoteAddress(), 0 , e.protocol.hashIV)%buckets], 1))) |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
ipH.SetChecksum(0) |
| 638 |
ipH.SetChecksum(^ipH.CalculateChecksum()) |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if !parse.IPv4(pkt) || !header.IPv4(pkt.NetworkHeader().View()).IsValid(pktSize) { |
| 647 |
return &tcpip.ErrMalformedHeader{} |
| 648 |
} |
| 649 |
|
| 650 |
return e.writePacket(r, pkt, true ) |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
func (e *endpoint) forwardPacket(pkt *stack.PacketBuffer) ip.ForwardingError { |
| 655 |
h := header.IPv4(pkt.NetworkHeader().View()) |
| 656 |
|
| 657 |
dstAddr := h.DestinationAddress() |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
if header.IsV4LinkLocalUnicastAddress(h.SourceAddress()) { |
| 669 |
return &ip.ErrLinkLocalSourceAddress{} |
| 670 |
} |
| 671 |
if header.IsV4LinkLocalUnicastAddress(dstAddr) || header.IsV4LinkLocalMulticastAddress(dstAddr) { |
| 672 |
return &ip.ErrLinkLocalDestinationAddress{} |
| 673 |
} |
| 674 |
|
| 675 |
ttl := h.TTL() |
| 676 |
if ttl == 0 { |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
_ = e.protocol.returnError(&icmpReasonTTLExceeded{}, pkt) |
| 687 |
return &ip.ErrTTLExceeded{} |
| 688 |
} |
| 689 |
|
| 690 |
if opts := h.Options(); len(opts) != 0 { |
| 691 |
newOpts, _, optProblem := e.processIPOptions(pkt, opts, &optionUsageForward{}) |
| 692 |
if optProblem != nil { |
| 693 |
if optProblem.NeedICMP { |
| 694 |
_ = e.protocol.returnError(&icmpReasonParamProblem{ |
| 695 |
pointer: optProblem.Pointer, |
| 696 |
forwarding: true, |
| 697 |
}, pkt) |
| 698 |
} |
| 699 |
return &ip.ErrParameterProblem{} |
| 700 |
} |
| 701 |
copied := copy(opts, newOpts) |
| 702 |
if copied != len(newOpts) { |
| 703 |
panic(fmt.Sprintf("copied %d bytes of new options, expected %d bytes", copied, len(newOpts))) |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
for i := copied; i < len(opts); i++ { |
| 711 |
|
| 712 |
opts[i] = byte(header.IPv4OptionListEndType) |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
stk := e.protocol.stack |
| 717 |
|
| 718 |
|
| 719 |
if ep := e.protocol.findEndpointWithAddress(dstAddr); ep != nil { |
| 720 |
inNicName := stk.FindNICNameFromID(e.nic.ID()) |
| 721 |
outNicName := stk.FindNICNameFromID(ep.nic.ID()) |
| 722 |
if ok := stk.IPTables().CheckForward(pkt, inNicName, outNicName); !ok { |
| 723 |
|
| 724 |
e.stats.ip.IPTablesForwardDropped.Increment() |
| 725 |
return nil |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
ep.handleValidatedPacket(h, pkt, e.nic.Name() ) |
| 730 |
return nil |
| 731 |
} |
| 732 |
|
| 733 |
r, err := stk.FindRoute(0, "", dstAddr, ProtocolNumber, false ) |
| 734 |
switch err.(type) { |
| 735 |
case nil: |
| 736 |
case *tcpip.ErrNoRoute, *tcpip.ErrNetworkUnreachable: |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
_ = e.protocol.returnError(&icmpReasonNetworkUnreachable{}, pkt) |
| 741 |
return &ip.ErrNoRoute{} |
| 742 |
default: |
| 743 |
return &ip.ErrOther{Err: err} |
| 744 |
} |
| 745 |
defer r.Release() |
| 746 |
|
| 747 |
inNicName := stk.FindNICNameFromID(e.nic.ID()) |
| 748 |
outNicName := stk.FindNICNameFromID(r.NICID()) |
| 749 |
if ok := stk.IPTables().CheckForward(pkt, inNicName, outNicName); !ok { |
| 750 |
|
| 751 |
e.stats.ip.IPTablesForwardDropped.Increment() |
| 752 |
return nil |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
newPkt := pkt.DeepCopyForForwarding(int(r.MaxHeaderLength())) |
| 759 |
newHdr := header.IPv4(newPkt.NetworkHeader().View()) |
| 760 |
defer newPkt.DecRef() |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
newHdr.SetTTL(ttl - 1) |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
newHdr.SetChecksum(0) |
| 773 |
newHdr.SetChecksum(^newHdr.CalculateChecksum()) |
| 774 |
|
| 775 |
forwardToEp, ok := e.protocol.getEndpointForNIC(r.NICID()) |
| 776 |
if !ok { |
| 777 |
|
| 778 |
return &ip.ErrOther{Err: &tcpip.ErrUnknownDevice{}} |
| 779 |
} |
| 780 |
|
| 781 |
switch err := forwardToEp.writePacket(r, newPkt, true ); err.(type) { |
| 782 |
case nil: |
| 783 |
return nil |
| 784 |
case *tcpip.ErrMessageTooLong: |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
_ = e.protocol.returnError(&icmpReasonFragmentationNeeded{}, pkt) |
| 795 |
return &ip.ErrMessageTooLong{} |
| 796 |
default: |
| 797 |
return &ip.ErrOther{Err: err} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) { |
| 804 |
stats := e.stats.ip |
| 805 |
|
| 806 |
stats.PacketsReceived.Increment() |
| 807 |
|
| 808 |
if !e.isEnabled() { |
| 809 |
stats.DisabledPacketsReceived.Increment() |
| 810 |
return |
| 811 |
} |
| 812 |
|
| 813 |
h, ok := e.protocol.parseAndValidate(pkt) |
| 814 |
if !ok { |
| 815 |
stats.MalformedPacketsReceived.Increment() |
| 816 |
return |
| 817 |
} |
| 818 |
|
| 819 |
if !e.nic.IsLoopback() { |
| 820 |
if !e.protocol.options.AllowExternalLoopbackTraffic { |
| 821 |
if header.IsV4LoopbackAddress(h.SourceAddress()) { |
| 822 |
stats.InvalidSourceAddressesReceived.Increment() |
| 823 |
return |
| 824 |
} |
| 825 |
|
| 826 |
if header.IsV4LoopbackAddress(h.DestinationAddress()) { |
| 827 |
stats.InvalidDestinationAddressesReceived.Increment() |
| 828 |
return |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
if e.protocol.stack.HandleLocal() { |
| 833 |
addressEndpoint := e.AcquireAssignedAddress(header.IPv4(pkt.NetworkHeader().View()).SourceAddress(), e.nic.Promiscuous(), stack.CanBePrimaryEndpoint) |
| 834 |
if addressEndpoint != nil { |
| 835 |
addressEndpoint.DecRef() |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
stats.InvalidSourceAddressesReceived.Increment() |
| 841 |
return |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
inNicName := e.protocol.stack.FindNICNameFromID(e.nic.ID()) |
| 847 |
if ok := e.protocol.stack.IPTables().CheckPrerouting(pkt, e, inNicName); !ok { |
| 848 |
|
| 849 |
stats.IPTablesPreroutingDropped.Increment() |
| 850 |
return |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
e.handleValidatedPacket(h, pkt, e.nic.Name() ) |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
func (e *endpoint) handleLocalPacket(pkt *stack.PacketBuffer, canSkipRXChecksum bool) { |
| 861 |
stats := e.stats.ip |
| 862 |
stats.PacketsReceived.Increment() |
| 863 |
|
| 864 |
pkt = pkt.CloneToInbound() |
| 865 |
defer pkt.DecRef() |
| 866 |
pkt.RXTransportChecksumValidated = canSkipRXChecksum |
| 867 |
|
| 868 |
h, ok := e.protocol.parseAndValidate(pkt) |
| 869 |
if !ok { |
| 870 |
stats.MalformedPacketsReceived.Increment() |
| 871 |
return |
| 872 |
} |
| 873 |
|
| 874 |
e.handleValidatedPacket(h, pkt, e.nic.Name() ) |
| 875 |
} |
| 876 |
|
| 877 |
func (e *endpoint) handleValidatedPacket(h header.IPv4, pkt *stack.PacketBuffer, inNICName string) { |
| 878 |
pkt.NICID = e.nic.ID() |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
if !h.More() && h.FragmentOffset() == 0 { |
| 884 |
e.dispatcher.DeliverRawPacket(h.TransportProtocol(), pkt) |
| 885 |
} |
| 886 |
|
| 887 |
stats := e.stats |
| 888 |
stats.ip.ValidPacketsReceived.Increment() |
| 889 |
|
| 890 |
srcAddr := h.SourceAddress() |
| 891 |
dstAddr := h.DestinationAddress() |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
if srcAddr == header.IPv4Broadcast || header.IsV4MulticastAddress(srcAddr) { |
| 898 |
stats.ip.InvalidSourceAddressesReceived.Increment() |
| 899 |
return |
| 900 |
} |
| 901 |
|
| 902 |
if addressEndpoint := e.AcquireAssignedAddress(srcAddr, false , stack.NeverPrimaryEndpoint); addressEndpoint != nil { |
| 903 |
subnet := addressEndpoint.Subnet() |
| 904 |
addressEndpoint.DecRef() |
| 905 |
if subnet.IsBroadcast(srcAddr) { |
| 906 |
stats.ip.InvalidSourceAddressesReceived.Increment() |
| 907 |
return |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
if addressEndpoint := e.AcquireAssignedAddress(dstAddr, e.nic.Promiscuous(), stack.CanBePrimaryEndpoint); addressEndpoint != nil { |
| 915 |
subnet := addressEndpoint.AddressWithPrefix().Subnet() |
| 916 |
addressEndpoint.DecRef() |
| 917 |
pkt.NetworkPacketInfo.LocalAddressBroadcast = subnet.IsBroadcast(dstAddr) || dstAddr == header.IPv4Broadcast |
| 918 |
} else if !e.IsInGroup(dstAddr) { |
| 919 |
if !e.Forwarding() { |
| 920 |
stats.ip.InvalidDestinationAddressesReceived.Increment() |
| 921 |
return |
| 922 |
} |
| 923 |
switch err := e.forwardPacket(pkt); err.(type) { |
| 924 |
case nil: |
| 925 |
return |
| 926 |
case *ip.ErrLinkLocalSourceAddress: |
| 927 |
stats.ip.Forwarding.LinkLocalSource.Increment() |
| 928 |
case *ip.ErrLinkLocalDestinationAddress: |
| 929 |
stats.ip.Forwarding.LinkLocalDestination.Increment() |
| 930 |
case *ip.ErrTTLExceeded: |
| 931 |
stats.ip.Forwarding.ExhaustedTTL.Increment() |
| 932 |
case *ip.ErrNoRoute: |
| 933 |
stats.ip.Forwarding.Unrouteable.Increment() |
| 934 |
case *ip.ErrParameterProblem: |
| 935 |
stats.ip.MalformedPacketsReceived.Increment() |
| 936 |
case *ip.ErrMessageTooLong: |
| 937 |
stats.ip.Forwarding.PacketTooBig.Increment() |
| 938 |
default: |
| 939 |
panic(fmt.Sprintf("unexpected error %s while trying to forward packet: %#v", err, pkt)) |
| 940 |
} |
| 941 |
stats.ip.Forwarding.Errors.Increment() |
| 942 |
return |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
if ok := e.protocol.stack.IPTables().CheckInput(pkt, inNICName); !ok { |
| 948 |
|
| 949 |
stats.ip.IPTablesInputDropped.Increment() |
| 950 |
return |
| 951 |
} |
| 952 |
|
| 953 |
if h.More() || h.FragmentOffset() != 0 { |
| 954 |
if pkt.Data().Size()+pkt.TransportHeader().View().Size() == 0 { |
| 955 |
|
| 956 |
|
| 957 |
stats.ip.MalformedPacketsReceived.Increment() |
| 958 |
stats.ip.MalformedFragmentsReceived.Increment() |
| 959 |
return |
| 960 |
} |
| 961 |
if opts := h.Options(); len(opts) != 0 { |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
if _, _, optProblem := e.processIPOptions(pkt, opts, &optionUsageVerify{}); optProblem != nil { |
| 966 |
if optProblem.NeedICMP { |
| 967 |
_ = e.protocol.returnError(&icmpReasonParamProblem{ |
| 968 |
pointer: optProblem.Pointer, |
| 969 |
}, pkt) |
| 970 |
e.stats.ip.MalformedPacketsReceived.Increment() |
| 971 |
} |
| 972 |
return |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
start := h.FragmentOffset() |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
if int(start)+pkt.Data().Size() > header.IPv4MaximumPayloadSize { |
| 985 |
stats.ip.MalformedPacketsReceived.Increment() |
| 986 |
stats.ip.MalformedFragmentsReceived.Increment() |
| 987 |
return |
| 988 |
} |
| 989 |
|
| 990 |
proto := h.Protocol() |
| 991 |
resPkt, transProtoNum, ready, err := e.protocol.fragmentation.Process( |
| 992 |
|
| 993 |
|
| 994 |
fragmentation.FragmentID{ |
| 995 |
Source: h.SourceAddress(), |
| 996 |
Destination: h.DestinationAddress(), |
| 997 |
ID: uint32(h.ID()), |
| 998 |
Protocol: proto, |
| 999 |
}, |
| 1000 |
start, |
| 1001 |
start+uint16(pkt.Data().Size())-1, |
| 1002 |
h.More(), |
| 1003 |
proto, |
| 1004 |
pkt, |
| 1005 |
) |
| 1006 |
if err != nil { |
| 1007 |
stats.ip.MalformedPacketsReceived.Increment() |
| 1008 |
stats.ip.MalformedFragmentsReceived.Increment() |
| 1009 |
return |
| 1010 |
} |
| 1011 |
if !ready { |
| 1012 |
return |
| 1013 |
} |
| 1014 |
pkt = resPkt |
| 1015 |
h = header.IPv4(pkt.NetworkHeader().View()) |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
h.SetTotalLength(uint16(pkt.Data().Size() + len(h))) |
| 1020 |
h.SetFlagsFragmentOffset(0, 0) |
| 1021 |
|
| 1022 |
e.protocol.parseTransport(pkt, tcpip.TransportProtocolNumber(transProtoNum)) |
| 1023 |
|
| 1024 |
|
| 1025 |
e.dispatcher.DeliverRawPacket(h.TransportProtocol(), pkt) |
| 1026 |
} |
| 1027 |
stats.ip.PacketsDelivered.Increment() |
| 1028 |
|
| 1029 |
p := h.TransportProtocol() |
| 1030 |
if p == header.ICMPv4ProtocolNumber { |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
pkt.TransportProtocolNumber = p |
| 1035 |
e.handleICMP(pkt) |
| 1036 |
return |
| 1037 |
} |
| 1038 |
|
| 1039 |
var hasRouterAlertOption bool |
| 1040 |
if opts := h.Options(); len(opts) != 0 { |
| 1041 |
newOpts, processedOpts, optProblem := e.processIPOptions(pkt, opts, &optionUsageReceive{}) |
| 1042 |
if optProblem != nil { |
| 1043 |
if optProblem.NeedICMP { |
| 1044 |
_ = e.protocol.returnError(&icmpReasonParamProblem{ |
| 1045 |
pointer: optProblem.Pointer, |
| 1046 |
}, pkt) |
| 1047 |
stats.ip.MalformedPacketsReceived.Increment() |
| 1048 |
} |
| 1049 |
return |
| 1050 |
} |
| 1051 |
hasRouterAlertOption = processedOpts.routerAlert |
| 1052 |
copied := copy(opts, newOpts) |
| 1053 |
if copied != len(newOpts) { |
| 1054 |
panic(fmt.Sprintf("copied %d bytes of new options, expected %d bytes", copied, len(newOpts))) |
| 1055 |
} |
| 1056 |
for i := copied; i < len(opts); i++ { |
| 1057 |
|
| 1058 |
opts[i] = byte(header.IPv4OptionListEndType) |
| 1059 |
} |
| 1060 |
} |
| 1061 |
if p == header.IGMPProtocolNumber { |
| 1062 |
e.mu.Lock() |
| 1063 |
e.mu.igmp.handleIGMP(pkt, hasRouterAlertOption) |
| 1064 |
e.mu.Unlock() |
| 1065 |
return |
| 1066 |
} |
| 1067 |
|
| 1068 |
switch res := e.dispatcher.DeliverTransportPacket(p, pkt); res { |
| 1069 |
case stack.TransportPacketHandled: |
| 1070 |
case stack.TransportPacketDestinationPortUnreachable: |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
_ = e.protocol.returnError(&icmpReasonPortUnreachable{}, pkt) |
| 1077 |
case stack.TransportPacketProtocolUnreachable: |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
_ = e.protocol.returnError(&icmpReasonProtoUnreachable{}, pkt) |
| 1083 |
default: |
| 1084 |
panic(fmt.Sprintf("unrecognized result from DeliverTransportPacket = %d", res)) |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
func (e *endpoint) Close() { |
| 1090 |
e.mu.Lock() |
| 1091 |
e.disableLocked() |
| 1092 |
e.mu.addressableEndpointState.Cleanup() |
| 1093 |
e.mu.Unlock() |
| 1094 |
|
| 1095 |
e.protocol.forgetEndpoint(e.nic.ID()) |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
func (e *endpoint) AddAndAcquirePermanentAddress(addr tcpip.AddressWithPrefix, properties stack.AddressProperties) (stack.AddressEndpoint, tcpip.Error) { |
| 1100 |
e.mu.RLock() |
| 1101 |
defer e.mu.RUnlock() |
| 1102 |
|
| 1103 |
ep, err := e.mu.addressableEndpointState.AddAndAcquirePermanentAddress(addr, properties) |
| 1104 |
if err == nil { |
| 1105 |
e.mu.igmp.sendQueuedReports() |
| 1106 |
} |
| 1107 |
return ep, err |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
func (e *endpoint) RemovePermanentAddress(addr tcpip.Address) tcpip.Error { |
| 1112 |
e.mu.RLock() |
| 1113 |
defer e.mu.RUnlock() |
| 1114 |
return e.mu.addressableEndpointState.RemovePermanentAddress(addr) |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
func (e *endpoint) MainAddress() tcpip.AddressWithPrefix { |
| 1119 |
e.mu.RLock() |
| 1120 |
defer e.mu.RUnlock() |
| 1121 |
return e.mu.addressableEndpointState.MainAddress() |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
func (e *endpoint) AcquireAssignedAddress(localAddr tcpip.Address, allowTemp bool, tempPEB stack.PrimaryEndpointBehavior) stack.AddressEndpoint { |
| 1126 |
e.mu.RLock() |
| 1127 |
defer e.mu.RUnlock() |
| 1128 |
|
| 1129 |
loopback := e.nic.IsLoopback() |
| 1130 |
return e.mu.addressableEndpointState.AcquireAssignedAddressOrMatching(localAddr, func(addressEndpoint stack.AddressEndpoint) bool { |
| 1131 |
subnet := addressEndpoint.Subnet() |
| 1132 |
|
| 1133 |
|
| 1134 |
return subnet.IsBroadcast(localAddr) || (loopback && subnet.Contains(localAddr)) |
| 1135 |
}, allowTemp, tempPEB) |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
func (e *endpoint) AcquireOutgoingPrimaryAddress(remoteAddr tcpip.Address, allowExpired bool) stack.AddressEndpoint { |
| 1140 |
e.mu.RLock() |
| 1141 |
defer e.mu.RUnlock() |
| 1142 |
return e.acquireOutgoingPrimaryAddressRLocked(remoteAddr, allowExpired) |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
func (e *endpoint) acquireOutgoingPrimaryAddressRLocked(remoteAddr tcpip.Address, allowExpired bool) stack.AddressEndpoint { |
| 1150 |
return e.mu.addressableEndpointState.AcquireOutgoingPrimaryAddress(remoteAddr, allowExpired) |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
func (e *endpoint) PrimaryAddresses() []tcpip.AddressWithPrefix { |
| 1155 |
e.mu.RLock() |
| 1156 |
defer e.mu.RUnlock() |
| 1157 |
return e.mu.addressableEndpointState.PrimaryAddresses() |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
func (e *endpoint) PermanentAddresses() []tcpip.AddressWithPrefix { |
| 1162 |
e.mu.RLock() |
| 1163 |
defer e.mu.RUnlock() |
| 1164 |
return e.mu.addressableEndpointState.PermanentAddresses() |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
func (e *endpoint) JoinGroup(addr tcpip.Address) tcpip.Error { |
| 1169 |
e.mu.Lock() |
| 1170 |
defer e.mu.Unlock() |
| 1171 |
return e.joinGroupLocked(addr) |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
func (e *endpoint) joinGroupLocked(addr tcpip.Address) tcpip.Error { |
| 1178 |
if !header.IsV4MulticastAddress(addr) { |
| 1179 |
return &tcpip.ErrBadAddress{} |
| 1180 |
} |
| 1181 |
|
| 1182 |
e.mu.igmp.joinGroup(addr) |
| 1183 |
return nil |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
func (e *endpoint) LeaveGroup(addr tcpip.Address) tcpip.Error { |
| 1188 |
e.mu.Lock() |
| 1189 |
defer e.mu.Unlock() |
| 1190 |
return e.leaveGroupLocked(addr) |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
func (e *endpoint) leaveGroupLocked(addr tcpip.Address) tcpip.Error { |
| 1197 |
return e.mu.igmp.leaveGroup(addr) |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
func (e *endpoint) IsInGroup(addr tcpip.Address) bool { |
| 1202 |
e.mu.RLock() |
| 1203 |
defer e.mu.RUnlock() |
| 1204 |
return e.mu.igmp.isInGroup(addr) |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
func (e *endpoint) Stats() stack.NetworkEndpointStats { |
| 1209 |
return &e.stats.localStats |
| 1210 |
} |
| 1211 |
|
| 1212 |
var _ stack.NetworkProtocol = (*protocol)(nil) |
| 1213 |
var _ fragmentation.TimeoutHandler = (*protocol)(nil) |
| 1214 |
|
| 1215 |
type protocol struct { |
| 1216 |
stack *stack.Stack |
| 1217 |
|
| 1218 |
mu struct { |
| 1219 |
sync.RWMutex |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
eps map[tcpip.NICID]*endpoint |
| 1224 |
|
| 1225 |
|
| 1226 |
icmpRateLimitedTypes map[header.ICMPv4Type]struct{} |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
defaultTTL uint32 |
| 1234 |
|
| 1235 |
ids []uint32 |
| 1236 |
hashIV uint32 |
| 1237 |
|
| 1238 |
fragmentation *fragmentation.Fragmentation |
| 1239 |
|
| 1240 |
options Options |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
func (p *protocol) Number() tcpip.NetworkProtocolNumber { |
| 1245 |
return ProtocolNumber |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
func (p *protocol) MinimumPacketSize() int { |
| 1250 |
return header.IPv4MinimumSize |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
func (*protocol) ParseAddresses(v buffer.View) (src, dst tcpip.Address) { |
| 1255 |
h := header.IPv4(v) |
| 1256 |
return h.SourceAddress(), h.DestinationAddress() |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
func (p *protocol) SetOption(option tcpip.SettableNetworkProtocolOption) tcpip.Error { |
| 1261 |
switch v := option.(type) { |
| 1262 |
case *tcpip.DefaultTTLOption: |
| 1263 |
p.SetDefaultTTL(uint8(*v)) |
| 1264 |
return nil |
| 1265 |
default: |
| 1266 |
return &tcpip.ErrUnknownProtocolOption{} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
func (p *protocol) Option(option tcpip.GettableNetworkProtocolOption) tcpip.Error { |
| 1272 |
switch v := option.(type) { |
| 1273 |
case *tcpip.DefaultTTLOption: |
| 1274 |
*v = tcpip.DefaultTTLOption(p.DefaultTTL()) |
| 1275 |
return nil |
| 1276 |
default: |
| 1277 |
return &tcpip.ErrUnknownProtocolOption{} |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
func (p *protocol) SetDefaultTTL(ttl uint8) { |
| 1283 |
atomic.StoreUint32(&p.defaultTTL, uint32(ttl)) |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
func (p *protocol) DefaultTTL() uint8 { |
| 1288 |
return uint8(atomic.LoadUint32(&p.defaultTTL)) |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
func (*protocol) Close() {} |
| 1293 |
|
| 1294 |
|
| 1295 |
func (*protocol) Wait() {} |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
func (p *protocol) parseAndValidate(pkt *stack.PacketBuffer) (header.IPv4, bool) { |
| 1302 |
transProtoNum, hasTransportHdr, ok := p.Parse(pkt) |
| 1303 |
if !ok { |
| 1304 |
return nil, false |
| 1305 |
} |
| 1306 |
|
| 1307 |
h := header.IPv4(pkt.NetworkHeader().View()) |
| 1308 |
|
| 1309 |
|
| 1310 |
if !h.IsValid(pkt.Size() - pkt.LinkHeader().View().Size()) { |
| 1311 |
return nil, false |
| 1312 |
} |
| 1313 |
|
| 1314 |
if !h.IsChecksumValid() { |
| 1315 |
return nil, false |
| 1316 |
} |
| 1317 |
|
| 1318 |
if hasTransportHdr { |
| 1319 |
p.parseTransport(pkt, transProtoNum) |
| 1320 |
} |
| 1321 |
|
| 1322 |
return h, true |
| 1323 |
} |
| 1324 |
|
| 1325 |
func (p *protocol) parseTransport(pkt *stack.PacketBuffer, transProtoNum tcpip.TransportProtocolNumber) { |
| 1326 |
if transProtoNum == header.ICMPv4ProtocolNumber { |
| 1327 |
|
| 1328 |
_ = parse.ICMPv4(pkt) |
| 1329 |
return |
| 1330 |
} |
| 1331 |
|
| 1332 |
switch err := p.stack.ParsePacketBufferTransport(transProtoNum, pkt); err { |
| 1333 |
case stack.ParsedOK: |
| 1334 |
case stack.UnknownTransportProtocol, stack.TransportLayerParseError: |
| 1335 |
|
| 1336 |
|
| 1337 |
default: |
| 1338 |
panic(fmt.Sprintf("unexpected error parsing transport header = %d", err)) |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
func (*protocol) Parse(pkt *stack.PacketBuffer) (proto tcpip.TransportProtocolNumber, hasTransportHdr bool, ok bool) { |
| 1344 |
if ok := parse.IPv4(pkt); !ok { |
| 1345 |
return 0, false, false |
| 1346 |
} |
| 1347 |
|
| 1348 |
ipHdr := header.IPv4(pkt.NetworkHeader().View()) |
| 1349 |
return ipHdr.TransportProtocol(), !ipHdr.More() && ipHdr.FragmentOffset() == 0, true |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
func (p *protocol) allowICMPReply(icmpType header.ICMPv4Type, code header.ICMPv4Code) bool { |
| 1355 |
|
| 1356 |
|
| 1357 |
if icmpType == header.ICMPv4DstUnreachable && code == header.ICMPv4FragmentationNeeded { |
| 1358 |
return true |
| 1359 |
} |
| 1360 |
p.mu.RLock() |
| 1361 |
defer p.mu.RUnlock() |
| 1362 |
|
| 1363 |
if _, ok := p.mu.icmpRateLimitedTypes[icmpType]; ok { |
| 1364 |
return p.stack.AllowICMPMessage() |
| 1365 |
} |
| 1366 |
return true |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
func calculateNetworkMTU(linkMTU, networkHeaderSize uint32) (uint32, tcpip.Error) { |
| 1372 |
if linkMTU < header.IPv4MinimumMTU { |
| 1373 |
return 0, &tcpip.ErrInvalidEndpointState{} |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
if networkHeaderSize > header.IPv4MaximumHeaderSize { |
| 1381 |
return 0, &tcpip.ErrMalformedHeader{} |
| 1382 |
} |
| 1383 |
|
| 1384 |
networkMTU := linkMTU |
| 1385 |
if networkMTU > MaxTotalSize { |
| 1386 |
networkMTU = MaxTotalSize |
| 1387 |
} |
| 1388 |
|
| 1389 |
return networkMTU - networkHeaderSize, nil |
| 1390 |
} |
| 1391 |
|
| 1392 |
func packetMustBeFragmented(pkt *stack.PacketBuffer, networkMTU uint32) bool { |
| 1393 |
payload := pkt.TransportHeader().View().Size() + pkt.Data().Size() |
| 1394 |
return pkt.GSOOptions.Type == stack.GSONone && uint32(payload) > networkMTU |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
func addressToUint32(addr tcpip.Address) uint32 { |
| 1404 |
_ = addr[3] |
| 1405 |
return uint32(addr[0]) | uint32(addr[1])<<8 | uint32(addr[2])<<16 | uint32(addr[3])<<24 |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
func hashRoute(srcAddr, dstAddr tcpip.Address, protocol tcpip.TransportProtocolNumber, hashIV uint32) uint32 { |
| 1412 |
a := addressToUint32(srcAddr) |
| 1413 |
b := addressToUint32(dstAddr) |
| 1414 |
return hash.Hash3Words(a, b, uint32(protocol), hashIV) |
| 1415 |
} |
| 1416 |
|
| 1417 |
|
| 1418 |
type Options struct { |
| 1419 |
|
| 1420 |
IGMP IGMPOptions |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
AllowExternalLoopbackTraffic bool |
| 1425 |
} |
| 1426 |
|
| 1427 |
|
| 1428 |
func NewProtocolWithOptions(opts Options) stack.NetworkProtocolFactory { |
| 1429 |
ids := make([]uint32, buckets) |
| 1430 |
|
| 1431 |
|
| 1432 |
r := hash.RandN32(1 + buckets) |
| 1433 |
for i := range ids { |
| 1434 |
ids[i] = r[i] |
| 1435 |
} |
| 1436 |
hashIV := r[buckets] |
| 1437 |
|
| 1438 |
return func(s *stack.Stack) stack.NetworkProtocol { |
| 1439 |
p := &protocol{ |
| 1440 |
stack: s, |
| 1441 |
ids: ids, |
| 1442 |
hashIV: hashIV, |
| 1443 |
defaultTTL: DefaultTTL, |
| 1444 |
options: opts, |
| 1445 |
} |
| 1446 |
p.fragmentation = fragmentation.NewFragmentation(fragmentblockSize, fragmentation.HighFragThreshold, fragmentation.LowFragThreshold, ReassembleTimeout, s.Clock(), p) |
| 1447 |
p.mu.eps = make(map[tcpip.NICID]*endpoint) |
| 1448 |
|
| 1449 |
|
| 1450 |
p.mu.icmpRateLimitedTypes = map[header.ICMPv4Type]struct{}{ |
| 1451 |
header.ICMPv4DstUnreachable: struct{}{}, |
| 1452 |
header.ICMPv4SrcQuench: struct{}{}, |
| 1453 |
header.ICMPv4TimeExceeded: struct{}{}, |
| 1454 |
header.ICMPv4ParamProblem: struct{}{}, |
| 1455 |
} |
| 1456 |
return p |
| 1457 |
} |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
func NewProtocol(s *stack.Stack) stack.NetworkProtocol { |
| 1462 |
return NewProtocolWithOptions(Options{})(s) |
| 1463 |
} |
| 1464 |
|
| 1465 |
func buildNextFragment(pf *fragmentation.PacketFragmenter, originalIPHeader header.IPv4) (*stack.PacketBuffer, bool) { |
| 1466 |
fragPkt, offset, copied, more := pf.BuildNextFragment() |
| 1467 |
fragPkt.NetworkProtocolNumber = ProtocolNumber |
| 1468 |
|
| 1469 |
originalIPHeaderLength := len(originalIPHeader) |
| 1470 |
nextFragIPHeader := header.IPv4(fragPkt.NetworkHeader().Push(originalIPHeaderLength)) |
| 1471 |
fragPkt.NetworkProtocolNumber = ProtocolNumber |
| 1472 |
|
| 1473 |
if copied := copy(nextFragIPHeader, originalIPHeader); copied != len(originalIPHeader) { |
| 1474 |
panic(fmt.Sprintf("wrong number of bytes copied into fragmentIPHeaders: got = %d, want = %d", copied, originalIPHeaderLength)) |
| 1475 |
} |
| 1476 |
|
| 1477 |
flags := originalIPHeader.Flags() |
| 1478 |
if more { |
| 1479 |
flags |= header.IPv4FlagMoreFragments |
| 1480 |
} |
| 1481 |
nextFragIPHeader.SetFlagsFragmentOffset(flags, uint16(offset)) |
| 1482 |
nextFragIPHeader.SetTotalLength(uint16(nextFragIPHeader.HeaderLength()) + uint16(copied)) |
| 1483 |
nextFragIPHeader.SetChecksum(0) |
| 1484 |
nextFragIPHeader.SetChecksum(^nextFragIPHeader.CalculateChecksum()) |
| 1485 |
|
| 1486 |
return fragPkt, more |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
type optionAction uint8 |
| 1492 |
|
| 1493 |
const ( |
| 1494 |
|
| 1495 |
optionRemove optionAction = iota |
| 1496 |
|
| 1497 |
|
| 1498 |
optionProcess |
| 1499 |
|
| 1500 |
|
| 1501 |
optionVerify |
| 1502 |
|
| 1503 |
|
| 1504 |
optionPass |
| 1505 |
) |
| 1506 |
|
| 1507 |
|
| 1508 |
type optionActions struct { |
| 1509 |
|
| 1510 |
timestamp optionAction |
| 1511 |
|
| 1512 |
|
| 1513 |
recordRoute optionAction |
| 1514 |
|
| 1515 |
|
| 1516 |
routerAlert optionAction |
| 1517 |
|
| 1518 |
|
| 1519 |
unknown optionAction |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
type optionsUsage interface { |
| 1525 |
actions() optionActions |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
type optionUsageVerify struct{} |
| 1532 |
|
| 1533 |
|
| 1534 |
func (*optionUsageVerify) actions() optionActions { |
| 1535 |
return optionActions{ |
| 1536 |
timestamp: optionVerify, |
| 1537 |
recordRoute: optionVerify, |
| 1538 |
routerAlert: optionVerify, |
| 1539 |
unknown: optionRemove, |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
type optionUsageReceive struct{} |
| 1546 |
|
| 1547 |
|
| 1548 |
func (*optionUsageReceive) actions() optionActions { |
| 1549 |
return optionActions{ |
| 1550 |
timestamp: optionProcess, |
| 1551 |
recordRoute: optionProcess, |
| 1552 |
routerAlert: optionVerify, |
| 1553 |
unknown: optionPass, |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
type optionUsageForward struct{} |
| 1561 |
|
| 1562 |
|
| 1563 |
func (*optionUsageForward) actions() optionActions { |
| 1564 |
return optionActions{ |
| 1565 |
timestamp: optionProcess, |
| 1566 |
recordRoute: optionProcess, |
| 1567 |
routerAlert: optionVerify, |
| 1568 |
unknown: optionPass, |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
type optionUsageEcho struct{} |
| 1575 |
|
| 1576 |
|
| 1577 |
func (*optionUsageEcho) actions() optionActions { |
| 1578 |
return optionActions{ |
| 1579 |
timestamp: optionProcess, |
| 1580 |
recordRoute: optionProcess, |
| 1581 |
routerAlert: optionVerify, |
| 1582 |
unknown: optionRemove, |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
func handleTimestamp(tsOpt header.IPv4OptionTimestamp, localAddress tcpip.Address, clock tcpip.Clock, usage optionsUsage) *header.IPv4OptParameterProblem { |
| 1589 |
flags := tsOpt.Flags() |
| 1590 |
var entrySize uint8 |
| 1591 |
switch flags { |
| 1592 |
case header.IPv4OptionTimestampOnlyFlag: |
| 1593 |
entrySize = header.IPv4OptionTimestampSize |
| 1594 |
case |
| 1595 |
header.IPv4OptionTimestampWithIPFlag, |
| 1596 |
header.IPv4OptionTimestampWithPredefinedIPFlag: |
| 1597 |
entrySize = header.IPv4OptionTimestampWithAddrSize |
| 1598 |
default: |
| 1599 |
return &header.IPv4OptParameterProblem{ |
| 1600 |
Pointer: header.IPv4OptTSOFLWAndFLGOffset, |
| 1601 |
NeedICMP: true, |
| 1602 |
} |
| 1603 |
} |
| 1604 |
|
| 1605 |
pointer := tsOpt.Pointer() |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
if pointer <= header.IPv4OptionTimestampHdrLength { |
| 1610 |
return &header.IPv4OptParameterProblem{ |
| 1611 |
Pointer: header.IPv4OptTSPointerOffset, |
| 1612 |
NeedICMP: true, |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
nextSlot := pointer - (header.IPv4OptionTimestampHdrLength + 1) |
| 1619 |
optLen := tsOpt.Size() |
| 1620 |
dataLength := optLen - header.IPv4OptionTimestampHdrLength |
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
if pointer > optLen { |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
if flags == header.IPv4OptionTimestampWithPredefinedIPFlag { |
| 1646 |
|
| 1647 |
return nil |
| 1648 |
} |
| 1649 |
|
| 1650 |
if tsOpt.IncOverflow() != 0 { |
| 1651 |
return nil |
| 1652 |
} |
| 1653 |
|
| 1654 |
return &header.IPv4OptParameterProblem{ |
| 1655 |
Pointer: header.IPv4OptTSOFLWAndFLGOffset, |
| 1656 |
NeedICMP: true, |
| 1657 |
} |
| 1658 |
} |
| 1659 |
if nextSlot+entrySize > dataLength { |
| 1660 |
|
| 1661 |
|
| 1662 |
if false { |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
if dataLength%entrySize != 0 { |
| 1674 |
|
| 1675 |
|
| 1676 |
return &header.IPv4OptParameterProblem{ |
| 1677 |
Pointer: header.IPv4OptionLengthOffset, |
| 1678 |
NeedICMP: false, |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
} |
| 1683 |
return &header.IPv4OptParameterProblem{ |
| 1684 |
Pointer: header.IPv4OptTSPointerOffset, |
| 1685 |
NeedICMP: true, |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
if usage.actions().timestamp == optionProcess { |
| 1690 |
tsOpt.UpdateTimestamp(localAddress, clock) |
| 1691 |
} |
| 1692 |
return nil |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
func handleRecordRoute(rrOpt header.IPv4OptionRecordRoute, localAddress tcpip.Address, usage optionsUsage) *header.IPv4OptParameterProblem { |
| 1699 |
optlen := rrOpt.Size() |
| 1700 |
|
| 1701 |
if optlen < header.IPv4AddressSize+header.IPv4OptionRecordRouteHdrLength { |
| 1702 |
return &header.IPv4OptParameterProblem{ |
| 1703 |
Pointer: header.IPv4OptionLengthOffset, |
| 1704 |
NeedICMP: true, |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
pointer := rrOpt.Pointer() |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
if pointer <= header.IPv4OptionRecordRouteHdrLength { |
| 1715 |
return &header.IPv4OptParameterProblem{ |
| 1716 |
Pointer: header.IPv4OptRRPointerOffset, |
| 1717 |
NeedICMP: true, |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
if pointer > optlen { |
| 1735 |
return nil |
| 1736 |
} |
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
if pointer+header.IPv4AddressSize > optlen+1 { |
| 1742 |
if false { |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
if (optlen-header.IPv4OptionRecordRouteHdrLength)%header.IPv4AddressSize != 0 { |
| 1759 |
|
| 1760 |
return &header.IPv4OptParameterProblem{ |
| 1761 |
Pointer: header.IPv4OptionLengthOffset, |
| 1762 |
NeedICMP: true, |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
} |
| 1767 |
return &header.IPv4OptParameterProblem{ |
| 1768 |
Pointer: header.IPv4OptRRPointerOffset, |
| 1769 |
NeedICMP: true, |
| 1770 |
} |
| 1771 |
} |
| 1772 |
if usage.actions().recordRoute == optionVerify { |
| 1773 |
return nil |
| 1774 |
} |
| 1775 |
rrOpt.StoreAddress(localAddress) |
| 1776 |
return nil |
| 1777 |
} |
| 1778 |
|
| 1779 |
|
| 1780 |
func handleRouterAlert(raOpt header.IPv4OptionRouterAlert) *header.IPv4OptParameterProblem { |
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
if raOpt.Value() != header.IPv4OptionRouterAlertValue { |
| 1786 |
return &header.IPv4OptParameterProblem{ |
| 1787 |
Pointer: header.IPv4OptionRouterAlertValueOffset, |
| 1788 |
NeedICMP: true, |
| 1789 |
} |
| 1790 |
} |
| 1791 |
return nil |
| 1792 |
} |
| 1793 |
|
| 1794 |
type optionTracker struct { |
| 1795 |
timestamp bool |
| 1796 |
recordRoute bool |
| 1797 |
routerAlert bool |
| 1798 |
} |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
func (e *endpoint) processIPOptions(pkt *stack.PacketBuffer, opts header.IPv4Options, usage optionsUsage) (header.IPv4Options, optionTracker, *header.IPv4OptParameterProblem) { |
| 1807 |
stats := e.stats.ip |
| 1808 |
optIter := opts.MakeIterator() |
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
var seenOptions [math.MaxUint8 + 1]bool |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
localAddress := e.MainAddress().Address |
| 1819 |
if len(localAddress) == 0 { |
| 1820 |
h := header.IPv4(pkt.NetworkHeader().View()) |
| 1821 |
dstAddr := h.DestinationAddress() |
| 1822 |
if pkt.NetworkPacketInfo.LocalAddressBroadcast || header.IsV4MulticastAddress(dstAddr) { |
| 1823 |
return nil, optionTracker{}, &header.IPv4OptParameterProblem{ |
| 1824 |
NeedICMP: false, |
| 1825 |
} |
| 1826 |
} |
| 1827 |
localAddress = dstAddr |
| 1828 |
} |
| 1829 |
|
| 1830 |
var optionsProcessed optionTracker |
| 1831 |
for { |
| 1832 |
option, done, optProblem := optIter.Next() |
| 1833 |
if done || optProblem != nil { |
| 1834 |
return optIter.Finalize(), optionsProcessed, optProblem |
| 1835 |
} |
| 1836 |
optType := option.Type() |
| 1837 |
if optType == header.IPv4OptionNOPType { |
| 1838 |
optIter.PushNOPOrEnd(optType) |
| 1839 |
continue |
| 1840 |
} |
| 1841 |
if optType == header.IPv4OptionListEndType { |
| 1842 |
optIter.PushNOPOrEnd(optType) |
| 1843 |
return optIter.Finalize(), optionsProcessed, nil |
| 1844 |
} |
| 1845 |
|
| 1846 |
|
| 1847 |
if seenOptions[optType] { |
| 1848 |
return nil, optionTracker{}, &header.IPv4OptParameterProblem{ |
| 1849 |
Pointer: optIter.ErrCursor, |
| 1850 |
NeedICMP: true, |
| 1851 |
} |
| 1852 |
} |
| 1853 |
seenOptions[optType] = true |
| 1854 |
|
| 1855 |
optLen, optProblem := func() (int, *header.IPv4OptParameterProblem) { |
| 1856 |
switch option := option.(type) { |
| 1857 |
case *header.IPv4OptionTimestamp: |
| 1858 |
stats.OptionTimestampReceived.Increment() |
| 1859 |
optionsProcessed.timestamp = true |
| 1860 |
if usage.actions().timestamp != optionRemove { |
| 1861 |
clock := e.protocol.stack.Clock() |
| 1862 |
newBuffer := optIter.InitReplacement(option) |
| 1863 |
optProblem := handleTimestamp(header.IPv4OptionTimestamp(newBuffer), localAddress, clock, usage) |
| 1864 |
return len(newBuffer), optProblem |
| 1865 |
} |
| 1866 |
|
| 1867 |
case *header.IPv4OptionRecordRoute: |
| 1868 |
stats.OptionRecordRouteReceived.Increment() |
| 1869 |
optionsProcessed.recordRoute = true |
| 1870 |
if usage.actions().recordRoute != optionRemove { |
| 1871 |
newBuffer := optIter.InitReplacement(option) |
| 1872 |
optProblem := handleRecordRoute(header.IPv4OptionRecordRoute(newBuffer), localAddress, usage) |
| 1873 |
return len(newBuffer), optProblem |
| 1874 |
} |
| 1875 |
|
| 1876 |
case *header.IPv4OptionRouterAlert: |
| 1877 |
stats.OptionRouterAlertReceived.Increment() |
| 1878 |
optionsProcessed.routerAlert = true |
| 1879 |
if usage.actions().routerAlert != optionRemove { |
| 1880 |
newBuffer := optIter.InitReplacement(option) |
| 1881 |
optProblem := handleRouterAlert(header.IPv4OptionRouterAlert(newBuffer)) |
| 1882 |
return len(newBuffer), optProblem |
| 1883 |
} |
| 1884 |
|
| 1885 |
default: |
| 1886 |
stats.OptionUnknownReceived.Increment() |
| 1887 |
if usage.actions().unknown == optionPass { |
| 1888 |
return len(optIter.InitReplacement(option)), nil |
| 1889 |
} |
| 1890 |
} |
| 1891 |
return 0, nil |
| 1892 |
}() |
| 1893 |
|
| 1894 |
if optProblem != nil { |
| 1895 |
optProblem.Pointer += optIter.ErrCursor |
| 1896 |
return nil, optionTracker{}, optProblem |
| 1897 |
} |
| 1898 |
optIter.ConsumeBuffer(optLen) |
| 1899 |
} |
| 1900 |
} |