1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
|
#!/usr/bin/python3
#
# Copyright (C) 2023 Canonical, Ltd.
# Authors: Danilo Egea Gondolfo <danilo.egea.gondolfo@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import defaultdict
import ipaddress
import json
from socket import AF_INET, AF_INET6
from typing import AbstractSet
from netplan.netdef import NetplanRoute
from netplan_cli.cli.state import SystemConfigState, NetplanConfigState, DEVICE_TYPES
from netplan_cli.cli.utils import is_valid_macaddress, route_table_lookup
class DiffJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, NetplanRoute):
return obj.to_dict()
# Shouldn't be reached as the only non-serializable type we have at the moment is NetplanRoute
return json.JSONEncoder.default(self, obj) # pragma: nocover (only NetplanRoute requires the encoder)
class NetplanDiffState():
'''
DiffState is mainly responsible for getting both system's and Netplan's configuration
state, compare them and provide a data-structure containing the differences it found.
'''
def __init__(self, system_state: SystemConfigState, netplan_state: NetplanConfigState):
self.system_state = system_state
self.netplan_state = netplan_state
self.route_lookup_table_names = {}
def get_full_state(self) -> dict:
'''
Return the states of both the system and Netplan in a common representation
that makes it easier to compare them.
'''
full_state = {
'interfaces': {}
}
system_interfaces = self._get_system_interfaces()
netplan_interfaces = self._get_netplan_interfaces()
# Merge all the interfaces in the same data structure
all_interfaces = set(list(system_interfaces.keys()) + list(netplan_interfaces.keys()))
for interface in all_interfaces:
full_state['interfaces'][interface] = {}
for interface, config in system_interfaces.items():
full_state['interfaces'][interface].update(config)
for interface, config in netplan_interfaces.items():
full_state['interfaces'][interface].update(config)
for interface in self.system_state.interface_list:
full_state['interfaces'][interface.name]['data_sources'] = interface.data_sources
return full_state
def get_diff(self, interface: str = '') -> dict:
'''
Compare the configuration of interfaces currently found in the system against Netplan configuration.
A number of heuristics are used to eliminate configuration that is automatically set in the system,
such as certain routes and IP addresses. That is necessary because this configuration will not be found
in Netplan. For example, if Netplan is enabling DHCP on an interface and not defining any extra IP addresses,
we don't count the IPs automatically assigned to the interface as a difference. We do though count the eventual
absence of addresses that should be assigned by DHCP as a difference.
'''
full_state = self.get_full_state()
interfaces = self._get_comparable_interfaces(full_state.get('interfaces', {}))
if interface:
if config := interfaces.get(interface):
interfaces = {interface: config}
else:
interfaces = {}
report = self._create_new_report()
self._analyze_missing_interfaces(report, interface)
for interface, config in interfaces.items():
netdef_id = config.get('system_state', {}).get('id')
index = config.get('system_state', {}).get('index')
iface = self._create_new_iface(netdef_id, interface, index)
self._analyze_ip_addresses(config, iface)
self._analyze_nameservers(config, iface)
self._analyze_search_domains(config, iface)
self._analyze_mac_addresses(config, iface)
self._analyze_routes(config, iface)
self._analyze_parent_links(config, iface)
report['interfaces'].update(iface)
# Sort the list of interfaces according to their indices.
report['interfaces'] = dict(sorted(report['interfaces'].items(), key=lambda iface: iface[1].get('index')))
return report
def _create_new_report(self) -> dict:
return {
'interfaces': {},
'missing_interfaces_system': {},
'missing_interfaces_netplan': {},
}
def _create_new_iface(self, netdef_id: str, interface: str, index: int) -> dict:
return {
interface: {
'index': index,
'name': interface,
'id': netdef_id,
'system_state': {},
'netplan_state': {},
}
}
def _analyze_ip_addresses(self, config: dict, iface: dict) -> None:
name = list(iface.keys())[0]
netplan_ips = {ip for ip in config.get('netplan_state', {}).get('addresses', [])}
netplan_ips = self._normalize_ip_addresses(netplan_ips)
missing_dhcp4_address = config.get('netplan_state', {}).get('dhcp4', False)
missing_dhcp6_address = config.get('netplan_state', {}).get('dhcp6', False)
link_local = config.get('netplan_state', {}).get('link_local', [])
system_ips = set()
for addr, addr_data in config.get('system_state', {}).get('addresses', {}).items():
ip = ipaddress.ip_interface(addr)
flags = addr_data.get('flags', [])
# Select only static IPs
if not {'dhcp', 'dynamic', 'link', 'ra'}.intersection(flags):
system_ips.add(addr)
# Handle the link local address
# If it's present but the respective setting is not enabled in the netdef
# it's considered a difference.
if 'link' in flags and ip.is_link_local:
if isinstance(ip.ip, ipaddress.IPv4Address) and 'ipv4' not in link_local:
system_ips.add(addr)
if isinstance(ip.ip, ipaddress.IPv6Address) and 'ipv6' not in link_local:
system_ips.add(addr)
# We assume the IPv4 was acquired via DHCP if any of the flags
# 'dhcp' or 'dynamic' is set
# For IPv6, DHCP is not the only way to acquire a dynamic address,
# so we only check for the 'dhcp' flag
if isinstance(ip.ip, ipaddress.IPv4Address):
if {'dhcp', 'dynamic'}.intersection(flags):
missing_dhcp4_address = False
if isinstance(ip.ip, ipaddress.IPv6Address):
if 'dhcp' in flags:
missing_dhcp6_address = False
# Handle RA addresses
# We only consider the presence of addresses with the 'ra' flag to be a diff
# if 'accept_ra' is set to false.
# When it's unset, networkd will enable it by default.
accept_ra = config.get('netplan_state', {}).get('accept_ra')
if 'ra' in flags and accept_ra is False:
system_ips.add(addr)
present_only_in_netplan = netplan_ips.difference(system_ips)
present_only_in_system = system_ips.difference(netplan_ips)
if missing_dhcp4_address:
iface[name]['system_state']['missing_dhcp4_address'] = True
if missing_dhcp6_address:
iface[name]['system_state']['missing_dhcp6_address'] = True
if present_only_in_system:
iface[name]['netplan_state'].update({
'missing_addresses': list(sorted(present_only_in_system)),
})
if present_only_in_netplan:
iface[name]['system_state'].update({
'missing_addresses': list(sorted(present_only_in_netplan)),
})
def _get_comparable_interfaces(self, interfaces: dict) -> dict:
''' In order to compare interfaces, they must exist in the system AND in Netplan.
Here we filter out interfaces that don't have a system_state, a netplan_state
or a netdef ID.
There is a special case where the interface will have a system_state and a netdef_id
but will be missing in Netplan. That will happen when the user removes the interface
only from Netplan but doesn't run netplan apply.
'''
filtered = {}
for interface, config in interfaces.items():
if config.get('system_state') is None or config.get('netplan_state') is None:
continue
if not config.get('system_state', {}).get('id'):
continue
filtered[interface] = config
return filtered
def _normalize_ip_addresses(self, addresses: set) -> set:
''' Apply some transformations to IP addresses so their representation
will match the system's.
'''
new_ips_set = set()
for ip in addresses:
ip = self._compress_ipv6_address(ip)
new_ips_set.add(ip)
return new_ips_set
def _compress_ipv6_address(self, address: str) -> str:
'''
Compress IPv6 addresses to match the system representation
Example: 1:2:0:0::123/64 -> 1:2::123/64
1:2:0:0::123 -> 1:2::123
If "address" is not an IPv6Address, return the original value
'''
try:
addr = ipaddress.ip_interface(address)
if '/' in address:
return addr.with_prefixlen
return str(addr.ip)
except ValueError:
return address
def _analyze_nameservers(self, config: dict, iface: dict) -> None:
name = list(iface.keys())[0]
data_sources = config.get('data_sources', {}).get('dns', {})
# TODO: improve analysis of configuration received from DHCP
# when Network Manager is used
netplan_nameservers = set(config.get('netplan_state', {}).get('nameservers_addresses', []))
system_nameservers = set(config.get('system_state', {}).get('nameservers_addresses', []))
dhcp4_ns = {ns for ns in data_sources if data_sources[ns] == 'DHCPv4'}
dhcp6_ns = {ns for ns in data_sources if data_sources[ns] == 'DHCPv6'}
ndisc_ns = {ns for ns in data_sources if data_sources[ns] == 'NDisc'}
# Filter out dynamically assigned DNS data
accept_ra_enabled = config.get('netplan_state', {}).get('accept_ra') is not False
link_local_enabled = 'ipv6' in config.get('netplan_state', {}).get('link_local', [])
if dhcp4_ns or dhcp6_ns or ndisc_ns:
if config.get('netplan_state', {}).get('dhcp4'):
system_nameservers = system_nameservers - dhcp4_ns
# networkd will enable DHCPv6 if RA is enabled even if DHCPv6 is set to false
# see systemd.network(5)
if accept_ra_enabled:
system_nameservers = system_nameservers - dhcp6_ns
link_local_ns = {ns for ns in system_nameservers if ns in ndisc_ns}
if link_local_enabled and accept_ra_enabled:
system_nameservers = system_nameservers - link_local_ns
else:
# Heuristic used when we don't have data sources for DNS information (when
# Network Manager is used for example)
# If the netplan configuration has DHCP enabled and an empty list of nameservers
# we assume it's dynamic. So we don't take these addresses into account when comparing
# them, except for the ones we already identified as RA/LL.
# Note: Some useful information can be found in /var/run/systemd/netif/leases/
# but the lease files have a comment saying they shouldn't be parsed.
# There is a feature request to expose more DHCP information via the DBus API
# https://github.com/systemd/systemd/issues/27699
if not netplan_nameservers:
if config.get('netplan_state', {}).get('dhcp4'):
system_nameservers = {ns for ns in system_nameservers
if not isinstance(ipaddress.ip_address(ns), ipaddress.IPv4Address)}
if config.get('netplan_state', {}).get('dhcp6'):
system_nameservers = {ns for ns in system_nameservers
if not isinstance(ipaddress.ip_address(ns), ipaddress.IPv6Address)}
present_only_in_netplan = netplan_nameservers.difference(system_nameservers)
present_only_in_system = system_nameservers.difference(netplan_nameservers)
if present_only_in_system:
iface[name]['netplan_state'].update({
'missing_nameservers_addresses': list(present_only_in_system),
})
if present_only_in_netplan:
iface[name]['system_state'].update({
'missing_nameservers_addresses': list(present_only_in_netplan),
})
def _analyze_search_domains(self, config: dict, iface: dict) -> None:
name = list(iface.keys())[0]
netplan_search_domains = set(config.get('netplan_state', {}).get('nameservers_search', []))
system_search_domains = set(config.get('system_state', {}).get('nameservers_search', []))
data_sources = config.get('data_sources', {}).get('search', {})
dhcp4_search = {s for s in data_sources if data_sources[s] == 'DHCPv4'}
dhcp6_search = {s for s in data_sources if data_sources[s] == 'DHCPv6'}
if dhcp4_search or dhcp6_search:
if config.get('netplan_state', {}).get('dhcp4'):
system_search_domains = system_search_domains - dhcp4_search
# networkd will enable DHCPv6 if RA is enabled even if DHCPv6 is set to false
# see systemd.network(5)
accept_ra_enabled = config.get('netplan_state', {}).get('accept_ra') is not False
if accept_ra_enabled:
system_search_domains = system_search_domains - dhcp6_search
else:
# Heuristic used when we don't have data sources for DNS information (when
# Network Manager is used for example)
# If the netplan configuration has DHCP enabled and an empty list of search domains
# we assume it's dynamic
if not netplan_search_domains:
if config.get('netplan_state', {}).get('dhcp4') or config.get('netplan_state', {}).get('dhcp6'):
system_search_domains = set()
present_only_in_netplan = netplan_search_domains.difference(system_search_domains)
present_only_in_system = system_search_domains.difference(netplan_search_domains)
if present_only_in_system:
iface[name]['netplan_state'].update({
'missing_nameservers_search': list(present_only_in_system),
})
if present_only_in_netplan:
iface[name]['system_state'].update({
'missing_nameservers_search': list(present_only_in_netplan),
})
def _analyze_mac_addresses(self, config: dict, iface: dict) -> None:
name = list(iface.keys())[0]
system_macaddress = config.get('system_state', {}).get('macaddress')
netplan_macaddress = config.get('netplan_state', {}).get('macaddress')
# if the macaddress in netplan is an special option (such as 'random')
# don't try to diff it against the system MAC address
if netplan_macaddress and not is_valid_macaddress(netplan_macaddress):
return
if system_macaddress and netplan_macaddress:
if system_macaddress != netplan_macaddress:
iface[name]['system_state'].update({
'missing_macaddress': netplan_macaddress
})
iface[name]['netplan_state'].update({
'missing_macaddress': system_macaddress
})
def _analyze_routes(self, config: dict, iface: dict) -> None:
name = list(iface.keys())[0]
netplan_routes = set(config.get('netplan_state', {}).get('routes', []))
system_routes = set(config.get('system_state', {}).get('routes', []))
netplan_routes = self._normalize_routes(netplan_routes)
# If gateway4 and/or gateway6 is present, merge them in the routes set
if netplan_gateways := self._normalize_gateways(config.get('netplan_state', {})):
netplan_routes.update(netplan_gateways)
# Filter out some routes that are expected to be added automatically
system_addresses = [ip for ip in config.get('system_state', {}).get('addresses', {})]
system_routes = self._filter_system_routes(system_routes, system_addresses, config)
present_only_in_netplan = netplan_routes.difference(system_routes)
present_only_in_system = system_routes.difference(netplan_routes)
if present_only_in_system:
iface[name]['netplan_state'].update({
'missing_routes': [route for route in sorted(present_only_in_system, key=lambda r: r.to)],
})
if present_only_in_netplan:
iface[name]['system_state'].update({
'missing_routes': [route for route in sorted(present_only_in_netplan, key=lambda r: r.to)],
})
def _analyze_missing_interfaces(self, report: dict, interface: str) -> None:
netplan_interfaces = {iface for iface in self.netplan_state.netdefs}
system_interfaces_netdef_ids = {iface.netdef_id for iface in self.system_state.interface_list if iface.netdef_id}
netplan_only = netplan_interfaces.difference(system_interfaces_netdef_ids)
# Filtering out disconnected wifi netdefs
# If a wifi netdef is present in the netplan_only list it's because it's disconnected
netplan_only = list(filter(lambda i: self.netplan_state.netdefs.get(i).type != 'wifis', netplan_only))
system_only = []
for iface in self.system_state.interface_list:
if iface.netdef_id not in netplan_interfaces:
system_only.append(iface.name)
netplan_only = sorted(netplan_only)
system_only = sorted(system_only)
if interface:
netplan_only = filter(lambda i: i == interface, netplan_only)
system_only = filter(lambda i: i == interface, system_only)
system_state = self.system_state.get_data()
for iface in netplan_only:
iface_type = self.netplan_state.netdefs.get(iface).type
report['missing_interfaces_system'][iface] = {
'type': DEVICE_TYPES.get(iface_type, 'other')
}
for iface in system_only:
report['missing_interfaces_netplan'][iface] = {
'type': system_state.get(iface).get('type', 'other'),
'index': system_state.get(iface).get('index'),
}
def _analyze_parent_links(self, config: dict, iface: dict) -> None:
'''
Analyse if interfaces such as bonds, bridges and VRFs are correctly attached to their
members and vice versa.
'''
name = list(iface.keys())[0]
bond = [config.get('system_state', {}).get('bond'), config.get('netplan_state', {}).get('bond')]
bridge = [config.get('system_state', {}).get('bridge'), config.get('netplan_state', {}).get('bridge')]
vrf = [config.get('system_state', {}).get('vrf'), config.get('netplan_state', {}).get('vrf')]
interfaces = [config.get('system_state', {}).get('interfaces', []), config.get('netplan_state', {}).get('interfaces', [])]
if bond != [None, None] and bond[0] != bond[1]:
if bond[0]:
iface[name]['netplan_state']['missing_bond_link'] = bond[0]
if bond[1]:
iface[name]['system_state']['missing_bond_link'] = bond[1]
if bridge != [None, None] and bridge[0] != bridge[1]:
if bridge[0]:
iface[name]['netplan_state']['missing_bridge_link'] = bridge[0]
if bridge[1]:
iface[name]['system_state']['missing_bridge_link'] = bridge[1]
if vrf != [None, None] and vrf[0] != vrf[1]:
if vrf[0]:
iface[name]['netplan_state']['missing_vrf_link'] = vrf[0]
if vrf[1]:
iface[name]['system_state']['missing_vrf_link'] = vrf[1]
if interfaces != [[], []]:
system = set(interfaces[0])
netplan = set(interfaces[1])
if system != netplan:
if missing_system := netplan - system:
iface[name]['system_state']['missing_interfaces'] = list(missing_system)
if missing_netplan := system - netplan:
iface[name]['netplan_state']['missing_interfaces'] = list(missing_netplan)
def _normalize_routes(self, routes: set) -> set:
''' Apply some transformations to Netplan routes so their representation
will match the system's.
'''
new_routes_set = set()
for route in routes:
# If the table is unspecified we set it to main
if route.table == NetplanRoute._TABLE_UNSPEC_:
route.table = self._default_route_tables_name_to_number('main')
# If the addresses are IPv6, compress them so it will match the system representation
route.to = self._compress_ipv6_address(route.to)
route.from_addr = self._compress_ipv6_address(route.from_addr)
route.via = self._compress_ipv6_address(route.via)
# If the route.to prefix is either /32 and /128 we remove it to match
# the system representation:
if route.to != 'default':
ip_prefix = route.to.split('/')
if ip_prefix[1] == '32' or ip_prefix[1] == '128':
route.to = ip_prefix[0]
new_routes_set.add(route)
return new_routes_set
def _normalize_gateways(self, config: dict) -> AbstractSet[NetplanRoute]:
''' Convert the gateway4 and gateway6 properties to NetplanRoutes.
The only information stored by these properties is the destination IP for
the default route. Networkd and Network Manager will set a default metric
for it when the route is installed. As we have no control over it, there
will always be a diff. The same is true for the route table number.
To avoid the noise in the diff report when these properties are used,
we use the following heuristic: if there is one and only one static default
route in the system and gateway4 and/or gateway6 are used, we set the same
metric and table as found in the system.
'''
routes_set = set()
system_interfaces = self.system_state.get_data()
interface = config.get('id')
ifaces = [iface for iface in system_interfaces.values()
if iface.get('id') == interface]
routes = [route for iface in ifaces for route in iface.get('routes', [])]
if gateway4 := config.get('gateway4'):
default_routes = [route for route in routes
if route.get('to') == 'default'
and route.get('family') == AF_INET.value
and route.get('protocol') == 'static']
route = NetplanRoute(to='default', via=gateway4, family=AF_INET.value, protocol='static')
if len(default_routes) == 1:
if metric := default_routes[0].get('metric'):
route.metric = metric
if table := default_routes[0].get('table'):
route.table = self._default_route_tables_name_to_number(table)
routes_set.add(route)
if gateway6 := config.get('gateway6'):
default_routes = [route for route in routes
if route.get('to') == 'default'
and route.get('family') == AF_INET6.value
and route.get('protocol') == 'static']
# Compress the address so it will match the system representation
gateway6 = self._compress_ipv6_address(gateway6)
route = NetplanRoute(to='default', via=gateway6, family=AF_INET6.value, protocol='static')
if len(default_routes) == 1:
if metric := default_routes[0].get('metric'):
route.metric = metric
if table := default_routes[0].get('table'):
route.table = self._default_route_tables_name_to_number(table)
routes_set.add(route)
return routes_set
def _filter_system_routes(self, system_routes: AbstractSet[NetplanRoute], system_addresses: list[str], config: dict) -> set:
'''
Some routes found in the system are installed automatically/dynamically without
being configured in Netplan.
Here we implement some heuristics to remove these routes from the list we want
to compare. We do that because these type of routes will probably never be found in the
Netplan configuration so there is no point in comparing them against Netplan.
'''
local_networks = [str(ipaddress.ip_interface(ip).network) for ip in system_addresses]
# filter out the local link network as we give special treatment to it
local_networks = list(filter(lambda n: n != 'fe80::/64', local_networks))
addresses = [str(ipaddress.ip_interface(ip).ip) for ip in system_addresses]
link_local = config.get('netplan_state', {}).get('link_local', [])
routes = set()
for route in system_routes:
# Filter out link routes (but not link local as we handle them differently)
if route.scope == 'link' and route.to != 'default' and not ipaddress.ip_interface(route.to).is_link_local:
continue
# Filter out routes installed by DHCP
if route.protocol == 'dhcp':
continue
# Filter out routes installed by RA if accept_ra is not false
accept_ra = config.get('netplan_state', {}).get('accept_ra')
if route.protocol == 'ra' and accept_ra is not False:
continue
# Filter out Link Local routes
# We only filter them out if the respective 'link-local' setting is present in the netdef
if route.to != 'default':
route_to = ipaddress.ip_interface(route.to)
if route_to.is_link_local:
if route.family == AF_INET6.value and 'ipv6' in link_local:
continue
if route.family == AF_INET.value and 'ipv4' in link_local:
continue
# Filter out host scoped routes
if (route.scope == 'host' and route.type == 'local' and
(route.to in addresses or ipaddress.ip_interface(route.to).is_loopback)):
continue
# Filter out the default IPv6 multicast route
if route.family == AF_INET6.value and route.type == 'multicast' and route.to == 'ff00::/8':
continue
# Filter IPv6 local routes
if (route.family == AF_INET6.value and route.protocol != 'ra'
and (route.to in local_networks or route.to in addresses)):
continue
routes.add(route)
return routes
def _get_netplan_interfaces(self) -> dict:
system_interfaces = self.system_state.get_data()
interfaces = {}
for interface, config in self.netplan_state.netdefs.items():
iface = {}
iface[interface] = {'netplan_state': {'id': interface}}
iface_ref = iface[interface]['netplan_state']
iface_ref['type'] = DEVICE_TYPES.get(config.type, 'other')
iface_ref['dhcp4'] = config.dhcp4
iface_ref['dhcp6'] = config.dhcp6
iface_ref['link_local'] = config.link_local
if config.accept_ra is not None:
iface_ref['accept_ra'] = config.accept_ra
addresses = [addr for addr in config.addresses]
if addresses:
iface_ref['addresses'] = {}
for addr in addresses:
flags = {}
if addr.label:
flags['label'] = addr.label
if addr.lifetime:
flags['lifetime'] = addr.lifetime
iface_ref['addresses'][str(addr)] = {'flags': flags}
if nameservers := list(config.nameserver_addresses):
iface_ref['nameservers_addresses'] = nameservers
if search := list(config.nameserver_search):
iface_ref['nameservers_search'] = search
if routes := list(config.routes):
iface_ref['routes'] = routes
if gateway4 := config._gateway4:
iface_ref['gateway4'] = gateway4
if gateway6 := config._gateway6:
iface_ref['gateway6'] = gateway6
if mac := config.macaddress:
iface_ref['macaddress'] = mac
if bridge := config.links.get('bridge'):
iface_ref['bridge'] = bridge.id
if bond := config.links.get('bond'):
iface_ref['bond'] = bond.id
if vrf := config.links.get('vrf'):
iface_ref['vrf'] = vrf.id
if interface not in system_interfaces:
# If the netdef ID doesn't correspond to any interface name in the system,
# it might be associated with multiple system interfaces, such as when the 'match' key is used,
# or the interface name is set in the passthrough section, such as when we create a connection via
# Network Manager and the netdef ID is the UUID of the connetion.
# In these cases, we need to look for all the system's interfaces
# pointing to this netdef and add one netdef entry per device.
found_some = False
for key, value in system_interfaces.items():
if netdef_id := value.get('id'):
if netdef_id == interface:
found_some = True
interfaces[key] = iface[interface]
# If we don't find any system interface associated with the netdef
# that's because it's not matching any device. In this case, we add the
# netdef ID to the list anyway.
if not found_some:
interfaces.update(iface)
else:
interfaces.update(iface)
self._netplan_state_find_parents(interfaces)
return interfaces
def _netplan_state_find_parents(self, interfaces: dict) -> None:
''' Associates interfaces with their parents '''
parents = defaultdict(set)
for interface, config in interfaces.items():
if link := config['netplan_state'].get('bridge'):
parents[link].add(interface)
if link := config['netplan_state'].get('bond'):
parents[link].add(interface)
if link := config['netplan_state'].get('vrf'):
parents[link].add(interface)
for interface, members in parents.items():
interfaces[interface]['netplan_state']['interfaces'] = list(members)
def _get_system_interfaces(self) -> dict:
interfaces = {}
for interface, config in self.system_state.get_data().items():
if interface == 'netplan-global-state':
continue
device_type = config.get('type')
interfaces[interface] = {'system_state': {'type': device_type}}
if netdef_id := config.get('id'):
interfaces[interface]['system_state']['id'] = netdef_id
iface_ref = interfaces[interface]['system_state']
if index := config.get('index'):
iface_ref['index'] = index
addresses = {}
for addr in config.get('addresses', []):
ip = list(addr.keys())[0]
prefix = addr.get(ip).get('prefix')
full_addr = f'{ip}/{prefix}'
addresses[full_addr] = {'flags': addr.get(ip).get('flags', [])}
if addresses:
iface_ref['addresses'] = addresses
if nameservers := config.get('dns_addresses'):
iface_ref['nameservers_addresses'] = nameservers
if search := config.get('dns_search'):
iface_ref['nameservers_search'] = search
if routes := config.get('routes'):
iface_ref['routes'] = [self._system_route_to_netplan(route) for route in routes]
if mac := config.get('macaddress'):
iface_ref['macaddress'] = mac
if uplink_interfaces := config.get('interfaces'):
iface_ref['interfaces'] = uplink_interfaces
if bond := config.get('bond'):
iface_ref['bond'] = bond
if bridge := config.get('bridge'):
iface_ref['bridge'] = bridge
if vrf := config.get('vrf'):
iface_ref['vrf'] = vrf
return interfaces
def _system_route_to_netplan(self, system_route: dict) -> NetplanRoute:
route = {}
if family := system_route.get('family'):
route['family'] = family
if to := system_route.get('to'):
route['to'] = to
if via := system_route.get('via'):
route['via'] = via
if from_addr := system_route.get('from'):
route['from_addr'] = from_addr
if metric := system_route.get('metric'):
route['metric'] = metric
if scope := system_route.get('scope'):
route['scope'] = scope
if route_type := system_route.get('type'):
route['type'] = route_type
if protocol := system_route.get('protocol'):
route['protocol'] = protocol
if table := system_route.get('table'):
route['table'] = self._default_route_tables_name_to_number(table)
return NetplanRoute(**route)
def _default_route_tables_name_to_number(self, name: str) -> int:
if name.isdigit():
return int(name)
if not self.route_lookup_table_names:
self.route_lookup_table_names = route_table_lookup()
return self.route_lookup_table_names.get(name, 0)
|