Expose address deprecation on Stack

Add a method for setting an address to be deprecated/preferred to
AddressableEndpoint, so that addresses added from outside the stack
can be deprecated/renewed respectively, e.g. by a DHCPv6 client.

PiperOrigin-RevId: 426435598
This commit is contained in:
Tony Gong
2022-02-04 10:29:52 -08:00
committed by gVisor bot
parent a87bb4aae2
commit 518535de9b
7 changed files with 93 additions and 1 deletions
+7
View File
@@ -1053,6 +1053,13 @@ func (e *endpoint) RemovePermanentAddress(addr tcpip.Address) tcpip.Error {
return e.addressableEndpointState.RemovePermanentAddress(addr)
}
// SetDeprecated implements stack.AddressableEndpoint.
func (e *endpoint) SetDeprecated(addr tcpip.Address, deprecated bool) tcpip.Error {
e.mu.RLock()
defer e.mu.RUnlock()
return e.addressableEndpointState.SetDeprecated(addr, deprecated)
}
// MainAddress implements stack.AddressableEndpoint.
func (e *endpoint) MainAddress() tcpip.AddressWithPrefix {
e.mu.RLock()
+7
View File
@@ -1681,6 +1681,13 @@ func (e *endpoint) getAddressRLocked(localAddr tcpip.Address) stack.AddressEndpo
return e.mu.addressableEndpointState.GetAddress(localAddr)
}
// SetDeprecated implements stack.AddressableEndpoint.
func (e *endpoint) SetDeprecated(addr tcpip.Address, deprecated bool) tcpip.Error {
e.mu.RLock()
defer e.mu.RUnlock()
return e.mu.addressableEndpointState.SetDeprecated(addr, deprecated)
}
// MainAddress implements stack.AddressableEndpoint.
func (e *endpoint) MainAddress() tcpip.AddressWithPrefix {
e.mu.RLock()
@@ -374,6 +374,19 @@ func (a *AddressableEndpointState) decAddressRefLocked(addrState *addressState)
a.releaseAddressStateLocked(addrState)
}
// SetDeprecated implements stack.AddressableEndpoint.
func (a *AddressableEndpointState) SetDeprecated(addr tcpip.Address, deprecated bool) tcpip.Error {
a.mu.Lock()
defer a.mu.Unlock()
addrState, ok := a.mu.endpoints[addr]
if !ok {
return &tcpip.ErrBadLocalAddress{}
}
addrState.SetDeprecated(deprecated)
return nil
}
// MainAddress implements AddressableEndpoint.
func (a *AddressableEndpointState) MainAddress() tcpip.AddressWithPrefix {
a.mu.RLock()
+18
View File
@@ -591,6 +591,24 @@ func (n *nic) removeAddress(addr tcpip.Address) tcpip.Error {
return &tcpip.ErrBadLocalAddress{}
}
func (n *nic) setAddressDeprecated(addr tcpip.Address, deprecated bool) tcpip.Error {
for _, ep := range n.networkEndpoints {
ep, ok := ep.(AddressableEndpoint)
if !ok {
continue
}
switch err := ep.SetDeprecated(addr, deprecated); err.(type) {
case *tcpip.ErrBadLocalAddress:
continue
default:
return err
}
}
return &tcpip.ErrBadLocalAddress{}
}
func (n *nic) getLinkAddress(addr, localAddr tcpip.Address, protocol tcpip.NetworkProtocolNumber, onResolve func(LinkResolutionResult)) tcpip.Error {
linkRes, ok := n.linkAddrResolvers[protocol]
if !ok {
+6
View File
@@ -491,6 +491,12 @@ type AddressableEndpoint interface {
// permanent address.
RemovePermanentAddress(addr tcpip.Address) tcpip.Error
// SetDeprecated sets whether the address should be deprecated or not.
//
// Returns *tcpip.ErrBadLocalAddress if the endpoint does not have the passed
// address.
SetDeprecated(addr tcpip.Address, deprecated bool) tcpip.Error
// MainAddress returns the endpoint's primary permanent address.
MainAddress() tcpip.AddressWithPrefix
+12
View File
@@ -943,6 +943,18 @@ func (s *Stack) RemoveAddress(id tcpip.NICID, addr tcpip.Address) tcpip.Error {
return &tcpip.ErrUnknownNICID{}
}
// SetAddressDeprecated sets an address to be deprecated or preferred.
func (s *Stack) SetAddressDeprecated(id tcpip.NICID, addr tcpip.Address, deprecated bool) tcpip.Error {
s.mu.RLock()
defer s.mu.RUnlock()
if nic, ok := s.nics[id]; ok {
return nic.setAddressDeprecated(addr, deprecated)
}
return &tcpip.ErrUnknownNICID{}
}
// AllAddresses returns a map of NICIDs to their protocol addresses (primary
// and non-primary).
func (s *Stack) AllAddresses() map[tcpip.NICID][]tcpip.ProtocolAddress {
+30 -1
View File
@@ -2980,7 +2980,7 @@ func TestIPv6SourceAddressSelectionScopeAndSameAddress(t *testing.T) {
properties stack.AddressProperties
}
// Rule 3 is not tested here, and is instead tested by NDP's AutoGenAddr test.
// Rule 3 is also tested by NDP's AutoGenAddr test.
tests := []struct {
name string
slaacPrefixForTempAddrBeforeNICAddrAdd tcpip.AddressWithPrefix
@@ -3101,6 +3101,35 @@ func TestIPv6SourceAddressSelectionScopeAndSameAddress(t *testing.T) {
expectedLocalAddr: linkLocalAddr1,
},
// Test Rule 3 of RFC 6724 section 5 (avoid deprecated addresses).
{
name: "Deprecated least preferred (last address)",
nicAddrs: []addressWithProperties{
{addr: globalAddr1},
{
addr: globalAddr2,
properties: stack.AddressProperties{
Deprecated: true,
},
},
},
remoteAddr: globalAddr3,
expectedLocalAddr: globalAddr1,
},
{
name: "Deprecated least preferred (first address)",
nicAddrs: []addressWithProperties{
{
addr: globalAddr2,
properties: stack.AddressProperties{
Deprecated: true,
},
},
{addr: globalAddr1},
},
remoteAddr: globalAddr3,
expectedLocalAddr: globalAddr1,
},
// Test Rule 6 of 6724 section 5 (prefer matching label).
{
name: "Unique Local most preferred (last address)",