| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
import argparse |
| 11 |
import os |
| 12 |
import string |
| 13 |
import sys |
| 14 |
import subprocess |
| 15 |
import json |
| 16 |
import struct |
| 17 |
import fcntl |
| 18 |
|
| 19 |
import yaml |
| 20 |
|
| 21 |
|
| 22 |
def get_sudo_uid() -> int: |
| 23 |
uid = os.environ["SUDO_UID"] |
| 24 |
if uid is not None: |
| 25 |
return int(uid) |
| 26 |
|
| 27 |
|
| 28 |
def get_denylist(): |
| 29 |
denylist_file = "/etc/labgrid/helpers.yaml" |
| 30 |
try: |
| 31 |
with open(denylist_file) as stream: |
| 32 |
data = yaml.load(stream, Loader=yaml.SafeLoader) |
| 33 |
except (PermissionError, FileNotFoundError, AttributeError) as e: |
| 34 |
raise Exception(f"No configuration file ({denylist_file}), inaccessable or invalid yaml") from e |
| 35 |
|
| 36 |
denylist = data.get("raw-interface", {}).get("denied-interfaces", []) |
| 37 |
|
| 38 |
if not isinstance(denylist, list): |
| 39 |
raise Exception("No explicit denied-interfaces or not a list, please check your configuration") |
| 40 |
|
| 41 |
denylist.append("lo") |
| 42 |
|
| 43 |
return denylist |
| 44 |
|
| 45 |
def open_tap(name, device="/dev/net/tun"): |
| 46 |
TUNSETIFF = 0x400454CA |
| 47 |
IFF_NO_PI = 0x1000 |
| 48 |
O_RDWR = 0x2 |
| 49 |
IFF_TAP = 0x0002 |
| 50 |
|
| 51 |
flags = IFF_TAP | IFF_NO_PI |
| 52 |
name = name.encode() |
| 53 |
ifr_name = name + b"\x00" * (16 - len(name)) |
| 54 |
ifr = struct.pack("16sH22s", ifr_name, flags, b"\x00" * 22) |
| 55 |
|
| 56 |
fd = os.open(device, O_RDWR) |
| 57 |
fcntl.ioctl(fd, TUNSETIFF, ifr) |
| 58 |
return fd |
| 59 |
|
| 60 |
|
| 61 |
def handle_ns_macvtap(options): |
| 62 |
|
| 63 |
|
| 64 |
output = subprocess.check_output( |
| 65 |
["ip", "-j", "-echo", "link", "add", "link", options.ifname, "type", "macvtap", "mode", "bridge"] |
| 66 |
) |
| 67 |
output = json.loads(output) |
| 68 |
assert len(output) == 1 |
| 69 |
ifindex_new = output[0]["ifindex"] |
| 70 |
ifname_new = output[0]["ifname"] |
| 71 |
assert ifname_new.startswith("macvtap") |
| 72 |
try: |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
subprocess.check_call(["ip", "link", "set", "allmulticast", "on", "dev", ifname_new]) |
| 80 |
|
| 81 |
if options.mac_address: |
| 82 |
subprocess.check_call( |
| 83 |
["ip", "link", "set", "address", options.mac_address, "dev", ifname_new]) |
| 84 |
subprocess.check_call( |
| 85 |
["ip", "link", "set", "dev", ifname_new, "name", "macvtap0", "up", "netns", str(options.pid), "index", str(ifindex_new)] |
| 86 |
) |
| 87 |
except: |
| 88 |
subprocess.check_call( |
| 89 |
["ip", "link", "del", ifname_new] |
| 90 |
) |
| 91 |
raise |
| 92 |
tap_name = f"/dev/tap{ifindex_new}" |
| 93 |
uid = get_sudo_uid() |
| 94 |
os.chown( |
| 95 |
path=tap_name, |
| 96 |
uid=uid, |
| 97 |
gid=-1, |
| 98 |
follow_symlinks=False, |
| 99 |
) |
| 100 |
|
| 101 |
with os.fdopen(open_tap(ifname_new, device=tap_name)) as macvtap_fd: |
| 102 |
os.set_inheritable(macvtap_fd.fileno(), True) |
| 103 |
os.execlp("labgrid-tap-fwd", "labgrid-tap-fwd", str(macvtap_fd.fileno())) |
| 104 |
|
| 105 |
|
| 106 |
def main(program, options): |
| 107 |
if not options.ifname: |
| 108 |
raise ValueError("Empty interface name.") |
| 109 |
if any((c == "/" or c.isspace()) for c in options.ifname): |
| 110 |
raise ValueError(f"Interface name '{options.ifname}' contains invalid characters.") |
| 111 |
if len(options.ifname) > 15: |
| 112 |
raise ValueError(f"Interface name '{options.ifname}' is too long.") |
| 113 |
|
| 114 |
denylist = get_denylist() |
| 115 |
|
| 116 |
if options.ifname in denylist: |
| 117 |
raise ValueError(f"Interface name '{options.ifname}' is denied in denylist.") |
| 118 |
|
| 119 |
programs = ["tcpreplay", "tcpdump", "ip", "ethtool", "ns-macvtap"] |
| 120 |
if program not in programs: |
| 121 |
raise ValueError(f"Invalid program {program} called with wrapper, valid programs are: {programs}") |
| 122 |
|
| 123 |
args = [ |
| 124 |
program, |
| 125 |
] |
| 126 |
|
| 127 |
if program == "tcpreplay": |
| 128 |
args.append(f"--intf1={options.ifname}") |
| 129 |
args.append("-") |
| 130 |
|
| 131 |
elif program == "tcpdump": |
| 132 |
args.append("-n") |
| 133 |
args.append(f"--interface={options.ifname}") |
| 134 |
|
| 135 |
args.append("--packet-buffered") |
| 136 |
|
| 137 |
args.append("--snapshot-length=0") |
| 138 |
args.append("-w") |
| 139 |
args.append("-") |
| 140 |
|
| 141 |
if options.count: |
| 142 |
args.append("-c") |
| 143 |
args.append(str(options.count)) |
| 144 |
|
| 145 |
if options.timeout: |
| 146 |
args = ["timeout", "--signal=INT", "--preserve-status", str(options.timeout)] + args |
| 147 |
|
| 148 |
elif program == "ip": |
| 149 |
args.append("link") |
| 150 |
args.append("set") |
| 151 |
args.append("dev") |
| 152 |
args.append(options.ifname) |
| 153 |
args.append(options.action) |
| 154 |
|
| 155 |
elif program == "ethtool": |
| 156 |
allowed_chars = set(string.ascii_letters + string.digits + "-/:") |
| 157 |
|
| 158 |
if options.subcommand == "change": |
| 159 |
for arg in options.ethtool_change_args: |
| 160 |
if arg.startswith("-") or not allowed_chars.issuperset(arg): |
| 161 |
raise ValueError(f"ethtool --change arg '{arg}' contains invalid characters") |
| 162 |
|
| 163 |
args.append("--change") |
| 164 |
args.append(options.ifname) |
| 165 |
args.extend(options.ethtool_change_args) |
| 166 |
|
| 167 |
elif options.subcommand == "set-eee": |
| 168 |
for arg in options.ethtool_set_eee_args: |
| 169 |
if arg.startswith("-") or not allowed_chars.issuperset(arg): |
| 170 |
raise ValueError(f"ethtool --set-eee arg '{arg}' contains invalid characters") |
| 171 |
|
| 172 |
args.append("--set-eee") |
| 173 |
args.append(options.ifname) |
| 174 |
args.extend(options.ethtool_set_eee_args) |
| 175 |
|
| 176 |
elif options.subcommand == "pause": |
| 177 |
for arg in options.ethtool_pause_args: |
| 178 |
if arg.startswith("-") or not allowed_chars.issuperset(arg): |
| 179 |
raise ValueError(f"ethtool --pause arg '{arg}' contains invalid characters") |
| 180 |
|
| 181 |
args.append("--pause") |
| 182 |
args.append(options.ifname) |
| 183 |
args.extend(options.ethtool_pause_args) |
| 184 |
|
| 185 |
elif program == "ns-macvtap": |
| 186 |
handle_ns_macvtap(options) |
| 187 |
return |
| 188 |
|
| 189 |
try: |
| 190 |
os.execvp(args[0], args) |
| 191 |
except FileNotFoundError as e: |
| 192 |
raise RuntimeError(f"Missing {program} binary") from e |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
parser = argparse.ArgumentParser() |
| 197 |
parser.add_argument("-d", "--debug", action="store_true", default=False, help="enable debug mode") |
| 198 |
subparsers = parser.add_subparsers(dest="program", help="program to run") |
| 199 |
|
| 200 |
|
| 201 |
tcpdump_parser = subparsers.add_parser("tcpdump") |
| 202 |
tcpdump_parser.add_argument("ifname", type=str, help="interface name") |
| 203 |
tcpdump_parser.add_argument("count", type=int, nargs="?", default=None, help="amount of frames to capture while recording") |
| 204 |
tcpdump_parser.add_argument( |
| 205 |
"--timeout", type=int, default=None, help="Amount of time to capture while recording. 0 means capture forever" |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
tcpreplay_parser = subparsers.add_parser("tcpreplay") |
| 210 |
tcpreplay_parser.add_argument("ifname", type=str, help="interface name") |
| 211 |
|
| 212 |
|
| 213 |
ip_parser = subparsers.add_parser("ip") |
| 214 |
ip_parser.add_argument("ifname", type=str, help="interface name") |
| 215 |
ip_parser.add_argument("action", type=str, choices=["up", "down"], help="action, one of {%(choices)s}") |
| 216 |
|
| 217 |
|
| 218 |
ethtool_parser = subparsers.add_parser("ethtool") |
| 219 |
ethtool_subparsers = ethtool_parser.add_subparsers(dest="subcommand") |
| 220 |
|
| 221 |
|
| 222 |
ethtool_change_parser = ethtool_subparsers.add_parser("change") |
| 223 |
ethtool_change_parser.add_argument("ifname", type=str, help="interface name") |
| 224 |
ethtool_change_parser.add_argument( |
| 225 |
"ethtool_change_args", metavar="ARG", nargs=argparse.REMAINDER, help="ethtool --change args" |
| 226 |
) |
| 227 |
|
| 228 |
|
| 229 |
ethtool_change_parser = ethtool_subparsers.add_parser("set-eee") |
| 230 |
ethtool_change_parser.add_argument("ifname", type=str, help="interface name") |
| 231 |
ethtool_change_parser.add_argument( |
| 232 |
"ethtool_set_eee_args", metavar="ARG", nargs=argparse.REMAINDER, help="ethtool --set-eee args" |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
ethtool_change_parser = ethtool_subparsers.add_parser("pause") |
| 237 |
ethtool_change_parser.add_argument("ifname", type=str, help="interface name") |
| 238 |
ethtool_change_parser.add_argument( |
| 239 |
"ethtool_pause_args", metavar="ARG", nargs=argparse.REMAINDER, help="ethtool --pause args" |
| 240 |
) |
| 241 |
|
| 242 |
|
| 243 |
ns_mactap_parser = subparsers.add_parser("ns-macvtap") |
| 244 |
ns_mactap_parser.add_argument("ifname", type=str, help="interface name") |
| 245 |
ns_mactap_parser.add_argument("pid", type=int, help="pid of namespace agent") |
| 246 |
ns_mactap_parser.add_argument("--mac-address", type=str, metavar="ADDRESS", help="Set interface MAC address to ADDRESS") |
| 247 |
|
| 248 |
args = parser.parse_args() |
| 249 |
try: |
| 250 |
main(args.program, args) |
| 251 |
except Exception as e: |
| 252 |
if args.debug: |
| 253 |
import traceback |
| 254 |
|
| 255 |
traceback.print_exc(file=sys.stderr) |
| 256 |
print(f"ERROR: {e}", file=sys.stderr) |
| 257 |
exit(1) |