| """ |
| Utility parsing functions |
| """ |
| |
| import sys |
| import socket |
| try: |
| import scapy.all as scapy |
| except: |
| try: |
| import scapy as scapy |
| except: |
| sys.exit("Need to install scapy for packet parsing") |
| |
| def parse_mac(mac_str): |
| """ |
| Parse a MAC address |
| |
| Parse a MAC address ':' separated string of hex digits to an |
| array of integer values. '00:d0:05:5d:24:00' => [0, 208, 5, 93, 36, 0] |
| @param mac_str The string to convert |
| @return Array of 6 integer values |
| """ |
| return map(lambda val: int(val, 16), mac_str.split(":")) |
| |
| def parse_ip(ip_str): |
| """ |
| Parse an IP address |
| |
| Parse an IP address '.' separated string of decimal digits to an |
| host ordered integer. '172.24.74.77' => |
| @param ip_str The string to convert |
| @return Integer value |
| """ |
| array = map(lambda val: int(val), ip_str.split(".")) |
| val = 0 |
| for a in array: |
| val <<= 8 |
| val += a |
| return val |
| |
| def parse_ipv6(ip_str): |
| """ |
| Parse an IPv6 address |
| |
| Parse a textual IPv6 address and return a 16 byte binary string. |
| """ |
| return socket.inet_pton(socket.AF_INET6, ip_str) |
| |
| def packet_type_classify(ether): |
| try: |
| dot1q = ether[scapy.Dot1Q] |
| except: |
| dot1q = None |
| |
| try: |
| ip = ether[scapy.IP] |
| except: |
| ip = None |
| |
| try: |
| tcp = ether[scapy.TCP] |
| except: |
| tcp = None |
| |
| try: |
| udp = ether[scapy.UDP] |
| except: |
| udp = None |
| |
| try: |
| icmp = ether[scapy.ICMP] |
| except: |
| icmp = None |
| |
| try: |
| arp = ether[scapy.ARP] |
| except: |
| arp = None |
| return (dot1q, ip, tcp, udp, icmp, arp) |
| |
| def packet_to_flow_match(packet): |
| """ |
| Create a flow match that matches packet with the given wildcards |
| |
| @param packet The packet to use as a flow template |
| @return An loxi.of10.match object |
| |
| @todo check min length of packet |
| """ |
| import ofp |
| if ofp.OFP_VERSION == 1: |
| return packet_to_flow_match_v1(packet) |
| elif ofp.OFP_VERSION == 3: |
| return packet_to_flow_match_v3(packet) |
| elif ofp.OFP_VERSION == 4: |
| return packet_to_flow_match_v4(packet) |
| else: |
| raise NotImplementedError() |
| |
| def packet_to_flow_match_v1(packet): |
| """ |
| OpenFlow 1.0 implementation of packet_to_flow_match |
| """ |
| import loxi.of10 as ofp |
| |
| if type(packet) == type(""): |
| ether = scapy.Ether(packet) |
| else: |
| ether = packet |
| |
| # For now, assume ether IP packet and ignore wildcards |
| try: |
| (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether) |
| except: |
| raise ValueError("could not classify packet") |
| |
| match = ofp.match() |
| match.wildcards = ofp.OFPFW_ALL |
| #@todo Check if packet is other than L2 format |
| match.eth_dst = parse_mac(ether.dst) |
| match.wildcards &= ~ofp.OFPFW_DL_DST |
| match.eth_src = parse_mac(ether.src) |
| match.wildcards &= ~ofp.OFPFW_DL_SRC |
| match.eth_type = ether.type |
| match.wildcards &= ~ofp.OFPFW_DL_TYPE |
| |
| if dot1q: |
| match.vlan_vid = dot1q.vlan |
| match.vlan_pcp = dot1q.prio |
| match.eth_type = dot1q.type |
| else: |
| match.vlan_vid = ofp.OFP_VLAN_NONE |
| match.vlan_pcp = 0 |
| match.wildcards &= ~ofp.OFPFW_DL_VLAN |
| match.wildcards &= ~ofp.OFPFW_DL_VLAN_PCP |
| |
| if ip: |
| match.ipv4_src = parse_ip(ip.src) |
| match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK |
| match.ipv4_dst = parse_ip(ip.dst) |
| match.wildcards &= ~ofp.OFPFW_NW_DST_MASK |
| match.ip_dscp = ip.tos |
| match.wildcards &= ~ofp.OFPFW_NW_TOS |
| |
| if tcp: |
| match.ip_proto = 6 |
| match.wildcards &= ~ofp.OFPFW_NW_PROTO |
| elif not tcp and udp: |
| tcp = udp |
| match.ip_proto = 17 |
| match.wildcards &= ~ofp.OFPFW_NW_PROTO |
| |
| if tcp: |
| match.tcp_src = tcp.sport |
| match.wildcards &= ~ofp.OFPFW_TP_SRC |
| match.tcp_dst = tcp.dport |
| match.wildcards &= ~ofp.OFPFW_TP_DST |
| |
| if icmp: |
| match.ip_proto = 1 |
| match.tcp_src = icmp.type |
| match.tcp_dst = icmp.code |
| match.wildcards &= ~ofp.OFPFW_NW_PROTO |
| |
| if arp: |
| match.ip_proto = arp.op |
| match.wildcards &= ~ofp.OFPFW_NW_PROTO |
| match.ipv4_src = parse_ip(arp.psrc) |
| match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK |
| match.ipv4_dst = parse_ip(arp.pdst) |
| match.wildcards &= ~ofp.OFPFW_NW_DST_MASK |
| |
| return match |
| |
| def packet_to_flow_match_v3(packet): |
| """ |
| OpenFlow 1.2 implementation of packet_to_flow_match |
| """ |
| import loxi.of12 as ofp |
| return packet_to_flow_match_oxm(packet, ofp) |
| |
| def packet_to_flow_match_v4(packet): |
| """ |
| OpenFlow 1.3 implementation of packet_to_flow_match |
| """ |
| import loxi.of13 as ofp |
| return packet_to_flow_match_oxm(packet, ofp) |
| |
| def packet_to_flow_match_oxm(packet, ofp): |
| def parse_ether_layer(layer, match): |
| assert(type(layer) == scapy.Ether) |
| match.oxm_list.append(ofp.oxm.eth_dst(parse_mac(layer.dst))) |
| match.oxm_list.append(ofp.oxm.eth_src(parse_mac(layer.src))) |
| |
| if type(layer.payload) == scapy.Dot1Q: |
| layer = layer.payload |
| match.oxm_list.append(ofp.oxm.eth_type(layer.type)) |
| match.oxm_list.append(ofp.oxm.vlan_vid(layer.vlan)) |
| match.oxm_list.append(ofp.oxm.vlan_pcp(layer.prio)) |
| else: |
| match.oxm_list.append(ofp.oxm.eth_type(layer.type)) |
| match.oxm_list.append(ofp.oxm.vlan_vid(ofp.OFP_VLAN_NONE)) |
| |
| if type(layer.payload) == scapy.IP: |
| parse_ipv4_layer(layer.payload, match) |
| elif type(layer.payload) == scapy.IPv6: |
| parse_ipv6_layer(layer.payload, match) |
| elif type(layer.payload) == scapy.ARP: |
| parse_arp_layer(layer.payload, match) |
| # TODO MPLS |
| |
| def parse_ipv4_layer(layer, match): |
| assert(type(layer) == scapy.IP) |
| match.oxm_list.append(ofp.oxm.ip_proto(layer.proto)) |
| match.oxm_list.append(ofp.oxm.ip_dscp(layer.tos >> 2)) |
| match.oxm_list.append(ofp.oxm.ip_ecn(layer.tos & 3)) |
| match.oxm_list.append(ofp.oxm.ipv4_src(parse_ip(layer.src))) |
| match.oxm_list.append(ofp.oxm.ipv4_dst(parse_ip(layer.dst))) |
| |
| if type(layer.payload) == scapy.TCP: |
| parse_tcp_layer(layer.payload, match) |
| elif type(layer.payload) == scapy.UDP: |
| parse_udp_layer(layer.payload, match) |
| elif type(layer.payload) == scapy.ICMP: |
| parse_icmpv4_layer(layer.payload, match) |
| # TODO SCTP |
| |
| def parse_tcp_layer(layer, match): |
| assert(type(layer) == scapy.TCP) |
| match.oxm_list.append(ofp.oxm.tcp_src(layer.sport)) |
| match.oxm_list.append(ofp.oxm.tcp_dst(layer.dport)) |
| |
| def parse_udp_layer(layer, match): |
| assert(type(layer) == scapy.UDP) |
| match.oxm_list.append(ofp.oxm.udp_src(layer.sport)) |
| match.oxm_list.append(ofp.oxm.udp_dst(layer.dport)) |
| |
| def parse_icmpv4_layer(layer, match): |
| assert(type(layer) == scapy.ICMP) |
| match.oxm_list.append(ofp.oxm.icmpv4_type(layer.type)) |
| match.oxm_list.append(ofp.oxm.icmpv4_code(layer.code)) |
| |
| def parse_arp_layer(layer, match): |
| assert(type(layer) == scapy.ARP) |
| match.oxm_list.append(ofp.oxm.arp_op(layer.op)) |
| match.oxm_list.append(ofp.oxm.arp_spa(parse_ip(layer.psrc))) |
| match.oxm_list.append(ofp.oxm.arp_tpa(parse_ip(layer.pdst))) |
| match.oxm_list.append(ofp.oxm.arp_sha(parse_mac(layer.hwsrc))) |
| match.oxm_list.append(ofp.oxm.arp_tha(parse_mac(layer.hwdst))) |
| |
| def parse_ipv6_layer(layer, match): |
| assert(type(layer) == scapy.IPv6) |
| # TODO handle chained headers |
| match.oxm_list.append(ofp.oxm.ip_proto(layer.nh)) |
| match.oxm_list.append(ofp.oxm.ip_dscp(layer.tc >> 2)) |
| match.oxm_list.append(ofp.oxm.ip_ecn(layer.tc & 3)) |
| match.oxm_list.append(ofp.oxm.ipv6_src(parse_ipv6(layer.src))) |
| match.oxm_list.append(ofp.oxm.ipv6_dst(parse_ipv6(layer.dst))) |
| match.oxm_list.append(ofp.oxm.ipv6_flabel(layer.fl)) |
| |
| if type(layer.payload) == scapy.TCP: |
| parse_tcp_layer(layer.payload, match) |
| elif type(layer.payload) == scapy.UDP: |
| parse_udp_layer(layer.payload, match) |
| elif layer.nh == 0x3a: |
| parse_icmpv6_layer(layer.payload, match) |
| # TODO ND |
| # TODO SCTP |
| |
| def parse_icmpv6_layer(layer, match): |
| match.oxm_list.append(ofp.oxm.icmpv6_type(layer.type)) |
| match.oxm_list.append(ofp.oxm.icmpv6_code(layer.code)) |
| |
| if type(packet) == type(""): |
| ether = scapy.Ether(packet) |
| else: |
| ether = packet |
| |
| match = ofp.match() |
| parse_ether_layer(packet, match) |
| return match |