-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework hw pytest #598
Merged
Merged
Rework hw pytest #598
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import multiprocessing | ||
import sys | ||
import time | ||
from datetime import datetime | ||
from scapy.all import * | ||
|
||
|
||
def is_ipip_pkt(pkt): | ||
return pkt[IPv6].nh == 4 or pkt[IPv6].nh == 41 | ||
|
||
def is_virtsvc_pkt(pkt): | ||
return pkt[IPv6].src in opts.virtsvc or pkt[IPv6].dst in opts.virtsvc | ||
|
||
# Filter for underlay packets not sent by this script (marked by the Traffic Class field) | ||
def is_ul_pkt(pkt): | ||
return IPv6 in pkt and pkt[IPv6].tc == 0 and (is_ipip_pkt(pkt) or is_virtsvc_pkt(pkt)) | ||
|
||
# Sends packets back over the interface | ||
def sender_loop(q): | ||
try: | ||
while True: | ||
iface, pkt = q.get() | ||
# Give receiver time to enter sniff() | ||
time.sleep(0.1) | ||
sendp(pkt, iface=iface, verbose=opts.verbose > 1) | ||
except KeyboardInterrupt: | ||
return | ||
|
||
# Simply receives all underlay packets and passes them to the sender with appropriate changes | ||
def receiver_loop(q, iface, mac): | ||
if opts.verbose: | ||
print(f"Listening on {iface}, mangling packets from {mac}") | ||
while True: | ||
pkts = sniff(iface=iface, count=1, lfilter=is_ul_pkt) | ||
if len(pkts) != 1: | ||
print(f"Sniffing on {iface} interrupted", file=sys.stderr) | ||
exit(1) | ||
pkt = pkts[0] | ||
if opts.verbose: | ||
summary = f"{pkt.sprintf('%Ether.src% -> %Ether.dst% / %IPv6.src% -> %IPv6.dst%')} / {pkt.summary().split(' / ', 2)[2]}" | ||
print(f"{datetime.now()} {iface}: {summary}") | ||
# Mark the sent-out packet as to not be sniffed by the receiver | ||
pkt[IPv6].tc = 0x0C | ||
# For packets originating from PF, change them so they do not pass directly to dpservice | ||
# But are caught by pytest instead | ||
if pkt[Ether].src == mac: | ||
pkt[Ether].type = 0x1337 | ||
q.put((iface, pkt)) | ||
|
||
|
||
class ReflectAction(argparse.Action): | ||
def __call__(self, parser, namespace, values, option_string=None): | ||
for value in values: | ||
spec = value.split(',') | ||
if len(spec) != 2: | ||
raise argparse.ArgumentError(self, f"Invalid IFACE,MAC tuple given: '{value}'") | ||
getattr(namespace, self.dest).append(value) | ||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description="Packet reflector for dpservice pytest suite") | ||
parser.add_argument('-v', '--verbose', action='count', default=0, help="more verbose output (use multiple times)") | ||
parser.add_argument('--virtsvc', action='append', default=[], help="virtual service endpoint(s)") | ||
parser.add_argument('reflect', metavar='IFACE,MAC', default=[], nargs='+', action=ReflectAction, help="interface(s) to listen on and *remote* MAC(s) to mangle") | ||
opts = parser.parse_args() | ||
|
||
q = multiprocessing.Queue() | ||
|
||
for spec in opts.reflect: | ||
iface, mac = spec.split(',') | ||
multiprocessing.Process(target=receiver_loop, args=(q, iface, mac)).start() | ||
|
||
sender_loop(q) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it pure refactoring if I get it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionally this is pure refactoring, but the reason to do this was because I need to encapsulate all
sniff()
calls to intercept the mangled packets with EtherType=1337 and fix them.These calls were the last three instances where a "naked" scapy call was being made.