| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package network |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"sync/atomic" |
| 22 |
|
| 23 |
"inet.af/netstack/sync" |
| 24 |
"inet.af/netstack/tcpip" |
| 25 |
"inet.af/netstack/tcpip/header" |
| 26 |
"inet.af/netstack/tcpip/stack" |
| 27 |
"inet.af/netstack/tcpip/transport" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
type Endpoint struct { |
| 35 |
|
| 36 |
stack *stack.Stack `state:"manual"` |
| 37 |
ops *tcpip.SocketOptions |
| 38 |
netProto tcpip.NetworkProtocolNumber |
| 39 |
transProto tcpip.TransportProtocolNumber |
| 40 |
|
| 41 |
mu sync.RWMutex `state:"nosave"` |
| 42 |
|
| 43 |
wasBound bool |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
owner tcpip.PacketOwner |
| 48 |
|
| 49 |
writeShutdown bool |
| 50 |
|
| 51 |
effectiveNetProto tcpip.NetworkProtocolNumber |
| 52 |
|
| 53 |
connectedRoute *stack.Route `state:"manual"` |
| 54 |
|
| 55 |
multicastMemberships map[multicastMembership]struct{} |
| 56 |
|
| 57 |
|
| 58 |
ttl uint8 |
| 59 |
|
| 60 |
|
| 61 |
multicastTTL uint8 |
| 62 |
|
| 63 |
|
| 64 |
multicastAddr tcpip.Address |
| 65 |
|
| 66 |
|
| 67 |
multicastNICID tcpip.NICID |
| 68 |
|
| 69 |
ipv4TOS uint8 |
| 70 |
|
| 71 |
ipv6TClass uint8 |
| 72 |
|
| 73 |
|
| 74 |
infoMu sync.RWMutex `state:"nosave"` |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
info stack.TransportEndpointInfo |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
state uint32 |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
type multicastMembership struct { |
| 104 |
nicID tcpip.NICID |
| 105 |
multicastAddr tcpip.Address |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func (e *Endpoint) Init(s *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, ops *tcpip.SocketOptions) { |
| 110 |
e.mu.Lock() |
| 111 |
memberships := e.multicastMemberships |
| 112 |
e.mu.Unlock() |
| 113 |
if memberships != nil { |
| 114 |
panic(fmt.Sprintf("endpoint is already initialized; got e.multicastMemberships = %#v, want = nil", memberships)) |
| 115 |
} |
| 116 |
|
| 117 |
switch netProto { |
| 118 |
case header.IPv4ProtocolNumber, header.IPv6ProtocolNumber: |
| 119 |
default: |
| 120 |
panic(fmt.Sprintf("invalid protocol number = %d", netProto)) |
| 121 |
} |
| 122 |
|
| 123 |
*e = Endpoint{ |
| 124 |
stack: s, |
| 125 |
ops: ops, |
| 126 |
netProto: netProto, |
| 127 |
transProto: transProto, |
| 128 |
|
| 129 |
info: stack.TransportEndpointInfo{ |
| 130 |
NetProto: netProto, |
| 131 |
TransProto: transProto, |
| 132 |
}, |
| 133 |
effectiveNetProto: netProto, |
| 134 |
|
| 135 |
multicastTTL: 1, |
| 136 |
multicastMemberships: make(map[multicastMembership]struct{}), |
| 137 |
} |
| 138 |
|
| 139 |
e.mu.Lock() |
| 140 |
defer e.mu.Unlock() |
| 141 |
e.setEndpointState(transport.DatagramEndpointStateInitial) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (e *Endpoint) NetProto() tcpip.NetworkProtocolNumber { |
| 146 |
return e.netProto |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
func (e *Endpoint) setEndpointState(state transport.DatagramEndpointState) { |
| 156 |
atomic.StoreUint32(&e.state, uint32(state)) |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (e *Endpoint) State() transport.DatagramEndpointState { |
| 161 |
return transport.DatagramEndpointState(atomic.LoadUint32(&e.state)) |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
func (e *Endpoint) Close() { |
| 167 |
e.mu.Lock() |
| 168 |
defer e.mu.Unlock() |
| 169 |
|
| 170 |
if e.State() == transport.DatagramEndpointStateClosed { |
| 171 |
return |
| 172 |
} |
| 173 |
|
| 174 |
for mem := range e.multicastMemberships { |
| 175 |
e.stack.LeaveGroup(e.netProto, mem.nicID, mem.multicastAddr) |
| 176 |
} |
| 177 |
e.multicastMemberships = nil |
| 178 |
|
| 179 |
if e.connectedRoute != nil { |
| 180 |
e.connectedRoute.Release() |
| 181 |
e.connectedRoute = nil |
| 182 |
} |
| 183 |
|
| 184 |
e.setEndpointState(transport.DatagramEndpointStateClosed) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func (e *Endpoint) SetOwner(owner tcpip.PacketOwner) { |
| 189 |
e.mu.Lock() |
| 190 |
defer e.mu.Unlock() |
| 191 |
e.owner = owner |
| 192 |
} |
| 193 |
|
| 194 |
func calculateTTL(route *stack.Route, ttl uint8, multicastTTL uint8) uint8 { |
| 195 |
if header.IsV4MulticastAddress(route.RemoteAddress()) || header.IsV6MulticastAddress(route.RemoteAddress()) { |
| 196 |
return multicastTTL |
| 197 |
} |
| 198 |
|
| 199 |
if ttl == 0 { |
| 200 |
return route.DefaultTTL() |
| 201 |
} |
| 202 |
|
| 203 |
return ttl |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
type WriteContext struct { |
| 208 |
transProto tcpip.TransportProtocolNumber |
| 209 |
route *stack.Route |
| 210 |
ttl uint8 |
| 211 |
tos uint8 |
| 212 |
owner tcpip.PacketOwner |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
func (c *WriteContext) Release() { |
| 217 |
c.route.Release() |
| 218 |
*c = WriteContext{} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
type WritePacketInfo struct { |
| 223 |
NetProto tcpip.NetworkProtocolNumber |
| 224 |
LocalAddress, RemoteAddress tcpip.Address |
| 225 |
MaxHeaderLength uint16 |
| 226 |
RequiresTXTransportChecksum bool |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
func (c *WriteContext) PacketInfo() WritePacketInfo { |
| 231 |
return WritePacketInfo{ |
| 232 |
NetProto: c.route.NetProto(), |
| 233 |
LocalAddress: c.route.LocalAddress(), |
| 234 |
RemoteAddress: c.route.RemoteAddress(), |
| 235 |
MaxHeaderLength: c.route.MaxHeaderLength(), |
| 236 |
RequiresTXTransportChecksum: c.route.RequiresTXTransportChecksum(), |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
func (c *WriteContext) WritePacket(pkt *stack.PacketBuffer, headerIncluded bool) tcpip.Error { |
| 242 |
pkt.Owner = c.owner |
| 243 |
|
| 244 |
if headerIncluded { |
| 245 |
return c.route.WriteHeaderIncludedPacket(pkt) |
| 246 |
} |
| 247 |
|
| 248 |
return c.route.WritePacket(stack.NetworkHeaderParams{ |
| 249 |
Protocol: c.transProto, |
| 250 |
TTL: c.ttl, |
| 251 |
TOS: c.tos, |
| 252 |
}, pkt) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
func (e *Endpoint) AcquireContextForWrite(opts tcpip.WriteOptions) (WriteContext, tcpip.Error) { |
| 257 |
e.mu.RLock() |
| 258 |
defer e.mu.RUnlock() |
| 259 |
|
| 260 |
|
| 261 |
if opts.More { |
| 262 |
return WriteContext{}, &tcpip.ErrInvalidOptionValue{} |
| 263 |
} |
| 264 |
|
| 265 |
if e.State() == transport.DatagramEndpointStateClosed { |
| 266 |
return WriteContext{}, &tcpip.ErrInvalidEndpointState{} |
| 267 |
} |
| 268 |
|
| 269 |
if e.writeShutdown { |
| 270 |
return WriteContext{}, &tcpip.ErrClosedForSend{} |
| 271 |
} |
| 272 |
|
| 273 |
route := e.connectedRoute |
| 274 |
if opts.To == nil { |
| 275 |
|
| 276 |
|
| 277 |
if e.State() != transport.DatagramEndpointStateConnected { |
| 278 |
return WriteContext{}, &tcpip.ErrDestinationRequired{} |
| 279 |
} |
| 280 |
|
| 281 |
route.Acquire() |
| 282 |
} else { |
| 283 |
|
| 284 |
|
| 285 |
nicID := opts.To.NIC |
| 286 |
if nicID == 0 { |
| 287 |
nicID = tcpip.NICID(e.ops.GetBindToDevice()) |
| 288 |
} |
| 289 |
info := e.Info() |
| 290 |
if info.BindNICID != 0 { |
| 291 |
if nicID != 0 && nicID != info.BindNICID { |
| 292 |
return WriteContext{}, &tcpip.ErrNoRoute{} |
| 293 |
} |
| 294 |
|
| 295 |
nicID = info.BindNICID |
| 296 |
} |
| 297 |
if nicID == 0 { |
| 298 |
nicID = info.RegisterNICID |
| 299 |
} |
| 300 |
|
| 301 |
dst, netProto, err := e.checkV4Mapped(*opts.To) |
| 302 |
if err != nil { |
| 303 |
return WriteContext{}, err |
| 304 |
} |
| 305 |
|
| 306 |
route, _, err = e.connectRouteRLocked(nicID, dst, netProto) |
| 307 |
if err != nil { |
| 308 |
return WriteContext{}, err |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if !e.ops.GetBroadcast() && route.IsOutboundBroadcast() { |
| 313 |
route.Release() |
| 314 |
return WriteContext{}, &tcpip.ErrBroadcastDisabled{} |
| 315 |
} |
| 316 |
|
| 317 |
var tos uint8 |
| 318 |
switch netProto := route.NetProto(); netProto { |
| 319 |
case header.IPv4ProtocolNumber: |
| 320 |
tos = e.ipv4TOS |
| 321 |
case header.IPv6ProtocolNumber: |
| 322 |
tos = e.ipv6TClass |
| 323 |
default: |
| 324 |
panic(fmt.Sprintf("invalid protocol number = %d", netProto)) |
| 325 |
} |
| 326 |
|
| 327 |
return WriteContext{ |
| 328 |
transProto: e.transProto, |
| 329 |
route: route, |
| 330 |
ttl: calculateTTL(route, e.ttl, e.multicastTTL), |
| 331 |
tos: tos, |
| 332 |
owner: e.owner, |
| 333 |
}, nil |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
func (e *Endpoint) Disconnect() { |
| 338 |
e.mu.Lock() |
| 339 |
defer e.mu.Unlock() |
| 340 |
|
| 341 |
if e.State() != transport.DatagramEndpointStateConnected { |
| 342 |
return |
| 343 |
} |
| 344 |
|
| 345 |
info := e.Info() |
| 346 |
|
| 347 |
if e.wasBound { |
| 348 |
info.ID = stack.TransportEndpointID{ |
| 349 |
LocalAddress: info.BindAddr, |
| 350 |
} |
| 351 |
e.setEndpointState(transport.DatagramEndpointStateBound) |
| 352 |
} else { |
| 353 |
info.ID = stack.TransportEndpointID{} |
| 354 |
e.setEndpointState(transport.DatagramEndpointStateInitial) |
| 355 |
} |
| 356 |
e.setInfo(info) |
| 357 |
|
| 358 |
e.connectedRoute.Release() |
| 359 |
e.connectedRoute = nil |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
func (e *Endpoint) connectRouteRLocked(nicID tcpip.NICID, addr tcpip.FullAddress, netProto tcpip.NetworkProtocolNumber) (*stack.Route, tcpip.NICID, tcpip.Error) { |
| 368 |
localAddr := e.Info().ID.LocalAddress |
| 369 |
if e.isBroadcastOrMulticast(nicID, netProto, localAddr) { |
| 370 |
|
| 371 |
localAddr = "" |
| 372 |
} |
| 373 |
|
| 374 |
if header.IsV4MulticastAddress(addr.Addr) || header.IsV6MulticastAddress(addr.Addr) { |
| 375 |
if nicID == 0 { |
| 376 |
nicID = e.multicastNICID |
| 377 |
} |
| 378 |
if localAddr == "" && nicID == 0 { |
| 379 |
localAddr = e.multicastAddr |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
r, err := e.stack.FindRoute(nicID, localAddr, addr.Addr, netProto, e.ops.GetMulticastLoop()) |
| 385 |
if err != nil { |
| 386 |
return nil, 0, err |
| 387 |
} |
| 388 |
return r, nicID, nil |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
func (e *Endpoint) Connect(addr tcpip.FullAddress) tcpip.Error { |
| 393 |
return e.ConnectAndThen(addr, func(_ tcpip.NetworkProtocolNumber, _, _ stack.TransportEndpointID) tcpip.Error { |
| 394 |
return nil |
| 395 |
}) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
func (e *Endpoint) ConnectAndThen(addr tcpip.FullAddress, f func(netProto tcpip.NetworkProtocolNumber, previousID, nextID stack.TransportEndpointID) tcpip.Error) tcpip.Error { |
| 406 |
addr.Port = 0 |
| 407 |
|
| 408 |
e.mu.Lock() |
| 409 |
defer e.mu.Unlock() |
| 410 |
|
| 411 |
info := e.Info() |
| 412 |
nicID := addr.NIC |
| 413 |
switch e.State() { |
| 414 |
case transport.DatagramEndpointStateInitial: |
| 415 |
case transport.DatagramEndpointStateBound, transport.DatagramEndpointStateConnected: |
| 416 |
if info.BindNICID == 0 { |
| 417 |
break |
| 418 |
} |
| 419 |
|
| 420 |
if nicID != 0 && nicID != info.BindNICID { |
| 421 |
return &tcpip.ErrInvalidEndpointState{} |
| 422 |
} |
| 423 |
|
| 424 |
nicID = info.BindNICID |
| 425 |
default: |
| 426 |
return &tcpip.ErrInvalidEndpointState{} |
| 427 |
} |
| 428 |
|
| 429 |
addr, netProto, err := e.checkV4Mapped(addr) |
| 430 |
if err != nil { |
| 431 |
return err |
| 432 |
} |
| 433 |
|
| 434 |
r, nicID, err := e.connectRouteRLocked(nicID, addr, netProto) |
| 435 |
if err != nil { |
| 436 |
return err |
| 437 |
} |
| 438 |
|
| 439 |
id := stack.TransportEndpointID{ |
| 440 |
LocalAddress: info.ID.LocalAddress, |
| 441 |
RemoteAddress: r.RemoteAddress(), |
| 442 |
} |
| 443 |
if e.State() == transport.DatagramEndpointStateInitial { |
| 444 |
id.LocalAddress = r.LocalAddress() |
| 445 |
} |
| 446 |
|
| 447 |
if err := f(r.NetProto(), info.ID, id); err != nil { |
| 448 |
return err |
| 449 |
} |
| 450 |
|
| 451 |
if e.connectedRoute != nil { |
| 452 |
|
| 453 |
e.connectedRoute.Release() |
| 454 |
} |
| 455 |
e.connectedRoute = r |
| 456 |
info.ID = id |
| 457 |
info.RegisterNICID = nicID |
| 458 |
e.setInfo(info) |
| 459 |
e.effectiveNetProto = netProto |
| 460 |
e.setEndpointState(transport.DatagramEndpointStateConnected) |
| 461 |
return nil |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
func (e *Endpoint) Shutdown() tcpip.Error { |
| 466 |
e.mu.Lock() |
| 467 |
defer e.mu.Unlock() |
| 468 |
|
| 469 |
switch state := e.State(); state { |
| 470 |
case transport.DatagramEndpointStateInitial, transport.DatagramEndpointStateClosed: |
| 471 |
return &tcpip.ErrNotConnected{} |
| 472 |
case transport.DatagramEndpointStateBound, transport.DatagramEndpointStateConnected: |
| 473 |
e.writeShutdown = true |
| 474 |
return nil |
| 475 |
default: |
| 476 |
panic(fmt.Sprintf("unhandled state = %s", state)) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
func (e *Endpoint) checkV4Mapped(addr tcpip.FullAddress) (tcpip.FullAddress, tcpip.NetworkProtocolNumber, tcpip.Error) { |
| 483 |
info := e.Info() |
| 484 |
unwrapped, netProto, err := info.AddrNetProtoLocked(addr, e.ops.GetV6Only()) |
| 485 |
if err != nil { |
| 486 |
return tcpip.FullAddress{}, 0, err |
| 487 |
} |
| 488 |
return unwrapped, netProto, nil |
| 489 |
} |
| 490 |
|
| 491 |
func (e *Endpoint) isBroadcastOrMulticast(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, addr tcpip.Address) bool { |
| 492 |
return addr == header.IPv4Broadcast || header.IsV4MulticastAddress(addr) || header.IsV6MulticastAddress(addr) || e.stack.IsSubnetBroadcast(nicID, netProto, addr) |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
func (e *Endpoint) Bind(addr tcpip.FullAddress) tcpip.Error { |
| 497 |
return e.BindAndThen(addr, func(tcpip.NetworkProtocolNumber, tcpip.Address) tcpip.Error { |
| 498 |
return nil |
| 499 |
}) |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
func (e *Endpoint) BindAndThen(addr tcpip.FullAddress, f func(tcpip.NetworkProtocolNumber, tcpip.Address) tcpip.Error) tcpip.Error { |
| 508 |
addr.Port = 0 |
| 509 |
|
| 510 |
e.mu.Lock() |
| 511 |
defer e.mu.Unlock() |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
if e.State() != transport.DatagramEndpointStateInitial { |
| 516 |
return &tcpip.ErrInvalidEndpointState{} |
| 517 |
} |
| 518 |
|
| 519 |
addr, netProto, err := e.checkV4Mapped(addr) |
| 520 |
if err != nil { |
| 521 |
return err |
| 522 |
} |
| 523 |
|
| 524 |
nicID := addr.NIC |
| 525 |
if len(addr.Addr) != 0 && !e.isBroadcastOrMulticast(addr.NIC, netProto, addr.Addr) { |
| 526 |
nicID = e.stack.CheckLocalAddress(nicID, netProto, addr.Addr) |
| 527 |
if nicID == 0 { |
| 528 |
return &tcpip.ErrBadLocalAddress{} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
if err := f(netProto, addr.Addr); err != nil { |
| 533 |
return err |
| 534 |
} |
| 535 |
|
| 536 |
e.wasBound = true |
| 537 |
|
| 538 |
info := e.Info() |
| 539 |
info.ID = stack.TransportEndpointID{ |
| 540 |
LocalAddress: addr.Addr, |
| 541 |
} |
| 542 |
info.BindNICID = addr.NIC |
| 543 |
info.RegisterNICID = nicID |
| 544 |
info.BindAddr = addr.Addr |
| 545 |
e.setInfo(info) |
| 546 |
e.effectiveNetProto = netProto |
| 547 |
e.setEndpointState(transport.DatagramEndpointStateBound) |
| 548 |
return nil |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
func (e *Endpoint) WasBound() bool { |
| 553 |
e.mu.RLock() |
| 554 |
defer e.mu.RUnlock() |
| 555 |
return e.wasBound |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
func (e *Endpoint) GetLocalAddress() tcpip.FullAddress { |
| 560 |
e.mu.RLock() |
| 561 |
defer e.mu.RUnlock() |
| 562 |
|
| 563 |
info := e.Info() |
| 564 |
addr := info.BindAddr |
| 565 |
if e.State() == transport.DatagramEndpointStateConnected { |
| 566 |
addr = e.connectedRoute.LocalAddress() |
| 567 |
} |
| 568 |
|
| 569 |
return tcpip.FullAddress{ |
| 570 |
NIC: info.RegisterNICID, |
| 571 |
Addr: addr, |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
func (e *Endpoint) GetRemoteAddress() (tcpip.FullAddress, bool) { |
| 577 |
e.mu.RLock() |
| 578 |
defer e.mu.RUnlock() |
| 579 |
|
| 580 |
if e.State() != transport.DatagramEndpointStateConnected { |
| 581 |
return tcpip.FullAddress{}, false |
| 582 |
} |
| 583 |
|
| 584 |
return tcpip.FullAddress{ |
| 585 |
Addr: e.connectedRoute.RemoteAddress(), |
| 586 |
NIC: e.Info().RegisterNICID, |
| 587 |
}, true |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
func (e *Endpoint) SetSockOptInt(opt tcpip.SockOptInt, v int) tcpip.Error { |
| 592 |
switch opt { |
| 593 |
case tcpip.MTUDiscoverOption: |
| 594 |
|
| 595 |
|
| 596 |
if v != tcpip.PMTUDiscoveryDont { |
| 597 |
return &tcpip.ErrNotSupported{} |
| 598 |
} |
| 599 |
|
| 600 |
case tcpip.MulticastTTLOption: |
| 601 |
e.mu.Lock() |
| 602 |
e.multicastTTL = uint8(v) |
| 603 |
e.mu.Unlock() |
| 604 |
|
| 605 |
case tcpip.TTLOption: |
| 606 |
e.mu.Lock() |
| 607 |
e.ttl = uint8(v) |
| 608 |
e.mu.Unlock() |
| 609 |
|
| 610 |
case tcpip.IPv4TOSOption: |
| 611 |
e.mu.Lock() |
| 612 |
e.ipv4TOS = uint8(v) |
| 613 |
e.mu.Unlock() |
| 614 |
|
| 615 |
case tcpip.IPv6TrafficClassOption: |
| 616 |
e.mu.Lock() |
| 617 |
e.ipv6TClass = uint8(v) |
| 618 |
e.mu.Unlock() |
| 619 |
} |
| 620 |
|
| 621 |
return nil |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
func (e *Endpoint) GetSockOptInt(opt tcpip.SockOptInt) (int, tcpip.Error) { |
| 626 |
switch opt { |
| 627 |
case tcpip.MTUDiscoverOption: |
| 628 |
|
| 629 |
return tcpip.PMTUDiscoveryDont, nil |
| 630 |
|
| 631 |
case tcpip.MulticastTTLOption: |
| 632 |
e.mu.Lock() |
| 633 |
v := int(e.multicastTTL) |
| 634 |
e.mu.Unlock() |
| 635 |
return v, nil |
| 636 |
|
| 637 |
case tcpip.TTLOption: |
| 638 |
e.mu.Lock() |
| 639 |
v := int(e.ttl) |
| 640 |
e.mu.Unlock() |
| 641 |
return v, nil |
| 642 |
|
| 643 |
case tcpip.IPv4TOSOption: |
| 644 |
e.mu.RLock() |
| 645 |
v := int(e.ipv4TOS) |
| 646 |
e.mu.RUnlock() |
| 647 |
return v, nil |
| 648 |
|
| 649 |
case tcpip.IPv6TrafficClassOption: |
| 650 |
e.mu.RLock() |
| 651 |
v := int(e.ipv6TClass) |
| 652 |
e.mu.RUnlock() |
| 653 |
return v, nil |
| 654 |
|
| 655 |
default: |
| 656 |
return -1, &tcpip.ErrUnknownProtocolOption{} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
func (e *Endpoint) SetSockOpt(opt tcpip.SettableSocketOption) tcpip.Error { |
| 662 |
switch v := opt.(type) { |
| 663 |
case *tcpip.MulticastInterfaceOption: |
| 664 |
e.mu.Lock() |
| 665 |
defer e.mu.Unlock() |
| 666 |
|
| 667 |
fa := tcpip.FullAddress{Addr: v.InterfaceAddr} |
| 668 |
fa, netProto, err := e.checkV4Mapped(fa) |
| 669 |
if err != nil { |
| 670 |
return err |
| 671 |
} |
| 672 |
nic := v.NIC |
| 673 |
addr := fa.Addr |
| 674 |
|
| 675 |
if nic == 0 && addr == "" { |
| 676 |
e.multicastAddr = "" |
| 677 |
e.multicastNICID = 0 |
| 678 |
break |
| 679 |
} |
| 680 |
|
| 681 |
if nic != 0 { |
| 682 |
if !e.stack.CheckNIC(nic) { |
| 683 |
return &tcpip.ErrBadLocalAddress{} |
| 684 |
} |
| 685 |
} else { |
| 686 |
nic = e.stack.CheckLocalAddress(0, netProto, addr) |
| 687 |
if nic == 0 { |
| 688 |
return &tcpip.ErrBadLocalAddress{} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if info := e.Info(); info.BindNICID != 0 && info.BindNICID != nic { |
| 693 |
return &tcpip.ErrInvalidEndpointState{} |
| 694 |
} |
| 695 |
|
| 696 |
e.multicastNICID = nic |
| 697 |
e.multicastAddr = addr |
| 698 |
|
| 699 |
case *tcpip.AddMembershipOption: |
| 700 |
if !header.IsV4MulticastAddress(v.MulticastAddr) && !header.IsV6MulticastAddress(v.MulticastAddr) { |
| 701 |
return &tcpip.ErrInvalidOptionValue{} |
| 702 |
} |
| 703 |
|
| 704 |
nicID := v.NIC |
| 705 |
|
| 706 |
if v.InterfaceAddr.Unspecified() { |
| 707 |
if nicID == 0 { |
| 708 |
if r, err := e.stack.FindRoute(0, "", v.MulticastAddr, e.netProto, false ); err == nil { |
| 709 |
nicID = r.NICID() |
| 710 |
r.Release() |
| 711 |
} |
| 712 |
} |
| 713 |
} else { |
| 714 |
nicID = e.stack.CheckLocalAddress(nicID, e.netProto, v.InterfaceAddr) |
| 715 |
} |
| 716 |
if nicID == 0 { |
| 717 |
return &tcpip.ErrUnknownDevice{} |
| 718 |
} |
| 719 |
|
| 720 |
memToInsert := multicastMembership{nicID: nicID, multicastAddr: v.MulticastAddr} |
| 721 |
|
| 722 |
e.mu.Lock() |
| 723 |
defer e.mu.Unlock() |
| 724 |
|
| 725 |
if _, ok := e.multicastMemberships[memToInsert]; ok { |
| 726 |
return &tcpip.ErrPortInUse{} |
| 727 |
} |
| 728 |
|
| 729 |
if err := e.stack.JoinGroup(e.netProto, nicID, v.MulticastAddr); err != nil { |
| 730 |
return err |
| 731 |
} |
| 732 |
|
| 733 |
e.multicastMemberships[memToInsert] = struct{}{} |
| 734 |
|
| 735 |
case *tcpip.RemoveMembershipOption: |
| 736 |
if !header.IsV4MulticastAddress(v.MulticastAddr) && !header.IsV6MulticastAddress(v.MulticastAddr) { |
| 737 |
return &tcpip.ErrInvalidOptionValue{} |
| 738 |
} |
| 739 |
|
| 740 |
nicID := v.NIC |
| 741 |
if v.InterfaceAddr.Unspecified() { |
| 742 |
if nicID == 0 { |
| 743 |
if r, err := e.stack.FindRoute(0, "", v.MulticastAddr, e.netProto, false ); err == nil { |
| 744 |
nicID = r.NICID() |
| 745 |
r.Release() |
| 746 |
} |
| 747 |
} |
| 748 |
} else { |
| 749 |
nicID = e.stack.CheckLocalAddress(nicID, e.netProto, v.InterfaceAddr) |
| 750 |
} |
| 751 |
if nicID == 0 { |
| 752 |
return &tcpip.ErrUnknownDevice{} |
| 753 |
} |
| 754 |
|
| 755 |
memToRemove := multicastMembership{nicID: nicID, multicastAddr: v.MulticastAddr} |
| 756 |
|
| 757 |
e.mu.Lock() |
| 758 |
defer e.mu.Unlock() |
| 759 |
|
| 760 |
if _, ok := e.multicastMemberships[memToRemove]; !ok { |
| 761 |
return &tcpip.ErrBadLocalAddress{} |
| 762 |
} |
| 763 |
|
| 764 |
if err := e.stack.LeaveGroup(e.netProto, nicID, v.MulticastAddr); err != nil { |
| 765 |
return err |
| 766 |
} |
| 767 |
|
| 768 |
delete(e.multicastMemberships, memToRemove) |
| 769 |
|
| 770 |
case *tcpip.SocketDetachFilterOption: |
| 771 |
return nil |
| 772 |
} |
| 773 |
return nil |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
func (e *Endpoint) GetSockOpt(opt tcpip.GettableSocketOption) tcpip.Error { |
| 778 |
switch o := opt.(type) { |
| 779 |
case *tcpip.MulticastInterfaceOption: |
| 780 |
e.mu.Lock() |
| 781 |
*o = tcpip.MulticastInterfaceOption{ |
| 782 |
NIC: e.multicastNICID, |
| 783 |
InterfaceAddr: e.multicastAddr, |
| 784 |
} |
| 785 |
e.mu.Unlock() |
| 786 |
|
| 787 |
default: |
| 788 |
return &tcpip.ErrUnknownProtocolOption{} |
| 789 |
} |
| 790 |
return nil |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
func (e *Endpoint) Info() stack.TransportEndpointInfo { |
| 795 |
e.infoMu.RLock() |
| 796 |
defer e.infoMu.RUnlock() |
| 797 |
return e.info |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
func (e *Endpoint) setInfo(info stack.TransportEndpointInfo) { |
| 807 |
e.infoMu.Lock() |
| 808 |
defer e.infoMu.Unlock() |
| 809 |
e.info = info |
| 810 |
} |