Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 1 | """ |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 2 | Utility parsing functions |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 3 | """ |
| 4 | |
| 5 | import sys |
Rich Lane | 0f79bee | 2013-05-13 17:37:43 -0700 | [diff] [blame] | 6 | import socket |
Rich Lane | a68176f | 2013-08-09 17:41:05 -0700 | [diff] [blame] | 7 | import packet as scapy |
Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 8 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 9 | def parse_mac(mac_str): |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 10 | """ |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 11 | Parse a MAC address |
| 12 | |
| 13 | Parse a MAC address ':' separated string of hex digits to an |
| 14 | array of integer values. '00:d0:05:5d:24:00' => [0, 208, 5, 93, 36, 0] |
| 15 | @param mac_str The string to convert |
| 16 | @return Array of 6 integer values |
| 17 | """ |
Rich Lane | a92f252 | 2012-10-04 18:11:04 -0700 | [diff] [blame] | 18 | return map(lambda val: int(val, 16), mac_str.split(":")) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 19 | |
| 20 | def parse_ip(ip_str): |
| 21 | """ |
| 22 | Parse an IP address |
| 23 | |
| 24 | Parse an IP address '.' separated string of decimal digits to an |
| 25 | host ordered integer. '172.24.74.77' => |
| 26 | @param ip_str The string to convert |
| 27 | @return Integer value |
| 28 | """ |
Rich Lane | a92f252 | 2012-10-04 18:11:04 -0700 | [diff] [blame] | 29 | array = map(lambda val: int(val), ip_str.split(".")) |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 30 | val = 0 |
| 31 | for a in array: |
| 32 | val <<= 8 |
| 33 | val += a |
| 34 | return val |
| 35 | |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 36 | def parse_ipv6(ip_str): |
| 37 | """ |
| 38 | Parse an IPv6 address |
| 39 | |
| 40 | Parse a textual IPv6 address and return a 16 byte binary string. |
| 41 | """ |
Rich Lane | 0f79bee | 2013-05-13 17:37:43 -0700 | [diff] [blame] | 42 | return socket.inet_pton(socket.AF_INET6, ip_str) |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 43 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 44 | def packet_type_classify(ether): |
| 45 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 46 | dot1q = ether[scapy.Dot1Q] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 47 | except: |
| 48 | dot1q = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 49 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 50 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 51 | ip = ether[scapy.IP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 52 | except: |
| 53 | ip = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 54 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 55 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 56 | tcp = ether[scapy.TCP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 57 | except: |
| 58 | tcp = None |
Dan Talayco | 08d9dfe | 2010-02-12 23:02:11 -0800 | [diff] [blame] | 59 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 60 | try: |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 61 | udp = ether[scapy.UDP] |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 62 | except: |
| 63 | udp = None |
| 64 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 65 | try: |
| 66 | icmp = ether[scapy.ICMP] |
| 67 | except: |
| 68 | icmp = None |
| 69 | |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 70 | try: |
| 71 | arp = ether[scapy.ARP] |
| 72 | except: |
| 73 | arp = None |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 74 | return (dot1q, ip, tcp, udp, icmp, arp) |
| 75 | |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 76 | def packet_to_flow_match(packet): |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 77 | """ |
| 78 | Create a flow match that matches packet with the given wildcards |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 79 | |
| 80 | @param packet The packet to use as a flow template |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 81 | @return An loxi.of10.match object |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 82 | |
Dan Talayco | a3b2018 | 2010-02-10 22:49:34 -0800 | [diff] [blame] | 83 | @todo check min length of packet |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 84 | """ |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 85 | import ofp |
| 86 | if ofp.OFP_VERSION == 1: |
| 87 | return packet_to_flow_match_v1(packet) |
| 88 | elif ofp.OFP_VERSION == 3: |
| 89 | return packet_to_flow_match_v3(packet) |
| 90 | elif ofp.OFP_VERSION == 4: |
| 91 | return packet_to_flow_match_v4(packet) |
Rich Lane | 2eba47a | 2014-12-09 14:29:08 -0800 | [diff] [blame^] | 92 | elif ofp.OFP_VERSION == 5: |
| 93 | return packet_to_flow_match_v5(packet) |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 94 | else: |
| 95 | raise NotImplementedError() |
Dan Talayco | b9cb548 | 2010-02-09 15:23:12 -0800 | [diff] [blame] | 96 | |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 97 | def packet_to_flow_match_v1(packet): |
| 98 | """ |
| 99 | OpenFlow 1.0 implementation of packet_to_flow_match |
| 100 | """ |
| 101 | import loxi.of10 as ofp |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 102 | |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 103 | if type(packet) == type(""): |
Dan Talayco | 958f3b9 | 2010-03-12 21:58:57 -0800 | [diff] [blame] | 104 | ether = scapy.Ether(packet) |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 105 | else: |
| 106 | ether = packet |
| 107 | |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 108 | # For now, assume ether IP packet and ignore wildcards |
Dan Talayco | cb6b5d7 | 2010-03-10 13:59:33 -0800 | [diff] [blame] | 109 | try: |
| 110 | (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether) |
| 111 | except: |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 112 | raise ValueError("could not classify packet") |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 113 | |
Rich Lane | 0237baf | 2013-03-11 22:34:59 -0700 | [diff] [blame] | 114 | match = ofp.match() |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 115 | match.wildcards = ofp.OFPFW_ALL |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 116 | #@todo Check if packet is other than L2 format |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 117 | match.eth_dst = parse_mac(ether.dst) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 118 | match.wildcards &= ~ofp.OFPFW_DL_DST |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 119 | match.eth_src = parse_mac(ether.src) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 120 | match.wildcards &= ~ofp.OFPFW_DL_SRC |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 121 | match.eth_type = ether.type |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 122 | match.wildcards &= ~ofp.OFPFW_DL_TYPE |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 123 | |
| 124 | if dot1q: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 125 | match.vlan_vid = dot1q.vlan |
| 126 | match.vlan_pcp = dot1q.prio |
| 127 | match.eth_type = dot1q.type |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 128 | else: |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 129 | match.vlan_vid = ofp.OFP_VLAN_NONE |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 130 | match.vlan_pcp = 0 |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 131 | match.wildcards &= ~ofp.OFPFW_DL_VLAN |
| 132 | match.wildcards &= ~ofp.OFPFW_DL_VLAN_PCP |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 133 | |
| 134 | if ip: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 135 | match.ipv4_src = parse_ip(ip.src) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 136 | match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 137 | match.ipv4_dst = parse_ip(ip.dst) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 138 | match.wildcards &= ~ofp.OFPFW_NW_DST_MASK |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 139 | match.ip_dscp = ip.tos |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 140 | match.wildcards &= ~ofp.OFPFW_NW_TOS |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 141 | |
| 142 | if tcp: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 143 | match.ip_proto = 6 |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 144 | match.wildcards &= ~ofp.OFPFW_NW_PROTO |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 145 | elif not tcp and udp: |
| 146 | tcp = udp |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 147 | match.ip_proto = 17 |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 148 | match.wildcards &= ~ofp.OFPFW_NW_PROTO |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 149 | |
| 150 | if tcp: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 151 | match.tcp_src = tcp.sport |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 152 | match.wildcards &= ~ofp.OFPFW_TP_SRC |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 153 | match.tcp_dst = tcp.dport |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 154 | match.wildcards &= ~ofp.OFPFW_TP_DST |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 155 | |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 156 | if icmp: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 157 | match.ip_proto = 1 |
| 158 | match.tcp_src = icmp.type |
| 159 | match.tcp_dst = icmp.code |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 160 | match.wildcards &= ~ofp.OFPFW_NW_PROTO |
Tatsuya Yabe | b8fb3c3 | 2010-06-14 15:48:36 -0700 | [diff] [blame] | 161 | |
Christian Dickmann | 8b59b4b | 2012-09-23 16:48:30 -0700 | [diff] [blame] | 162 | if arp: |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 163 | match.ip_proto = arp.op |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 164 | match.wildcards &= ~ofp.OFPFW_NW_PROTO |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 165 | match.ipv4_src = parse_ip(arp.psrc) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 166 | match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK |
Rich Lane | d0478ff | 2013-03-11 12:46:58 -0700 | [diff] [blame] | 167 | match.ipv4_dst = parse_ip(arp.pdst) |
Rich Lane | f688351 | 2013-03-11 17:00:09 -0700 | [diff] [blame] | 168 | match.wildcards &= ~ofp.OFPFW_NW_DST_MASK |
Dan Talayco | b66b112 | 2010-02-10 22:38:49 -0800 | [diff] [blame] | 169 | |
| 170 | return match |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 171 | |
| 172 | def packet_to_flow_match_v3(packet): |
| 173 | """ |
| 174 | OpenFlow 1.2 implementation of packet_to_flow_match |
| 175 | """ |
| 176 | import loxi.of12 as ofp |
| 177 | return packet_to_flow_match_oxm(packet, ofp) |
| 178 | |
| 179 | def packet_to_flow_match_v4(packet): |
| 180 | """ |
| 181 | OpenFlow 1.3 implementation of packet_to_flow_match |
| 182 | """ |
| 183 | import loxi.of13 as ofp |
| 184 | return packet_to_flow_match_oxm(packet, ofp) |
| 185 | |
Rich Lane | 2eba47a | 2014-12-09 14:29:08 -0800 | [diff] [blame^] | 186 | def packet_to_flow_match_v5(packet): |
| 187 | """ |
| 188 | OpenFlow 1.3 implementation of packet_to_flow_match |
| 189 | """ |
| 190 | import loxi.of14 as ofp |
| 191 | return packet_to_flow_match_oxm(packet, ofp) |
| 192 | |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 193 | def packet_to_flow_match_oxm(packet, ofp): |
| 194 | def parse_ether_layer(layer, match): |
| 195 | assert(type(layer) == scapy.Ether) |
| 196 | match.oxm_list.append(ofp.oxm.eth_dst(parse_mac(layer.dst))) |
| 197 | match.oxm_list.append(ofp.oxm.eth_src(parse_mac(layer.src))) |
| 198 | |
| 199 | if type(layer.payload) == scapy.Dot1Q: |
| 200 | layer = layer.payload |
| 201 | match.oxm_list.append(ofp.oxm.eth_type(layer.type)) |
Rich Lane | 3f17dbb | 2013-07-19 13:51:37 -0700 | [diff] [blame] | 202 | match.oxm_list.append(ofp.oxm.vlan_vid(ofp.OFPVID_PRESENT|layer.vlan)) |
Rich Lane | b625551 | 2013-05-07 14:58:43 -0700 | [diff] [blame] | 203 | match.oxm_list.append(ofp.oxm.vlan_pcp(layer.prio)) |
| 204 | else: |
| 205 | match.oxm_list.append(ofp.oxm.eth_type(layer.type)) |
| 206 | match.oxm_list.append(ofp.oxm.vlan_vid(ofp.OFP_VLAN_NONE)) |
| 207 | |
| 208 | if type(layer.payload) == scapy.IP: |
| 209 | parse_ipv4_layer(layer.payload, match) |
| 210 | elif type(layer.payload) == scapy.IPv6: |
| 211 | parse_ipv6_layer(layer.payload, match) |
| 212 | elif type(layer.payload) == scapy.ARP: |
| 213 | parse_arp_layer(layer.payload, match) |
| 214 | # TODO MPLS |
| 215 | |
| 216 | def parse_ipv4_layer(layer, match): |
| 217 | assert(type(layer) == scapy.IP) |
| 218 | match.oxm_list.append(ofp.oxm.ip_proto(layer.proto)) |
| 219 | match.oxm_list.append(ofp.oxm.ip_dscp(layer.tos >> 2)) |
| 220 | match.oxm_list.append(ofp.oxm.ip_ecn(layer.tos & 3)) |
| 221 | match.oxm_list.append(ofp.oxm.ipv4_src(parse_ip(layer.src))) |
| 222 | match.oxm_list.append(ofp.oxm.ipv4_dst(parse_ip(layer.dst))) |
| 223 | |
| 224 | if type(layer.payload) == scapy.TCP: |
| 225 | parse_tcp_layer(layer.payload, match) |
| 226 | elif type(layer.payload) == scapy.UDP: |
| 227 | parse_udp_layer(layer.payload, match) |
| 228 | elif type(layer.payload) == scapy.ICMP: |
| 229 | parse_icmpv4_layer(layer.payload, match) |
| 230 | # TODO SCTP |
| 231 | |
| 232 | def parse_tcp_layer(layer, match): |
| 233 | assert(type(layer) == scapy.TCP) |
| 234 | match.oxm_list.append(ofp.oxm.tcp_src(layer.sport)) |
| 235 | match.oxm_list.append(ofp.oxm.tcp_dst(layer.dport)) |
| 236 | |
| 237 | def parse_udp_layer(layer, match): |
| 238 | assert(type(layer) == scapy.UDP) |
| 239 | match.oxm_list.append(ofp.oxm.udp_src(layer.sport)) |
| 240 | match.oxm_list.append(ofp.oxm.udp_dst(layer.dport)) |
| 241 | |
| 242 | def parse_icmpv4_layer(layer, match): |
| 243 | assert(type(layer) == scapy.ICMP) |
| 244 | match.oxm_list.append(ofp.oxm.icmpv4_type(layer.type)) |
| 245 | match.oxm_list.append(ofp.oxm.icmpv4_code(layer.code)) |
| 246 | |
| 247 | def parse_arp_layer(layer, match): |
| 248 | assert(type(layer) == scapy.ARP) |
| 249 | match.oxm_list.append(ofp.oxm.arp_op(layer.op)) |
| 250 | match.oxm_list.append(ofp.oxm.arp_spa(parse_ip(layer.psrc))) |
| 251 | match.oxm_list.append(ofp.oxm.arp_tpa(parse_ip(layer.pdst))) |
| 252 | match.oxm_list.append(ofp.oxm.arp_sha(parse_mac(layer.hwsrc))) |
| 253 | match.oxm_list.append(ofp.oxm.arp_tha(parse_mac(layer.hwdst))) |
| 254 | |
| 255 | def parse_ipv6_layer(layer, match): |
| 256 | assert(type(layer) == scapy.IPv6) |
| 257 | # TODO handle chained headers |
| 258 | match.oxm_list.append(ofp.oxm.ip_proto(layer.nh)) |
| 259 | match.oxm_list.append(ofp.oxm.ip_dscp(layer.tc >> 2)) |
| 260 | match.oxm_list.append(ofp.oxm.ip_ecn(layer.tc & 3)) |
| 261 | match.oxm_list.append(ofp.oxm.ipv6_src(parse_ipv6(layer.src))) |
| 262 | match.oxm_list.append(ofp.oxm.ipv6_dst(parse_ipv6(layer.dst))) |
| 263 | match.oxm_list.append(ofp.oxm.ipv6_flabel(layer.fl)) |
| 264 | |
| 265 | if type(layer.payload) == scapy.TCP: |
| 266 | parse_tcp_layer(layer.payload, match) |
| 267 | elif type(layer.payload) == scapy.UDP: |
| 268 | parse_udp_layer(layer.payload, match) |
| 269 | elif layer.nh == 0x3a: |
| 270 | parse_icmpv6_layer(layer.payload, match) |
| 271 | # TODO ND |
| 272 | # TODO SCTP |
| 273 | |
| 274 | def parse_icmpv6_layer(layer, match): |
| 275 | match.oxm_list.append(ofp.oxm.icmpv6_type(layer.type)) |
| 276 | match.oxm_list.append(ofp.oxm.icmpv6_code(layer.code)) |
| 277 | |
| 278 | if type(packet) == type(""): |
| 279 | ether = scapy.Ether(packet) |
| 280 | else: |
| 281 | ether = packet |
| 282 | |
| 283 | match = ofp.match() |
| 284 | parse_ether_layer(packet, match) |
| 285 | return match |