| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package ports |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
"math/rand" |
| 22 |
"sync/atomic" |
| 23 |
|
| 24 |
"inet.af/netstack/sync" |
| 25 |
"inet.af/netstack/tcpip" |
| 26 |
"inet.af/netstack/tcpip/header" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
firstEphemeral = 16000 |
| 31 |
anyIPAddress tcpip.Address = "" |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
type Reservation struct { |
| 36 |
|
| 37 |
|
| 38 |
Networks []tcpip.NetworkProtocolNumber |
| 39 |
|
| 40 |
|
| 41 |
Transport tcpip.TransportProtocolNumber |
| 42 |
|
| 43 |
|
| 44 |
Addr tcpip.Address |
| 45 |
|
| 46 |
|
| 47 |
Port uint16 |
| 48 |
|
| 49 |
|
| 50 |
Flags Flags |
| 51 |
|
| 52 |
|
| 53 |
BindToDevice tcpip.NICID |
| 54 |
|
| 55 |
|
| 56 |
Dest tcpip.FullAddress |
| 57 |
} |
| 58 |
|
| 59 |
func (rs Reservation) dst() destination { |
| 60 |
return destination{ |
| 61 |
rs.Dest.Addr, |
| 62 |
rs.Dest.Port, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
type portDescriptor struct { |
| 67 |
network tcpip.NetworkProtocolNumber |
| 68 |
transport tcpip.TransportProtocolNumber |
| 69 |
port uint16 |
| 70 |
} |
| 71 |
|
| 72 |
type destination struct { |
| 73 |
addr tcpip.Address |
| 74 |
port uint16 |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
type destToCounter map[destination]FlagCounter |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
func (dc destToCounter) intersectionFlags(res Reservation) (BitFlags, int) { |
| 93 |
intersection := FlagMask |
| 94 |
var count int |
| 95 |
|
| 96 |
for dest, counter := range dc { |
| 97 |
if dest == res.dst() { |
| 98 |
intersection &= counter.SharedFlags() |
| 99 |
count++ |
| 100 |
continue |
| 101 |
} |
| 102 |
|
| 103 |
if dest.addr == anyIPAddress || res.Dest.Addr == anyIPAddress { |
| 104 |
|
| 105 |
intersection &= (^TupleOnlyFlag) | counter.SharedFlags() |
| 106 |
count++ |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return intersection, count |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
type deviceToDest map[tcpip.NICID]destToCounter |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func (dd deviceToDest) isAvailable(res Reservation, portSpecified bool) bool { |
| 127 |
flagBits := res.Flags.Bits() |
| 128 |
if res.BindToDevice == 0 { |
| 129 |
intersection := FlagMask |
| 130 |
for _, dest := range dd { |
| 131 |
flags, count := dest.intersectionFlags(res) |
| 132 |
if count == 0 { |
| 133 |
continue |
| 134 |
} |
| 135 |
intersection &= flags |
| 136 |
if intersection&flagBits == 0 { |
| 137 |
|
| 138 |
|
| 139 |
return false |
| 140 |
} |
| 141 |
} |
| 142 |
if !portSpecified && res.Transport == header.TCPProtocolNumber { |
| 143 |
return false |
| 144 |
} |
| 145 |
return true |
| 146 |
} |
| 147 |
|
| 148 |
intersection := FlagMask |
| 149 |
|
| 150 |
if dests, ok := dd[0]; ok { |
| 151 |
var count int |
| 152 |
intersection, count = dests.intersectionFlags(res) |
| 153 |
if count > 0 { |
| 154 |
if intersection&flagBits == 0 { |
| 155 |
return false |
| 156 |
} |
| 157 |
if !portSpecified && res.Transport == header.TCPProtocolNumber { |
| 158 |
return false |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if dests, ok := dd[res.BindToDevice]; ok { |
| 164 |
flags, count := dests.intersectionFlags(res) |
| 165 |
intersection &= flags |
| 166 |
if count > 0 { |
| 167 |
if intersection&flagBits == 0 { |
| 168 |
return false |
| 169 |
} |
| 170 |
if !portSpecified && res.Transport == header.TCPProtocolNumber { |
| 171 |
return false |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return true |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
type addrToDevice map[tcpip.Address]deviceToDest |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
func (ad addrToDevice) isAvailable(res Reservation, portSpecified bool) bool { |
| 186 |
if res.Addr == anyIPAddress { |
| 187 |
|
| 188 |
|
| 189 |
for _, devices := range ad { |
| 190 |
if !devices.isAvailable(res, portSpecified) { |
| 191 |
return false |
| 192 |
} |
| 193 |
} |
| 194 |
return true |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if devices, ok := ad[anyIPAddress]; ok { |
| 199 |
if !devices.isAvailable(res, portSpecified) { |
| 200 |
return false |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
if devices, ok := ad[res.Addr]; ok { |
| 206 |
if !devices.isAvailable(res, portSpecified) { |
| 207 |
return false |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return true |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
type PortManager struct { |
| 216 |
|
| 217 |
|
| 218 |
mu sync.RWMutex |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
allocatedPorts map[portDescriptor]addrToDevice |
| 223 |
|
| 224 |
|
| 225 |
ephemeralMu sync.RWMutex |
| 226 |
firstEphemeral uint16 |
| 227 |
numEphemeral uint16 |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
hint uint32 |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
func NewPortManager() *PortManager { |
| 239 |
return &PortManager{ |
| 240 |
allocatedPorts: make(map[portDescriptor]addrToDevice), |
| 241 |
firstEphemeral: firstEphemeral, |
| 242 |
numEphemeral: math.MaxUint16 - firstEphemeral + 1, |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
type PortTester func(port uint16) (good bool, err tcpip.Error) |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
func (pm *PortManager) PickEphemeralPort(rng *rand.Rand, testPort PortTester) (port uint16, err tcpip.Error) { |
| 256 |
pm.ephemeralMu.RLock() |
| 257 |
firstEphemeral := pm.firstEphemeral |
| 258 |
numEphemeral := pm.numEphemeral |
| 259 |
pm.ephemeralMu.RUnlock() |
| 260 |
|
| 261 |
offset := uint32(rng.Int31n(int32(numEphemeral))) |
| 262 |
return pickEphemeralPort(offset, firstEphemeral, numEphemeral, testPort) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
func (pm *PortManager) portHint() uint32 { |
| 267 |
return atomic.LoadUint32(&pm.hint) |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
func (pm *PortManager) incPortHint() { |
| 272 |
atomic.AddUint32(&pm.hint, 1) |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
func (pm *PortManager) PickEphemeralPortStable(offset uint32, testPort PortTester) (port uint16, err tcpip.Error) { |
| 280 |
pm.ephemeralMu.RLock() |
| 281 |
firstEphemeral := pm.firstEphemeral |
| 282 |
numEphemeral := pm.numEphemeral |
| 283 |
pm.ephemeralMu.RUnlock() |
| 284 |
|
| 285 |
p, err := pickEphemeralPort(pm.portHint()+offset, firstEphemeral, numEphemeral, testPort) |
| 286 |
if err == nil { |
| 287 |
pm.incPortHint() |
| 288 |
} |
| 289 |
return p, err |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
func pickEphemeralPort(offset uint32, first, count uint16, testPort PortTester) (port uint16, err tcpip.Error) { |
| 297 |
for i := uint32(0); i < uint32(count); i++ { |
| 298 |
port := uint16(uint32(first) + (offset+i)%uint32(count)) |
| 299 |
ok, err := testPort(port) |
| 300 |
if err != nil { |
| 301 |
return 0, err |
| 302 |
} |
| 303 |
|
| 304 |
if ok { |
| 305 |
return port, nil |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return 0, &tcpip.ErrNoPortAvailable{} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
func (pm *PortManager) ReservePort(rng *rand.Rand, res Reservation, testPort PortTester) (reservedPort uint16, err tcpip.Error) { |
| 321 |
pm.mu.Lock() |
| 322 |
defer pm.mu.Unlock() |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if res.Port != 0 { |
| 327 |
if !pm.reserveSpecificPortLocked(res, true ) { |
| 328 |
return 0, &tcpip.ErrPortInUse{} |
| 329 |
} |
| 330 |
if testPort != nil { |
| 331 |
ok, err := testPort(res.Port) |
| 332 |
if err != nil { |
| 333 |
pm.releasePortLocked(res) |
| 334 |
return 0, err |
| 335 |
} |
| 336 |
if !ok { |
| 337 |
pm.releasePortLocked(res) |
| 338 |
return 0, &tcpip.ErrPortInUse{} |
| 339 |
} |
| 340 |
} |
| 341 |
return res.Port, nil |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
return pm.PickEphemeralPort(rng, func(p uint16) (bool, tcpip.Error) { |
| 346 |
res.Port = p |
| 347 |
if !pm.reserveSpecificPortLocked(res, false ) { |
| 348 |
return false, nil |
| 349 |
} |
| 350 |
if testPort != nil { |
| 351 |
ok, err := testPort(p) |
| 352 |
if err != nil { |
| 353 |
pm.releasePortLocked(res) |
| 354 |
return false, err |
| 355 |
} |
| 356 |
if !ok { |
| 357 |
pm.releasePortLocked(res) |
| 358 |
return false, nil |
| 359 |
} |
| 360 |
} |
| 361 |
return true, nil |
| 362 |
}) |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
func (pm *PortManager) reserveSpecificPortLocked(res Reservation, portSpecified bool) bool { |
| 368 |
|
| 369 |
for _, network := range res.Networks { |
| 370 |
desc := portDescriptor{network, res.Transport, res.Port} |
| 371 |
if addrs, ok := pm.allocatedPorts[desc]; ok { |
| 372 |
if !addrs.isAvailable(res, portSpecified) { |
| 373 |
return false |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
flagBits := res.Flags.Bits() |
| 380 |
dst := res.dst() |
| 381 |
for _, network := range res.Networks { |
| 382 |
desc := portDescriptor{network, res.Transport, res.Port} |
| 383 |
addrToDev, ok := pm.allocatedPorts[desc] |
| 384 |
if !ok { |
| 385 |
addrToDev = make(addrToDevice) |
| 386 |
pm.allocatedPorts[desc] = addrToDev |
| 387 |
} |
| 388 |
devToDest, ok := addrToDev[res.Addr] |
| 389 |
if !ok { |
| 390 |
devToDest = make(deviceToDest) |
| 391 |
addrToDev[res.Addr] = devToDest |
| 392 |
} |
| 393 |
destToCntr := devToDest[res.BindToDevice] |
| 394 |
if destToCntr == nil { |
| 395 |
destToCntr = make(destToCounter) |
| 396 |
} |
| 397 |
counter := destToCntr[dst] |
| 398 |
counter.AddRef(flagBits) |
| 399 |
destToCntr[dst] = counter |
| 400 |
devToDest[res.BindToDevice] = destToCntr |
| 401 |
} |
| 402 |
|
| 403 |
return true |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
func (pm *PortManager) ReserveTuple(res Reservation) bool { |
| 408 |
flagBits := res.Flags.Bits() |
| 409 |
dst := res.dst() |
| 410 |
|
| 411 |
pm.mu.Lock() |
| 412 |
defer pm.mu.Unlock() |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
undo := false |
| 417 |
|
| 418 |
|
| 419 |
for _, network := range res.Networks { |
| 420 |
desc := portDescriptor{network, res.Transport, res.Port} |
| 421 |
addrToDev, ok := pm.allocatedPorts[desc] |
| 422 |
if !ok { |
| 423 |
addrToDev = make(addrToDevice) |
| 424 |
pm.allocatedPorts[desc] = addrToDev |
| 425 |
} |
| 426 |
devToDest, ok := addrToDev[res.Addr] |
| 427 |
if !ok { |
| 428 |
devToDest = make(deviceToDest) |
| 429 |
addrToDev[res.Addr] = devToDest |
| 430 |
} |
| 431 |
destToCntr := devToDest[res.BindToDevice] |
| 432 |
if destToCntr == nil { |
| 433 |
destToCntr = make(destToCounter) |
| 434 |
} |
| 435 |
|
| 436 |
counter := destToCntr[dst] |
| 437 |
if counter.TotalRefs() != 0 && counter.SharedFlags()&flagBits == 0 { |
| 438 |
|
| 439 |
undo = true |
| 440 |
} |
| 441 |
counter.AddRef(flagBits) |
| 442 |
destToCntr[dst] = counter |
| 443 |
devToDest[res.BindToDevice] = destToCntr |
| 444 |
} |
| 445 |
|
| 446 |
if undo { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
pm.releasePortLocked(res) |
| 451 |
return false |
| 452 |
} |
| 453 |
|
| 454 |
return true |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
func (pm *PortManager) ReleasePort(res Reservation) { |
| 460 |
pm.mu.Lock() |
| 461 |
defer pm.mu.Unlock() |
| 462 |
|
| 463 |
pm.releasePortLocked(res) |
| 464 |
} |
| 465 |
|
| 466 |
func (pm *PortManager) releasePortLocked(res Reservation) { |
| 467 |
dst := res.dst() |
| 468 |
for _, network := range res.Networks { |
| 469 |
desc := portDescriptor{network, res.Transport, res.Port} |
| 470 |
addrToDev, ok := pm.allocatedPorts[desc] |
| 471 |
if !ok { |
| 472 |
continue |
| 473 |
} |
| 474 |
devToDest, ok := addrToDev[res.Addr] |
| 475 |
if !ok { |
| 476 |
continue |
| 477 |
} |
| 478 |
destToCounter, ok := devToDest[res.BindToDevice] |
| 479 |
if !ok { |
| 480 |
continue |
| 481 |
} |
| 482 |
counter, ok := destToCounter[dst] |
| 483 |
if !ok { |
| 484 |
continue |
| 485 |
} |
| 486 |
counter.DropRef(res.Flags.Bits()) |
| 487 |
if counter.TotalRefs() > 0 { |
| 488 |
destToCounter[dst] = counter |
| 489 |
continue |
| 490 |
} |
| 491 |
delete(destToCounter, dst) |
| 492 |
if len(destToCounter) > 0 { |
| 493 |
continue |
| 494 |
} |
| 495 |
delete(devToDest, res.BindToDevice) |
| 496 |
if len(devToDest) > 0 { |
| 497 |
continue |
| 498 |
} |
| 499 |
delete(addrToDev, res.Addr) |
| 500 |
if len(addrToDev) > 0 { |
| 501 |
continue |
| 502 |
} |
| 503 |
delete(pm.allocatedPorts, desc) |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
func (pm *PortManager) PortRange() (uint16, uint16) { |
| 510 |
pm.ephemeralMu.RLock() |
| 511 |
defer pm.ephemeralMu.RUnlock() |
| 512 |
return pm.firstEphemeral, pm.firstEphemeral + pm.numEphemeral - 1 |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
func (pm *PortManager) SetPortRange(start uint16, end uint16) tcpip.Error { |
| 518 |
if start > end { |
| 519 |
return &tcpip.ErrInvalidPortRange{} |
| 520 |
} |
| 521 |
pm.ephemeralMu.Lock() |
| 522 |
defer pm.ephemeralMu.Unlock() |
| 523 |
pm.firstEphemeral = start |
| 524 |
pm.numEphemeral = end - start + 1 |
| 525 |
return nil |
| 526 |
} |