This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
/
app_testing_objects.py
1288 lines (1092 loc) · 43.3 KB
/
app_testing_objects.py
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import binascii
import collections
import fcntl
import os
import re
import select
import socket
import threading
import time
import eventlet
import greenlet
import netaddr
from neutron.agent.common import utils
from neutron_lib import constants as n_const
from os_ken.lib.packet import arp
from os_ken.lib.packet import dhcp
from os_ken.lib.packet import icmp
from os_ken.lib.packet import icmpv6
from os_ken.lib.packet import ipv4
from os_ken.lib.packet import ipv6
from os_ken.lib.packet import mpls
from os_ken.lib.packet import packet
from os_ken.lib.packet import udp
from os_ken.lib.packet import vlan
from oslo_log import log
import pytun
import six
from dragonflow import conf as cfg
from dragonflow.tests.fullstack import test_objects as objects
LOG = log.getLogger(__name__)
# NOTE(oanson) This function also exists in nova. However, to save the time it
# takes to install nova in the tests, for this one function, I copied it here.
def create_tap_dev(dev, mac_address=None):
"""Create a tap with name dev and MAC address mac_address on the
operating system.
:param dev: The name of the tap device to create
:type dev: String
:param mac_address: The MAC address of the device, format xx:xx:xx:xx:xx:xx
:type mac_address: String
"""
try:
# First, try with 'ip'
utils.execute(['ip', 'tuntap', 'add', dev, 'mode', 'tap'],
run_as_root=True, check_exit_code=[0, 2, 254])
except Exception:
try:
# Second option: tunctl
utils.execute(['tunctl', '-b', '-t', dev], run_as_root=True)
except Exception:
LOG.exception('Error while creating tap device {0}'.format(dev))
raise
if mac_address:
utils.execute(['ip', 'link', 'set', dev, 'address', mac_address],
run_as_root=True, check_exit_code=[0, 2, 254])
utils.execute(['ip', 'link', 'set', dev, 'up'], run_as_root=True,
check_exit_code=[0, 2, 254])
def delete_tap_device(dev):
"""Delete a tap with name dev on the operating system.
:param dev: The name of the tap device to delete
:type dev: String
"""
try:
# First, try with 'ip'
utils.execute(['ip', 'tuntap', 'del', 'dev', dev, 'mode', 'tap'],
run_as_root=True, check_exit_code=[0, 2, 254])
except Exception:
try:
# Second option: tunctl
utils.execute(['tunctl', '-d', dev], run_as_root=True)
except Exception:
LOG.exception('Error while deleting tap device {0}'.format(dev))
raise
def packet_raw_data_to_hex(buf):
return binascii.hexlify(str(buf).encode('utf-8', 'ignore')
).decode('utf-8', 'ignore')
class Topology(object):
"""Create and contain all the topology information. This includes routers,
subnets, and ports.
"""
def __init__(self, neutron, nb_api):
"""Create a network. That's our playing field."""
self._is_closed = False
self.neutron = neutron
self.nb_api = nb_api
self.external_network = objects.ExternalNetworkTestObj(neutron, nb_api)
self.exist_external_net = False
self.subnets = []
self.routers = []
self.networks = []
self.create_network()
# Because it's hard to get the default security group in this
# context, we create a fake one here to act like the default security
# group when creating a port with no security group specified.
self.fake_default_security_group = \
self._create_fake_default_security_group()
def _create_fake_default_security_group(self):
security_group = objects.SecGroupTestObj(self.neutron, self.nb_api)
security_group_id = security_group.create(
secgroup={'name': 'fakedefault'})
ingress_rule_info = {'ethertype': 'IPv4',
'direction': 'ingress',
'remote_group_id': security_group_id}
security_group.rule_create(secrule=ingress_rule_info)
return security_group
def delete(self):
"""Delete this topology. Also deletes all contained routers, subnets
and ports.
"""
for router in self.routers:
router.delete()
self.routers = []
for subnet in self.subnets:
subnet.delete()
self.subnets = []
for network in self.networks:
network.close()
if not self.exist_external_net:
self.external_network.close()
self.fake_default_security_group.close()
def close(self):
if not self._is_closed:
self._is_closed = True
self.delete()
def create_network(self):
network = objects.NetworkTestObj(self.neutron, self.nb_api)
self.networks.append(network)
network.create()
return network
def get_networks(self):
return self.networks
def create_subnet(self, network=None, cidr=None, enable_dhcp=True,
allocation_pool=()):
"""Create a subnet in this topology, with the given subnet address
range.
:param cidr: The subnet's address range, in form <IP>/<mask len>.
If it is None, the cidr will be allocated from default
subnetpool.
:type cidr: String
:param enable_dhcp: Whether to enable dhcp for this subnet.
:type cidr: Boolean
:param allocation_pool: Optional, allocation range for DHCP
:type allocation_pool: Tuple of 2 addresses (start, end)
"""
if not network:
network = self.networks[0]
subnet_id = len(self.subnets)
subnet = Subnet(self, network, subnet_id, cidr, enable_dhcp,
allocation_pool)
self.subnets.append(subnet)
return subnet
def create_router(self, subnet_ids):
"""Create a router in this topology, connected to the given subnets.
:param subnet_ids: List of subnet ids to which the router is connected
:type subnet_ids: List
"""
router_id = len(self.routers)
router = Router(self, router_id, subnet_ids)
self.routers.append(router)
return router
def create_external_network(self, router_ids):
"""Create external network in this topology, and use it as external
gateway to given routers.
"""
external_net = objects.find_first_network(self.neutron,
{'router:external': True})
if external_net:
self.exist_external_net = True
external_net_id = external_net['id']
else:
external_net_id = self.external_network.create()
for r in router_ids:
router = self.routers[r]
router.router.set_gateway(external_net_id)
return external_net_id
class Subnet(object):
"""Represent a single subnet."""
def __init__(self, topology, network, subnet_id, cidr, enable_dhcp,
allocation_pool):
"""Create the subnet under the given topology, with the given ID, and
the given address range.
:param topology: The topology to which the subnet belongs
:type topology: Topology
:param network: The network to which the subnet belongs
:type network: NetworkTestObj
:param subnet_id: The subnet's ID in the topology. Created by topology
:type subnet_id: Number (Opaque)
:param cidr: The address range for this subnet. Format IP/MaskLen.
If it is None, the cidr will be allocated from
default subnetpool.
:type cidr: String
:param enable_dhcp: Whether to enable dhcp for this subnet.
:type cidr: Boolean
:param allocation_pool: Allocation range for DHCP
:type allocation_pool: Tuple of (start, end) or empty tuple for
implicit range.
"""
self.topology = topology
self.subnet_id = subnet_id
self.ports = []
self.network = network
self.subnet = objects.SubnetTestObj(
self.topology.neutron,
self.topology.nb_api,
self.network.network_id
)
if cidr:
ip_version = self._get_ip_version(cidr)
subnet = {
'cidr': cidr,
'enable_dhcp': enable_dhcp,
'ip_version': ip_version,
'network_id': self.network.network_id
}
if allocation_pool:
start, end = allocation_pool
subnet['allocation_pools'] = [
{
'start': start,
'end': end,
},
]
self.subnet.create(subnet=subnet)
else:
self.subnet.create()
def update(self, updated_parameters):
self.subnet.update(updated_parameters)
def delete(self):
"""Delete this subnet, and all attached ports."""
for port in self.ports:
port.delete()
self.ports = []
self.subnet.close()
def create_port(self, security_groups=None):
"""Create a port attached to this subnet.
:param security_groups: The security groups that this port is
associating with
"""
port_id = len(self.ports)
security_groups_used = security_groups
if security_groups_used is None:
security_groups_used = \
[self.topology.fake_default_security_group.secgroup_id]
port = Port(self,
port_id=port_id,
security_groups=security_groups_used)
self.ports.append(port)
return port
def _get_ip_version(self, cidr):
"""
Calculates the IP version from the CIDR, and returns it.
Raises AddrFormatError if the CIDR is not correctly formatted
:param cidr: The address range for this subnet. Format IP/MaskLen
"""
network = netaddr.IPNetwork(cidr)
ip_version = network.ip.version
return ip_version
class Port(object):
"""Represent a single port. Also contains access to the underlying tap
device
"""
def __init__(self, subnet, port_id, security_groups=None):
"""Create a single port in the given subnet, with the given port_id
:param subnet: The subnet on which this port is created
:type subnet: Subnet
:param port_id: The ID of this port. Created internally by subnet
:type port_id: Number (Opaque)
"""
self.subnet = subnet
self.port_id = port_id
network_id = self.subnet.network.network_id
self.port = objects.PortTestObj(
self.subnet.topology.neutron,
self.subnet.topology.nb_api,
network_id,
)
parameters = {
'admin_state_up': True,
'fixed_ips': [{
'subnet_id': self.subnet.subnet.subnet_id,
}],
'network_id': network_id,
'binding:host_id': socket.gethostname(),
}
if security_groups is not None:
parameters["security_groups"] = security_groups
self.port.create(parameters)
self.tap = LogicalPortTap(self.port)
def update(self, updated_parameters):
self.port.update(updated_parameters)
def delete(self):
"""Delete this port. Delete the underlying tap device."""
self.tap.delete()
self.port.close()
def unbind(self):
"""
Unbind this port. Delete the underlying tap device, and updated
Neutron's binding profile
"""
self.tap.delete()
self.update({'binding:host_id': ''})
@property
def name(self):
"""Return the name of this port, i.e. the name of the underlying tap
device.
"""
return self.port.get_logical_port().id
class LogicalPortTap(object):
"""Represent a tap device on the operating system."""
def __init__(self, port):
"""Create a tap device represented by the given port.
:param port: The configuration info of this tap device
:type port: Port
"""
self.port = port
self.integration_bridge = cfg.CONF.df.integration_bridge
self.lport = self.port.get_logical_port()
self.tap = self._create_tap_device()
self.is_blocking = True
self._is_deleted = False
def _create_tap_device(self):
flags = pytun.IFF_TAP | pytun.IFF_NO_PI
name = self._get_tap_interface_name()
mac = self.lport.mac
mac.dialect = netaddr.mac_unix_expanded
create_tap_dev(name, str(mac))
tap = pytun.TunTapDevice(flags=flags, name=name)
self._connect_tap_device_to_vswitch(self.integration_bridge, tap.name)
tap.up()
return tap
def _get_tap_interface_name(self):
lport_name = self.lport.id
lport_name_prefix = lport_name[:11]
return 'tap{}'.format(lport_name_prefix)
def _connect_tap_device_to_vswitch(self, vswitch_name, tap_name):
"""Connect the tap device to the given vswitch, and add it to the
ovsdb.
:param vswitch_name: The name of the vswitch to connect the device
:type vswitch_name: String
:param tap_name: The name of the device to connect
:type tap_name: String
"""
full_args = ['ovs-vsctl', 'add-port', vswitch_name, tap_name]
utils.execute(full_args, run_as_root=True, process_input=None)
full_args = ['ovs-vsctl', 'set', 'interface', tap_name,
'external_ids:iface-id={}'.format(self.lport.id)]
utils.execute(full_args, run_as_root=True, process_input=None)
def _disconnect_tap_device_to_vswitch(self, vswitch_name, tap_name):
full_args = ['ovs-vsctl', 'del-port', vswitch_name, tap_name]
utils.execute(full_args, run_as_root=True, process_input=None)
def close(self):
self.tap.close()
def delete(self):
if self._is_deleted:
return
self._is_deleted = True
self._disconnect_tap_device_to_vswitch(self.integration_bridge,
self.tap.name)
LOG.info('Closing tap interface {} ({})'.format(
self.tap.name,
self.tap.fileno()))
self.tap.close()
delete_tap_device(self.tap.name)
def send(self, buf):
"""Send a packet out via the tap device.
:param buf: Raw packet data to send
:type buf: String or bytes to write
"""
LOG.info('send: via {}: {}'.format(
self.tap.name,
packet_raw_data_to_hex(buf)))
if isinstance(buf, bytearray):
buf = bytes(buf)
elif isinstance(buf, six.string_types):
buf = buf.encode('utf-8', 'ignore')
if self.is_blocking:
# Takes string and read-only bytes-like objects
return self.tap.write(buf)
else:
fd = self.tap.fileno()
# python3: os.write doesn't take strings
return os.write(fd, buf)
def read(self):
"""Read data from the tap device. This method may block if no data is
ready (i.e. no packet in buffer).
Return the read buffer, which is a String (encoded).
"""
if self.is_blocking:
buf = self.tap.read(self.tap.mtu)
else:
fd = self.tap.fileno()
rs, ws, xs = select.select((self.tap,), (), ())
buf = os.read(fd, self.tap.mtu)
LOG.info('receive: via {}: {}'.format(
self.tap.name,
packet_raw_data_to_hex(buf)))
return buf
def set_blocking(self, is_blocking):
"""Set the device to be blocking or non-blocking.
:param is_blocking: Set the blocking state to is_blocking
:type is_blocking: Boolean
"""
tap = self.tap
fd = tap.fileno()
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
if not is_blocking:
flags |= os.O_NONBLOCK
else:
flags &= ~os.O_NONBLOCK
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
self.is_blocking = is_blocking
class Router(object):
"""Represent a router in the topology."""
def __init__(self, topology, router_id, subnet_ids):
"""Create a router in the topology. Add router interfaces for each
subnet.
:param topology: The topology to which the router belongs
:type topology: Topology
:param router_id: The ID of the router. Created in Topology.
:type router_id: Number (opaque)
:param subnet_ids: List of subnets to which the router is connected
:type subnet_ids: List
"""
self.topology = topology
self.router_id = router_id
self.subnet_ids = subnet_ids
self.router = objects.RouterTestObj(
self.topology.neutron,
self.topology.nb_api,
)
self.router.create(router={
'admin_state_up': True
})
self.router_interfaces = {}
for subnet_id in self.subnet_ids:
subnet = self.topology.subnets[subnet_id]
subnet_uuid = subnet.subnet.subnet_id
router_interface = self.router.add_interface(subnet_id=subnet_uuid)
self.router_interfaces[subnet_id] = router_interface
def delete(self):
"""Delete this router."""
self.router.close()
class TimeoutException(Exception):
def __init__(self):
super(TimeoutException, self).__init__('Timeout')
class Policy(object):
"""Represent a policy, i.e. the expected packets on each port in the
topology, and the actions to take in each case.
"""
def __init__(self, initial_actions, port_policies, unknown_port_action):
"""Create a policy.
:param initial_actions: Take these actions when policy is started
:type initial_actions: List of Action
:param port_policies: The policy for each port in the topology
:type port_policies: dict (subnet_id, port_id) -> PortPolicy
:param unknown_port_action: Take this action for packets on ports not
in port_policies
:type unknown_port_action: Action
"""
self.initial_actions = initial_actions
self.port_policies = port_policies
self.unknown_port_action = unknown_port_action
self.threads = []
self.topology = None # Set on start
self.exceptions = collections.deque()
def handle_packet(self, port_thread, buf):
"""Event handler for a packet received on a port. Test the received
packet against the policy.
:param port_thread: Receiving port
:type port_thread: PortThread
:param buf: Packet data
:type buf: String (encoded)
"""
port = port_thread.port
port_id = port.port_id
subnet = port.subnet
subnet_id = subnet.subnet_id
try:
port_policy = self.port_policies[(subnet_id, port_id)]
try:
port_policy.handle_packet(self, port_thread, buf)
except Exception as e:
self.add_exception(e)
except KeyError:
try:
self.unknown_port_action(self, None, port_thread, buf)
except Exception as e:
self.add_exception(e)
def start(self, topology):
"""Start the policy on the given topology. Start threads listening on
the ports. Execute the initial actions.
:param topology: The topology on which to run the policy
:type topology: Topology
"""
if self.topology:
raise Exception('Policy already started')
self.topology = topology
# Start a thread for each port, listening on the LogicalPortTap
for subnet in topology.subnets:
for port in subnet.ports:
thread = PortThread(self.handle_packet, port)
thread.start()
self.threads.append(thread)
# Call the initial_actions
for action in self.initial_actions:
action(self, None, None, None)
def wait(self, timeout):
"""Wait for all the threads listening on devices to finish. Threads are
generally stopped via actions, and this command waits for the
simulation to end.
:param timeout: After this many seconds, throw an exception
:type timeout: Number
"""
exception = TimeoutException()
if timeout is not None:
entry_time = time.time()
for thread in self.threads:
thread.wait(timeout, exception)
if timeout is not None:
timeout -= time.time() - entry_time
if timeout <= 0:
raise exception
def stop(self):
"""Stop all threads. Prepare for a new simulation."""
for thread in self.threads:
thread.stop()
self.topology = None
def close(self):
if self.topology:
self.stop()
def add_exception(self, exception):
"""Exception handler. Record this exception to be read later by the
caller
:param exception: The exception to record
:type exception: Exception
"""
LOG.exception('Adding exception:')
self.exceptions.append(exception)
self.stop()
class PortPolicy(object):
"""A policy for a specific port. The rules to apply for an incoming packet,
and the relevant actions to take
"""
def __init__(self, rules, default_action):
"""Create a policy for a port.
:param rules: The rules against which to test incoming packets
:type rules: List of PortPolicyRule
:param default_action: The action to take for a packet not matching any
rules.
:type default_action: Action
"""
self.rules = rules
self.default_action = default_action
def handle_packet(self, policy, port_thread, buf):
"""Packet handler. Run the packet through the rules. Apply the relevant
actions.
:param port_thread: Receiving port
:type port_thread: PortThread
:param buf: Packet data
:type buf: String (encoded)
"""
for rule in self.rules:
if rule.apply_rule(policy, port_thread, buf):
return
self.default_action(policy, None, port_thread, buf)
class PortPolicyRule(object):
"""Represent a single policy rule. i.e. packet match parameters, and the
actions to take if the packet matches.
"""
def __init__(self, packet_filter, actions):
"""Create the rule.
:param packet_filter: The packet match parametrer
:type packet_filter: Filter
:param actions: The actions to take if the packet matches
:type actions: List of Action
"""
self.packet_filter = packet_filter
self.actions = actions
self.disabled = False
def match_packet(self, buf):
"""Check if the given packet matches this rule
:param buf: Raw packet data to send
:type buf: String (decoded)
"""
return self.packet_filter(buf)
def apply_rule(self, policy, port_thread, buf):
"""Check if the given packet matches this rule, and execute the
relevant actions if it does.
:param policy: The currently running policy
:type policy: Policy
:param port_thread: Receiving port
:type port_thread: PortThread
:param buf: Raw packet data to send
:type buf: String (decoded)
"""
if self.disabled:
return False
if not self.match_packet(buf):
return False
for action in self.actions:
action(policy, self, port_thread, buf)
return True
class Filter(object):
"""Base class of packet filters, i.e. match parameters."""
def __call__(self, buf):
"""Test if the packet matches this filter. Return True if it does, and
False otherwise.
:param buf: Packet data
:type buf: String (encoded)
"""
raise Exception('Filter not implemented')
class ExactMatchFilter(Filter):
def __init__(self, fixture):
self._fixture = fixture
def __call__(self, buf):
return self._fixture == buf
class OsKenIPv4Filter(object):
"""Use os_ken to parse the packet and test if it's IPv4."""
def __call__(self, buf):
pkt = packet.Packet(buf)
return (pkt.get_protocol(ipv4.ipv4) is not None)
class OsKenIPv6Filter(object):
"""Use os_ken to parse the packet and test if it's IPv6."""
def __call__(self, buf):
pkt = packet.Packet(buf)
return (pkt.get_protocol(ipv6.ipv6) is not None)
class OsKenFilterIcmpv6ProtocolType(object):
"""
Use os_ken to parse the object and see if it from the requested icmpv6 type
"""
type_ = icmpv6.icmpv6
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_protocol = pkt.get_protocol(icmpv6.icmpv6)
if not pkt_protocol:
return False
return pkt_protocol.type_ == self.type_
class OsKenNeighborSolicitationFilter(OsKenFilterIcmpv6ProtocolType):
"""Use os_ken to parse the packet and test if it's a Neighbor
Solicitaion
"""
type_ = icmpv6.ND_NEIGHBOR_SOLICIT
class OsKenNeighborAdvertisementFilter(OsKenFilterIcmpv6ProtocolType):
"""Use os_ken to parse the packet and test if it's a Neighbor
Advertisement
"""
type_ = icmpv6.ND_NEIGHBOR_ADVERT
class OsKenRouterSolicitationFilter(OsKenFilterIcmpv6ProtocolType):
"""Use os_ken to parse the packet and test if it's a Router Solicitation"""
type_ = icmpv6.ND_ROUTER_SOLICIT
class OsKenIpv6MulticastFilter(OsKenFilterIcmpv6ProtocolType):
"""Use os_ken to parse the object and see if it is a multicast request"""
type_ = icmpv6.MLDV2_LISTENER_REPORT
class OsKenARPRequestFilter(object):
"""Use os_ken to parse the packet and test if it's an ARP request."""
def __init__(self, arp_tpa=None):
self.arp_tpa = str(arp_tpa) if arp_tpa else None
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_arp_protocol = pkt.get_protocol(arp.arp)
if (not pkt_arp_protocol) or (
pkt_arp_protocol.opcode != arp.ARP_REQUEST):
return False
if self.arp_tpa is not None:
return pkt_arp_protocol.dst_ip == self.arp_tpa
return True
class OsKenARPReplyFilter(object):
"""Use os_ken to parse the packet and test if it's an ARP reply."""
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_arp_protocol = pkt.get_protocol(arp.arp)
if not pkt_arp_protocol:
return False
return pkt_arp_protocol.opcode == 2
class OsKenARPGratuitousFilter(object):
"""Use os_ken to parse the packet and test if it's a gratuitous ARP."""
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_arp_protocol = pkt.get_protocol(arp.arp)
if not pkt_arp_protocol:
return False
return pkt_arp_protocol.src_ip == pkt_arp_protocol.dst_ip
# Taken from the DHCP app
def _get_dhcp_message_type_opt(dhcp_packet):
for opt in dhcp_packet.options.option_list:
if opt.tag == dhcp.DHCP_MESSAGE_TYPE_OPT:
return ord(opt.value)
class OsKenDHCPFilter(object):
"""Use os_ken to parse the packet and test if it's a DHCP Ack"""
def __call__(self, buf):
pkt = packet.Packet(buf)
return (pkt.get_protocol(dhcp.dhcp) is not None)
class OsKenDHCPPacketTypeFilter(object):
"""Use os_ken to parse the packet and test if it's a DHCP Ack"""
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_dhcp_protocol = pkt.get_protocol(dhcp.dhcp)
if not pkt_dhcp_protocol:
return False
dhcp_type = _get_dhcp_message_type_opt(pkt_dhcp_protocol)
return dhcp_type == self.get_dhcp_packet_type()
def get_dhcp_packet_type(self):
raise Exception('DHCP packet type filter not fully implemented')
class OsKenDHCPOfferFilter(OsKenDHCPPacketTypeFilter):
def get_dhcp_packet_type(self):
return dhcp.DHCP_OFFER
class OsKenDHCPAckFilter(OsKenDHCPPacketTypeFilter):
def get_dhcp_packet_type(self):
return dhcp.DHCP_ACK
class OsKenICMPFilter(object):
def __init__(self, ethertype=n_const.IPv4):
super(OsKenICMPFilter, self).__init__()
self.ethertype = ethertype
def __call__(self, buf):
pkt = packet.Packet(buf)
if self.ethertype == n_const.IPv4:
pkt_icmp_protocol = pkt.get_protocol(icmp.icmp)
else:
pkt_icmp_protocol = pkt.get_protocol(icmpv6.icmpv6)
if not pkt_icmp_protocol:
return False
return self.filter_icmp(pkt, pkt_icmp_protocol)
def filter_icmp(self, pkt, icmp_prot):
return True
def is_same_icmp(self, icmp1, icmp2):
if icmp1.data.id != icmp2.data.id:
return False
if icmp1.data.seq != icmp2.data.seq:
return False
if icmp1.data.data != icmp2.data.data:
return False
return True
class OsKenICMPPingFilter(OsKenICMPFilter):
"""
A filter to detect ICMP echo request messages.
:param get_ping: Return an object contained the original echo request
:type get_ping: Callable with no arguments.
"""
def __init__(self, get_ping=None, ethertype=n_const.IPv4):
super(OsKenICMPPingFilter, self).__init__()
self.get_ping = get_ping
self.ethertype = ethertype
def filter_icmp(self, pkt, proto):
if self.ethertype == n_const.IPv4:
icmp_req = icmp.ICMP_ECHO_REQUEST
proto_type = proto.type
else:
icmp_req = icmpv6.ICMPV6_ECHO_REQUEST
proto_type = proto.type_
if proto_type != icmp_req:
return False
result = True
if self.get_ping is not None:
ping = self.get_ping()
result = super(OsKenICMPPingFilter, self).is_same_icmp(proto, ping)
return result
class OsKenICMPPongFilter(OsKenICMPFilter):
"""
A filter to detect ICMP echo reply messages.
:param get_ping: Return an object contained the original echo request
:type get_ping: Callable with no arguments.
"""
def __init__(self, get_ping, ethertype=n_const.IPv4):
super(OsKenICMPPongFilter, self).__init__()
self.get_ping = get_ping
self.ethertype = ethertype
def filter_icmp(self, pkt, icmp_prot):
if self.ethertype == n_const.IPv4:
icmp_req = icmp.ICMP_ECHO_REPLY
proto_type = icmp_prot.type
else:
icmp_req = icmpv6.ICMPV6_ECHO_REPLY
proto_type = icmp_prot.type_
if proto_type != icmp_req:
return False
ping = self.get_ping()
return super(OsKenICMPPongFilter, self).is_same_icmp(icmp_prot, ping)
class OsKenICMPTimeExceedFilter(OsKenICMPFilter):
"""
A filter to detect ICMP time exceed messages.
:param get_ip: Return an object contained the original IP header
:type get_ip: Callable with no arguments.
"""
def __init__(self, get_ip):
super(OsKenICMPTimeExceedFilter, self).__init__()
self.get_ip = get_ip
def filter_icmp(self, pkt, icmp_prot):
if icmp_prot.type != icmp.ICMP_TIME_EXCEEDED:
return False
ip_pkt = self.get_ip()
embedded_ip_pkt, c, p = ipv4.ipv4.parser(icmp_prot.data.data)
if ip_pkt.src != embedded_ip_pkt.src:
return False
if ip_pkt.dst != embedded_ip_pkt.dst:
return False
return True
class OsKenICMPUnreachFilter(OsKenICMPFilter):
"""
A filter to detect ICMP unreachable messages.
:param get_ip: Return an object contained the original IP header
:type get_ip: Callable with no arguments.
"""
def __init__(self, get_ip):
super(OsKenICMPUnreachFilter, self).__init__()
self.get_ip = get_ip
def filter_icmp(self, pkt, icmp_prot):
if icmp_prot.type != icmp.ICMP_DEST_UNREACH:
return False
ip_pkt = self.get_ip()
embedded_ip_pkt, c, p = ipv4.ipv4.parser(icmp_prot.data.data)
if ip_pkt.src != embedded_ip_pkt.src:
return False
if ip_pkt.dst != embedded_ip_pkt.dst:
return False
return True
class OsKenVLANTagFilter(object):
"""
A filter that detects a VLAN tagged packet
:param tag: The VLAN tag to detect. None for any
:type tag: Integer values 0-4096, or None
"""
def __init__(self, tag):
self.tag = tag
def __call__(self, buf):
pkt = packet.Packet(buf)
vlan_pkt = pkt.get_protocol(vlan.vlan)
if not vlan_pkt:
return False
if self.tag and self.tag != vlan_pkt.vid:
return False
return True
class AndingFilter(object):
def __init__(self, *filters):
self.filters = filters
def __call__(self, buf):
"""Return false if any filter returns false. Otherwise, return True"""
return all(filter_(buf) for filter_ in self.filters)
class OsKenMplsFilter(object):
def __init__(self, label=None):
self._label = label
def __call__(self, buf):
pkt = packet.Packet(buf)
pkt_mpls = pkt.get_protocol(mpls.mpls)
if pkt_mpls is None:
return False
if self._label is not None and pkt_mpls.label != self._label: