Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 1 | """ |
| 2 | OpenFlow message parsing functions |
| 3 | """ |
| 4 | |
| 5 | import sys |
| 6 | import logging |
| 7 | import message |
| 8 | from match_list import match_list |
| 9 | import match |
| 10 | #from error import * |
| 11 | #from action import * |
| 12 | #from action_list import action_list |
| 13 | import cstruct as ofp |
| 14 | |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 15 | try: |
| 16 | logging.getLogger("scapy.runtime").setLevel(logging.ERROR) |
| 17 | from scapy.all import * |
Rich Lane | 8f2861e | 2013-01-11 09:13:20 -0800 | [diff] [blame] | 18 | from oftest.mpls import * |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 19 | except ImportError: |
| 20 | sys.exit("Need to install scapy for packet parsing") |
| 21 | |
| 22 | """ |
| 23 | of_message.py |
| 24 | Contains wrapper functions and classes for the of_message namespace |
| 25 | that are generated by hand. It includes the rest of the wrapper |
| 26 | function information into the of_message namespace |
| 27 | """ |
| 28 | |
| 29 | parse_logger = logging.getLogger("parse") |
| 30 | #parse_logger.setLevel(logging.DEBUG) |
| 31 | |
| 32 | # These message types are subclassed |
| 33 | msg_type_subclassed = [ |
| 34 | ofp.OFPT_STATS_REQUEST, |
| 35 | ofp.OFPT_STATS_REPLY, |
| 36 | ofp.OFPT_ERROR |
| 37 | ] |
| 38 | |
| 39 | # Maps from sub-types to classes |
| 40 | stats_reply_to_class_map = { |
| 41 | ofp.OFPST_DESC : message.desc_stats_reply, |
| 42 | ofp.OFPST_FLOW : message.flow_stats_reply, |
| 43 | ofp.OFPST_AGGREGATE : message.aggregate_stats_reply, |
| 44 | ofp.OFPST_TABLE : message.table_stats_reply, |
| 45 | ofp.OFPST_PORT : message.port_stats_reply, |
| 46 | ofp.OFPST_QUEUE : message.queue_stats_reply, |
| 47 | ofp.OFPST_GROUP : message.group_stats_reply, |
| 48 | ofp.OFPST_GROUP_DESC : message.group_desc_stats_reply |
| 49 | # ofp.OFPST_EXPERIMENTER |
| 50 | } |
| 51 | |
| 52 | stats_request_to_class_map = { |
| 53 | ofp.OFPST_DESC : message.desc_stats_request, |
| 54 | ofp.OFPST_FLOW : message.flow_stats_request, |
| 55 | ofp.OFPST_AGGREGATE : message.aggregate_stats_request, |
| 56 | ofp.OFPST_TABLE : message.table_stats_request, |
| 57 | ofp.OFPST_PORT : message.port_stats_request, |
| 58 | ofp.OFPST_QUEUE : message.queue_stats_request, |
| 59 | ofp.OFPST_GROUP : message.group_stats_request, |
| 60 | ofp.OFPST_GROUP_DESC : message.group_desc_stats_request |
| 61 | # ofp.OFPST_EXPERIMENTER |
| 62 | } |
| 63 | |
| 64 | error_to_class_map = { |
| 65 | ofp.OFPET_HELLO_FAILED : message.hello_failed_error_msg, |
| 66 | ofp.OFPET_BAD_REQUEST : message.bad_request_error_msg, |
| 67 | ofp.OFPET_BAD_ACTION : message.bad_action_error_msg, |
| 68 | ofp.OFPET_BAD_INSTRUCTION : message.bad_instruction_error_msg, |
| 69 | ofp.OFPET_BAD_MATCH : message.bad_match_error_msg, |
| 70 | ofp.OFPET_FLOW_MOD_FAILED : message.flow_mod_failed_error_msg, |
| 71 | ofp.OFPET_GROUP_MOD_FAILED : message.group_mod_failed_error_msg, |
| 72 | ofp.OFPET_PORT_MOD_FAILED : message.port_mod_failed_error_msg, |
| 73 | ofp.OFPET_TABLE_MOD_FAILED : message.table_mod_failed_error_msg, |
| 74 | ofp.OFPET_QUEUE_OP_FAILED : message.queue_op_failed_error_msg, |
| 75 | ofp.OFPET_SWITCH_CONFIG_FAILED : message.switch_config_failed_error_msg |
| 76 | } |
| 77 | |
| 78 | # Map from header type value to the underlieing message class |
| 79 | msg_type_to_class_map = { |
| 80 | ofp.OFPT_HELLO : message.hello, |
| 81 | ofp.OFPT_ERROR : message.error, |
| 82 | ofp.OFPT_ECHO_REQUEST : message.echo_request, |
| 83 | ofp.OFPT_ECHO_REPLY : message.echo_reply, |
| 84 | ofp.OFPT_EXPERIMENTER : message.experimenter, |
| 85 | ofp.OFPT_FEATURES_REQUEST : message.features_request, |
| 86 | ofp.OFPT_FEATURES_REPLY : message.features_reply, |
| 87 | ofp.OFPT_GET_CONFIG_REQUEST : message.get_config_request, |
| 88 | ofp.OFPT_GET_CONFIG_REPLY : message.get_config_reply, |
| 89 | ofp.OFPT_SET_CONFIG : message.set_config, |
| 90 | ofp.OFPT_PACKET_IN : message.packet_in, |
| 91 | ofp.OFPT_FLOW_REMOVED : message.flow_removed, |
| 92 | ofp.OFPT_PORT_STATUS : message.port_status, |
| 93 | ofp.OFPT_PACKET_OUT : message.packet_out, |
| 94 | ofp.OFPT_FLOW_MOD : message.flow_mod, |
| 95 | ofp.OFPT_GROUP_MOD : message.group_mod, |
| 96 | ofp.OFPT_PORT_MOD : message.port_mod, |
| 97 | ofp.OFPT_TABLE_MOD : message.table_mod, |
| 98 | ofp.OFPT_STATS_REQUEST : message.stats_request, |
| 99 | ofp.OFPT_STATS_REPLY : message.stats_reply, |
| 100 | ofp.OFPT_BARRIER_REQUEST : message.barrier_request, |
| 101 | ofp.OFPT_BARRIER_REPLY : message.barrier_reply, |
| 102 | ofp.OFPT_QUEUE_GET_CONFIG_REQUEST : message.queue_get_config_request, |
| 103 | ofp.OFPT_QUEUE_GET_CONFIG_REPLY : message.queue_get_config_reply, |
| 104 | } |
| 105 | |
| 106 | def _of_message_to_object(binary_string): |
| 107 | """ |
| 108 | Map a binary string to the corresponding class. |
| 109 | |
| 110 | Appropriately resolves subclasses |
| 111 | """ |
| 112 | hdr = ofp.ofp_header() |
| 113 | hdr.unpack(binary_string) |
| 114 | # FIXME: Add error detection |
| 115 | if not hdr.type in msg_type_subclassed: |
| 116 | return msg_type_to_class_map[hdr.type]() |
| 117 | if hdr.type == ofp.OFPT_STATS_REQUEST: |
| 118 | sub_hdr = ofp.ofp_stats_request() |
| 119 | sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) |
| 120 | try: |
| 121 | obj = stats_request_to_class_map[sub_hdr.type]() |
| 122 | except LookupError: |
| 123 | obj = None |
| 124 | return obj |
| 125 | elif hdr.type == ofp.OFPT_STATS_REPLY: |
| 126 | sub_hdr = ofp.ofp_stats_reply() |
| 127 | sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) |
| 128 | try: |
| 129 | obj = stats_reply_to_class_map[sub_hdr.type]() |
| 130 | except LookupError: |
| 131 | obj = None |
| 132 | return obj |
| 133 | elif hdr.type == ofp.OFPT_ERROR: |
| 134 | sub_hdr = ofp.ofp_error_msg() |
| 135 | sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) |
| 136 | return error_to_class_map[sub_hdr.type]() |
| 137 | else: |
| 138 | parse_logger.error("Cannot parse pkt to message") |
| 139 | return None |
| 140 | |
| 141 | def of_message_parse(binary_string, raw=False): |
| 142 | """ |
| 143 | Parse an OpenFlow packet |
| 144 | |
| 145 | Parses a raw OpenFlow packet into a Python class, with class |
| 146 | members fully populated. |
| 147 | |
| 148 | @param binary_string The packet (string) to be parsed |
| 149 | @param raw If true, interpret the packet as an L2 packet. Not |
| 150 | yet supported. |
| 151 | @return An object of some message class or None if fails |
| 152 | Note that any data beyond that parsed is not returned |
| 153 | |
| 154 | """ |
| 155 | |
| 156 | if raw: |
| 157 | parse_logger.error("raw packet message parsing not supported") |
| 158 | return None |
| 159 | |
| 160 | obj = _of_message_to_object(binary_string) |
| 161 | if obj: |
| 162 | obj.unpack(binary_string) |
| 163 | return obj |
| 164 | |
| 165 | |
| 166 | def of_header_parse(binary_string, raw=False): |
| 167 | """ |
| 168 | Parse only the header from an OpenFlow packet |
| 169 | |
| 170 | Parses the header from a raw OpenFlow packet into a |
| 171 | an ofp_header Python class. |
| 172 | |
| 173 | @param binary_string The packet (string) to be parsed |
| 174 | @param raw If true, interpret the packet as an L2 packet. Not |
| 175 | yet supported. |
| 176 | @return An ofp_header object |
| 177 | |
| 178 | """ |
| 179 | |
| 180 | if raw: |
| 181 | parse_logger.error("raw packet message parsing not supported") |
| 182 | return None |
| 183 | |
| 184 | hdr = ofp.ofp_header() |
| 185 | hdr.unpack(binary_string) |
| 186 | |
| 187 | return hdr |
| 188 | |
| 189 | map_wc_field_to_match_member = { |
| 190 | 'OFPFW_DL_VLAN' : 'dl_vlan', |
| 191 | 'OFPFW_DL_SRC' : 'dl_src', |
| 192 | 'OFPFW_DL_DST' : 'dl_dst', |
| 193 | 'OFPFW_DL_TYPE' : 'dl_type', |
| 194 | 'OFPFW_NW_PROTO' : 'nw_proto', |
| 195 | 'OFPFW_TP_SRC' : 'tp_src', |
| 196 | 'OFPFW_TP_DST' : 'tp_dst', |
| 197 | 'OFPFW_NW_SRC_SHIFT' : 'nw_src_shift', |
| 198 | 'OFPFW_NW_SRC_BITS' : 'nw_src_bits', |
| 199 | 'OFPFW_NW_SRC_MASK' : 'nw_src_mask', |
| 200 | 'OFPFW_NW_SRC_ALL' : 'nw_src_all', |
| 201 | 'OFPFW_NW_DST_SHIFT' : 'nw_dst_shift', |
| 202 | 'OFPFW_NW_DST_BITS' : 'nw_dst_bits', |
| 203 | 'OFPFW_NW_DST_MASK' : 'nw_dst_mask', |
| 204 | 'OFPFW_NW_DST_ALL' : 'nw_dst_all', |
| 205 | 'OFPFW_DL_VLAN_PCP' : 'dl_vlan_pcp', |
| 206 | 'OFPFW_NW_TOS' : 'nw_tos' |
| 207 | } |
| 208 | |
| 209 | |
| 210 | def parse_mac(mac_str): |
| 211 | """ |
| 212 | Parse a MAC address |
| 213 | |
| 214 | Parse a MAC address ':' separated string of hex digits to an |
| 215 | array of integer values. '00:d0:05:5d:24:00' => [0, 208, 5, 93, 36, 0] |
| 216 | @param mac_str The string to convert |
| 217 | @return Array of 6 integer values |
| 218 | """ |
| 219 | return map(lambda val:eval("0x" + val), mac_str.split(":")) |
| 220 | |
| 221 | def parse_ip(ip_str): |
| 222 | """ |
| 223 | Parse an IP address |
| 224 | |
| 225 | Parse an IP address '.' separated string of decimal digits to an |
| 226 | host ordered integer. '172.24.74.77' => |
| 227 | @param ip_str The string to convert |
| 228 | @return Integer value |
| 229 | """ |
| 230 | array = map(lambda val:eval(val),ip_str.split(".")) |
| 231 | val = 0 |
| 232 | for a in array: |
| 233 | val <<= 8 |
| 234 | val += a |
| 235 | return val |
| 236 | |
| 237 | def packet_to_flow_match(packet): |
| 238 | """ |
| 239 | Create a flow match that matches packet with the given wildcards |
| 240 | |
| 241 | @param packet The packet to use as a flow template |
| 242 | @param pkt_format Currently only L2 is supported. Will indicate the |
| 243 | overall packet type for parsing |
| 244 | @return An ofp_match object if successful. None if format is not |
| 245 | recognized. The wildcards of the match will be cleared for the |
| 246 | values extracted from the packet. |
| 247 | |
| 248 | @todo check min length of packet |
| 249 | @todo Check if packet is other than L2 format |
| 250 | @todo implement other fields covered by OpenFlow 1.2 |
| 251 | """ |
| 252 | match_ls = match_list() |
| 253 | |
| 254 | if Ether in packet: |
| 255 | ether = packet[Ether] |
| 256 | eth_type = match.eth_type(ether.type) |
| 257 | eth_dst = match.eth_dst(parse_mac(ether.dst)) |
| 258 | eth_src = match.eth_src(parse_mac(ether.src)) |
| 259 | match_ls.add(eth_type) |
| 260 | match_ls.add(eth_dst) |
| 261 | match_ls.add(eth_src) |
| 262 | else: |
| 263 | return match_ls |
| 264 | |
| 265 | if Dot1Q in packet: |
| 266 | #TODO: nicer way to get last vlan tag? |
| 267 | vlan = packet[Dot1Q:0] |
| 268 | vlan_vid = match.vlan_vid(vlan.vlan) |
| 269 | vlan_pcp = match.vlan_pcp(vlan.prio) |
| 270 | match_ls.add(vlan_vid) |
| 271 | match_ls.add(vlan_pcp) |
| 272 | vlan_pl = vlan.payload |
| 273 | while vlan_pl is not None and vlan_pl.name == Dot1Q.name: |
| 274 | vlan = vlan_pl |
| 275 | vlan_pl = vlan.payload |
| 276 | #We need to overwrite the already |
| 277 | # inserted eth_type |
| 278 | eth_index = match.tlvs.index() |
| 279 | eth_type = match.eth_type(vlan.type) |
| 280 | match_ls.tlvs.insert(vlan.type,eth_index) |
| 281 | #TODO ARP |
| 282 | |
| 283 | if MPLS in packet: |
| 284 | mpls = packet[MPLS:0] |
| 285 | mpls_label = match.mpls_label(mpls.label) |
| 286 | mpls_tc = match.mpls_tc(mpls.cos) |
| 287 | match_ls.add(mpls_label) |
| 288 | match_ls.add(mpls_tc) |
| 289 | return match_ls |
| 290 | |
| 291 | if IP in packet: |
| 292 | ip = packet[IP] |
| 293 | ipv4_src = match.ipv4_src(parse_ip(ip.src)) |
| 294 | ipv4_dst = match.ipv4_dst(parse_ip(ip.dst)) |
| 295 | ip_dscp = match.ip_dscp(ip.tos >> 2) |
| 296 | ip_ecn = match.ip_ecn(ip.tos & 0x03) |
| 297 | match_ls.add(ipv4_src) |
| 298 | match_ls.add(ipv4_dst) |
| 299 | match_ls.add(ip_dscp) |
| 300 | match_ls.add(ip_ecn) |
| 301 | else: |
| 302 | return match_ls |
| 303 | |
| 304 | if TCP in packet: |
| 305 | tcp = packet[TCP] |
| 306 | ip_proto = match.ip_proto(6) |
| 307 | tcp_src = match.tcp_src(tcp.sport) |
| 308 | tcp_dst = match.tcp_dst(tcp.dport) |
| 309 | match_ls.add(ip_proto) |
| 310 | match_ls.add(tcp_src) |
| 311 | match_ls.add(tcp_dst) |
| 312 | return match_ls |
| 313 | |
| 314 | if UDP in packet: |
| 315 | udp = packet[UDP] |
| 316 | ip_proto = match.ip_proto(17) |
| 317 | udp_src = match.tcp_src(udp.sport) |
| 318 | udp_dst = match.tcp_dst(udp.dport) |
| 319 | match_ls.add(ip_proto) |
| 320 | match_ls.add(udp_src) |
| 321 | match_ls.add(udp_dst) |
Rich Lane | dc04c91 | 2013-01-11 09:13:37 -0800 | [diff] [blame] | 322 | return match_ls |
Rich Lane | 629393f | 2013-01-10 15:37:33 -0800 | [diff] [blame] | 323 | |
| 324 | if ICMP in packet: |
| 325 | icmp = packet[ICMP] |
| 326 | ip_proto = match.ip_proto(1) |
| 327 | icmp_type = match.icmp_type(icmp.type) |
| 328 | icmp_code = match.icmp_code(icmp.code) |
| 329 | match_ls.add(icmp_type) |
| 330 | match_ls.add(icmp_code) |
| 331 | return match_ls |
| 332 | |
| 333 | return match_ls |