Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nlpacket: use IFA_LOCAL if exist #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 27 additions & 19 deletions ifupdown2/lib/nlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,8 +1565,13 @@ def __check_and_replace_address(address_list, new_addr):
:return:
"""
ip_with_prefix = new_addr.get_attribute_value(Address.IFA_ADDRESS)
iplocal_with_prefix = new_addr.get_attribute_value(Address.IFA_LOCAL)

for index, addr in enumerate(address_list):
if addr.get_attribute_value(Address.IFA_LOCAL) == iplocal_with_prefix:
address_list[index] = new_addr
return True

if addr.get_attribute_value(Address.IFA_ADDRESS) == ip_with_prefix:
address_list[index] = new_addr
return True
Expand Down Expand Up @@ -1631,31 +1636,29 @@ def force_remove_addr(self, ifname, addr):
obj_to_remove = None

for cache_addr in self._addr_cache[ifname][addr.version]:
try:
if cache_addr.attributes[Address.IFA_LOCAL].value is not None:
if cache_addr.attributes[Address.IFA_LOCAL].value == addr:
obj_to_remove = cache_addr
elif cache_addr.attributes[Address.IFA_ADDRESS].value is not None:
if cache_addr.attributes[Address.IFA_ADDRESS].value == addr:
obj_to_remove = cache_addr
except:
try:
if cache_addr.attributes[Address.IFA_LOCAL].value == addr:
obj_to_remove = cache_addr
except:
return
else:
return
if obj_to_remove:
self._addr_cache[ifname][addr.version].remove(obj_to_remove)
except:
pass

def remove_address(self, addr_to_remove):
ifname, _ = self._address_get_ifname_and_ifindex(addr_to_remove)

with self._cache_lock:
# iterate through the interface addresses
# to find which one to remove from the cache
try:
ip_version = addr_to_remove.get_attribute_value(Address.IFA_ADDRESS).version
ip_version = addr_to_remove.get_attribute_value(Address.IFA_LOCAL).version
except:
try:
ip_version = addr_to_remove.get_attribute_value(Address.IFA_LOCAL).version
ip_version = addr_to_remove.get_attribute_value(Address.IFA_ADDRESS).version
except:
# print debug error
return
Expand Down Expand Up @@ -1694,10 +1697,16 @@ def get_ip_addresses(self, ifname: str) -> list:
intf_addresses = self._addr_cache[ifname]

for addr in intf_addresses.get(4, []):
addresses.append(addr.attributes[Address.IFA_ADDRESS].value)
if addr.attributes[Address.IFA_LOCAL].value is not None:
addresses.append(addr.attributes[Address.IFA_LOCAL].value)
elif addr.attributes[Address.IFA_ADDRESS].value is not None:
addresses.append(addr.attributes[Address.IFA_ADDRESS].value)

for addr in intf_addresses.get(6, []):
addresses.append(addr.attributes[Address.IFA_ADDRESS].value)
if addr.attributes[Address.IFA_LOCAL].value is not None:
addresses.append(addr.attributes[Address.IFA_LOCAL].value)
elif addr.attributes[Address.IFA_ADDRESS].value is not None:
addresses.append(addr.attributes[Address.IFA_ADDRESS].value)

return addresses
except (KeyError, AttributeError):
Expand Down Expand Up @@ -1872,15 +1881,14 @@ def addr_is_cached(self, ifname, addr):
with self._cache_lock:
for cache_addr in self._addr_cache[ifname][addr.version]:
try:
ifa_local = cache_addr.attributes[Address.IFA_LOCAL].value
if ifa_local is not None and ifa_local.ip == addr.ip and ifa_local.prefixlen == addr.prefixlen:
return True
ifa_address = cache_addr.attributes[Address.IFA_ADDRESS].value
if ifa_address.ip == addr.ip and ifa_address.prefixlen == addr.prefixlen:
if ifa_address is not None and ifa_address.ip == addr.ip and ifa_address.prefixlen == addr.prefixlen:
return True
except:
try:
ifa_local = cache_addr.attributes[Address.IFA_LOCAL].value
return ifa_local.ip == addr.ip and ifa_local.prefixlen == addr.prefixlen
except:
pass
pass
except (KeyError, AttributeError):
pass
return False
Expand Down Expand Up @@ -3056,7 +3064,6 @@ def addr_add(self, ifname, addr, broadcast=None, peer=None, scope=None, preferre
packet.flags = NLM_F_CREATE | NLM_F_REQUEST | NLM_F_ACK
packet.family = self.IPNetwork_version_to_family.get(addr.version)

packet.add_attribute(Address.IFA_ADDRESS, addr)
packet.add_attribute(Address.IFA_LOCAL, addr)

if broadcast:
Expand Down Expand Up @@ -3086,6 +3093,7 @@ def addr_add(self, ifname, addr, broadcast=None, peer=None, scope=None, preferre
packet.add_attribute(Address.IFA_ADDRESS, peer)
packet_prefixlen = peer.prefixlen
else:
packet.add_attribute(Address.IFA_ADDRESS, addr)
packet_prefixlen = addr.prefixlen

self.logger.info(" ".join(log_msg))
Expand Down