blob: d75c7cd5b8bedce26a635b2c56fbdd9490bfed3c [file] [log] [blame]
Dan Talaycob66b1122010-02-10 22:38:49 -08001"""
Rich Lanef6883512013-03-11 17:00:09 -07002Utility parsing functions
Dan Talaycob66b1122010-02-10 22:38:49 -08003"""
4
5import sys
Dan Talayco235d7cb2010-03-12 10:03:18 -08006import logging
Rich Lanee717c6e2013-03-12 10:25:50 -07007import ofp
Dan Talaycob66b1122010-02-10 22:38:49 -08008try:
Dan Talayco958f3b92010-03-12 21:58:57 -08009 import scapy.all as scapy
Dan Talaycob66b1122010-02-10 22:38:49 -080010except:
Dan Talaycod2ca1032010-03-10 14:40:26 -080011 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080012 import scapy as scapy
Dan Talaycod2ca1032010-03-10 14:40:26 -080013 except:
14 sys.exit("Need to install scapy for packet parsing")
Dan Talaycof75360a2010-02-05 22:22:54 -080015
Dan Talaycob66b1122010-02-10 22:38:49 -080016map_wc_field_to_match_member = {
Rich Laned0478ff2013-03-11 12:46:58 -070017 'OFPFW_DL_VLAN' : 'vlan_vid',
18 'OFPFW_DL_SRC' : 'eth_src',
19 'OFPFW_DL_DST' : 'eth_dst',
20 'OFPFW_DL_TYPE' : 'eth_type',
21 'OFPFW_NW_PROTO' : 'ip_proto',
22 'OFPFW_TP_SRC' : 'tcp_src',
23 'OFPFW_TP_DST' : 'tcp_dst',
Dan Talaycob66b1122010-02-10 22:38:49 -080024 'OFPFW_NW_SRC_SHIFT' : 'nw_src_shift',
25 'OFPFW_NW_SRC_BITS' : 'nw_src_bits',
26 'OFPFW_NW_SRC_MASK' : 'nw_src_mask',
27 'OFPFW_NW_SRC_ALL' : 'nw_src_all',
28 'OFPFW_NW_DST_SHIFT' : 'nw_dst_shift',
29 'OFPFW_NW_DST_BITS' : 'nw_dst_bits',
30 'OFPFW_NW_DST_MASK' : 'nw_dst_mask',
31 'OFPFW_NW_DST_ALL' : 'nw_dst_all',
Rich Laned0478ff2013-03-11 12:46:58 -070032 'OFPFW_DL_VLAN_PCP' : 'vlan_pcp',
33 'OFPFW_NW_TOS' : 'ip_dscp'
Dan Talaycob66b1122010-02-10 22:38:49 -080034}
35
36
37def parse_mac(mac_str):
Dan Talaycob9cb5482010-02-09 15:23:12 -080038 """
Dan Talaycob66b1122010-02-10 22:38:49 -080039 Parse a MAC address
40
41 Parse a MAC address ':' separated string of hex digits to an
42 array of integer values. '00:d0:05:5d:24:00' => [0, 208, 5, 93, 36, 0]
43 @param mac_str The string to convert
44 @return Array of 6 integer values
45 """
Rich Lanea92f2522012-10-04 18:11:04 -070046 return map(lambda val: int(val, 16), mac_str.split(":"))
Dan Talaycob66b1122010-02-10 22:38:49 -080047
48def parse_ip(ip_str):
49 """
50 Parse an IP address
51
52 Parse an IP address '.' separated string of decimal digits to an
53 host ordered integer. '172.24.74.77' =>
54 @param ip_str The string to convert
55 @return Integer value
56 """
Rich Lanea92f2522012-10-04 18:11:04 -070057 array = map(lambda val: int(val), ip_str.split("."))
Dan Talaycob66b1122010-02-10 22:38:49 -080058 val = 0
59 for a in array:
60 val <<= 8
61 val += a
62 return val
63
64def packet_type_classify(ether):
65 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080066 dot1q = ether[scapy.Dot1Q]
Dan Talaycob66b1122010-02-10 22:38:49 -080067 except:
68 dot1q = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -080069
Dan Talaycob66b1122010-02-10 22:38:49 -080070 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080071 ip = ether[scapy.IP]
Dan Talaycob66b1122010-02-10 22:38:49 -080072 except:
73 ip = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -080074
Dan Talaycob66b1122010-02-10 22:38:49 -080075 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080076 tcp = ether[scapy.TCP]
Dan Talaycob66b1122010-02-10 22:38:49 -080077 except:
78 tcp = None
Dan Talayco08d9dfe2010-02-12 23:02:11 -080079
Dan Talaycob66b1122010-02-10 22:38:49 -080080 try:
Dan Talayco958f3b92010-03-12 21:58:57 -080081 udp = ether[scapy.UDP]
Dan Talaycob66b1122010-02-10 22:38:49 -080082 except:
83 udp = None
84
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -070085 try:
86 icmp = ether[scapy.ICMP]
87 except:
88 icmp = None
89
Christian Dickmann8b59b4b2012-09-23 16:48:30 -070090 try:
91 arp = ether[scapy.ARP]
92 except:
93 arp = None
Dan Talaycob66b1122010-02-10 22:38:49 -080094 return (dot1q, ip, tcp, udp, icmp, arp)
95
96def packet_to_flow_match(packet, pkt_format="L2"):
97 """
98 Create a flow match that matches packet with the given wildcards
Dan Talaycob9cb5482010-02-09 15:23:12 -080099
100 @param packet The packet to use as a flow template
Dan Talaycoa3b20182010-02-10 22:49:34 -0800101 @param pkt_format Currently only L2 is supported. Will indicate the
102 overall packet type for parsing
Dan Talaycob66b1122010-02-10 22:38:49 -0800103 @return An ofp_match object if successful. None if format is not
104 recognized. The wildcards of the match will be cleared for the
105 values extracted from the packet.
106
Dan Talaycoa3b20182010-02-10 22:49:34 -0800107 @todo check min length of packet
108 @todo Check if packet is other than L2 format
109 @todo Implement ICMP and ARP fields
Dan Talaycob9cb5482010-02-09 15:23:12 -0800110 """
111
Dan Talaycob66b1122010-02-10 22:38:49 -0800112 #@todo check min length of packet
Dan Talaycob66b1122010-02-10 22:38:49 -0800113 if pkt_format.upper() != "L2":
Rich Lanef6883512013-03-11 17:00:09 -0700114 logging.error("Only L2 supported for packet_to_flow")
Dan Talaycob66b1122010-02-10 22:38:49 -0800115 return None
116
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800117 if type(packet) == type(""):
Dan Talayco958f3b92010-03-12 21:58:57 -0800118 ether = scapy.Ether(packet)
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800119 else:
120 ether = packet
121
Dan Talaycob66b1122010-02-10 22:38:49 -0800122 # For now, assume ether IP packet and ignore wildcards
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800123 try:
124 (dot1q, ip, tcp, udp, icmp, arp) = packet_type_classify(ether)
125 except:
Rich Lanef6883512013-03-11 17:00:09 -0700126 logging.error("packet_to_flow_match: Classify error")
Dan Talaycocb6b5d72010-03-10 13:59:33 -0800127 return None
Dan Talaycob66b1122010-02-10 22:38:49 -0800128
Rich Lane0237baf2013-03-11 22:34:59 -0700129 match = ofp.match()
Rich Lanef6883512013-03-11 17:00:09 -0700130 match.wildcards = ofp.OFPFW_ALL
Dan Talaycob66b1122010-02-10 22:38:49 -0800131 #@todo Check if packet is other than L2 format
Rich Laned0478ff2013-03-11 12:46:58 -0700132 match.eth_dst = parse_mac(ether.dst)
Rich Lanef6883512013-03-11 17:00:09 -0700133 match.wildcards &= ~ofp.OFPFW_DL_DST
Rich Laned0478ff2013-03-11 12:46:58 -0700134 match.eth_src = parse_mac(ether.src)
Rich Lanef6883512013-03-11 17:00:09 -0700135 match.wildcards &= ~ofp.OFPFW_DL_SRC
Rich Laned0478ff2013-03-11 12:46:58 -0700136 match.eth_type = ether.type
Rich Lanef6883512013-03-11 17:00:09 -0700137 match.wildcards &= ~ofp.OFPFW_DL_TYPE
Dan Talaycob66b1122010-02-10 22:38:49 -0800138
139 if dot1q:
Rich Laned0478ff2013-03-11 12:46:58 -0700140 match.vlan_vid = dot1q.vlan
141 match.vlan_pcp = dot1q.prio
142 match.eth_type = dot1q.type
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700143 else:
Rich Lanef6883512013-03-11 17:00:09 -0700144 match.vlan_vid = ofp.OFP_VLAN_NONE
Rich Laned0478ff2013-03-11 12:46:58 -0700145 match.vlan_pcp = 0
Rich Lanef6883512013-03-11 17:00:09 -0700146 match.wildcards &= ~ofp.OFPFW_DL_VLAN
147 match.wildcards &= ~ofp.OFPFW_DL_VLAN_PCP
Dan Talaycob66b1122010-02-10 22:38:49 -0800148
149 if ip:
Rich Laned0478ff2013-03-11 12:46:58 -0700150 match.ipv4_src = parse_ip(ip.src)
Rich Lanef6883512013-03-11 17:00:09 -0700151 match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK
Rich Laned0478ff2013-03-11 12:46:58 -0700152 match.ipv4_dst = parse_ip(ip.dst)
Rich Lanef6883512013-03-11 17:00:09 -0700153 match.wildcards &= ~ofp.OFPFW_NW_DST_MASK
Rich Laned0478ff2013-03-11 12:46:58 -0700154 match.ip_dscp = ip.tos
Rich Lanef6883512013-03-11 17:00:09 -0700155 match.wildcards &= ~ofp.OFPFW_NW_TOS
Dan Talaycob66b1122010-02-10 22:38:49 -0800156
157 if tcp:
Rich Laned0478ff2013-03-11 12:46:58 -0700158 match.ip_proto = 6
Rich Lanef6883512013-03-11 17:00:09 -0700159 match.wildcards &= ~ofp.OFPFW_NW_PROTO
Dan Talayco89d57342010-06-07 16:24:59 -0700160 elif not tcp and udp:
161 tcp = udp
Rich Laned0478ff2013-03-11 12:46:58 -0700162 match.ip_proto = 17
Rich Lanef6883512013-03-11 17:00:09 -0700163 match.wildcards &= ~ofp.OFPFW_NW_PROTO
Dan Talayco89d57342010-06-07 16:24:59 -0700164
165 if tcp:
Rich Laned0478ff2013-03-11 12:46:58 -0700166 match.tcp_src = tcp.sport
Rich Lanef6883512013-03-11 17:00:09 -0700167 match.wildcards &= ~ofp.OFPFW_TP_SRC
Rich Laned0478ff2013-03-11 12:46:58 -0700168 match.tcp_dst = tcp.dport
Rich Lanef6883512013-03-11 17:00:09 -0700169 match.wildcards &= ~ofp.OFPFW_TP_DST
Dan Talaycob66b1122010-02-10 22:38:49 -0800170
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700171 if icmp:
Rich Laned0478ff2013-03-11 12:46:58 -0700172 match.ip_proto = 1
173 match.tcp_src = icmp.type
174 match.tcp_dst = icmp.code
Rich Lanef6883512013-03-11 17:00:09 -0700175 match.wildcards &= ~ofp.OFPFW_NW_PROTO
Tatsuya Yabeb8fb3c32010-06-14 15:48:36 -0700176
Christian Dickmann8b59b4b2012-09-23 16:48:30 -0700177 if arp:
Rich Laned0478ff2013-03-11 12:46:58 -0700178 match.ip_proto = arp.op
Rich Lanef6883512013-03-11 17:00:09 -0700179 match.wildcards &= ~ofp.OFPFW_NW_PROTO
Rich Laned0478ff2013-03-11 12:46:58 -0700180 match.ipv4_src = parse_ip(arp.psrc)
Rich Lanef6883512013-03-11 17:00:09 -0700181 match.wildcards &= ~ofp.OFPFW_NW_SRC_MASK
Rich Laned0478ff2013-03-11 12:46:58 -0700182 match.ipv4_dst = parse_ip(arp.pdst)
Rich Lanef6883512013-03-11 17:00:09 -0700183 match.wildcards &= ~ofp.OFPFW_NW_DST_MASK
Dan Talaycob66b1122010-02-10 22:38:49 -0800184
185 return match