From 8bcf817b5e9c2d0ebd23be98696b6835dd4fffeb Mon Sep 17 00:00:00 2001
From: Pierre Lorinquer <pierre.lorinquer@ssi.gouv.fr>
Date: Wed, 29 Mar 2017 10:07:13 +0200
Subject: [PATCH] Fix variable length field computation in EAP_MD5, EAP_TLS and
 EAP_FAST. Example: >>> EAP_FAST().show() Traceback (most recent call last):  
 File "<console>", line 1, in <module>   File "scapy/packet.py", line 923, in
 show     return self._show_or_dump(dump, indent, lvl, label_lvl)   File
 "scapy/packet.py", line 906, in _show_or_dump     reprval =
 f.i2repr(self,fvalue)   File "scapy/fields.py", line 572, in i2repr    
 return x[:self.length_from(pkt)].encode("hex")   File "scapy/layers/l2.py",
 line 590, in <lambda>     XStrLenField('data', '', length_from=lambda pkt:
 pkt.len - 10 if pkt.L == 1 else pkt.len - 6) TypeError: unsupported operand
 type(s) for -: 'NoneType' and 'int'

---
 scapy/layers/l2.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/scapy/layers/l2.py b/scapy/layers/l2.py
index 252d8a22..d5032ef7 100644
--- a/scapy/layers/l2.py
+++ b/scapy/layers/l2.py
@@ -539,11 +539,12 @@ class EAP_MD5(EAP):
         ByteEnumField("code", 1, eap_codes),
         ByteField("id", 0),
         FieldLenField("len", None, fmt="H", length_of="optional_name",
-                      adjust=lambda p, x: x + p.value_size + 6),
+                adjust=lambda p, x: (x + p.value_size + 6) if p.value_size is not None else 6),
         ByteEnumField("type", 4, eap_types),
-        FieldLenField("value_size", 0, fmt="B", length_of="value"),
+        FieldLenField("value_size", None, fmt="B", length_of="value"),
         XStrLenField("value", '', length_from=lambda p: p.value_size),
-        XStrLenField("optional_name", '', length_from=lambda p: p.len - p.value_size - 6)
+        XStrLenField("optional_name", '',
+                length_from=lambda p: (p.len - p.value_size - 6) if p.len is not None and p.value_size is not None else 0)
     ]
 
 
@@ -564,7 +565,8 @@ class EAP_TLS(EAP):
         BitField('S', 0, 1),
         BitField('reserved', 0, 5),
         ConditionalField(IntField('tls_message_len', 0), lambda pkt: pkt.L == 1),
-        XStrLenField('tls_data', '', length_from=lambda pkt: pkt.len - 10 if pkt.L == 1 else pkt.len - 6)
+        XStrLenField('tls_data', '',
+                length_from=lambda pkt: pkt.len - 10 if pkt.len is not None and pkt.L == 1 else pkt.len - 6 if pkt.len is not None else 0)
     ]
 
 
@@ -587,7 +589,8 @@ class EAP_FAST(EAP):
         BitField('reserved', 0, 2),
         BitField('version', 0, 3),
         ConditionalField(IntField('message_len', 0), lambda pkt: pkt.L == 1),
-        XStrLenField('data', '', length_from=lambda pkt: pkt.len - 10 if pkt.L == 1 else pkt.len - 6)
+        XStrLenField('data', '', 
+                length_from=lambda pkt: pkt.len - 10 if pkt.len is not None and pkt.L == 1 else pkt.len - 6 if pkt.len is not None else 0)
     ]
 
 
-- 
GitLab