master-thesis/p4app/controller.py

440 lines
15 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2019-02-21 22:38:09 +00:00
import nnpy
import struct
from p4utils.utils.topology import Topology
from p4utils.utils.sswitch_API import SimpleSwitchAPI
from scapy.all import sniff, get_if_list, Ether, get_if_hwaddr, sendp
2019-03-04 17:08:10 +00:00
from scapy.all import IP, Raw, IPv6, TCP, TCP_client, Ether
from scapy.all import sniff
2019-03-04 17:16:18 +00:00
from scapy.all import Packet, BitField, IntEnumField, ShortField, XShortEnumField, ShortEnumField
2019-03-05 19:15:55 +00:00
from scapy.all import ICMPv6ND_NS, ICMPv6ND_RS, ICMPv6NDOptSrcLLAddr, ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr
2019-03-05 21:04:04 +00:00
from scapy.all import ICMPv6EchoRequest, ICMPv6EchoReply
2019-03-04 17:05:56 +00:00
from scapy.data import ETHER_TYPES
2019-02-21 22:38:09 +00:00
import sys
import re
import logging
import argparse
2019-02-23 14:13:47 +00:00
import subprocess
import ipaddress
2019-02-21 22:38:09 +00:00
logging.basicConfig()
log = logging.getLogger("main")
2019-03-04 16:50:38 +00:00
cpu_fields = {
1: 'ICMP6_NS',
2: 'ICMP6_GENERAL',
3: 'DEBUG'
}
class CpuHeader(Packet):
name = 'CpuPacket'
fields_desc = [
2019-03-04 17:16:18 +00:00
ShortEnumField('task', 1, cpu_fields ),
2019-03-04 17:05:56 +00:00
ShortField('ingress_port', 0),
XShortEnumField("type", 0x9000, ETHER_TYPES)
]
2019-02-21 22:38:09 +00:00
class L2Controller(object):
def __init__(self, sw_name):
2019-02-23 13:36:19 +00:00
# Command line mapping
self.modes = ['base', 'router', "range_router" ]
2019-02-23 17:58:04 +00:00
2019-03-04 16:50:38 +00:00
# Reverse maps the cpu header
self.task = dict(reversed(item) for item in cpu_fields.items())
self.info={}
2019-03-04 14:30:38 +00:00
self.info['ndp_multicast'] = ipaddress.ip_network("ff02::1:ff00:0/104")
2019-03-05 15:59:27 +00:00
self.info['mac_address'] = "00:00:0a:00:00:42"
2019-03-04 14:32:27 +00:00
self.info['v6_mask'] = 64
2019-03-04 14:30:38 +00:00
self.info['v6_base'] = ipaddress.ip_network("2001:db8::/32")
self.info['v6_gen'] = self.info['v6_base'].subnets(new_prefix=self.info['v6_mask'])
2019-03-04 14:32:27 +00:00
self.info['v4_mask'] = 24
2019-03-04 14:30:38 +00:00
self.info['v4_base'] = ipaddress.ip_network("10.0.0.0/8")
self.info['v4_gen'] = self.info['v4_base'].subnets(new_prefix=self.info['v4_mask'])
self.info['v4_nat64_base'] = ipaddress.ip_network("10.1.0.0/16")
self.info['v4_nat64_map'] = self.info['v4_nat64_base'].subnets(new_prefix=self.info['v4_mask'])
2019-03-04 14:30:38 +00:00
2019-03-04 18:06:09 +00:00
self.info['switch_suffix'] = 0x42
self.info['nat64_prefix'] = ipaddress.ip_network("64:ff9b::/96")
self.v6_routes = {}
self.v6_routes[None] = []
self.v6_routes['base'] = []
2019-03-04 14:30:38 +00:00
for port in range(1,3):
net = self.info['v6_gen'].next()
self.v6_routes['base'].append({
"net": net,
"port": port}
)
2019-02-23 17:58:04 +00:00
self.v6_routes['router'] = self.v6_routes['base']
self.v6_routes['range_router'] = self.v6_routes['base']
2019-02-23 13:36:19 +00:00
self.v4_routes = {}
self.v4_routes[None] = []
self.v4_routes['base'] = []
2019-03-04 14:30:38 +00:00
for port in range(3,5):
net = self.info['v4_gen'].next()
self.v4_routes['base'].append({
"net": net,
"port": port}
)
2019-02-23 17:58:04 +00:00
self.v4_routes['router'] = self.v4_routes['base']
self.v4_routes['range_router'] = self.v4_routes['base']
2019-02-23 17:58:04 +00:00
self.v6_addresses = {}
self.v6_addresses[None] = []
for mode in self.modes:
self.v6_addresses[mode] = [ net['net'][self.info['switch_suffix']] for net in self.v6_routes[mode] ]
2019-02-23 17:58:04 +00:00
self.v4_addresses = {}
self.v4_addresses[None] = []
for mode in self.modes:
self.v4_addresses[mode] = [ net['net'][self.info['switch_suffix']] for net in self.v4_routes[mode] ]
self.nat64_map = {}
# init default
for mode in self.modes:
self.nat64_map[mode] = []
2019-03-14 15:59:55 +00:00
# specific settings -- only need the address (=offset), no mask
for mode in ["range_router"]:
for net in self.v6_routes[mode]:
2019-03-14 16:03:56 +00:00
v6_net = net['net']
v4_net = self.info['v4_nat64_map'].next()
self.nat64_map[mode].append({
"v6_network": v6_net,
"v4_network": v4_net,
2019-03-14 16:03:56 +00:00
"nat64_prefix": self.info['nat64_prefix']
})
2019-02-23 13:36:19 +00:00
self.init_boilerplate(sw_name)
def gen_ndp_multicast_addr(self, addr):
""" append the 24 bit of the address to the multicast address"""
2019-03-04 14:30:38 +00:00
last_24 = int(addr) & 0xffffff
addr = self.info['ndp_multicast'][last_24]
return addr
def init_ndp(self):
""" initialise neighbor discovery protocol"""
# https://en.wikipedia.org/wiki/Solicited-node_multicast_address
ndp_prefix = "ff02::1:ff00:0/104"
all_ports = range(1,5)
# create multicast nodes
for rid in range(1,5):
ports = [ x for x in all_ports if not x == rid ]
n_handle = self.controller.mc_node_create(rid, ports)
log.debug("Creating MC node rid={} ports={} handle={}".format(rid, ports, n_handle))
g_handle = self.controller.mc_mgrp_create(rid)
log.debug("Creating MC group mgrp={} handle={} && associating afterwards".format(rid, g_handle))
self.controller.mc_node_associate(g_handle, n_handle)
self.controller.table_clear("ndp")
for port in all_ports:
self.controller.table_add("ndp", "multicast_pkg", [ndp_prefix, str(port)], [str(port)])
2019-02-28 09:56:22 +00:00
# Special rule for switch entries
self.controller.table_add("ndp_answer", "icmp6_neighbor_solicitation", ["ff02::1:ff00:42", "135"], ["2001:db8:61::42"])
2019-02-21 22:38:09 +00:00
def init_boilerplate(self, sw_name):
self.topo = Topology(db="topology.db")
self.sw_name = sw_name
self.thrift_port = self.topo.get_thrift_port(sw_name)
self.cpu_port = self.topo.get_cpu_port_index(self.sw_name)
self.controller = SimpleSwitchAPI(self.thrift_port)
self.intf = str(self.topo.get_cpu_port_intf(self.sw_name).replace("eth0", "eth1"))
self.controller.reset_state()
if self.cpu_port:
self.controller.mirroring_add(100, self.cpu_port)
2019-03-03 21:37:58 +00:00
# self.init_ndp()
def config(self):
self.fill_tables()
self.config_hosts()
def listen_to_icmp6_multicast(self):
"""Only needed for debugging"""
2019-03-04 14:30:38 +00:00
net = self.info['ndp_multicast']
self.controller.table_add("v6_networks", "controller_debug", [str(net)])
2019-02-23 17:58:04 +00:00
2019-02-21 22:38:09 +00:00
def fill_tables(self):
self.controller.table_clear("v6_networks")
for v6route in self.v6_routes[self.mode]:
2019-03-04 14:30:38 +00:00
self.controller.table_add("v6_networks", "set_egress_port", [str(v6route['net'])], [str(v6route['port'])])
2019-03-05 15:19:43 +00:00
if self.args.multicast_to_controller:
self.listen_to_icmp6_multicast()
self.controller.table_clear("v4_routing")
for v4route in self.v4_routes[self.mode]:
2019-03-04 14:30:38 +00:00
self.controller.table_add("v4_networks", "set_egress_port", [str(v4route['net'])], [str(v4route['port'])])
2019-02-23 17:58:04 +00:00
self.controller.table_clear("v6_addresses")
for v6addr in self.v6_addresses[self.mode]:
log.debug("Adding v6 address: {}".format(v6addr))
2019-03-04 14:30:38 +00:00
icmp6_addr = self.gen_ndp_multicast_addr(v6addr)
2019-03-05 21:31:05 +00:00
another_addr = v6addr +1
another_addr_ns = self.gen_ndp_multicast_addr(another_addr)
2019-02-23 17:58:04 +00:00
self.controller.table_add("v6_addresses", "controller_reply", [str(v6addr)], [str(self.task['ICMP6_GENERAL'])])
self.controller.table_add("v6_addresses", "controller_reply", [str(icmp6_addr)], [str(self.task['ICMP6_NS'])])
2019-02-23 17:58:04 +00:00
# Experimental: controller does NDP, switch does ICMP6 echo reply
self.controller.table_add("v6_addresses", "controller_reply", [str(another_addr_ns)], [str(self.task['ICMP6_NS'])])
2019-03-05 21:31:05 +00:00
self.controller.table_add("v6_addresses", "icmp6_echo_reply", [str(another_addr)])
for nat64map in self.nat64_map[self.mode]:
self.static_nat64_mapping(**nat64map)
2019-03-14 13:37:45 +00:00
def static_nat64_mapping(self, nat64_prefix, v6_network, v4_network):
2019-03-14 15:43:57 +00:00
log.info("NAT64 map: {} -> {} -> {}".format(nat64_prefix, v6_network, v4_network))
2019-03-14 16:03:56 +00:00
self.controller.table_add("v6_networks", "nat64_static", [str(nat64_prefix)],
2019-03-14 16:06:39 +00:00
[str(v6_network.network_address),
str(v4_network.network_address),
2019-03-14 16:03:56 +00:00
str(nat64_prefix.network_address)]
)
self.controller.table_add("v4_networks", "nat46_static", [str(v4_network)],
2019-03-14 16:06:39 +00:00
[str(v6_network.network_address),
str(v4_network.network_address),
2019-03-14 16:03:56 +00:00
str(nat64_prefix.network_address)]
)
def config_hosts(self):
""" Assumptions:
- all routes are networks (no /128 v6 or /32 v4
- hosts get the first ip address in the network
"""
2019-02-23 14:13:47 +00:00
for v6route in self.v6_routes[self.mode]:
2019-02-23 14:13:47 +00:00
host = "h{}".format(v6route['port'])
dev = "{}-eth0".format(host)
2019-03-04 14:38:16 +00:00
net = v6route['net']
2019-03-04 14:38:42 +00:00
ipaddr = "{}/{}".format(net[1],net.prefixlen)
2019-03-06 13:00:35 +00:00
router = str(net[self.info['switch_suffix']])
2019-03-06 12:48:02 +00:00
self.config_v6_host(host, str(net), str(ipaddr), dev, router)
2019-03-04 14:34:12 +00:00
for v4route in self.v4_routes[self.mode]:
host = "h{}".format(v4route['port'])
dev = "{}-eth0".format(host)
net = v4route['net']
ipaddr = "{}/{}".format(net[1],net.prefixlen)
router = str(net[self.info['switch_suffix']])
self.config_v4_host(host, str(net), str(ipaddr), dev, router)
2019-03-04 14:34:12 +00:00
@staticmethod
2019-03-06 12:48:02 +00:00
def config_v6_host(host, net, ipaddr, dev, router=None):
log.debug("Config v6 host: {} {}->{} on {}".format(host, net, ipaddr, dev))
2019-03-04 14:34:12 +00:00
subprocess.call(["mx", host, "ip", "addr", "flush", "dev", dev])
for v6dev in [ "lo", "default", "all", dev ]:
subprocess.call(["mx", host, "sysctl", "net.ipv6.conf.{}.disable_ipv6=0".format(v6dev)])
# Set down & up to regain link local address
subprocess.call(["mx", host, "ip", "link", "set", dev, "down"])
subprocess.call(["mx", host, "ip", "link", "set", dev, "up"])
subprocess.call(["mx", host, "ip", "addr", "add", ipaddr, "dev", dev])
2019-02-23 14:13:47 +00:00
if router:
subprocess.call(["mx", host, "ip", "route", "add", "default", "via", router])
@staticmethod
def config_v4_host(host, net, ipaddr, dev, router=None):
log.debug("Config v4 host: {} {}->{} on {}".format(host, net, ipaddr, dev))
subprocess.call(["mx", host, "ip", "addr", "flush", "dev", dev])
subprocess.call(["mx", host, "ip", "addr", "add", ipaddr, "dev", dev])
2019-03-06 12:48:02 +00:00
if router:
subprocess.call(["mx", host, "ip", "route", "add", "default", "via", router])
2019-02-21 22:38:09 +00:00
def debug_print_pkg(self, pkg, msg="INCOMING"):
2019-02-24 14:58:15 +00:00
log.debug("{}: {}".format(msg, pkg.__repr__()))
2019-02-21 22:38:09 +00:00
def debug_format_pkg(self, pkg):
packet = Ether(str(pkg))
if packet.type == 0x800:
ip = pkg.getlayer(IP)
elif packet.type == 0x86dd:
ip = pkg.getlayer(IPv6)
# tcp = pkg.getlayer(TCP)
# raw = pkg.getlayer(Raw)
# return "{}:{} => {}:{}: flags={} seq={} ack={} raw={}".format(
# ip.src, tcp.sport,
# ip.dst, tcp.dport,
# tcp.flags,
# tcp.seq,
# tcp.ack,
# raw)
2019-03-05 21:04:04 +00:00
def handle_icmp6_echo_request(self, pkg):
"""
Sample from the wire:
DEBUG:main:reassambled=<Ether dst=00:00:0a:00:00:42 src=00:00:0a:00:00:01 type=0x86dd |<IPv6 version=6 tc=0 fl=474570 plen=64 nh=ICMPv6 hlim=64 src=2001:db8::1 dst=2001:db8::42 |<ICMPv6EchoRequest type=Echo Request code=0 cksum=0xb76d id=0x16cd seq=0x1 data='\x1f\xe2~\\\x00\x00\x00\x00\xf8\x82\x00\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567' |>>>
"""
log.info("Replying to ICMP packet")
dst_mac = pkg[Ether].src
src_mac = pkg[Ether].dst
dst_addr = pkg[IPv6].src
src_addr = pkg[IPv6].dst
e = Ether(src=src_mac, dst=dst_mac)
i = IPv6(src=src_addr, dst=dst_addr)
i2 = ICMPv6EchoReply(id=pkg[ICMPv6EchoRequest].id,
seq=pkg[ICMPv6EchoRequest].seq,
data=pkg[ICMPv6EchoRequest].data)
i2.cksum = None
2019-03-05 21:06:17 +00:00
answer = e / i / i2
2019-03-05 21:04:04 +00:00
self.send_pkg(answer)
2019-03-05 15:59:27 +00:00
def handle_icmp6_ns(self, pkg):
""" Solicitated NA"""
# Both ways should work
dst_mac = pkg[Ether].src
2019-03-05 16:01:21 +00:00
dst_mac = pkg[ICMPv6NDOptSrcLLAddr].lladdr
2019-03-05 15:59:27 +00:00
src_mac = self.info['mac_address']
dst_addr = pkg[IPv6].src
src_addr = pkg[ICMPv6ND_NS].tgt
e = Ether(src=src_mac, dst=dst_mac)
i = IPv6(src=src_addr, dst=dst_addr)
# S=1 -> solicitated
2019-03-05 19:12:24 +00:00
i2 = ICMPv6ND_NA(S=1, R=0, tgt=src_addr)
2019-03-05 20:49:50 +00:00
# try5: cksum not chksum !
i2.cksum = None
2019-03-05 19:15:55 +00:00
i3 = ICMPv6NDOptDstLLAddr(lladdr=src_mac)
2019-03-05 15:59:27 +00:00
answer = e / i / i2 / i3
2019-03-05 20:22:55 +00:00
2019-03-05 20:37:38 +00:00
# try 4
2019-03-05 20:49:50 +00:00
# for l in [Ether, IPv6, ICMPv6ND_NA, ICMPv6NDOptDstLLAddr]:
# try:
# del answer[l].chksum
# except AttributeError:
# pass
2019-03-05 20:37:38 +00:00
2019-03-05 20:22:55 +00:00
# Let scapy recalc checksum (try3)
2019-03-05 20:37:38 +00:00
# answer = answer.__class__(str(answer))
2019-03-05 20:22:55 +00:00
2019-03-05 16:08:31 +00:00
self.send_pkg(answer)
2019-03-05 15:59:27 +00:00
2019-03-05 16:08:31 +00:00
def send_pkg(self, pkg):
self.debug_print_pkg(pkg, "OUTGOING")
2019-03-05 16:09:17 +00:00
sendp(pkg, iface=self.intf, verbose=False)
2019-03-05 15:59:27 +00:00
2019-02-21 22:38:09 +00:00
def recv_msg_cpu(self, pkg):
packet = Ether(str(pkg))
2019-02-24 14:54:32 +00:00
self.debug_print_pkg(pkg)
if packet.type == 0x0800:
2019-02-21 22:38:09 +00:00
pass
elif packet.type == 0x86dd:
pass
elif packet.type == 0x4242:
2019-03-04 15:38:06 +00:00
cpu_header = CpuHeader(packet.payload)
2019-03-04 17:39:09 +00:00
log.debug("cpu = {}".format(cpu_header.__repr__()))
2019-03-04 16:50:38 +00:00
2019-03-04 17:55:25 +00:00
ether_orig = Ether(src=packet.src, dst=packet.dst, type=cpu_header.type)
orig_packet = ether_orig / IPv6(cpu_header.load)
2019-03-05 15:59:27 +00:00
log.debug("reassambled={}".format(orig_packet.__repr__()))
2019-03-04 17:55:25 +00:00
if cpu_header.task == self.task['DEBUG']:
2019-03-05 15:14:36 +00:00
log.debug("Debug purpose only")
elif cpu_header.task == self.task['ICMP6_NS']:
2019-03-05 16:08:31 +00:00
log.info("Doing neighbor solicitation")
2019-03-05 15:59:27 +00:00
self.handle_icmp6_ns(orig_packet)
elif cpu_header.task == self.task['ICMP6_GENERAL']:
2019-03-05 21:04:04 +00:00
if ICMPv6EchoRequest in orig_packet:
2019-03-05 21:05:26 +00:00
self.handle_icmp6_echo_request(orig_packet)
2019-03-04 17:55:25 +00:00
2019-02-21 22:38:09 +00:00
else:
2019-03-04 13:24:02 +00:00
print("Broken pkg: {}".format(pkg.__repr__()))
2019-02-21 22:38:09 +00:00
return
def run_cpu_port_loop(self):
sniff(iface=self.intf, prn=self.recv_msg_cpu)
def commandline(self):
parser = argparse.ArgumentParser(description='controller++')
parser.add_argument('--mode', help='Select mode / settings to use', choices=self.modes)
2019-02-23 17:25:53 +00:00
parser.add_argument('--debug', help='Enable debug logging', action='store_true')
2019-03-05 16:08:31 +00:00
parser.add_argument('--verbose', help='Enable verbose logging', action='store_true')
2019-03-05 15:19:43 +00:00
parser.add_argument('--multicast-to-controller', help='Send debug multicast to controller', action='store_true')
2019-02-23 17:26:34 +00:00
self.args = parser.parse_args()
2019-02-23 17:27:09 +00:00
self.mode = self.args.mode
2019-03-04 13:57:27 +00:00
self.debug = self.args.debug
2019-02-21 22:38:09 +00:00
if __name__ == "__main__":
import sys
import os
if "DEBUG" in os.environ:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log.info("Booting...")
log.debug("Debug enabled.")
sw_name = "s1"
controller = L2Controller(sw_name)
controller.commandline()
2019-02-23 17:25:53 +00:00
if controller.args.debug:
log.setLevel(logging.DEBUG)
2019-03-05 16:08:31 +00:00
elif controller.args.verbose:
log.setLevel(logging.INFO)
else:
log.setLevel(logging.WARNING)
controller.config()
controller.run_cpu_port_loop()