Commit da36c35a authored by Simon Redman's avatar Simon Redman

Add helper for constructing site-local ipv6 addresses from existing information

parent ec6160eb
......@@ -107,6 +107,31 @@ def parse_local_interface_addresses(netgraph):
return interface_map
def convert_link_local_addr_to_ULA(link_local_interface, ipv4_interface):
"""
Combine a link's ipv4 address and ipv6 interface ID to form an ipv6 Unique Local Address
:param link_local_interface: link-local ipv6 address from which to read the interface ID
:param ipv4_interface: ipv4 address from which to read the lan's netmask
:return: IPv6Interface object
"""
assert ipv4_interface.network.prefixlen <= 24, "This method assumes a /24 prefix or larger"
old_network = str(link_local_interface.network)
assert old_network.startswith('fe80'), "Passed network is not link-local!"
new_network = 'fd00' # Start with the link-local prefix
# Use the link's IPv4 network address as the network ID of the newly-created network
network_id = str(ipv4_interface.network.network_address).replace('.', ':')
# Cram these two parts together and call it a prefix. Note that this only works assuming a /24 or larger prefix, otherwise the bottom bits might conflict with the top of the interface ID
new_network = new_network + ':' + network_id + '::' + '/' + str(link_local_interface.network.prefixlen)
new_network = ipaddress.IPv6Network(new_network)
new_network = int(new_network.network_address.exploded.replace(':',''), 16) & int(new_network.netmask.exploded.replace(':',''), 16)
old_host_id = int(link_local_interface.ip.exploded.replace(':',''), 16) & int(link_local_interface.hostmask.exploded.replace(':',''), 16)
site_local_addr = ipaddress.IPv6Address(new_network + old_host_id)
# Add back the prefix length
site_local_intf = ipaddress.IPv6Interface(site_local_addr.exploded + '/' + str(link_local_interface.network.prefixlen))
return site_local_intf
if __name__ == "__main__":
parser = argparse.ArgumentParser("Generate IPv6 Unique Local Addresses for all nodes in the network")
parser.add_argument("--in-file", action='store', type=str, required=True,
......@@ -128,12 +153,12 @@ if __name__ == "__main__":
netgraph.graph._node[hostnames[host_idx]]['session'] = sessions[host_idx]
link_local_map = parse_local_interface_addresses(netgraph)
ipv4_map = parse_ipv4_networks(netgraph)
site_local_map = {}
ULA_map = {}
for host in link_local_map:
site_local_map[host] = {}
ULA_map[host] = {}
for interface in link_local_map[host]:
link_local_addr = link_local_map[host][interface]
ipv4_addr = ipv4_map[host][interface]
site_local_map[host][interface] = convert_link_local_addr_to_site_local(link_local_addr, ipv4_addr)
ULA_map[host][interface] = convert_link_local_addr_to_ULA(link_local_addr, ipv4_addr)
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment