Changeset 14025 in genesis for tools/syntax-checker.py


Ignore:
Timestamp:
Jan 22, 2018, 12:49:48 PM (7 years ago)
Author:
rick
Message:

Fix overlapping IP networks

The syntax checker did not check for overlapping network ranges. For example
/24 defined at Dwars overlapped with /23 of Meerburgkerk. This is causing
issues with routing and network propogation.

While here ensure all ns_ip has proper subnet set

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/syntax-checker.py

    r13987 r14025  
    66import gformat
    77import sys
     8import ipaddress
    89
    910from collections import defaultdict
     
    1112__version__ = '$Id$'
    1213
    13 allowed_multi_use = ['0.0.0.0', '192.168.1.', '192.168.178.']
     14allowed_multi_use = map(lambda x: ipaddress.ip_network(x, strict=True), [
     15    u'192.168.0.0/22',
     16    u'192.168.0.0/16',
     17    u'192.168.0.0/24',
     18    u'192.168.1.0/24',
     19    u'192.168.178.0/24',
     20    ])
     21
     22
     23
     24
    1425
    1526def check_double_ip():
     
    1930      print "## Processing host %-25s: " % host,
    2031      datadump = gformat.get_yaml(host,add_version_info=False)
     32      masterip_addr = ipaddress.IPv4Interface(unicode(datadump['masterip']))
     33      masterip_is_used = False
     34
    2135      # Check syntax of defined variables
    2236      _ = gformat.generate_wleiden_yaml(datadump)
     
    2539        iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
    2640        for iface_key in iface_keys:
    27           # Virtual interfaces bridge interfaces do not have IP addreses
    28           if not datadump[iface_key].has_key('ip'):
     41          # Extra (descriptive entries) are ignored
     42          if '_extra' in iface_key:
    2943            continue
    3044
    31           l = datadump[iface_key]['ip']
    32           addr, mask = l.split('/')
     45          # Process actual and virtual IP (avoiding clashes with nanostation IP)
     46          for entry in ['ip', 'ns_ip']:
     47            if entry in datadump[iface_key]:
     48              addr = ipaddress.IPv4Interface(unicode(datadump[iface_key][entry]))
     49              if masterip_addr in addr.network:
     50                masterip_is_used = True
     51              pool[addr.network].append((host, iface_key, entry, addr))
    3352
    34           pool[addr].append((host, iface_key))
    3553
    36         iface_key = 'masterip'
    37         addr = datadump['masterip']
    3854        # Add masterip to the list if IP has not been defined at interface
    39         if not host in [x[0] for x in pool[addr]]:
    40           pool[addr].append((host, 'masterip'))
     55        if not masterip_is_used:
     56            pool[masterip_addr.network].append((host, 'masterip', '', masterip_addr))
    4157
    4258        print "OK"
     
    4864    sys.exit(1)
    4965
    50   error = False
    51   for addr,leden in pool.iteritems():
    52     if len(leden) > 1:
    53       if not any(map(lambda x: addr.startswith(x), allowed_multi_use)):
    54         print "[ERROR] Multiple usages of IP %s:" % (addr)
    55         for host, key in leden:
    56           print "  - %s - %s" % (host, key)
    57         error = True
     66  errors = 0
     67  keys = sorted(pool.keys(),reverse=True)
    5868
    59   if error:
    60     print "# Errors found"
     69  for i,network in enumerate(keys):
     70    if not network in allowed_multi_use:
     71      for network2 in keys[i+1:]:
     72        if not network2 in allowed_multi_use and network2.overlaps(network):
     73          errors += 1
     74          print "[ERROR#%i] network %s overlaps with %s:" % (errors, network, network2)
     75          for (host, key, entry, addr) in sorted(pool[network] + pool[network2]):
     76            print "  - %-20s - %-20s - %-5s - %s" % (host, key, entry, addr)
     77
     78      leden = sorted(pool[network])
     79      for i,lid in enumerate(leden):
     80        for lid2 in leden[i+1:]:
     81          if lid[3] == lid2[3]:
     82            errors += 1
     83            print "[ERROR#%i] Multiple usages of IP %s:" % (errors, lid[3])
     84            print "  - %-20s - %-20s - %-5s" % (lid[0], lid[1], lid[2])
     85            print "  - %-20s - %-20s - %-5s" % (lid2[0], lid2[1], lid2[2])
     86           
     87  if errors > 0:
     88    print "# %i Errors found" % errors
    6189    return 1
    6290  else:
Note: See TracChangeset for help on using the changeset viewer.