aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/netpfil/common/pft_ping.py
blob: d8aafc8842659ff3ddbd408402ca8d0555054994 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/usr/bin/env python3
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2017 Kristof Provost <kp@FreeBSD.org>
# Copyright (c) 2023 Kajetan Staszkiewicz <vegeta@tuxpowered.net>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

import argparse
import logging
logging.getLogger("scapy").setLevel(logging.CRITICAL)
import math
import scapy.all as sp
import sys

from copy import copy
from sniffer import Sniffer

logging.basicConfig(format='%(message)s')
LOGGER = logging.getLogger(__name__)

PAYLOAD_MAGIC = bytes.fromhex('42c0ffee')

def build_payload(l):
    pl = len(PAYLOAD_MAGIC)
    ret = PAYLOAD_MAGIC * math.floor(l/pl)
    ret += PAYLOAD_MAGIC[0:(l % pl)]
    return ret


def clean_params(params):
    # Prepare a copy of safe copy of params
    ret = copy(params)
    ret.pop('src_address')
    ret.pop('dst_address')
    ret.pop('flags')
    return ret

def prepare_ipv6(send_params):
    src_address = send_params.get('src_address')
    dst_address = send_params.get('dst_address')
    hlim = send_params.get('hlim')
    tc = send_params.get('tc')
    ip6 = sp.IPv6(dst=dst_address)
    if src_address:
        ip6.src = src_address
    if hlim:
        ip6.hlim = hlim
    if tc:
        ip6.tc = tc
    return ip6


def prepare_ipv4(send_params):
    src_address = send_params.get('src_address')
    dst_address = send_params.get('dst_address')
    flags = send_params.get('flags')
    tos = send_params.get('tc')
    ttl = send_params.get('hlim')
    opt = send_params.get('nop')
    options = ''
    if opt:
        options='\x00'
    ip = sp.IP(dst=dst_address, options=options)
    if src_address:
        ip.src = src_address
    if flags:
        ip.flags = flags
    if tos:
        ip.tos = tos
    if ttl:
        ip.ttl = ttl
    return ip


def send_icmp_ping(send_params):
    send_length = send_params['length']
    send_frag_length = send_params['frag_length']
    packets = []
    ether = sp.Ether()
    if ':' in send_params['dst_address']:
        ip6 = prepare_ipv6(send_params)
        icmp = sp.ICMPv6EchoRequest(data=sp.raw(build_payload(send_length)))
        if send_frag_length:
            for packet in sp.fragment(ip6 / icmp, fragsize=send_frag_length):
                packets.append(ether / packet)
        else:
            packets.append(ether / ip6 / icmp)

    else:
        ip = prepare_ipv4(send_params)
        icmp = sp.ICMP(type='echo-request')
        raw = sp.raw(build_payload(send_length))
        if send_frag_length:
            for packet in sp.fragment(ip / icmp / raw, fragsize=send_frag_length):
                packets.append(ether / packet)
        else:
            packets.append(ether / ip / icmp / raw)
    for packet in packets:
        sp.sendp(packet, iface=send_params['sendif'], verbose=False)


def send_tcp_syn(send_params):
    tcpopt_unaligned = send_params.get('tcpopt_unaligned')
    seq = send_params.get('seq')
    mss = send_params.get('mss')
    ether = sp.Ether()
    opts=[('Timestamp', (1, 1)), ('MSS', mss if mss else 1280)]
    if tcpopt_unaligned:
        opts = [('NOP', 0 )] + opts
    if ':' in send_params['dst_address']:
        ip = prepare_ipv6(send_params)
    else:
        ip = prepare_ipv4(send_params)
    tcp = sp.TCP(
        sport=send_params.get('sport'), dport=send_params.get('dport'),
        flags='S', options=opts, seq=seq,
    )
    req = ether / ip / tcp
    sp.sendp(req, iface=send_params['sendif'], verbose=False)


def send_ping(ping_type, send_params):
    if ping_type == 'icmp':
        send_icmp_ping(send_params)
    elif (
        ping_type == 'tcpsyn' or
        ping_type == 'tcp3way'
    ):
        send_tcp_syn(send_params)
    else:
        raise Exception('Unspported ping type')


def check_ipv4(expect_params, packet):
    src_address = expect_params.get('src_address')
    dst_address = expect_params.get('dst_address')
    flags = expect_params.get('flags')
    tos = expect_params.get('tc')
    ttl = expect_params.get('hlim')
    ip = packet.getlayer(sp.IP)
    LOGGER.debug(f'Packet: {ip}')
    if not ip:
        LOGGER.debug('Packet is not IPv4!')
        return False
    if src_address and ip.src != src_address:
        LOGGER.debug(f'Wrong IPv4 source {ip.src}, expected {src_address}')
        return False
    if dst_address and ip.dst != dst_address:
        LOGGER.debug(f'Wrong IPv4 destination {ip.dst}, expected {dst_address}')
        return False
    chksum = ip.chksum
    ip.chksum = None
    new_chksum = sp.IP(sp.raw(ip)).chksum
    if chksum != new_chksum:
        LOGGER.debug(f'Wrong IPv4 checksum {chksum}, expected {new_chksum}')
        return False
    if flags and ip.flags != flags:
        LOGGER.debug(f'Wrong IP flags value {ip.flags}, expected {flags}')
        return False
    if tos and ip.tos != tos:
        LOGGER.debug(f'Wrong ToS value {ip.tos}, expected {tos}')
        return False
    if ttl and ip.ttl != ttl:
        LOGGER.debug(f'Wrong TTL value {ip.ttl}, expected {ttl}')
        return False
    return True


def check_ipv6(expect_params, packet):
    src_address = expect_params.get('src_address')
    dst_address = expect_params.get('dst_address')
    flags = expect_params.get('flags')
    hlim = expect_params.get('hlim')
    tc = expect_params.get('tc')
    ip6 = packet.getlayer(sp.IPv6)
    if not ip6:
        LOGGER.debug('Packet is not IPv6!')
        return False
    if src_address and ip6.src != src_address:
        LOGGER.debug(f'Wrong IPv6 source {ip6.src}, expected {src_address}')
        return False
    if dst_address and ip6.dst != dst_address:
        LOGGER.debug(f'Wrong IPv6 destination {ip6.dst}, expected {dst_address}')
        return False
    # IPv6 has no IP-level checksum.
    if flags:
        raise Exception("There's no fragmentation flags in IPv6")
    if hlim and ip6.hlim != hlim:
        LOGGER.debug(f'Wrong Hop Limit value {ip6.hlim}, expected {hlim}')
        return False
    if tc and ip6.tc != tc:
        LOGGER.debug(f'Wrong TC value {ip6.tc}, expected {tc}')
        return False
    return True


def check_ping_4(expect_params, packet):
    expect_length = expect_params['length']
    if not check_ipv4(expect_params, packet):
        return False
    icmp = packet.getlayer(sp.ICMP)
    if not icmp:
        LOGGER.debug('Packet is not IPv4 ICMP!')
        return False
    raw = packet.getlayer(sp.Raw)
    if not raw:
        LOGGER.debug('Packet contains no payload!')
        return False
    if raw.load != build_payload(expect_length):
        LOGGER.debug('Payload magic does not match!')
        return False
    return True


def check_ping_request_4(expect_params, packet):
    if not check_ping_4(expect_params, packet):
        return False
    icmp = packet.getlayer(sp.ICMP)
    if sp.icmptypes[icmp.type] != 'echo-request':
        LOGGER.debug('Packet is not IPv4 ICMP Echo Request!')
        return False
    return True


def check_ping_reply_4(expect_params, packet):
    if not check_ping_4(expect_params, packet):
        return False
    icmp = packet.getlayer(sp.ICMP)
    if sp.icmptypes[icmp.type] != 'echo-reply':
        LOGGER.debug('Packet is not IPv4 ICMP Echo Reply!')
        return False
    return True


def check_ping_request_6(expect_params, packet):
    expect_length = expect_params['length']
    if not check_ipv6(expect_params, packet):
        return False
    icmp = packet.getlayer(sp.ICMPv6EchoRequest)
    if not icmp:
        LOGGER.debug('Packet is not IPv6 ICMP Echo Request!')
        return False
    if icmp.data != build_payload(expect_length):
        LOGGER.debug('Payload magic does not match!')
        return False
    return True


def check_ping_reply_6(expect_params, packet):
    expect_length = expect_params['length']
    if not check_ipv6(expect_params, packet):
        return False
    icmp = packet.getlayer(sp.ICMPv6EchoReply)
    if not icmp:
        LOGGER.debug('Packet is not IPv6 ICMP Echo Reply!')
        return False
    if icmp.data != build_payload(expect_length):
        LOGGER.debug('Payload magic does not match!')
        return False
    return True


def check_ping_request(args, packet):
    src_address = args['expect_params'].get('src_address')
    dst_address = args['expect_params'].get('dst_address')
    if not (src_address or dst_address):
        raise Exception('Source or destination address must be given to match the ping request!')
    if (
        (src_address and ':' in src_address) or
        (dst_address and ':' in dst_address)
    ):
        return check_ping_request_6(args['expect_params'], packet)
    else:
        return check_ping_request_4(args['expect_params'], packet)


def check_ping_reply(args, packet):
    src_address = args['expect_params'].get('src_address')
    dst_address = args['expect_params'].get('dst_address')
    if not (src_address or dst_address):
        raise Exception('Source or destination address must be given to match the ping reply!')
    if (
        (src_address and ':' in src_address) or
        (dst_address and ':' in dst_address)
    ):
        return check_ping_reply_6(args['expect_params'], packet)
    else:
        return check_ping_reply_4(args['expect_params'], packet)


def check_tcp(expect_params, packet):
    tcp_flags = expect_params.get('tcp_flags')
    mss = expect_params.get('mss')
    seq = expect_params.get('seq')
    tcp = packet.getlayer(sp.TCP)
    if not tcp:
        LOGGER.debug('Packet is not TCP!')
        return False
    chksum = tcp.chksum
    tcp.chksum = None
    newpacket = sp.Ether(sp.raw(packet[sp.Ether]))
    new_chksum = newpacket[sp.TCP].chksum
    if new_chksum and chksum != new_chksum:
        LOGGER.debug(f'Wrong TCP checksum {chksum}, expected {new_chksum}!')
        return False
    if tcp_flags and tcp.flags != tcp_flags:
        LOGGER.debug(f'Wrong TCP flags {tcp.flags}, expected {tcp_flags}!')
        return False
    if seq:
        if tcp_flags == 'S':
            tcp_seq = tcp.seq
        elif tcp_flags == 'SA':
            tcp_seq = tcp.ack - 1
        if seq != tcp_seq:
            LOGGER.debug(f'Wrong TCP Sequence Number {tcp_seq}, expected {seq}')
            return False
    if mss:
        for option in tcp.options:
            if option[0] == 'MSS':
                if option[1] != mss:
                    LOGGER.debug(f'Wrong TCP MSS {option[1]}, expected {mss}')
                    return False
    return True


def check_tcp_syn_request_4(expect_params, packet):
    if not check_ipv4(expect_params, packet):
        return False
    if not check_tcp(expect_params | {'tcp_flags': 'S'}, packet):
        return False
    return True


def check_tcp_syn_reply_4(send_params, expect_params, packet):
    if not check_ipv4(expect_params, packet):
        return False
    if not check_tcp(expect_params | {'tcp_flags': 'SA'}, packet):
        return False
    return True


def check_tcp_3way_4(args, packet):
    send_params = args['send_params']

    expect_params_sa = clean_params(args['expect_params'])
    expect_params_sa['src_address'] = send_params['dst_address']
    expect_params_sa['dst_address'] = send_params['src_address']

    # Sniff incoming SYN+ACK packet
    if (
        check_ipv4(expect_params_sa, packet) and
        check_tcp(expect_params_sa | {'tcp_flags': 'SA'}, packet)
    ):
        ether = sp.Ether()
        ip_sa = packet.getlayer(sp.IP)
        tcp_sa = packet.getlayer(sp.TCP)
        reply_params = clean_params(send_params)
        reply_params['src_address'] = ip_sa.dst
        reply_params['dst_address'] = ip_sa.src
        ip_a = prepare_ipv4(reply_params)
        tcp_a = sp.TCP(
            sport=tcp_sa.dport, dport=tcp_sa.sport, flags='A',
            seq=tcp_sa.ack, ack=tcp_sa.seq + 1,
        )
        req = ether / ip_a / tcp_a
        sp.sendp(req, iface=send_params['sendif'], verbose=False)
        return True

    return False


def check_tcp_syn_request_6(expect_params, packet):
    if not check_ipv6(expect_params, packet):
        return False
    if not check_tcp(expect_params | {'tcp_flags': 'S'}, packet):
        return False
    return True


def check_tcp_syn_reply_6(expect_params, packet):
    if not check_ipv6(expect_params, packet):
        return False
    if not check_tcp(expect_params | {'tcp_flags': 'SA'}, packet):
        return False
    return True


def check_tcp_3way_6(args, packet):
    send_params = args['send_params']

    expect_params_sa = clean_params(args['expect_params'])
    expect_params_sa['src_address'] = send_params['dst_address']
    expect_params_sa['dst_address'] = send_params['src_address']

    # Sniff incoming SYN+ACK packet
    if (
        check_ipv6(expect_params_sa, packet) and
        check_tcp(expect_params_sa | {'tcp_flags': 'SA'}, packet)
    ):
        ether = sp.Ether()
        ip6_sa = packet.getlayer(sp.IPv6)
        tcp_sa = packet.getlayer(sp.TCP)
        reply_params = clean_params(send_params)
        reply_params['src_address'] = ip6_sa.dst
        reply_params['dst_address'] = ip6_sa.src
        ip_a = prepare_ipv6(reply_params)
        tcp_a = sp.TCP(
            sport=tcp_sa.dport, dport=tcp_sa.sport, flags='A',
            seq=tcp_sa.ack, ack=tcp_sa.seq + 1,
        )
        req = ether / ip_a / tcp_a
        sp.sendp(req, iface=send_params['sendif'], verbose=False)
        return True

    return False


def check_tcp_syn_request(args, packet):
    expect_params = args['expect_params']
    src_address = expect_params.get('src_address')
    dst_address = expect_params.get('dst_address')
    if not (src_address or dst_address):
        raise Exception('Source or destination address must be given to match the tcp syn request!')
    if (
        (src_address and ':' in src_address) or
        (dst_address and ':' in dst_address)
    ):
        return check_tcp_syn_request_6(expect_params, packet)
    else:
        return check_tcp_syn_request_4(expect_params, packet)


def check_tcp_syn_reply(args, packet):
    expect_params = args['expect_params']
    src_address = expect_params.get('src_address')
    dst_address = expect_params.get('dst_address')
    if not (src_address or dst_address):
        raise Exception('Source or destination address must be given to match the tcp syn reply!')
    if (
        (src_address and ':' in src_address) or
        (dst_address and ':' in dst_address)
    ):
        return check_tcp_syn_reply_6(expect_params, packet)
    else:
        return check_tcp_syn_reply_4(expect_params, packet)

def check_tcp_3way(args, packet):
    expect_params = args['expect_params']
    src_address = expect_params.get('src_address')
    dst_address = expect_params.get('dst_address')
    if not (src_address or dst_address):
        raise Exception('Source or destination address must be given to match the tcp syn reply!')
    if (
            (src_address and ':' in src_address) or
            (dst_address and ':' in dst_address)
    ):
        return check_tcp_3way_6(args, packet)
    else:
        return check_tcp_3way_4(args, packet)


def setup_sniffer(
        recvif, ping_type, sniff_type, expect_params, defrag, send_params,
):
    if ping_type == 'icmp' and sniff_type == 'request':
        checkfn = check_ping_request
    elif ping_type == 'icmp' and sniff_type == 'reply':
        checkfn = check_ping_reply
    elif ping_type == 'tcpsyn' and sniff_type == 'request':
        checkfn = check_tcp_syn_request
    elif ping_type == 'tcpsyn' and sniff_type == 'reply':
        checkfn = check_tcp_syn_reply
    elif ping_type == 'tcp3way' and sniff_type == 'reply':
        checkfn = check_tcp_3way
    else:
        raise Exception('Unspported ping or sniff type')

    return Sniffer(
        {'send_params': send_params, 'expect_params': expect_params},
        checkfn, recvif, defrag=defrag,
    )


def parse_args():
    parser = argparse.ArgumentParser("pft_ping.py",
        description="Ping test tool")

    # Parameters of sent ping request
    parser.add_argument('--sendif', required=True,
        help='The interface through which the packet(s) will be sent')
    parser.add_argument('--to', required=True,
        help='The destination IP address for the ping request')
    parser.add_argument('--ping-type',
        choices=('icmp', 'tcpsyn', 'tcp3way'),
        help='Type of ping: ICMP (default) or TCP SYN or 3-way TCP handshake',
        default='icmp')
    parser.add_argument('--fromaddr',
        help='The source IP address for the ping request')

    # Where to look for packets to analyze.
    # The '+' format is ugly as it mixes positional with optional syntax.
    # But we have no positional parameters so I guess it's fine to use it.
    parser.add_argument('--recvif', nargs='+',
        help='The interfaces on which to expect the ping request')
    parser.add_argument('--replyif', nargs='+',
        help='The interfaces which to expect the ping response')

    # Packet settings
    parser_send = parser.add_argument_group('Values set in transmitted packets')
    parser_send.add_argument('--send-flags', type=str,
        help='IPv4 fragmentation flags')
    parser_send.add_argument('--send-frag-length', type=int,
        help='Force IP fragmentation with given fragment length')
    parser_send.add_argument('--send-hlim', type=int,
        help='IPv6 Hop Limit or IPv4 Time To Live')
    parser_send.add_argument('--send-mss', type=int,
        help='TCP Maximum Segment Size')
    parser_send.add_argument('--send-seq', type=int,
        help='TCP sequence number')
    parser_send.add_argument('--send-sport', type=int,
        help='TCP source port')
    parser_send.add_argument('--send-dport', type=int, default=9,
        help='TCP destination port')
    parser_send.add_argument('--send-length', type=int, default=len(PAYLOAD_MAGIC),
        help='ICMP Echo Request payload size')
    parser_send.add_argument('--send-tc', type=int,
        help='IPv6 Traffic Class or IPv4 DiffServ / ToS')
    parser_send.add_argument('--send-tcpopt-unaligned', action='store_true',
        help='Include unaligned TCP options')
    parser_send.add_argument('--send-nop', action='store_true',
        help='Include a NOP IPv4 option')

    # Expectations
    parser_expect = parser.add_argument_group('Values expected in sniffed packets')
    parser_expect.add_argument('--expect-flags', type=str,
        help='IPv4 fragmentation flags')
    parser_expect.add_argument('--expect-hlim', type=int,
        help='IPv6 Hop Limit or IPv4 Time To Live')
    parser_expect.add_argument('--expect-mss', type=int,
        help='TCP Maximum Segment Size')
    parser_send.add_argument('--expect-seq', type=int,
        help='TCP sequence number')
    parser_expect.add_argument('--expect-tc', type=int,
        help='IPv6 Traffic Class or IPv4 DiffServ / ToS')

    parser.add_argument('-v', '--verbose', action='store_true',
        help=('Enable verbose logging. Apart of potentially useful information '
            'you might see warnings from parsing packets like NDP or other '
            'packets not related to the test being run. Use only when '
            'developing because real tests expect empty stderr and stdout.'))

    return parser.parse_args()


def main():
    args = parse_args()

    if args.verbose:
        LOGGER.setLevel(logging.DEBUG)

    # Split parameters into send and expect parameters. Parameters might be
    # missing from the command line, always fill the dictionaries with None.
    send_params = {}
    expect_params = {}
    for param_name in (
        'flags', 'hlim', 'length', 'mss', 'seq', 'tc', 'frag_length',
        'sport', 'dport',
    ):
        param_arg = vars(args).get(f'send_{param_name}')
        send_params[param_name] = param_arg if param_arg else None
        param_arg = vars(args).get(f'expect_{param_name}')
        expect_params[param_name] = param_arg if param_arg else None

    expect_params['length'] = send_params['length']
    send_params['tcpopt_unaligned'] = args.send_tcpopt_unaligned
    send_params['nop'] = args.send_nop
    send_params['src_address'] = args.fromaddr if args.fromaddr else None
    send_params['dst_address'] = args.to
    send_params['sendif'] = args.sendif

    # We may not have a default route. Tell scapy where to start looking for routes
    sp.conf.iface6 = args.sendif

    # Configuration sanity checking.
    if not (args.replyif or args.recvif):
        raise Exception('With no reply or recv interface specified no traffic '
            'can be sniffed and verified!'
        )

    sniffers = []

    if send_params['frag_length']:
        defrag = True
    else:
        defrag = False

    if args.recvif:
        sniffer_params = copy(expect_params)
        sniffer_params['src_address'] = None
        sniffer_params['dst_address'] = args.to
        for iface in args.recvif:
            LOGGER.debug(f'Installing receive sniffer on {iface}')
            sniffers.append(
                setup_sniffer(iface, args.ping_type, 'request',
                              sniffer_params, defrag, send_params,
            ))

    if args.replyif:
        sniffer_params = copy(expect_params)
        sniffer_params['src_address'] = args.to
        sniffer_params['dst_address'] = None
        for iface in args.replyif:
            LOGGER.debug(f'Installing reply sniffer on {iface}')
            sniffers.append(
                setup_sniffer(iface, args.ping_type, 'reply',
                              sniffer_params, defrag, send_params,
            ))

    LOGGER.debug(f'Installed {len(sniffers)} sniffers')

    send_ping(args.ping_type, send_params)

    err = 0
    sniffer_num = 0
    for sniffer in sniffers:
        sniffer.join()
        if sniffer.correctPackets == 1:
            LOGGER.debug(f'Expected ping has been sniffed on {sniffer._recvif}.')
        else:
            # Set a bit in err for each failed sniffer.
            err |= 1<<sniffer_num
            if sniffer.correctPackets > 1:
                LOGGER.debug(f'Duplicated ping has been sniffed on {sniffer._recvif}!')
            else:
                LOGGER.debug(f'Expected ping has not been sniffed on {sniffer._recvif}!')
        sniffer_num += 1

    return err


if __name__ == '__main__':
    sys.exit(main())