| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package stack |
| 21 |
|
| 22 |
import ( |
| 23 |
"encoding/binary" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"math/rand" |
| 27 |
"sync/atomic" |
| 28 |
"time" |
| 29 |
|
| 30 |
"golang.org/x/time/rate" |
| 31 |
"inet.af/netstack/atomicbitops" |
| 32 |
cryptorand "inet.af/netstack/rand" |
| 33 |
"inet.af/netstack/sync" |
| 34 |
"inet.af/netstack/tcpip" |
| 35 |
"inet.af/netstack/tcpip/buffer" |
| 36 |
"inet.af/netstack/tcpip/header" |
| 37 |
"inet.af/netstack/tcpip/ports" |
| 38 |
"inet.af/netstack/waiter" |
| 39 |
) |
| 40 |
|
| 41 |
const ( |
| 42 |
|
| 43 |
DefaultTOS = 0 |
| 44 |
) |
| 45 |
|
| 46 |
type transportProtocolState struct { |
| 47 |
proto TransportProtocol |
| 48 |
defaultHandler func(id TransportEndpointID, pkt *PacketBuffer) bool |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
type ResumableEndpoint interface { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Resume(*Stack) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
type uniqueIDGenerator atomicbitops.AlignedAtomicUint64 |
| 62 |
|
| 63 |
func (u *uniqueIDGenerator) UniqueID() uint64 { |
| 64 |
return ((*atomicbitops.AlignedAtomicUint64)(u)).Add(1) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
type Stack struct { |
| 70 |
transportProtocols map[tcpip.TransportProtocolNumber]*transportProtocolState |
| 71 |
networkProtocols map[tcpip.NetworkProtocolNumber]NetworkProtocol |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
rawFactory RawFactory |
| 76 |
packetEndpointWriteSupported bool |
| 77 |
|
| 78 |
demux *transportDemuxer |
| 79 |
|
| 80 |
stats tcpip.Stats |
| 81 |
|
| 82 |
|
| 83 |
route struct { |
| 84 |
mu struct { |
| 85 |
sync.RWMutex |
| 86 |
|
| 87 |
table []tcpip.Route |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
mu sync.RWMutex |
| 92 |
nics map[tcpip.NICID]*nic |
| 93 |
defaultForwardingEnabled map[tcpip.NetworkProtocolNumber]struct{} |
| 94 |
|
| 95 |
|
| 96 |
cleanupEndpointsMu sync.Mutex |
| 97 |
cleanupEndpoints map[TransportEndpoint]struct{} |
| 98 |
|
| 99 |
*ports.PortManager |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
tcpProbeFunc atomic.Value |
| 104 |
|
| 105 |
|
| 106 |
clock tcpip.Clock |
| 107 |
|
| 108 |
|
| 109 |
handleLocal bool |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
tables *IPTables |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
resumableEndpoints []ResumableEndpoint |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
icmpRateLimiter *ICMPRateLimiter |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
seed uint32 |
| 127 |
|
| 128 |
|
| 129 |
nudConfigs NUDConfigurations |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
nudDisp NUDDispatcher |
| 134 |
|
| 135 |
|
| 136 |
uniqueIDGenerator UniqueID |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
randomGenerator *rand.Rand |
| 141 |
|
| 142 |
|
| 143 |
secureRNG io.Reader |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
sendBufferSize tcpip.SendBufferSizeOption |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
receiveBufferSize tcpip.ReceiveBufferSizeOption |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
tcpInvalidRateLimit time.Duration |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
tsOffsetSecret uint32 |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
type UniqueID interface { |
| 172 |
UniqueID() uint64 |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
type NetworkProtocolFactory func(*Stack) NetworkProtocol |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
type TransportProtocolFactory func(*Stack) TransportProtocol |
| 186 |
|
| 187 |
|
| 188 |
type Options struct { |
| 189 |
|
| 190 |
NetworkProtocols []NetworkProtocolFactory |
| 191 |
|
| 192 |
|
| 193 |
TransportProtocols []TransportProtocolFactory |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
Clock tcpip.Clock |
| 199 |
|
| 200 |
|
| 201 |
Stats tcpip.Stats |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
HandleLocal bool |
| 207 |
|
| 208 |
|
| 209 |
UniqueID UniqueID |
| 210 |
|
| 211 |
|
| 212 |
NUDConfigs NUDConfigurations |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
NUDDisp NUDDispatcher |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
RawFactory RawFactory |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
AllowPacketEndpointWrite bool |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
RandSource rand.Source |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
IPTables *IPTables |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
DefaultIPTables func(clock tcpip.Clock, rand *rand.Rand) *IPTables |
| 242 |
|
| 243 |
|
| 244 |
SecureRNG io.Reader |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
type TransportEndpointInfo struct { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
NetProto tcpip.NetworkProtocolNumber |
| 256 |
TransProto tcpip.TransportProtocolNumber |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
ID TransportEndpointID |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
BindNICID tcpip.NICID |
| 265 |
BindAddr tcpip.Address |
| 266 |
|
| 267 |
|
| 268 |
RegisterNICID tcpip.NICID |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
func (t *TransportEndpointInfo) AddrNetProtoLocked(addr tcpip.FullAddress, v6only bool) (tcpip.FullAddress, tcpip.NetworkProtocolNumber, tcpip.Error) { |
| 278 |
netProto := t.NetProto |
| 279 |
switch len(addr.Addr) { |
| 280 |
case header.IPv4AddressSize: |
| 281 |
netProto = header.IPv4ProtocolNumber |
| 282 |
case header.IPv6AddressSize: |
| 283 |
if header.IsV4MappedAddress(addr.Addr) { |
| 284 |
netProto = header.IPv4ProtocolNumber |
| 285 |
addr.Addr = addr.Addr[header.IPv6AddressSize-header.IPv4AddressSize:] |
| 286 |
if addr.Addr == header.IPv4Any { |
| 287 |
addr.Addr = "" |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
switch len(t.ID.LocalAddress) { |
| 293 |
case header.IPv4AddressSize: |
| 294 |
if len(addr.Addr) == header.IPv6AddressSize { |
| 295 |
return tcpip.FullAddress{}, 0, &tcpip.ErrInvalidEndpointState{} |
| 296 |
} |
| 297 |
case header.IPv6AddressSize: |
| 298 |
if len(addr.Addr) == header.IPv4AddressSize { |
| 299 |
return tcpip.FullAddress{}, 0, &tcpip.ErrNetworkUnreachable{} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
switch { |
| 304 |
case netProto == t.NetProto: |
| 305 |
case netProto == header.IPv4ProtocolNumber && t.NetProto == header.IPv6ProtocolNumber: |
| 306 |
if v6only { |
| 307 |
return tcpip.FullAddress{}, 0, &tcpip.ErrNoRoute{} |
| 308 |
} |
| 309 |
default: |
| 310 |
return tcpip.FullAddress{}, 0, &tcpip.ErrInvalidEndpointState{} |
| 311 |
} |
| 312 |
|
| 313 |
return addr, netProto, nil |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
func (*TransportEndpointInfo) IsEndpointInfo() {} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
func New(opts Options) *Stack { |
| 331 |
clock := opts.Clock |
| 332 |
if clock == nil { |
| 333 |
clock = tcpip.NewStdClock() |
| 334 |
} |
| 335 |
|
| 336 |
if opts.UniqueID == nil { |
| 337 |
opts.UniqueID = new(uniqueIDGenerator) |
| 338 |
} |
| 339 |
|
| 340 |
if opts.SecureRNG == nil { |
| 341 |
opts.SecureRNG = cryptorand.Reader |
| 342 |
} |
| 343 |
|
| 344 |
randSrc := opts.RandSource |
| 345 |
if randSrc == nil { |
| 346 |
var v int64 |
| 347 |
if err := binary.Read(opts.SecureRNG, binary.LittleEndian, &v); err != nil { |
| 348 |
panic(err) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
randSrc = &lockedRandomSource{src: rand.NewSource(v)} |
| 353 |
} |
| 354 |
randomGenerator := rand.New(randSrc) |
| 355 |
|
| 356 |
if opts.IPTables == nil { |
| 357 |
if opts.DefaultIPTables == nil { |
| 358 |
opts.DefaultIPTables = DefaultTables |
| 359 |
} |
| 360 |
opts.IPTables = opts.DefaultIPTables(clock, randomGenerator) |
| 361 |
} |
| 362 |
|
| 363 |
opts.NUDConfigs.resetInvalidFields() |
| 364 |
|
| 365 |
s := &Stack{ |
| 366 |
transportProtocols: make(map[tcpip.TransportProtocolNumber]*transportProtocolState), |
| 367 |
networkProtocols: make(map[tcpip.NetworkProtocolNumber]NetworkProtocol), |
| 368 |
nics: make(map[tcpip.NICID]*nic), |
| 369 |
packetEndpointWriteSupported: opts.AllowPacketEndpointWrite, |
| 370 |
defaultForwardingEnabled: make(map[tcpip.NetworkProtocolNumber]struct{}), |
| 371 |
cleanupEndpoints: make(map[TransportEndpoint]struct{}), |
| 372 |
PortManager: ports.NewPortManager(), |
| 373 |
clock: clock, |
| 374 |
stats: opts.Stats.FillIn(), |
| 375 |
handleLocal: opts.HandleLocal, |
| 376 |
tables: opts.IPTables, |
| 377 |
icmpRateLimiter: NewICMPRateLimiter(clock), |
| 378 |
seed: randomGenerator.Uint32(), |
| 379 |
nudConfigs: opts.NUDConfigs, |
| 380 |
uniqueIDGenerator: opts.UniqueID, |
| 381 |
nudDisp: opts.NUDDisp, |
| 382 |
randomGenerator: randomGenerator, |
| 383 |
secureRNG: opts.SecureRNG, |
| 384 |
sendBufferSize: tcpip.SendBufferSizeOption{ |
| 385 |
Min: MinBufferSize, |
| 386 |
Default: DefaultBufferSize, |
| 387 |
Max: DefaultMaxBufferSize, |
| 388 |
}, |
| 389 |
receiveBufferSize: tcpip.ReceiveBufferSizeOption{ |
| 390 |
Min: MinBufferSize, |
| 391 |
Default: DefaultBufferSize, |
| 392 |
Max: DefaultMaxBufferSize, |
| 393 |
}, |
| 394 |
tcpInvalidRateLimit: defaultTCPInvalidRateLimit, |
| 395 |
tsOffsetSecret: randomGenerator.Uint32(), |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
for _, netProtoFactory := range opts.NetworkProtocols { |
| 400 |
netProto := netProtoFactory(s) |
| 401 |
s.networkProtocols[netProto.Number()] = netProto |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
for _, transProtoFactory := range opts.TransportProtocols { |
| 406 |
transProto := transProtoFactory(s) |
| 407 |
s.transportProtocols[transProto.Number()] = &transportProtocolState{ |
| 408 |
proto: transProto, |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
s.rawFactory = opts.RawFactory |
| 414 |
|
| 415 |
|
| 416 |
s.demux = newTransportDemuxer(s) |
| 417 |
|
| 418 |
return s |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
func (s *Stack) UniqueID() uint64 { |
| 423 |
return s.uniqueIDGenerator.UniqueID() |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
func (s *Stack) SetNetworkProtocolOption(network tcpip.NetworkProtocolNumber, option tcpip.SettableNetworkProtocolOption) tcpip.Error { |
| 431 |
netProto, ok := s.networkProtocols[network] |
| 432 |
if !ok { |
| 433 |
return &tcpip.ErrUnknownProtocol{} |
| 434 |
} |
| 435 |
return netProto.SetOption(option) |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
func (s *Stack) NetworkProtocolOption(network tcpip.NetworkProtocolNumber, option tcpip.GettableNetworkProtocolOption) tcpip.Error { |
| 448 |
netProto, ok := s.networkProtocols[network] |
| 449 |
if !ok { |
| 450 |
return &tcpip.ErrUnknownProtocol{} |
| 451 |
} |
| 452 |
return netProto.Option(option) |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
func (s *Stack) SetTransportProtocolOption(transport tcpip.TransportProtocolNumber, option tcpip.SettableTransportProtocolOption) tcpip.Error { |
| 460 |
transProtoState, ok := s.transportProtocols[transport] |
| 461 |
if !ok { |
| 462 |
return &tcpip.ErrUnknownProtocol{} |
| 463 |
} |
| 464 |
return transProtoState.proto.SetOption(option) |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
func (s *Stack) TransportProtocolOption(transport tcpip.TransportProtocolNumber, option tcpip.GettableTransportProtocolOption) tcpip.Error { |
| 475 |
transProtoState, ok := s.transportProtocols[transport] |
| 476 |
if !ok { |
| 477 |
return &tcpip.ErrUnknownProtocol{} |
| 478 |
} |
| 479 |
return transProtoState.proto.Option(option) |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
func (s *Stack) SetTransportProtocolHandler(p tcpip.TransportProtocolNumber, h func(TransportEndpointID, *PacketBuffer) bool) { |
| 488 |
state := s.transportProtocols[p] |
| 489 |
if state != nil { |
| 490 |
state.defaultHandler = h |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
func (s *Stack) Clock() tcpip.Clock { |
| 497 |
return s.clock |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
func (s *Stack) Stats() tcpip.Stats { |
| 505 |
return s.stats |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
func (s *Stack) SetNICForwarding(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber, enable bool) tcpip.Error { |
| 511 |
s.mu.RLock() |
| 512 |
defer s.mu.RUnlock() |
| 513 |
|
| 514 |
nic, ok := s.nics[id] |
| 515 |
if !ok { |
| 516 |
return &tcpip.ErrUnknownNICID{} |
| 517 |
} |
| 518 |
|
| 519 |
return nic.setForwarding(protocol, enable) |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
func (s *Stack) NICForwarding(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber) (bool, tcpip.Error) { |
| 524 |
s.mu.RLock() |
| 525 |
defer s.mu.RUnlock() |
| 526 |
|
| 527 |
nic, ok := s.nics[id] |
| 528 |
if !ok { |
| 529 |
return false, &tcpip.ErrUnknownNICID{} |
| 530 |
} |
| 531 |
|
| 532 |
return nic.forwarding(protocol) |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
func (s *Stack) SetForwardingDefaultAndAllNICs(protocol tcpip.NetworkProtocolNumber, enable bool) tcpip.Error { |
| 538 |
s.mu.Lock() |
| 539 |
defer s.mu.Unlock() |
| 540 |
|
| 541 |
doneOnce := false |
| 542 |
for id, nic := range s.nics { |
| 543 |
if err := nic.setForwarding(protocol, enable); err != nil { |
| 544 |
|
| 545 |
|
| 546 |
if doneOnce { |
| 547 |
panic(fmt.Sprintf("nic(id=%d).setForwarding(%d, %t): %s", id, protocol, enable, err)) |
| 548 |
} |
| 549 |
|
| 550 |
return err |
| 551 |
} |
| 552 |
|
| 553 |
doneOnce = true |
| 554 |
} |
| 555 |
|
| 556 |
if enable { |
| 557 |
s.defaultForwardingEnabled[protocol] = struct{}{} |
| 558 |
} else { |
| 559 |
delete(s.defaultForwardingEnabled, protocol) |
| 560 |
} |
| 561 |
|
| 562 |
return nil |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
func (s *Stack) PortRange() (uint16, uint16) { |
| 568 |
return s.PortManager.PortRange() |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
func (s *Stack) SetPortRange(start uint16, end uint16) tcpip.Error { |
| 574 |
return s.PortManager.SetPortRange(start, end) |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
func (s *Stack) SetRouteTable(table []tcpip.Route) { |
| 582 |
s.route.mu.Lock() |
| 583 |
defer s.route.mu.Unlock() |
| 584 |
s.route.mu.table = table |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
func (s *Stack) GetRouteTable() []tcpip.Route { |
| 589 |
s.route.mu.RLock() |
| 590 |
defer s.route.mu.RUnlock() |
| 591 |
return append([]tcpip.Route(nil), s.route.mu.table...) |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
func (s *Stack) AddRoute(route tcpip.Route) { |
| 596 |
s.route.mu.Lock() |
| 597 |
defer s.route.mu.Unlock() |
| 598 |
s.route.mu.table = append(s.route.mu.table, route) |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
func (s *Stack) RemoveRoutes(match func(tcpip.Route) bool) { |
| 603 |
s.route.mu.Lock() |
| 604 |
defer s.route.mu.Unlock() |
| 605 |
|
| 606 |
var filteredRoutes []tcpip.Route |
| 607 |
for _, route := range s.route.mu.table { |
| 608 |
if !match(route) { |
| 609 |
filteredRoutes = append(filteredRoutes, route) |
| 610 |
} |
| 611 |
} |
| 612 |
s.route.mu.table = filteredRoutes |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
func (s *Stack) NewEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { |
| 617 |
t, ok := s.transportProtocols[transport] |
| 618 |
if !ok { |
| 619 |
return nil, &tcpip.ErrUnknownProtocol{} |
| 620 |
} |
| 621 |
|
| 622 |
return t.proto.NewEndpoint(network, waiterQueue) |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
func (s *Stack) NewRawEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue, associated bool) (tcpip.Endpoint, tcpip.Error) { |
| 629 |
if s.rawFactory == nil { |
| 630 |
return nil, &tcpip.ErrNotPermitted{} |
| 631 |
} |
| 632 |
|
| 633 |
if !associated { |
| 634 |
return s.rawFactory.NewUnassociatedEndpoint(s, network, transport, waiterQueue) |
| 635 |
} |
| 636 |
|
| 637 |
t, ok := s.transportProtocols[transport] |
| 638 |
if !ok { |
| 639 |
return nil, &tcpip.ErrUnknownProtocol{} |
| 640 |
} |
| 641 |
|
| 642 |
return t.proto.NewRawEndpoint(network, waiterQueue) |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
func (s *Stack) NewPacketEndpoint(cooked bool, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, tcpip.Error) { |
| 648 |
if s.rawFactory == nil { |
| 649 |
return nil, &tcpip.ErrNotPermitted{} |
| 650 |
} |
| 651 |
|
| 652 |
return s.rawFactory.NewPacketEndpoint(s, cooked, netProto, waiterQueue) |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
type NICContext interface{} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
type NICOptions struct { |
| 661 |
|
| 662 |
Name string |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
Disabled bool |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
Context NICContext |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
func (s *Stack) CreateNICWithOptions(id tcpip.NICID, ep LinkEndpoint, opts NICOptions) tcpip.Error { |
| 681 |
s.mu.Lock() |
| 682 |
defer s.mu.Unlock() |
| 683 |
|
| 684 |
|
| 685 |
if _, ok := s.nics[id]; ok { |
| 686 |
return &tcpip.ErrDuplicateNICID{} |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
if opts.Name != "" { |
| 691 |
for _, n := range s.nics { |
| 692 |
if n.Name() == opts.Name { |
| 693 |
return &tcpip.ErrDuplicateNICID{} |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
n := newNIC(s, id, opts.Name, ep, opts.Context) |
| 699 |
for proto := range s.defaultForwardingEnabled { |
| 700 |
if err := n.setForwarding(proto, true); err != nil { |
| 701 |
panic(fmt.Sprintf("newNIC(%d, ...).setForwarding(%d, true): %s", id, proto, err)) |
| 702 |
} |
| 703 |
} |
| 704 |
s.nics[id] = n |
| 705 |
if !opts.Disabled { |
| 706 |
return n.enable() |
| 707 |
} |
| 708 |
|
| 709 |
return nil |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
func (s *Stack) CreateNIC(id tcpip.NICID, ep LinkEndpoint) tcpip.Error { |
| 715 |
return s.CreateNICWithOptions(id, ep, NICOptions{}) |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
func (s *Stack) GetLinkEndpointByName(name string) LinkEndpoint { |
| 720 |
s.mu.RLock() |
| 721 |
defer s.mu.RUnlock() |
| 722 |
for _, nic := range s.nics { |
| 723 |
if nic.Name() == name { |
| 724 |
return nic.LinkEndpoint |
| 725 |
} |
| 726 |
} |
| 727 |
return nil |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
func (s *Stack) EnableNIC(id tcpip.NICID) tcpip.Error { |
| 733 |
s.mu.RLock() |
| 734 |
defer s.mu.RUnlock() |
| 735 |
|
| 736 |
nic, ok := s.nics[id] |
| 737 |
if !ok { |
| 738 |
return &tcpip.ErrUnknownNICID{} |
| 739 |
} |
| 740 |
|
| 741 |
return nic.enable() |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
func (s *Stack) DisableNIC(id tcpip.NICID) tcpip.Error { |
| 746 |
s.mu.RLock() |
| 747 |
defer s.mu.RUnlock() |
| 748 |
|
| 749 |
nic, ok := s.nics[id] |
| 750 |
if !ok { |
| 751 |
return &tcpip.ErrUnknownNICID{} |
| 752 |
} |
| 753 |
|
| 754 |
nic.disable() |
| 755 |
return nil |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
func (s *Stack) CheckNIC(id tcpip.NICID) bool { |
| 760 |
s.mu.RLock() |
| 761 |
defer s.mu.RUnlock() |
| 762 |
|
| 763 |
nic, ok := s.nics[id] |
| 764 |
if !ok { |
| 765 |
return false |
| 766 |
} |
| 767 |
|
| 768 |
return nic.Enabled() |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
func (s *Stack) RemoveNIC(id tcpip.NICID) tcpip.Error { |
| 773 |
s.mu.Lock() |
| 774 |
defer s.mu.Unlock() |
| 775 |
|
| 776 |
return s.removeNICLocked(id) |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
func (s *Stack) removeNICLocked(id tcpip.NICID) tcpip.Error { |
| 783 |
nic, ok := s.nics[id] |
| 784 |
if !ok { |
| 785 |
return &tcpip.ErrUnknownNICID{} |
| 786 |
} |
| 787 |
if nic.IsLoopback() { |
| 788 |
return &tcpip.ErrNotSupported{} |
| 789 |
} |
| 790 |
delete(s.nics, id) |
| 791 |
|
| 792 |
|
| 793 |
s.route.mu.Lock() |
| 794 |
n := 0 |
| 795 |
for i, r := range s.route.mu.table { |
| 796 |
s.route.mu.table[i] = tcpip.Route{} |
| 797 |
if r.NIC != id { |
| 798 |
|
| 799 |
s.route.mu.table[n] = r |
| 800 |
n++ |
| 801 |
} |
| 802 |
} |
| 803 |
s.route.mu.table = s.route.mu.table[:n] |
| 804 |
s.route.mu.Unlock() |
| 805 |
|
| 806 |
return nic.remove() |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
type NICInfo struct { |
| 811 |
Name string |
| 812 |
LinkAddress tcpip.LinkAddress |
| 813 |
ProtocolAddresses []tcpip.ProtocolAddress |
| 814 |
|
| 815 |
|
| 816 |
Flags NICStateFlags |
| 817 |
|
| 818 |
|
| 819 |
MTU uint32 |
| 820 |
|
| 821 |
Stats tcpip.NICStats |
| 822 |
|
| 823 |
|
| 824 |
NetworkStats map[tcpip.NetworkProtocolNumber]NetworkEndpointStats |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
Context NICContext |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
ARPHardwareType header.ARPHardwareType |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
Forwarding map[tcpip.NetworkProtocolNumber]bool |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
func (s *Stack) HasNIC(id tcpip.NICID) bool { |
| 842 |
s.mu.RLock() |
| 843 |
_, ok := s.nics[id] |
| 844 |
s.mu.RUnlock() |
| 845 |
return ok |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
func (s *Stack) NICInfo() map[tcpip.NICID]NICInfo { |
| 850 |
s.mu.RLock() |
| 851 |
defer s.mu.RUnlock() |
| 852 |
|
| 853 |
nics := make(map[tcpip.NICID]NICInfo) |
| 854 |
for id, nic := range s.nics { |
| 855 |
flags := NICStateFlags{ |
| 856 |
Up: true, |
| 857 |
Running: nic.Enabled(), |
| 858 |
Promiscuous: nic.Promiscuous(), |
| 859 |
Loopback: nic.IsLoopback(), |
| 860 |
} |
| 861 |
|
| 862 |
netStats := make(map[tcpip.NetworkProtocolNumber]NetworkEndpointStats) |
| 863 |
for proto, netEP := range nic.networkEndpoints { |
| 864 |
netStats[proto] = netEP.Stats() |
| 865 |
} |
| 866 |
|
| 867 |
info := NICInfo{ |
| 868 |
Name: nic.name, |
| 869 |
LinkAddress: nic.LinkEndpoint.LinkAddress(), |
| 870 |
ProtocolAddresses: nic.primaryAddresses(), |
| 871 |
Flags: flags, |
| 872 |
MTU: nic.LinkEndpoint.MTU(), |
| 873 |
Stats: nic.stats.local, |
| 874 |
NetworkStats: netStats, |
| 875 |
Context: nic.context, |
| 876 |
ARPHardwareType: nic.LinkEndpoint.ARPHardwareType(), |
| 877 |
Forwarding: make(map[tcpip.NetworkProtocolNumber]bool), |
| 878 |
} |
| 879 |
|
| 880 |
for proto := range s.networkProtocols { |
| 881 |
switch forwarding, err := nic.forwarding(proto); err.(type) { |
| 882 |
case nil: |
| 883 |
info.Forwarding[proto] = forwarding |
| 884 |
case *tcpip.ErrUnknownProtocol: |
| 885 |
panic(fmt.Sprintf("expected network protocol %d to be available on NIC %d", proto, nic.ID())) |
| 886 |
case *tcpip.ErrNotSupported: |
| 887 |
|
| 888 |
default: |
| 889 |
panic(fmt.Sprintf("nic(id=%d).forwarding(%d): %s", nic.ID(), proto, err)) |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
nics[id] = info |
| 894 |
} |
| 895 |
return nics |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
type NICStateFlags struct { |
| 900 |
|
| 901 |
Up bool |
| 902 |
|
| 903 |
|
| 904 |
Running bool |
| 905 |
|
| 906 |
|
| 907 |
Promiscuous bool |
| 908 |
|
| 909 |
|
| 910 |
Loopback bool |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
func (s *Stack) AddProtocolAddress(id tcpip.NICID, protocolAddress tcpip.ProtocolAddress, properties AddressProperties) tcpip.Error { |
| 916 |
s.mu.RLock() |
| 917 |
defer s.mu.RUnlock() |
| 918 |
|
| 919 |
nic, ok := s.nics[id] |
| 920 |
if !ok { |
| 921 |
return &tcpip.ErrUnknownNICID{} |
| 922 |
} |
| 923 |
|
| 924 |
return nic.addAddress(protocolAddress, properties) |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
func (s *Stack) RemoveAddress(id tcpip.NICID, addr tcpip.Address) tcpip.Error { |
| 930 |
s.mu.RLock() |
| 931 |
defer s.mu.RUnlock() |
| 932 |
|
| 933 |
if nic, ok := s.nics[id]; ok { |
| 934 |
return nic.removeAddress(addr) |
| 935 |
} |
| 936 |
|
| 937 |
return &tcpip.ErrUnknownNICID{} |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
func (s *Stack) AllAddresses() map[tcpip.NICID][]tcpip.ProtocolAddress { |
| 943 |
s.mu.RLock() |
| 944 |
defer s.mu.RUnlock() |
| 945 |
|
| 946 |
nics := make(map[tcpip.NICID][]tcpip.ProtocolAddress) |
| 947 |
for id, nic := range s.nics { |
| 948 |
nics[id] = nic.allPermanentAddresses() |
| 949 |
} |
| 950 |
return nics |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
func (s *Stack) GetMainNICAddress(id tcpip.NICID, protocol tcpip.NetworkProtocolNumber) (tcpip.AddressWithPrefix, tcpip.Error) { |
| 958 |
s.mu.RLock() |
| 959 |
defer s.mu.RUnlock() |
| 960 |
|
| 961 |
nic, ok := s.nics[id] |
| 962 |
if !ok { |
| 963 |
return tcpip.AddressWithPrefix{}, &tcpip.ErrUnknownNICID{} |
| 964 |
} |
| 965 |
|
| 966 |
return nic.PrimaryAddress(protocol) |
| 967 |
} |
| 968 |
|
| 969 |
func (s *Stack) getAddressEP(nic *nic, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) AssignableAddressEndpoint { |
| 970 |
if len(localAddr) == 0 { |
| 971 |
return nic.primaryEndpoint(netProto, remoteAddr) |
| 972 |
} |
| 973 |
return nic.findEndpoint(netProto, localAddr, CanBePrimaryEndpoint) |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
func (s *Stack) findLocalRouteFromNICRLocked(localAddressNIC *nic, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) *Route { |
| 981 |
localAddressEndpoint := localAddressNIC.getAddressOrCreateTempInner(netProto, localAddr, false , NeverPrimaryEndpoint) |
| 982 |
if localAddressEndpoint == nil { |
| 983 |
return nil |
| 984 |
} |
| 985 |
|
| 986 |
var outgoingNIC *nic |
| 987 |
|
| 988 |
if localAddressNIC.hasAddress(netProto, remoteAddr) { |
| 989 |
outgoingNIC = localAddressNIC |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
if outgoingNIC == nil { |
| 995 |
for _, nic := range s.nics { |
| 996 |
if nic.hasAddress(netProto, remoteAddr) { |
| 997 |
outgoingNIC = nic |
| 998 |
break |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
if outgoingNIC == nil { |
| 1006 |
localAddressEndpoint.DecRef() |
| 1007 |
return nil |
| 1008 |
} |
| 1009 |
|
| 1010 |
r := makeLocalRoute( |
| 1011 |
netProto, |
| 1012 |
localAddr, |
| 1013 |
remoteAddr, |
| 1014 |
outgoingNIC, |
| 1015 |
localAddressNIC, |
| 1016 |
localAddressEndpoint, |
| 1017 |
) |
| 1018 |
|
| 1019 |
if r.IsOutboundBroadcast() { |
| 1020 |
r.Release() |
| 1021 |
return nil |
| 1022 |
} |
| 1023 |
|
| 1024 |
return r |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
func (s *Stack) findLocalRouteRLocked(localAddressNICID tcpip.NICID, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber) *Route { |
| 1034 |
if len(localAddr) == 0 { |
| 1035 |
localAddr = remoteAddr |
| 1036 |
} |
| 1037 |
|
| 1038 |
if localAddressNICID == 0 { |
| 1039 |
for _, localAddressNIC := range s.nics { |
| 1040 |
if r := s.findLocalRouteFromNICRLocked(localAddressNIC, localAddr, remoteAddr, netProto); r != nil { |
| 1041 |
return r |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
return nil |
| 1046 |
} |
| 1047 |
|
| 1048 |
if localAddressNIC, ok := s.nics[localAddressNICID]; ok { |
| 1049 |
return s.findLocalRouteFromNICRLocked(localAddressNIC, localAddr, remoteAddr, netProto) |
| 1050 |
} |
| 1051 |
|
| 1052 |
return nil |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
func (s *Stack) HandleLocal() bool { |
| 1057 |
return s.handleLocal |
| 1058 |
} |
| 1059 |
|
| 1060 |
func isNICForwarding(nic *nic, proto tcpip.NetworkProtocolNumber) bool { |
| 1061 |
switch forwarding, err := nic.forwarding(proto); err.(type) { |
| 1062 |
case nil: |
| 1063 |
return forwarding |
| 1064 |
case *tcpip.ErrUnknownProtocol: |
| 1065 |
panic(fmt.Sprintf("expected network protocol %d to be available on NIC %d", proto, nic.ID())) |
| 1066 |
case *tcpip.ErrNotSupported: |
| 1067 |
|
| 1068 |
return false |
| 1069 |
default: |
| 1070 |
panic(fmt.Sprintf("nic(id=%d).forwarding(%d): %s", nic.ID(), proto, err)) |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address, netProto tcpip.NetworkProtocolNumber, multicastLoop bool) (*Route, tcpip.Error) { |
| 1086 |
s.mu.RLock() |
| 1087 |
defer s.mu.RUnlock() |
| 1088 |
|
| 1089 |
isLinkLocal := header.IsV6LinkLocalUnicastAddress(remoteAddr) || header.IsV6LinkLocalMulticastAddress(remoteAddr) |
| 1090 |
isLocalBroadcast := remoteAddr == header.IPv4Broadcast |
| 1091 |
isMulticast := header.IsV4MulticastAddress(remoteAddr) || header.IsV6MulticastAddress(remoteAddr) |
| 1092 |
isLoopback := header.IsV4LoopbackAddress(remoteAddr) || header.IsV6LoopbackAddress(remoteAddr) |
| 1093 |
needRoute := !(isLocalBroadcast || isMulticast || isLinkLocal || isLoopback) |
| 1094 |
|
| 1095 |
if s.handleLocal && !isMulticast && !isLocalBroadcast { |
| 1096 |
if r := s.findLocalRouteRLocked(id, localAddr, remoteAddr, netProto); r != nil { |
| 1097 |
return r, nil |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
if id != 0 && !needRoute { |
| 1104 |
if nic, ok := s.nics[id]; ok && nic.Enabled() { |
| 1105 |
if addressEndpoint := s.getAddressEP(nic, localAddr, remoteAddr, netProto); addressEndpoint != nil { |
| 1106 |
return makeRoute( |
| 1107 |
netProto, |
| 1108 |
"", |
| 1109 |
localAddr, |
| 1110 |
remoteAddr, |
| 1111 |
nic, |
| 1112 |
nic, |
| 1113 |
addressEndpoint, |
| 1114 |
s.handleLocal, |
| 1115 |
multicastLoop, |
| 1116 |
), nil |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
if isLoopback { |
| 1121 |
return nil, &tcpip.ErrBadLocalAddress{} |
| 1122 |
} |
| 1123 |
return nil, &tcpip.ErrNetworkUnreachable{} |
| 1124 |
} |
| 1125 |
|
| 1126 |
onlyGlobalAddresses := !header.IsV6LinkLocalUnicastAddress(localAddr) && !isLinkLocal |
| 1127 |
|
| 1128 |
|
| 1129 |
var chosenRoute tcpip.Route |
| 1130 |
if r := func() *Route { |
| 1131 |
s.route.mu.RLock() |
| 1132 |
defer s.route.mu.RUnlock() |
| 1133 |
|
| 1134 |
for _, route := range s.route.mu.table { |
| 1135 |
if len(remoteAddr) != 0 && !route.Destination.Contains(remoteAddr) { |
| 1136 |
continue |
| 1137 |
} |
| 1138 |
|
| 1139 |
nic, ok := s.nics[route.NIC] |
| 1140 |
if !ok || !nic.Enabled() { |
| 1141 |
continue |
| 1142 |
} |
| 1143 |
|
| 1144 |
if id == 0 || id == route.NIC { |
| 1145 |
if addressEndpoint := s.getAddressEP(nic, localAddr, remoteAddr, netProto); addressEndpoint != nil { |
| 1146 |
var gateway tcpip.Address |
| 1147 |
if needRoute { |
| 1148 |
gateway = route.Gateway |
| 1149 |
} |
| 1150 |
r := constructAndValidateRoute(netProto, addressEndpoint, nic , nic , gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop) |
| 1151 |
if r == nil { |
| 1152 |
panic(fmt.Sprintf("non-forwarding route validation failed with route table entry = %#v, id = %d, localAddr = %s, remoteAddr = %s", route, id, localAddr, remoteAddr)) |
| 1153 |
} |
| 1154 |
return r |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
if onlyGlobalAddresses && chosenRoute == (tcpip.Route{}) && isNICForwarding(nic, netProto) { |
| 1166 |
chosenRoute = route |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
return nil |
| 1171 |
}(); r != nil { |
| 1172 |
return r, nil |
| 1173 |
} |
| 1174 |
|
| 1175 |
if chosenRoute != (tcpip.Route{}) { |
| 1176 |
|
| 1177 |
|
| 1178 |
nic, ok := s.nics[chosenRoute.NIC] |
| 1179 |
if !ok { |
| 1180 |
|
| 1181 |
panic(fmt.Sprintf("chosen route must have a valid NIC with ID = %d", chosenRoute.NIC)) |
| 1182 |
} |
| 1183 |
|
| 1184 |
var gateway tcpip.Address |
| 1185 |
if needRoute { |
| 1186 |
gateway = chosenRoute.Gateway |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
if id != 0 { |
| 1191 |
if aNIC, ok := s.nics[id]; ok { |
| 1192 |
if addressEndpoint := s.getAddressEP(aNIC, localAddr, remoteAddr, netProto); addressEndpoint != nil { |
| 1193 |
if r := constructAndValidateRoute(netProto, addressEndpoint, aNIC , nic , gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop); r != nil { |
| 1194 |
return r, nil |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
return nil, &tcpip.ErrNoRoute{} |
| 1200 |
} |
| 1201 |
|
| 1202 |
if id == 0 { |
| 1203 |
|
| 1204 |
|
| 1205 |
for _, aNIC := range s.nics { |
| 1206 |
addressEndpoint := s.getAddressEP(aNIC, localAddr, remoteAddr, netProto) |
| 1207 |
if addressEndpoint == nil { |
| 1208 |
continue |
| 1209 |
} |
| 1210 |
|
| 1211 |
if r := constructAndValidateRoute(netProto, addressEndpoint, aNIC , nic , gateway, localAddr, remoteAddr, s.handleLocal, multicastLoop); r != nil { |
| 1212 |
return r, nil |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
if needRoute { |
| 1219 |
return nil, &tcpip.ErrNoRoute{} |
| 1220 |
} |
| 1221 |
if header.IsV6LoopbackAddress(remoteAddr) { |
| 1222 |
return nil, &tcpip.ErrBadLocalAddress{} |
| 1223 |
} |
| 1224 |
return nil, &tcpip.ErrNetworkUnreachable{} |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
func (s *Stack) CheckNetworkProtocol(protocol tcpip.NetworkProtocolNumber) bool { |
| 1230 |
_, ok := s.networkProtocols[protocol] |
| 1231 |
return ok |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
func (s *Stack) CheckDuplicateAddress(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, h DADCompletionHandler) (DADCheckAddressDisposition, tcpip.Error) { |
| 1237 |
nic, ok := s.nics[nicID] |
| 1238 |
if !ok { |
| 1239 |
return 0, &tcpip.ErrUnknownNICID{} |
| 1240 |
} |
| 1241 |
|
| 1242 |
return nic.checkDuplicateAddress(protocol, addr, h) |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
func (s *Stack) CheckLocalAddress(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.NICID { |
| 1249 |
s.mu.RLock() |
| 1250 |
defer s.mu.RUnlock() |
| 1251 |
|
| 1252 |
|
| 1253 |
if nicID != 0 { |
| 1254 |
nic, ok := s.nics[nicID] |
| 1255 |
if !ok { |
| 1256 |
return 0 |
| 1257 |
} |
| 1258 |
|
| 1259 |
if nic.CheckLocalAddress(protocol, addr) { |
| 1260 |
return nic.id |
| 1261 |
} |
| 1262 |
|
| 1263 |
return 0 |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
for _, nic := range s.nics { |
| 1268 |
if nic.CheckLocalAddress(protocol, addr) { |
| 1269 |
return nic.id |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
return 0 |
| 1274 |
} |
| 1275 |
|
| 1276 |
|
| 1277 |
func (s *Stack) SetPromiscuousMode(nicID tcpip.NICID, enable bool) tcpip.Error { |
| 1278 |
s.mu.RLock() |
| 1279 |
defer s.mu.RUnlock() |
| 1280 |
|
| 1281 |
nic, ok := s.nics[nicID] |
| 1282 |
if !ok { |
| 1283 |
return &tcpip.ErrUnknownNICID{} |
| 1284 |
} |
| 1285 |
|
| 1286 |
nic.setPromiscuousMode(enable) |
| 1287 |
|
| 1288 |
return nil |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
func (s *Stack) SetSpoofing(nicID tcpip.NICID, enable bool) tcpip.Error { |
| 1294 |
s.mu.RLock() |
| 1295 |
defer s.mu.RUnlock() |
| 1296 |
|
| 1297 |
nic, ok := s.nics[nicID] |
| 1298 |
if !ok { |
| 1299 |
return &tcpip.ErrUnknownNICID{} |
| 1300 |
} |
| 1301 |
|
| 1302 |
nic.setSpoofing(enable) |
| 1303 |
|
| 1304 |
return nil |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
type LinkResolutionResult struct { |
| 1309 |
LinkAddress tcpip.LinkAddress |
| 1310 |
Err tcpip.Error |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
func (s *Stack) GetLinkAddress(nicID tcpip.NICID, addr, localAddr tcpip.Address, protocol tcpip.NetworkProtocolNumber, onResolve func(LinkResolutionResult)) tcpip.Error { |
| 1330 |
s.mu.RLock() |
| 1331 |
nic, ok := s.nics[nicID] |
| 1332 |
s.mu.RUnlock() |
| 1333 |
if !ok { |
| 1334 |
return &tcpip.ErrUnknownNICID{} |
| 1335 |
} |
| 1336 |
|
| 1337 |
return nic.getLinkAddress(addr, localAddr, protocol, onResolve) |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
func (s *Stack) Neighbors(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber) ([]NeighborEntry, tcpip.Error) { |
| 1342 |
s.mu.RLock() |
| 1343 |
nic, ok := s.nics[nicID] |
| 1344 |
s.mu.RUnlock() |
| 1345 |
|
| 1346 |
if !ok { |
| 1347 |
return nil, &tcpip.ErrUnknownNICID{} |
| 1348 |
} |
| 1349 |
|
| 1350 |
return nic.neighbors(protocol) |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
func (s *Stack) AddStaticNeighbor(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address, linkAddr tcpip.LinkAddress) tcpip.Error { |
| 1355 |
s.mu.RLock() |
| 1356 |
nic, ok := s.nics[nicID] |
| 1357 |
s.mu.RUnlock() |
| 1358 |
|
| 1359 |
if !ok { |
| 1360 |
return &tcpip.ErrUnknownNICID{} |
| 1361 |
} |
| 1362 |
|
| 1363 |
return nic.addStaticNeighbor(addr, protocol, linkAddr) |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
func (s *Stack) RemoveNeighbor(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) tcpip.Error { |
| 1370 |
s.mu.RLock() |
| 1371 |
nic, ok := s.nics[nicID] |
| 1372 |
s.mu.RUnlock() |
| 1373 |
|
| 1374 |
if !ok { |
| 1375 |
return &tcpip.ErrUnknownNICID{} |
| 1376 |
} |
| 1377 |
|
| 1378 |
return nic.removeNeighbor(protocol, addr) |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
func (s *Stack) ClearNeighbors(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber) tcpip.Error { |
| 1383 |
s.mu.RLock() |
| 1384 |
nic, ok := s.nics[nicID] |
| 1385 |
s.mu.RUnlock() |
| 1386 |
|
| 1387 |
if !ok { |
| 1388 |
return &tcpip.ErrUnknownNICID{} |
| 1389 |
} |
| 1390 |
|
| 1391 |
return nic.clearNeighbors(protocol) |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
func (s *Stack) RegisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) tcpip.Error { |
| 1399 |
return s.demux.registerEndpoint(netProtos, protocol, id, ep, flags, bindToDevice) |
| 1400 |
} |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
func (s *Stack) CheckRegisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, flags ports.Flags, bindToDevice tcpip.NICID) tcpip.Error { |
| 1405 |
return s.demux.checkEndpoint(netProtos, protocol, id, flags, bindToDevice) |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
func (s *Stack) UnregisterTransportEndpoint(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) { |
| 1411 |
s.demux.unregisterEndpoint(netProtos, protocol, id, ep, flags, bindToDevice) |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
func (s *Stack) StartTransportEndpointCleanup(netProtos []tcpip.NetworkProtocolNumber, protocol tcpip.TransportProtocolNumber, id TransportEndpointID, ep TransportEndpoint, flags ports.Flags, bindToDevice tcpip.NICID) { |
| 1417 |
s.cleanupEndpointsMu.Lock() |
| 1418 |
s.cleanupEndpoints[ep] = struct{}{} |
| 1419 |
s.cleanupEndpointsMu.Unlock() |
| 1420 |
|
| 1421 |
s.demux.unregisterEndpoint(netProtos, protocol, id, ep, flags, bindToDevice) |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
func (s *Stack) CompleteTransportEndpointCleanup(ep TransportEndpoint) { |
| 1427 |
s.cleanupEndpointsMu.Lock() |
| 1428 |
delete(s.cleanupEndpoints, ep) |
| 1429 |
s.cleanupEndpointsMu.Unlock() |
| 1430 |
} |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
func (s *Stack) FindTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, id TransportEndpointID, nicID tcpip.NICID) TransportEndpoint { |
| 1435 |
return s.demux.findTransportEndpoint(netProto, transProto, id, nicID) |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
func (s *Stack) RegisterRawTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, ep RawTransportEndpoint) tcpip.Error { |
| 1442 |
return s.demux.registerRawEndpoint(netProto, transProto, ep) |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
func (s *Stack) UnregisterRawTransportEndpoint(netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, ep RawTransportEndpoint) { |
| 1448 |
s.demux.unregisterRawEndpoint(netProto, transProto, ep) |
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
func (s *Stack) RegisterRestoredEndpoint(e ResumableEndpoint) { |
| 1454 |
s.mu.Lock() |
| 1455 |
s.resumableEndpoints = append(s.resumableEndpoints, e) |
| 1456 |
s.mu.Unlock() |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
func (s *Stack) RegisteredEndpoints() []TransportEndpoint { |
| 1461 |
s.mu.Lock() |
| 1462 |
defer s.mu.Unlock() |
| 1463 |
var es []TransportEndpoint |
| 1464 |
for _, e := range s.demux.protocol { |
| 1465 |
es = append(es, e.transportEndpoints()...) |
| 1466 |
} |
| 1467 |
return es |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
func (s *Stack) CleanupEndpoints() []TransportEndpoint { |
| 1472 |
s.cleanupEndpointsMu.Lock() |
| 1473 |
es := make([]TransportEndpoint, 0, len(s.cleanupEndpoints)) |
| 1474 |
for e := range s.cleanupEndpoints { |
| 1475 |
es = append(es, e) |
| 1476 |
} |
| 1477 |
s.cleanupEndpointsMu.Unlock() |
| 1478 |
return es |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
func (s *Stack) RestoreCleanupEndpoints(es []TransportEndpoint) { |
| 1484 |
s.cleanupEndpointsMu.Lock() |
| 1485 |
for _, e := range es { |
| 1486 |
s.cleanupEndpoints[e] = struct{}{} |
| 1487 |
} |
| 1488 |
s.cleanupEndpointsMu.Unlock() |
| 1489 |
} |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
func (s *Stack) Close() { |
| 1495 |
for _, e := range s.RegisteredEndpoints() { |
| 1496 |
e.Abort() |
| 1497 |
} |
| 1498 |
for _, p := range s.transportProtocols { |
| 1499 |
p.proto.Close() |
| 1500 |
} |
| 1501 |
for _, p := range s.networkProtocols { |
| 1502 |
p.Close() |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
func (s *Stack) Wait() { |
| 1514 |
for _, e := range s.RegisteredEndpoints() { |
| 1515 |
e.Wait() |
| 1516 |
} |
| 1517 |
for _, e := range s.CleanupEndpoints() { |
| 1518 |
e.Wait() |
| 1519 |
} |
| 1520 |
for _, p := range s.transportProtocols { |
| 1521 |
p.proto.Wait() |
| 1522 |
} |
| 1523 |
for _, p := range s.networkProtocols { |
| 1524 |
p.Wait() |
| 1525 |
} |
| 1526 |
|
| 1527 |
s.mu.RLock() |
| 1528 |
defer s.mu.RUnlock() |
| 1529 |
for _, n := range s.nics { |
| 1530 |
n.LinkEndpoint.Wait() |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
func (s *Stack) Resume() { |
| 1537 |
|
| 1538 |
|
| 1539 |
s.mu.Lock() |
| 1540 |
eps := s.resumableEndpoints |
| 1541 |
s.resumableEndpoints = nil |
| 1542 |
s.mu.Unlock() |
| 1543 |
for _, e := range eps { |
| 1544 |
e.Resume(s) |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
func (s *Stack) RegisterPacketEndpoint(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) tcpip.Error { |
| 1552 |
s.mu.Lock() |
| 1553 |
defer s.mu.Unlock() |
| 1554 |
|
| 1555 |
|
| 1556 |
if nicID == 0 { |
| 1557 |
|
| 1558 |
for _, nic := range s.nics { |
| 1559 |
if err := nic.registerPacketEndpoint(netProto, ep); err != nil { |
| 1560 |
s.unregisterPacketEndpointLocked(0, netProto, ep) |
| 1561 |
return err |
| 1562 |
} |
| 1563 |
} |
| 1564 |
return nil |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
nic, ok := s.nics[nicID] |
| 1569 |
if !ok { |
| 1570 |
return &tcpip.ErrUnknownNICID{} |
| 1571 |
} |
| 1572 |
if err := nic.registerPacketEndpoint(netProto, ep); err != nil { |
| 1573 |
return err |
| 1574 |
} |
| 1575 |
|
| 1576 |
return nil |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
func (s *Stack) UnregisterPacketEndpoint(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) { |
| 1583 |
s.mu.Lock() |
| 1584 |
defer s.mu.Unlock() |
| 1585 |
s.unregisterPacketEndpointLocked(nicID, netProto, ep) |
| 1586 |
} |
| 1587 |
|
| 1588 |
func (s *Stack) unregisterPacketEndpointLocked(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, ep PacketEndpoint) { |
| 1589 |
|
| 1590 |
if nicID == 0 { |
| 1591 |
|
| 1592 |
for _, nic := range s.nics { |
| 1593 |
nic.unregisterPacketEndpoint(netProto, ep) |
| 1594 |
} |
| 1595 |
return |
| 1596 |
} |
| 1597 |
|
| 1598 |
|
| 1599 |
nic, ok := s.nics[nicID] |
| 1600 |
if !ok { |
| 1601 |
return |
| 1602 |
} |
| 1603 |
nic.unregisterPacketEndpoint(netProto, ep) |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
func (s *Stack) WritePacketToRemote(nicID tcpip.NICID, remote tcpip.LinkAddress, netProto tcpip.NetworkProtocolNumber, payload buffer.VectorisedView) tcpip.Error { |
| 1609 |
s.mu.Lock() |
| 1610 |
nic, ok := s.nics[nicID] |
| 1611 |
s.mu.Unlock() |
| 1612 |
if !ok { |
| 1613 |
return &tcpip.ErrUnknownDevice{} |
| 1614 |
} |
| 1615 |
pkt := NewPacketBuffer(PacketBufferOptions{ |
| 1616 |
ReserveHeaderBytes: int(nic.MaxHeaderLength()), |
| 1617 |
Data: payload, |
| 1618 |
}) |
| 1619 |
defer pkt.DecRef() |
| 1620 |
pkt.NetworkProtocolNumber = netProto |
| 1621 |
return nic.WritePacketToRemote(remote, netProto, pkt) |
| 1622 |
} |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
func (s *Stack) WriteRawPacket(nicID tcpip.NICID, proto tcpip.NetworkProtocolNumber, payload buffer.VectorisedView) tcpip.Error { |
| 1627 |
s.mu.RLock() |
| 1628 |
nic, ok := s.nics[nicID] |
| 1629 |
s.mu.RUnlock() |
| 1630 |
if !ok { |
| 1631 |
return &tcpip.ErrUnknownNICID{} |
| 1632 |
} |
| 1633 |
|
| 1634 |
pkt := NewPacketBuffer(PacketBufferOptions{ |
| 1635 |
Data: payload, |
| 1636 |
}) |
| 1637 |
defer pkt.DecRef() |
| 1638 |
pkt.NetworkProtocolNumber = proto |
| 1639 |
return nic.WriteRawPacket(pkt) |
| 1640 |
} |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
func (s *Stack) NetworkProtocolInstance(num tcpip.NetworkProtocolNumber) NetworkProtocol { |
| 1646 |
if p, ok := s.networkProtocols[num]; ok { |
| 1647 |
return p |
| 1648 |
} |
| 1649 |
return nil |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
func (s *Stack) TransportProtocolInstance(num tcpip.TransportProtocolNumber) TransportProtocol { |
| 1656 |
if pState, ok := s.transportProtocols[num]; ok { |
| 1657 |
return pState.proto |
| 1658 |
} |
| 1659 |
return nil |
| 1660 |
} |
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
func (s *Stack) AddTCPProbe(probe TCPProbeFunc) { |
| 1674 |
s.tcpProbeFunc.Store(probe) |
| 1675 |
} |
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
func (s *Stack) GetTCPProbe() TCPProbeFunc { |
| 1680 |
p := s.tcpProbeFunc.Load() |
| 1681 |
if p == nil { |
| 1682 |
return nil |
| 1683 |
} |
| 1684 |
return p.(TCPProbeFunc) |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
func (s *Stack) RemoveTCPProbe() { |
| 1693 |
|
| 1694 |
s.tcpProbeFunc.Store(TCPProbeFunc(nil)) |
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
func (s *Stack) JoinGroup(protocol tcpip.NetworkProtocolNumber, nicID tcpip.NICID, multicastAddr tcpip.Address) tcpip.Error { |
| 1699 |
s.mu.RLock() |
| 1700 |
defer s.mu.RUnlock() |
| 1701 |
|
| 1702 |
if nic, ok := s.nics[nicID]; ok { |
| 1703 |
return nic.joinGroup(protocol, multicastAddr) |
| 1704 |
} |
| 1705 |
return &tcpip.ErrUnknownNICID{} |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
func (s *Stack) LeaveGroup(protocol tcpip.NetworkProtocolNumber, nicID tcpip.NICID, multicastAddr tcpip.Address) tcpip.Error { |
| 1710 |
s.mu.RLock() |
| 1711 |
defer s.mu.RUnlock() |
| 1712 |
|
| 1713 |
if nic, ok := s.nics[nicID]; ok { |
| 1714 |
return nic.leaveGroup(protocol, multicastAddr) |
| 1715 |
} |
| 1716 |
return &tcpip.ErrUnknownNICID{} |
| 1717 |
} |
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
func (s *Stack) IsInGroup(nicID tcpip.NICID, multicastAddr tcpip.Address) (bool, tcpip.Error) { |
| 1722 |
s.mu.RLock() |
| 1723 |
defer s.mu.RUnlock() |
| 1724 |
|
| 1725 |
if nic, ok := s.nics[nicID]; ok { |
| 1726 |
return nic.isInGroup(multicastAddr), nil |
| 1727 |
} |
| 1728 |
return false, &tcpip.ErrUnknownNICID{} |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
func (s *Stack) IPTables() *IPTables { |
| 1733 |
return s.tables |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
func (s *Stack) ICMPLimit() rate.Limit { |
| 1739 |
return s.icmpRateLimiter.Limit() |
| 1740 |
} |
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
func (s *Stack) SetICMPLimit(newLimit rate.Limit) { |
| 1745 |
s.icmpRateLimiter.SetLimit(newLimit) |
| 1746 |
} |
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
func (s *Stack) ICMPBurst() int { |
| 1751 |
return s.icmpRateLimiter.Burst() |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
func (s *Stack) SetICMPBurst(burst int) { |
| 1757 |
s.icmpRateLimiter.SetBurst(burst) |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
func (s *Stack) AllowICMPMessage() bool { |
| 1763 |
return s.icmpRateLimiter.Allow() |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
func (s *Stack) GetNetworkEndpoint(nicID tcpip.NICID, proto tcpip.NetworkProtocolNumber) (NetworkEndpoint, tcpip.Error) { |
| 1769 |
s.mu.Lock() |
| 1770 |
defer s.mu.Unlock() |
| 1771 |
|
| 1772 |
nic, ok := s.nics[nicID] |
| 1773 |
if !ok { |
| 1774 |
return nil, &tcpip.ErrUnknownNICID{} |
| 1775 |
} |
| 1776 |
|
| 1777 |
return nic.getNetworkEndpoint(proto), nil |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
func (s *Stack) NUDConfigurations(id tcpip.NICID, proto tcpip.NetworkProtocolNumber) (NUDConfigurations, tcpip.Error) { |
| 1782 |
s.mu.RLock() |
| 1783 |
nic, ok := s.nics[id] |
| 1784 |
s.mu.RUnlock() |
| 1785 |
|
| 1786 |
if !ok { |
| 1787 |
return NUDConfigurations{}, &tcpip.ErrUnknownNICID{} |
| 1788 |
} |
| 1789 |
|
| 1790 |
return nic.nudConfigs(proto) |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
func (s *Stack) SetNUDConfigurations(id tcpip.NICID, proto tcpip.NetworkProtocolNumber, c NUDConfigurations) tcpip.Error { |
| 1798 |
s.mu.RLock() |
| 1799 |
nic, ok := s.nics[id] |
| 1800 |
s.mu.RUnlock() |
| 1801 |
|
| 1802 |
if !ok { |
| 1803 |
return &tcpip.ErrUnknownNICID{} |
| 1804 |
} |
| 1805 |
|
| 1806 |
return nic.setNUDConfigs(proto, c) |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
func (s *Stack) Seed() uint32 { |
| 1813 |
return s.seed |
| 1814 |
} |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
func (s *Stack) Rand() *rand.Rand { |
| 1819 |
return s.randomGenerator |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
func (s *Stack) SecureRNG() io.Reader { |
| 1825 |
return s.secureRNG |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
func (s *Stack) FindNICNameFromID(id tcpip.NICID) string { |
| 1830 |
s.mu.RLock() |
| 1831 |
defer s.mu.RUnlock() |
| 1832 |
|
| 1833 |
nic, ok := s.nics[id] |
| 1834 |
if !ok { |
| 1835 |
return "" |
| 1836 |
} |
| 1837 |
|
| 1838 |
return nic.Name() |
| 1839 |
} |
| 1840 |
|
| 1841 |
|
| 1842 |
type ParseResult int |
| 1843 |
|
| 1844 |
const ( |
| 1845 |
|
| 1846 |
ParsedOK ParseResult = iota |
| 1847 |
|
| 1848 |
|
| 1849 |
UnknownTransportProtocol |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
TransportLayerParseError |
| 1854 |
) |
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
func (s *Stack) ParsePacketBufferTransport(protocol tcpip.TransportProtocolNumber, pkt *PacketBuffer) ParseResult { |
| 1859 |
pkt.TransportProtocolNumber = protocol |
| 1860 |
|
| 1861 |
state, ok := s.transportProtocols[protocol] |
| 1862 |
if !ok { |
| 1863 |
return UnknownTransportProtocol |
| 1864 |
} |
| 1865 |
|
| 1866 |
if !state.proto.Parse(pkt) { |
| 1867 |
return TransportLayerParseError |
| 1868 |
} |
| 1869 |
|
| 1870 |
return ParsedOK |
| 1871 |
} |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
func (s *Stack) networkProtocolNumbers() []tcpip.NetworkProtocolNumber { |
| 1876 |
protos := make([]tcpip.NetworkProtocolNumber, 0, len(s.networkProtocols)) |
| 1877 |
for p := range s.networkProtocols { |
| 1878 |
protos = append(protos, p) |
| 1879 |
} |
| 1880 |
return protos |
| 1881 |
} |
| 1882 |
|
| 1883 |
func isSubnetBroadcastOnNIC(nic *nic, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool { |
| 1884 |
addressEndpoint := nic.getAddressOrCreateTempInner(protocol, addr, false , NeverPrimaryEndpoint) |
| 1885 |
if addressEndpoint == nil { |
| 1886 |
return false |
| 1887 |
} |
| 1888 |
|
| 1889 |
subnet := addressEndpoint.Subnet() |
| 1890 |
addressEndpoint.DecRef() |
| 1891 |
return subnet.IsBroadcast(addr) |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
func (s *Stack) IsSubnetBroadcast(nicID tcpip.NICID, protocol tcpip.NetworkProtocolNumber, addr tcpip.Address) bool { |
| 1902 |
s.mu.RLock() |
| 1903 |
defer s.mu.RUnlock() |
| 1904 |
|
| 1905 |
if nicID != 0 { |
| 1906 |
nic, ok := s.nics[nicID] |
| 1907 |
if !ok { |
| 1908 |
return false |
| 1909 |
} |
| 1910 |
|
| 1911 |
return isSubnetBroadcastOnNIC(nic, protocol, addr) |
| 1912 |
} |
| 1913 |
|
| 1914 |
for _, nic := range s.nics { |
| 1915 |
if isSubnetBroadcastOnNIC(nic, protocol, addr) { |
| 1916 |
return true |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
return false |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
func (s *Stack) PacketEndpointWriteSupported() bool { |
| 1926 |
return s.packetEndpointWriteSupported |
| 1927 |
} |