mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
nlpacket: add new attribute: AttributeStringInterfaceName with length check
Ticket: CM-12302 Reviewed By: Daniel, Roopa, Nikhil G Testing Done: ifupdown2 smoke test With this pretty straight forward, we introduce a new Attribute in nlmanager, so that we have single check and no code redundancy for ifname length. This also prevent loosing time sending a netlink packet for an known error. Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
This commit is contained in:
@@ -39,6 +39,9 @@ from struct import pack, unpack, calcsize
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Interface name buffer size #define IFNAMSIZ 16 (kernel source)
|
||||
IF_NAME_SIZE = 15 # 15 because python doesn't have \0
|
||||
|
||||
# Netlink message types
|
||||
NLMSG_NOOP = 0x01
|
||||
NLMSG_ERROR = 0x02
|
||||
@@ -185,6 +188,15 @@ class Attribute(object):
|
||||
def __str__(self):
|
||||
return self.string
|
||||
|
||||
def set_value(self, value):
|
||||
self.value = value
|
||||
|
||||
def set_nested(self, nested):
|
||||
self.nested = nested
|
||||
|
||||
def set_net_byteorder(self, net_byteorder):
|
||||
self.net_byteorder = net_byteorder
|
||||
|
||||
def pad_bytes_needed(self, length):
|
||||
"""
|
||||
Return the number of bytes that should be added to align on a 4-byte boundry
|
||||
@@ -331,6 +343,17 @@ class AttributeString(Attribute):
|
||||
raise
|
||||
|
||||
|
||||
class AttributeStringInterfaceName(AttributeString):
|
||||
|
||||
def __init__(self, atype, string, logger):
|
||||
AttributeString.__init__(self, atype, string, logger)
|
||||
|
||||
def set_value(self, value):
|
||||
if value and len(value) > IF_NAME_SIZE:
|
||||
raise Exception('interface name exceeds max length of %d' % IF_NAME_SIZE)
|
||||
self.value = value
|
||||
|
||||
|
||||
class AttributeIPAddress(Attribute):
|
||||
|
||||
def __init__(self, atype, string, family, logger):
|
||||
@@ -1330,9 +1353,9 @@ class NetlinkPacket(object):
|
||||
else:
|
||||
attr = attr_class(attr_type, attr_string, self.log)
|
||||
|
||||
attr.value = value
|
||||
attr.nested = nested
|
||||
attr.net_byteorder = net_byteorder
|
||||
attr.set_value(value)
|
||||
attr.set_nested(nested)
|
||||
attr.set_net_byteorder(net_byteorder)
|
||||
|
||||
# self.attributes is a dictionary keyed by the attribute type where
|
||||
# the value is an instance of the corresponding AttributeXXXX class.
|
||||
@@ -1675,7 +1698,7 @@ class Link(NetlinkPacket):
|
||||
IFLA_UNSPEC : ('IFLA_UNSPEC', AttributeGeneric),
|
||||
IFLA_ADDRESS : ('IFLA_ADDRESS', AttributeMACAddress),
|
||||
IFLA_BROADCAST : ('IFLA_BROADCAST', AttributeMACAddress),
|
||||
IFLA_IFNAME : ('IFLA_IFNAME', AttributeString),
|
||||
IFLA_IFNAME : ('IFLA_IFNAME', AttributeStringInterfaceName),
|
||||
IFLA_MTU : ('IFLA_MTU', AttributeFourByteValue),
|
||||
IFLA_LINK : ('IFLA_LINK', AttributeFourByteValue),
|
||||
IFLA_QDISC : ('IFLA_QDISC', AttributeString),
|
||||
|
Reference in New Issue
Block a user