From d0c2cf6c1de2baeb83d74abb2e42a0a9ad75d6ff Mon Sep 17 00:00:00 2001 From: Julien Fortin Date: Thu, 11 Aug 2016 03:05:04 +0200 Subject: [PATCH] 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 --- nlmanager/nlpacket.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/nlmanager/nlpacket.py b/nlmanager/nlpacket.py index ee01d69..f4539aa 100644 --- a/nlmanager/nlpacket.py +++ b/nlmanager/nlpacket.py @@ -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),