Skip to content
15 changes: 11 additions & 4 deletions backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,15 +1625,16 @@ def configureNetworking(mounts, admin_iface, admin_bridge, admin_config, hn_conf
print >>mc, "NETMASK='%s'" % admin_config.netmask
if admin_config.gateway:
print >>mc, "GATEWAY='%s'" % admin_config.gateway
if manual_nameservers:
print >>mc, "DNS='%s'" % (','.join(nameservers),)
if domain:
print >>mc, "DOMAIN='%s'" % domain
print >>mc, "MODEV6='%s'" % netinterface.NetInterface.getModeStr(admin_config.modev6)
if admin_config.modev6 == netinterface.NetInterface.Static:
print >>mc, "IPv6='%s'" % admin_config.ipv6addr
if admin_config.ipv6_gateway:
print >>mc, "IPv6_GATEWAY='%s'" % admin_config.ipv6_gateway
if admin_config.isStatic():
if manual_nameservers:
print >>mc, "DNS='%s'" % (','.join(nameservers),)
if domain:
print >>mc, "DOMAIN='%s'" % domain
if admin_config.vlan:
print >>mc, "VLAN='%d'" % admin_config.vlan
mc.close()
Expand Down Expand Up @@ -1676,12 +1677,18 @@ def configureNetworking(mounts, admin_iface, admin_bridge, admin_config, hn_conf
# now we need to write /etc/sysconfig/network
nfd = open("%s/etc/sysconfig/network" % mounts["root"], "w")
nfd.write("NETWORKING=yes\n")
ipv6_conf = open("%s/etc/sysctl.d/91-net-ipv6.conf" % mounts["root"], "w")
if admin_config.modev6:
nfd.write("NETWORKING_IPV6=yes\n")
util.runCmd2(['chroot', mounts['root'], 'systemctl', 'enable', 'ip6tables'])
for i in ['all', 'default']:
ipv6_conf.write('net.ipv6.conf.%s.disable_ipv6=0\n' % i)
else:
nfd.write("NETWORKING_IPV6=no\n")
for i in ['all', 'default']:
ipv6_conf.write('net.ipv6.conf.%s.disable_ipv6=1\n' % i)
netutil.disable_ipv6_module(mounts["root"])
ipv6_conf.close()
nfd.write("IPV6_AUTOCONF=no\n")
nfd.write('NTPSERVERARGS="iburst prefer"\n')
nfd.close()
Expand Down
73 changes: 50 additions & 23 deletions netinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,31 @@ def __init__(self, mode, hwaddr, ipaddr=None, netmask=None, gateway=None,
assert ipaddr
assert netmask

self.mode = mode
ipv6 = ipaddr.find(':') > -1 if ipaddr else False

self.hwaddr = hwaddr
if mode == self.Static:
self.ipaddr = ipaddr
self.netmask = netmask
self.gateway = gateway
self.dns = dns
self.domain = domain
else:
if ipv6:
self.mode = None
self.ipaddr = None
self.netmask = None
self.gateway = None
self.dns = None
self.domain = None
self.vlan = vlan

# Initialise IPv6 to None.
self.modev6 = None
self.ipv6addr = None
self.ipv6_gateway = None
self.modev6 = mode
self.ipv6addr = ipaddr + "/" + netmask if mode == self.Static else None
self.ipv6_gateway = gateway if mode == self.Static else None
else:
self.modev6 = None
self.ipv6addr = None
self.ipv6_gateway = None

self.mode = mode
self.ipaddr = ipaddr if mode == self.Static else None
self.netmask = netmask if mode == self.Static else None
self.gateway = gateway if mode == self.Static else None

self.dns = dns if mode == self.Static else None
self.domain = domain if mode == self.Static else None
self.vlan = vlan

def __repr__(self):
hw = "hwaddr = '%s' " % self.hwaddr
Expand Down Expand Up @@ -132,7 +137,7 @@ def valid(self):

def isStatic(self):
""" Returns true if a static interface configuration is represented. """
return self.mode == self.Static
return self.mode == self.Static or (self.mode == None and self.modev6 == self.Static)

def isVlan(self):
return self.vlan is not None
Expand All @@ -151,13 +156,12 @@ def writeDebStyleInterface(self, iface, f):

# Debian style interfaces are only used for the installer; dom0 only uses CentOS style
# IPv6 is only enabled through answerfiles and so is not supported here.
assert self.modev6 is None
assert self.mode
assert self.modev6 or self.mode
iface_vlan = self.getInterfaceName(iface)

if self.mode == self.DHCP:
f.write("iface %s inet dhcp\n" % iface_vlan)
else:
elif self.mode == self.Static:
# CA-11825: broadcast needs to be determined for non-standard networks
bcast = self.getBroadcast()
f.write("iface %s inet static\n" % iface_vlan)
Expand All @@ -168,32 +172,55 @@ def writeDebStyleInterface(self, iface, f):
if self.gateway:
f.write(" gateway %s\n" % self.gateway)

if self.modev6 == self.DHCP:
f.write("iface %s inet6 dhcp\n" % iface_vlan)
elif self.modev6 == self.Static:
f.write("iface %s inet6 static\n" % iface_vlan)
f.write(" address %s\n" % self.ipv6addr)
if self.ipv6_gateway:
f.write(" gateway %s\n" % self.ipv6_gateway)

def writeRHStyleInterface(self, iface):
""" Write a RedHat-style configuration entry for this interface to
file object f using interface name iface. """

assert self.modev6 is None
assert self.mode
assert self.modev6 or self.mode
iface_vlan = self.getInterfaceName(iface)

f = open('/etc/sysconfig/network-scripts/ifcfg-%s' % iface_vlan, 'w')
f.write("DEVICE=%s\n" % iface_vlan)
f.write("ONBOOT=yes\n")
if self.mode == self.DHCP:
if self.mode == self.DHCP or self.modev6 == self.DHCP:
f.write("BOOTPROTO=dhcp\n")
f.write("PERSISTENT_DHCLIENT=1\n")
else:
f.write("BOOTPROTO=none\n")

if self.mode == self.Static:
# CA-11825: broadcast needs to be determined for non-standard networks
bcast = self.getBroadcast()
f.write("BOOTPROTO=none\n")
f.write("IPADDR=%s\n" % self.ipaddr)
if bcast is not None:
f.write("BROADCAST=%s\n" % bcast)
f.write("NETMASK=%s\n" % self.netmask)
if self.gateway:
f.write("GATEWAY=%s\n" % self.gateway)

if self.modev6:
f.write("NETWORKING_IPV6=yes\n")
f.write("IPV6INIT=yes\n")
f.write("IPV6_AUTOCONF=no\n")
if self.modev6 == self.DHCP:
f.write("DHCPV6C=yes\n")
elif self.modev6 == self.Static:
f.write("IPV6ADDR=%s\n" % self.ipv6addr)
if self.ipv6_gateway:
prefix = self.ipv6addr.split("/")[1]
f.write("IPV6_DEFAULTGW=%s/%s\n" % (self.ipv6_gateway, prefix))

if self.vlan:
f.write("VLAN=yes\n")

f.close()


Expand Down
18 changes: 8 additions & 10 deletions netutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ def interfaceUp(interface):
if rc != 0:
return False
inets = filter(lambda x: x.startswith(" inet "), out.split("\n"))
return len(inets) == 1
if len(inets) == 1:
return True

inet6s = filter(lambda x: x.startswith(" inet6 "), out.split("\n"))
return len(inet6s) > 1 # Not just the fe80:: address

# work out if a link is up:
def linkUp(interface):
Expand Down Expand Up @@ -236,15 +240,9 @@ def valid_vlan(vlan):
return True

def valid_ip_addr(addr):
if not re.match('^\d+\.\d+\.\d+\.\d+$', addr):
return False
els = addr.split('.')
if len(els) != 4:
return False
for el in els:
if int(el) > 255:
return False
return True
ipv4_re = '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}'
ipv6_re = '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
return re.match(ipv4_re, addr) or re.match(ipv6_re, addr)

def network(ipaddr, netmask):
ip = map(int,ipaddr.split('.',3))
Expand Down
Loading