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

[config] Add more IP address validation checks #626

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ def get_interface_naming_mode():
mode = "default"
return mode

def is_ipaddress_overlapped(ip_addr):
"""Check if the ip address overlapped with existing networks
"""
config_db = ConfigDBConnector()
config_db.connect()
interface_dict = config_db.get_table('INTERFACE')
interface_dict.update(config_db.get_table('PORTCHANNEL_INTERFACE'))
interface_dict.update(config_db.get_table('VLAN_INTERFACE'))
interface_dict.update(config_db.get_table('LOOPBACK_INTERFACE'))
ip_network = ipaddress.ip_network(unicode(ip_addr), strict=False)
for key in interface_dict.keys():
if not isinstance(key, tuple):
continue
if ipaddress.ip_network(unicode(key[1]), strict=False).overlaps(ip_network):
return True
return False

def _is_neighbor_ipaddress(ipaddress):
"""Returns True if a neighbor has the IP address <ipaddress>, False if not
"""
Expand Down Expand Up @@ -1058,7 +1075,11 @@ def add(ctx, interface_name, ip_addr):
ctx.fail("'interface_name' is None!")

try:
ipaddress.ip_network(unicode(ip_addr), strict=False)
ip_network = ipaddress.ip_network(unicode(ip_addr), strict=False)
if not interface_name.startswith("Loopback") and ip_network.prefixlen == ip_network.max_prefixlen:
ctx.fail("Bad mask /{} for IP address {}".format(ip_network.max_prefixlen, ip_addr))
if is_ipaddress_overlapped(ip_addr):
ctx.fail("IP address {} overlaps with existing subnet".format(ip_addr))
if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), {"NULL": "NULL"})
config_db.set_entry("INTERFACE", interface_name, {"NULL": "NULL"})
Expand Down Expand Up @@ -1093,7 +1114,9 @@ def remove(ctx, interface_name, ip_addr):

if_table = ""
try:
ipaddress.ip_network(unicode(ip_addr), strict=False)
ip_network = ipaddress.ip_network(unicode(ip_addr), strict=False)
if not interface_name.startswith("Loopback") and ip_network.prefixlen == ip_network.max_prefixlen:
ctx.fail("Bad mask /{} for IP address {}".format(ip_network.max_prefixlen, ip_addr))
if interface_name.startswith("Ethernet"):
config_db.set_entry("INTERFACE", (interface_name, ip_addr), None)
if_table = "INTERFACE"
Expand Down