aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/netpfil/common/pft_ping.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/sys/netpfil/common/pft_ping.py')
-rw-r--r--tests/sys/netpfil/common/pft_ping.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/sys/netpfil/common/pft_ping.py b/tests/sys/netpfil/common/pft_ping.py
index 812250803309..957123e4f6f8 100644
--- a/tests/sys/netpfil/common/pft_ping.py
+++ b/tests/sys/netpfil/common/pft_ping.py
@@ -115,6 +115,35 @@ def check_ping6_request(args, packet):
return True
+def check_ping_reply(args, packet):
+ return check_ping4_reply(args, packet)
+
+def check_ping4_reply(args, packet):
+ """
+ Check that this is a reply to the ping request we sent
+ """
+ dst_ip = args.to[0]
+
+ ip = packet.getlayer(sp.IP)
+ if not ip:
+ return False
+ if ip.src != dst_ip:
+ return False
+
+ icmp = packet.getlayer(sp.ICMP)
+ if not icmp:
+ return False
+ if sp.icmptypes[icmp.type] != 'echo-reply':
+ return False
+
+ raw = packet.getlayer(sp.Raw)
+ if not raw:
+ return False
+ if raw.load != PAYLOAD_MAGIC:
+ return False
+
+ return True
+
def ping(send_if, dst_ip, args):
ether = sp.Ether()
ip = sp.IP(dst=dst_ip)
@@ -124,6 +153,9 @@ def ping(send_if, dst_ip, args):
if args.send_tos:
ip.tos = int(args.send_tos[0])
+ if args.fromaddr:
+ ip.src = args.fromaddr[0]
+
req = ether / ip / icmp / raw
sp.sendp(req, iface=send_if, verbose=False)
@@ -132,6 +164,9 @@ def ping6(send_if, dst_ip, args):
ip6 = sp.IPv6(dst=dst_ip)
icmp = sp.ICMPv6EchoRequest(data=sp.raw(PAYLOAD_MAGIC))
+ if args.fromaddr:
+ ip.src = args.fromaddr[0]
+
req = ether / ip6 / icmp
sp.sendp(req, iface=send_if, verbose=False)
@@ -189,6 +224,8 @@ def main():
required=True,
help='The interface through which the packet(s) will be sent')
parser.add_argument('--recvif', nargs=1,
+ help='The interface on which to expect the ICMP echo request')
+ parser.add_argument('--replyif', nargs=1,
help='The interface on which to expect the ICMP echo response')
parser.add_argument('--checkdup', nargs=1,
help='The interface on which to expect the duplicated ICMP packets')
@@ -197,6 +234,8 @@ def main():
parser.add_argument('--to', nargs=1,
required=True,
help='The destination IP address for the ICMP echo request')
+ parser.add_argument('--fromaddr', nargs=1,
+ help='The source IP address for the ICMP echo request')
# TCP options
parser.add_argument('--tcpsyn', action='store_true',
@@ -225,6 +264,11 @@ def main():
sniffer = Sniffer(args, checkfn)
+ replysniffer = None
+ if not args.replyif is None:
+ checkfn=check_ping_reply
+ replysniffer = Sniffer(args, checkfn, recvif=args.replyif[0])
+
dupsniffer = None
if args.checkdup is not None:
dupsniffer = Sniffer(args, check_dup, recvif=args.checkdup[0])
@@ -250,5 +294,13 @@ def main():
else:
sys.exit(1)
+ if replysniffer:
+ replysniffer.join()
+
+ if replysniffer.foundCorrectPacket:
+ sys.exit(0)
+ else:
+ sys.exit(1)
+
if __name__ == '__main__':
main()