Commit 82187135 authored by René Nussbaumer's avatar René Nussbaumer Committed by Iustin Pop
Browse files

Normalize MAC addresses to all lower.



This change will normalize the MAC to all lower after validation.
Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 7699c3af
...@@ -5770,16 +5770,14 @@ class LUCreateInstance(LogicalUnit): ...@@ -5770,16 +5770,14 @@ class LUCreateInstance(LogicalUnit):
# MAC address verification # MAC address verification
mac = nic.get("mac", constants.VALUE_AUTO) mac = nic.get("mac", constants.VALUE_AUTO)
if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
if not utils.IsValidMac(mac.lower()): mac = utils.NormalizeAndValidateMac(mac)
raise errors.OpPrereqError("Invalid MAC address specified: %s" %
mac, errors.ECODE_INVAL) try:
else: self.cfg.ReserveMAC(mac, self.proc.GetECId())
try: except errors.ReservationError:
self.cfg.ReserveMAC(mac, self.proc.GetECId()) raise errors.OpPrereqError("MAC address %s already in use"
except errors.ReservationError: " in cluster" % mac,
raise errors.OpPrereqError("MAC address %s already in use" errors.ECODE_NOTUNIQUE)
" in cluster" % mac,
errors.ECODE_NOTUNIQUE)
# bridge verification # bridge verification
bridge = nic.get("bridge", None) bridge = nic.get("bridge", None)
...@@ -7536,9 +7534,8 @@ class LUSetInstanceParams(LogicalUnit): ...@@ -7536,9 +7534,8 @@ class LUSetInstanceParams(LogicalUnit):
if 'mac' in nic_dict: if 'mac' in nic_dict:
nic_mac = nic_dict['mac'] nic_mac = nic_dict['mac']
if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE): if nic_mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
if not utils.IsValidMac(nic_mac): nic_mac = utils.NormalizeAndValidateMac(nic_mac)
raise errors.OpPrereqError("Invalid MAC address %s" % nic_mac,
errors.ECODE_INVAL)
if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO: if nic_op != constants.DDM_ADD and nic_mac == constants.VALUE_AUTO:
raise errors.OpPrereqError("'auto' is not a valid MAC address when" raise errors.OpPrereqError("'auto' is not a valid MAC address when"
" modifying an existing nic", " modifying an existing nic",
......
...@@ -1398,20 +1398,26 @@ def UniqueSequence(seq): ...@@ -1398,20 +1398,26 @@ def UniqueSequence(seq):
return [i for i in seq if i not in seen and not seen.add(i)] return [i for i in seq if i not in seen and not seen.add(i)]
def IsValidMac(mac): def NormalizeAndValidateMac(mac):
"""Predicate to check if a MAC address is valid. """Normalizes and check if a MAC address is valid.
Checks whether the supplied MAC address is formally correct, only Checks whether the supplied MAC address is formally correct, only
accepts colon separated format. accepts colon separated format. Normalize it to all lower.
@type mac: str @type mac: str
@param mac: the MAC to be validated @param mac: the MAC to be validated
@rtype: boolean @rtype: str
@return: True is the MAC seems valid @return: returns the normalized and validated MAC.
@raise errors.OpPrereqError: If the MAC isn't valid
""" """
mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$") mac_check = re.compile("^([0-9a-f]{2}(:|$)){6}$", re.I)
return mac_check.match(mac) is not None if not mac_check.match(mac):
raise errors.OpPrereqError("Invalid MAC address specified: %s" %
mac, errors.ECODE_INVAL)
return mac.lower()
def TestDelay(duration): def TestDelay(duration):
......
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