From c2ff2ce970ff51339086216022f630c341684332 Mon Sep 17 00:00:00 2001
From: Pierre LALET <pierre.lalet@cea.fr>
Date: Sun, 20 Mar 2016 17:28:59 +0100
Subject: [PATCH] Pad packet when needed (fixes #90)

---
 scapy/arch/linux.py | 19 ++++++++++++++++---
 scapy/config.py     |  1 +
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/scapy/arch/linux.py b/scapy/arch/linux.py
index db8b5c72..504ad693 100644
--- a/scapy/arch/linux.py
+++ b/scapy/arch/linux.py
@@ -13,6 +13,7 @@ from select import select
 from fcntl import ioctl
 import scapy.utils
 import scapy.utils6
+from scapy.packet import Packet, Padding
 from scapy.config import conf
 from scapy.data import *
 from scapy.supersocket import SuperSocket
@@ -398,14 +399,15 @@ class L3PacketSocket(SuperSocket):
             sx = str(ll(x))
             x.sent_time = time.time()
             self.outs.sendto(sx, sdto)
-        except socket.error,msg:
+        except socket.error, msg:
             x.sent_time = time.time()  # bad approximation
-            if conf.auto_fragment and msg[0] == 90:
+            if msg[0] == 22 and len(sx) < conf.min_pkt_size:
+                self.outs.send(sx + "\x00" * (conf.min_pkt_size - len(sx)))
+            elif conf.auto_fragment and msg[0] == 90:
                 for p in x.fragment():
                     self.outs.sendto(str(ll(p)), sdto)
             else:
                 raise
-                    
 
 
 
@@ -460,6 +462,17 @@ class L2Socket(SuperSocket):
             q = conf.raw_layer(pkt)
         q.time = get_last_packet_timestamp(self.ins)
         return q
+    def send(self, x):
+        try:
+            return SuperSocket.send(self, x)
+        except socket.error, msg:
+            if msg[0] == 22 and len(x) < conf.min_pkt_size:
+                padding = "\x00" * (conf.min_pkt_size - len(x))
+                if isinstance(x, Packet):
+                    return SuperSocket.send(self, x / Padding(load=padding))
+                else:
+                    return SuperSocket.send(self, str(x) + padding)
+            raise
 
 
 class L2ListenSocket(SuperSocket):
diff --git a/scapy/config.py b/scapy/config.py
index 91da07f7..19c74b3d 100755
--- a/scapy/config.py
+++ b/scapy/config.py
@@ -345,6 +345,7 @@ extensions_paths: path or list of paths where extensions are to be looked for
     L3socket = None
     L2socket = None
     L2listen = None
+    min_pkt_size = 60
     histfile = os.path.join(os.path.expanduser("~"), ".scapy_history")
     padding = 1
     except_filter = ""
-- 
GitLab