Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1 | /* |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA |
| 17 | */ |
| 18 | |
Joe Perches | 2235ad1 | 2014-02-03 17:18:21 -0800 | [diff] [blame] | 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 21 | #include "flow.h" |
| 22 | #include "datapath.h" |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/if_ether.h> |
| 27 | #include <linux/if_vlan.h> |
| 28 | #include <net/llc_pdu.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/jhash.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/llc.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/in.h> |
| 35 | #include <linux/rcupdate.h> |
| 36 | #include <linux/if_arp.h> |
| 37 | #include <linux/ip.h> |
| 38 | #include <linux/ipv6.h> |
| 39 | #include <linux/sctp.h> |
| 40 | #include <linux/tcp.h> |
| 41 | #include <linux/udp.h> |
| 42 | #include <linux/icmp.h> |
| 43 | #include <linux/icmpv6.h> |
| 44 | #include <linux/rculist.h> |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 45 | #include <net/geneve.h> |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 46 | #include <net/ip.h> |
| 47 | #include <net/ipv6.h> |
| 48 | #include <net/ndisc.h> |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 49 | #include <net/mpls.h> |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 50 | |
| 51 | #include "flow_netlink.h" |
| 52 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 53 | static void update_range(struct sw_flow_match *match, |
| 54 | size_t offset, size_t size, bool is_mask) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 55 | { |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 56 | struct sw_flow_key_range *range; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 57 | size_t start = rounddown(offset, sizeof(long)); |
| 58 | size_t end = roundup(offset + size, sizeof(long)); |
| 59 | |
| 60 | if (!is_mask) |
| 61 | range = &match->range; |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 62 | else |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 63 | range = &match->mask->range; |
| 64 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 65 | if (range->start == range->end) { |
| 66 | range->start = start; |
| 67 | range->end = end; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if (range->start > start) |
| 72 | range->start = start; |
| 73 | |
| 74 | if (range->end < end) |
| 75 | range->end = end; |
| 76 | } |
| 77 | |
| 78 | #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ |
| 79 | do { \ |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 80 | update_range(match, offsetof(struct sw_flow_key, field), \ |
| 81 | sizeof((match)->key->field), is_mask); \ |
| 82 | if (is_mask) \ |
| 83 | (match)->mask->key.field = value; \ |
| 84 | else \ |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 85 | (match)->key->field = value; \ |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 86 | } while (0) |
| 87 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 88 | #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \ |
| 89 | do { \ |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 90 | update_range(match, offset, len, is_mask); \ |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 91 | if (is_mask) \ |
| 92 | memcpy((u8 *)&(match)->mask->key + offset, value_p, \ |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 93 | len); \ |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 94 | else \ |
| 95 | memcpy((u8 *)(match)->key + offset, value_p, len); \ |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 96 | } while (0) |
| 97 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 98 | #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \ |
| 99 | SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \ |
| 100 | value_p, len, is_mask) |
| 101 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 102 | #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \ |
| 103 | do { \ |
| 104 | update_range(match, offsetof(struct sw_flow_key, field), \ |
| 105 | sizeof((match)->key->field), is_mask); \ |
| 106 | if (is_mask) \ |
| 107 | memset((u8 *)&(match)->mask->key.field, value, \ |
| 108 | sizeof((match)->mask->key.field)); \ |
| 109 | else \ |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 110 | memset((u8 *)&(match)->key->field, value, \ |
| 111 | sizeof((match)->key->field)); \ |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 112 | } while (0) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 113 | |
| 114 | static bool match_validate(const struct sw_flow_match *match, |
| 115 | u64 key_attrs, u64 mask_attrs) |
| 116 | { |
| 117 | u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET; |
| 118 | u64 mask_allowed = key_attrs; /* At most allow all key attributes */ |
| 119 | |
| 120 | /* The following mask attributes allowed only if they |
| 121 | * pass the validation tests. */ |
| 122 | mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4) |
| 123 | | (1 << OVS_KEY_ATTR_IPV6) |
| 124 | | (1 << OVS_KEY_ATTR_TCP) |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 125 | | (1 << OVS_KEY_ATTR_TCP_FLAGS) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 126 | | (1 << OVS_KEY_ATTR_UDP) |
| 127 | | (1 << OVS_KEY_ATTR_SCTP) |
| 128 | | (1 << OVS_KEY_ATTR_ICMP) |
| 129 | | (1 << OVS_KEY_ATTR_ICMPV6) |
| 130 | | (1 << OVS_KEY_ATTR_ARP) |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 131 | | (1 << OVS_KEY_ATTR_ND) |
| 132 | | (1 << OVS_KEY_ATTR_MPLS)); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 133 | |
| 134 | /* Always allowed mask fields. */ |
| 135 | mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL) |
| 136 | | (1 << OVS_KEY_ATTR_IN_PORT) |
| 137 | | (1 << OVS_KEY_ATTR_ETHERTYPE)); |
| 138 | |
| 139 | /* Check key attributes. */ |
| 140 | if (match->key->eth.type == htons(ETH_P_ARP) |
| 141 | || match->key->eth.type == htons(ETH_P_RARP)) { |
| 142 | key_expected |= 1 << OVS_KEY_ATTR_ARP; |
| 143 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 144 | mask_allowed |= 1 << OVS_KEY_ATTR_ARP; |
| 145 | } |
| 146 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 147 | if (eth_p_mpls(match->key->eth.type)) { |
| 148 | key_expected |= 1 << OVS_KEY_ATTR_MPLS; |
| 149 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 150 | mask_allowed |= 1 << OVS_KEY_ATTR_MPLS; |
| 151 | } |
| 152 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 153 | if (match->key->eth.type == htons(ETH_P_IP)) { |
| 154 | key_expected |= 1 << OVS_KEY_ATTR_IPV4; |
| 155 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 156 | mask_allowed |= 1 << OVS_KEY_ATTR_IPV4; |
| 157 | |
| 158 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 159 | if (match->key->ip.proto == IPPROTO_UDP) { |
| 160 | key_expected |= 1 << OVS_KEY_ATTR_UDP; |
| 161 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 162 | mask_allowed |= 1 << OVS_KEY_ATTR_UDP; |
| 163 | } |
| 164 | |
| 165 | if (match->key->ip.proto == IPPROTO_SCTP) { |
| 166 | key_expected |= 1 << OVS_KEY_ATTR_SCTP; |
| 167 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 168 | mask_allowed |= 1 << OVS_KEY_ATTR_SCTP; |
| 169 | } |
| 170 | |
| 171 | if (match->key->ip.proto == IPPROTO_TCP) { |
| 172 | key_expected |= 1 << OVS_KEY_ATTR_TCP; |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 173 | key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS; |
| 174 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 175 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP; |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 176 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS; |
| 177 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (match->key->ip.proto == IPPROTO_ICMP) { |
| 181 | key_expected |= 1 << OVS_KEY_ATTR_ICMP; |
| 182 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 183 | mask_allowed |= 1 << OVS_KEY_ATTR_ICMP; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (match->key->eth.type == htons(ETH_P_IPV6)) { |
| 189 | key_expected |= 1 << OVS_KEY_ATTR_IPV6; |
| 190 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) |
| 191 | mask_allowed |= 1 << OVS_KEY_ATTR_IPV6; |
| 192 | |
| 193 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 194 | if (match->key->ip.proto == IPPROTO_UDP) { |
| 195 | key_expected |= 1 << OVS_KEY_ATTR_UDP; |
| 196 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 197 | mask_allowed |= 1 << OVS_KEY_ATTR_UDP; |
| 198 | } |
| 199 | |
| 200 | if (match->key->ip.proto == IPPROTO_SCTP) { |
| 201 | key_expected |= 1 << OVS_KEY_ATTR_SCTP; |
| 202 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 203 | mask_allowed |= 1 << OVS_KEY_ATTR_SCTP; |
| 204 | } |
| 205 | |
| 206 | if (match->key->ip.proto == IPPROTO_TCP) { |
| 207 | key_expected |= 1 << OVS_KEY_ATTR_TCP; |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 208 | key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS; |
| 209 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 210 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP; |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 211 | mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS; |
| 212 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (match->key->ip.proto == IPPROTO_ICMPV6) { |
| 216 | key_expected |= 1 << OVS_KEY_ATTR_ICMPV6; |
| 217 | if (match->mask && (match->mask->key.ip.proto == 0xff)) |
| 218 | mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6; |
| 219 | |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 220 | if (match->key->tp.src == |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 221 | htons(NDISC_NEIGHBOUR_SOLICITATION) || |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 222 | match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 223 | key_expected |= 1 << OVS_KEY_ATTR_ND; |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 224 | if (match->mask && (match->mask->key.tp.src == htons(0xffff))) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 225 | mask_allowed |= 1 << OVS_KEY_ATTR_ND; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if ((key_attrs & key_expected) != key_expected) { |
| 232 | /* Key attributes check failed. */ |
| 233 | OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n", |
Daniele Di Proietto | cc23ebf | 2014-02-03 14:09:01 -0800 | [diff] [blame] | 234 | (unsigned long long)key_attrs, (unsigned long long)key_expected); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if ((mask_attrs & mask_allowed) != mask_attrs) { |
| 239 | /* Mask attributes check failed. */ |
| 240 | OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n", |
Daniele Di Proietto | cc23ebf | 2014-02-03 14:09:01 -0800 | [diff] [blame] | 241 | (unsigned long long)mask_attrs, (unsigned long long)mask_allowed); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
Joe Stringer | 41af73e | 2014-10-18 16:14:14 -0700 | [diff] [blame] | 248 | size_t ovs_key_attr_size(void) |
| 249 | { |
| 250 | /* Whenever adding new OVS_KEY_ FIELDS, we should consider |
| 251 | * updating this function. |
| 252 | */ |
| 253 | BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22); |
| 254 | |
| 255 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ |
| 256 | + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */ |
| 257 | + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */ |
| 258 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */ |
| 259 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */ |
| 260 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */ |
| 261 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */ |
| 262 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */ |
| 263 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ |
| 264 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */ |
| 265 | + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */ |
| 266 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ |
| 267 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ |
| 268 | + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */ |
| 269 | + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */ |
| 270 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ |
| 271 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ |
| 272 | + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */ |
| 273 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ |
| 274 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ |
| 275 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ |
| 276 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ |
| 277 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ |
| 278 | } |
| 279 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 280 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
| 281 | static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { |
| 282 | [OVS_KEY_ATTR_ENCAP] = -1, |
| 283 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), |
| 284 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), |
| 285 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), |
| 286 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), |
| 287 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), |
| 288 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), |
| 289 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), |
| 290 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), |
| 291 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 292 | [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16), |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 293 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), |
| 294 | [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp), |
| 295 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), |
| 296 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), |
| 297 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), |
| 298 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 299 | [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32), |
| 300 | [OVS_KEY_ATTR_DP_HASH] = sizeof(u32), |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 301 | [OVS_KEY_ATTR_TUNNEL] = -1, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 302 | [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls), |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | static bool is_all_zero(const u8 *fp, size_t size) |
| 306 | { |
| 307 | int i; |
| 308 | |
| 309 | if (!fp) |
| 310 | return false; |
| 311 | |
| 312 | for (i = 0; i < size; i++) |
| 313 | if (fp[i]) |
| 314 | return false; |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | static int __parse_flow_nlattrs(const struct nlattr *attr, |
| 320 | const struct nlattr *a[], |
| 321 | u64 *attrsp, bool nz) |
| 322 | { |
| 323 | const struct nlattr *nla; |
| 324 | u64 attrs; |
| 325 | int rem; |
| 326 | |
| 327 | attrs = *attrsp; |
| 328 | nla_for_each_nested(nla, attr, rem) { |
| 329 | u16 type = nla_type(nla); |
| 330 | int expected_len; |
| 331 | |
| 332 | if (type > OVS_KEY_ATTR_MAX) { |
| 333 | OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n", |
| 334 | type, OVS_KEY_ATTR_MAX); |
| 335 | return -EINVAL; |
| 336 | } |
| 337 | |
| 338 | if (attrs & (1 << type)) { |
| 339 | OVS_NLERR("Duplicate key attribute (type %d).\n", type); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | expected_len = ovs_key_lens[type]; |
| 344 | if (nla_len(nla) != expected_len && expected_len != -1) { |
| 345 | OVS_NLERR("Key attribute has unexpected length (type=%d" |
| 346 | ", length=%d, expected=%d).\n", type, |
| 347 | nla_len(nla), expected_len); |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | if (!nz || !is_all_zero(nla_data(nla), expected_len)) { |
| 352 | attrs |= 1 << type; |
| 353 | a[type] = nla; |
| 354 | } |
| 355 | } |
| 356 | if (rem) { |
| 357 | OVS_NLERR("Message has %d unknown bytes.\n", rem); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | *attrsp = attrs; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int parse_flow_mask_nlattrs(const struct nlattr *attr, |
| 366 | const struct nlattr *a[], u64 *attrsp) |
| 367 | { |
| 368 | return __parse_flow_nlattrs(attr, a, attrsp, true); |
| 369 | } |
| 370 | |
| 371 | static int parse_flow_nlattrs(const struct nlattr *attr, |
| 372 | const struct nlattr *a[], u64 *attrsp) |
| 373 | { |
| 374 | return __parse_flow_nlattrs(attr, a, attrsp, false); |
| 375 | } |
| 376 | |
| 377 | static int ipv4_tun_from_nlattr(const struct nlattr *attr, |
| 378 | struct sw_flow_match *match, bool is_mask) |
| 379 | { |
| 380 | struct nlattr *a; |
| 381 | int rem; |
| 382 | bool ttl = false; |
| 383 | __be16 tun_flags = 0; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 384 | unsigned long opt_key_offset; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 385 | |
| 386 | nla_for_each_nested(a, attr, rem) { |
| 387 | int type = nla_type(a); |
| 388 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { |
| 389 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), |
| 390 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), |
| 391 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), |
| 392 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, |
| 393 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, |
| 394 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, |
| 395 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 396 | [OVS_TUNNEL_KEY_ATTR_OAM] = 0, |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 397 | [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | if (type > OVS_TUNNEL_KEY_ATTR_MAX) { |
| 401 | OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n", |
| 402 | type, OVS_TUNNEL_KEY_ATTR_MAX); |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 406 | if (ovs_tunnel_key_lens[type] != nla_len(a) && |
| 407 | ovs_tunnel_key_lens[type] != -1) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 408 | OVS_NLERR("IPv4 tunnel attribute type has unexpected " |
| 409 | " length (type=%d, length=%d, expected=%d).\n", |
| 410 | type, nla_len(a), ovs_tunnel_key_lens[type]); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | switch (type) { |
| 415 | case OVS_TUNNEL_KEY_ATTR_ID: |
| 416 | SW_FLOW_KEY_PUT(match, tun_key.tun_id, |
| 417 | nla_get_be64(a), is_mask); |
| 418 | tun_flags |= TUNNEL_KEY; |
| 419 | break; |
| 420 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: |
| 421 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, |
| 422 | nla_get_be32(a), is_mask); |
| 423 | break; |
| 424 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: |
| 425 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, |
| 426 | nla_get_be32(a), is_mask); |
| 427 | break; |
| 428 | case OVS_TUNNEL_KEY_ATTR_TOS: |
| 429 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, |
| 430 | nla_get_u8(a), is_mask); |
| 431 | break; |
| 432 | case OVS_TUNNEL_KEY_ATTR_TTL: |
| 433 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl, |
| 434 | nla_get_u8(a), is_mask); |
| 435 | ttl = true; |
| 436 | break; |
| 437 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: |
| 438 | tun_flags |= TUNNEL_DONT_FRAGMENT; |
| 439 | break; |
| 440 | case OVS_TUNNEL_KEY_ATTR_CSUM: |
| 441 | tun_flags |= TUNNEL_CSUM; |
| 442 | break; |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 443 | case OVS_TUNNEL_KEY_ATTR_OAM: |
| 444 | tun_flags |= TUNNEL_OAM; |
| 445 | break; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 446 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: |
| 447 | tun_flags |= TUNNEL_OPTIONS_PRESENT; |
| 448 | if (nla_len(a) > sizeof(match->key->tun_opts)) { |
| 449 | OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n", |
| 450 | nla_len(a), |
| 451 | sizeof(match->key->tun_opts)); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | if (nla_len(a) % 4 != 0) { |
| 456 | OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n", |
| 457 | nla_len(a)); |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | /* We need to record the length of the options passed |
| 462 | * down, otherwise packets with the same format but |
| 463 | * additional options will be silently matched. |
| 464 | */ |
| 465 | if (!is_mask) { |
| 466 | SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a), |
| 467 | false); |
| 468 | } else { |
| 469 | /* This is somewhat unusual because it looks at |
| 470 | * both the key and mask while parsing the |
| 471 | * attributes (and by extension assumes the key |
| 472 | * is parsed first). Normally, we would verify |
| 473 | * that each is the correct length and that the |
| 474 | * attributes line up in the validate function. |
| 475 | * However, that is difficult because this is |
| 476 | * variable length and we won't have the |
| 477 | * information later. |
| 478 | */ |
| 479 | if (match->key->tun_opts_len != nla_len(a)) { |
| 480 | OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).", |
| 481 | match->key->tun_opts_len, |
| 482 | nla_len(a)); |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, |
| 487 | true); |
| 488 | } |
| 489 | |
| 490 | opt_key_offset = (unsigned long)GENEVE_OPTS( |
| 491 | (struct sw_flow_key *)0, |
| 492 | nla_len(a)); |
| 493 | SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, |
| 494 | nla_data(a), nla_len(a), |
| 495 | is_mask); |
| 496 | break; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 497 | default: |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 498 | OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n", |
| 499 | type); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 500 | return -EINVAL; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask); |
| 505 | |
| 506 | if (rem > 0) { |
| 507 | OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem); |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | |
| 511 | if (!is_mask) { |
| 512 | if (!match->key->tun_key.ipv4_dst) { |
| 513 | OVS_NLERR("IPv4 tunnel destination address is zero.\n"); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | if (!ttl) { |
| 518 | OVS_NLERR("IPv4 tunnel TTL not specified.\n"); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 526 | static int __ipv4_tun_to_nlattr(struct sk_buff *skb, |
| 527 | const struct ovs_key_ipv4_tunnel *output, |
| 528 | const struct geneve_opt *tun_opts, |
| 529 | int swkey_tun_opts_len) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 530 | { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 531 | if (output->tun_flags & TUNNEL_KEY && |
| 532 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) |
| 533 | return -EMSGSIZE; |
| 534 | if (output->ipv4_src && |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 535 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 536 | return -EMSGSIZE; |
| 537 | if (output->ipv4_dst && |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 538 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 539 | return -EMSGSIZE; |
| 540 | if (output->ipv4_tos && |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 541 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 542 | return -EMSGSIZE; |
| 543 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl)) |
| 544 | return -EMSGSIZE; |
| 545 | if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) && |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 546 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 547 | return -EMSGSIZE; |
| 548 | if ((output->tun_flags & TUNNEL_CSUM) && |
Jesse Gross | 67fa034 | 2014-10-03 15:35:30 -0700 | [diff] [blame] | 549 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) |
| 550 | return -EMSGSIZE; |
| 551 | if ((output->tun_flags & TUNNEL_OAM) && |
| 552 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 553 | return -EMSGSIZE; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 554 | if (tun_opts && |
| 555 | nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, |
| 556 | swkey_tun_opts_len, tun_opts)) |
| 557 | return -EMSGSIZE; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | static int ipv4_tun_to_nlattr(struct sk_buff *skb, |
| 564 | const struct ovs_key_ipv4_tunnel *output, |
| 565 | const struct geneve_opt *tun_opts, |
| 566 | int swkey_tun_opts_len) |
| 567 | { |
| 568 | struct nlattr *nla; |
| 569 | int err; |
| 570 | |
| 571 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); |
| 572 | if (!nla) |
| 573 | return -EMSGSIZE; |
| 574 | |
| 575 | err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len); |
| 576 | if (err) |
| 577 | return err; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 578 | |
| 579 | nla_nest_end(skb, nla); |
| 580 | return 0; |
| 581 | } |
| 582 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 583 | static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs, |
| 584 | const struct nlattr **a, bool is_mask) |
| 585 | { |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 586 | if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) { |
| 587 | u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]); |
| 588 | |
| 589 | SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask); |
| 590 | *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH); |
| 591 | } |
| 592 | |
| 593 | if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) { |
| 594 | u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]); |
| 595 | |
| 596 | SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask); |
| 597 | *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID); |
| 598 | } |
| 599 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 600 | if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) { |
| 601 | SW_FLOW_KEY_PUT(match, phy.priority, |
| 602 | nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask); |
| 603 | *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY); |
| 604 | } |
| 605 | |
| 606 | if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) { |
| 607 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); |
| 608 | |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 609 | if (is_mask) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 610 | in_port = 0xffffffff; /* Always exact match in_port. */ |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 611 | } else if (in_port >= DP_MAX_PORTS) { |
| 612 | OVS_NLERR("Port (%d) exceeds maximum allowable (%d).\n", |
| 613 | in_port, DP_MAX_PORTS); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 614 | return -EINVAL; |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 615 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 616 | |
| 617 | SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask); |
| 618 | *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT); |
| 619 | } else if (!is_mask) { |
| 620 | SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask); |
| 621 | } |
| 622 | |
| 623 | if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) { |
| 624 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); |
| 625 | |
| 626 | SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask); |
| 627 | *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK); |
| 628 | } |
| 629 | if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) { |
| 630 | if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match, |
| 631 | is_mask)) |
| 632 | return -EINVAL; |
| 633 | *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL); |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 638 | static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs, |
| 639 | const struct nlattr **a, bool is_mask) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 640 | { |
| 641 | int err; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 642 | |
| 643 | err = metadata_from_nlattrs(match, &attrs, a, is_mask); |
| 644 | if (err) |
| 645 | return err; |
| 646 | |
| 647 | if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) { |
| 648 | const struct ovs_key_ethernet *eth_key; |
| 649 | |
| 650 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); |
| 651 | SW_FLOW_KEY_MEMCPY(match, eth.src, |
| 652 | eth_key->eth_src, ETH_ALEN, is_mask); |
| 653 | SW_FLOW_KEY_MEMCPY(match, eth.dst, |
| 654 | eth_key->eth_dst, ETH_ALEN, is_mask); |
| 655 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET); |
| 656 | } |
| 657 | |
| 658 | if (attrs & (1 << OVS_KEY_ATTR_VLAN)) { |
| 659 | __be16 tci; |
| 660 | |
| 661 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
| 662 | if (!(tci & htons(VLAN_TAG_PRESENT))) { |
| 663 | if (is_mask) |
| 664 | OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n"); |
| 665 | else |
| 666 | OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n"); |
| 667 | |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
| 671 | SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask); |
| 672 | attrs &= ~(1 << OVS_KEY_ATTR_VLAN); |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 673 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 674 | |
| 675 | if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) { |
| 676 | __be16 eth_type; |
| 677 | |
| 678 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); |
| 679 | if (is_mask) { |
| 680 | /* Always exact match EtherType. */ |
| 681 | eth_type = htons(0xffff); |
| 682 | } else if (ntohs(eth_type) < ETH_P_802_3_MIN) { |
| 683 | OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n", |
| 684 | ntohs(eth_type), ETH_P_802_3_MIN); |
| 685 | return -EINVAL; |
| 686 | } |
| 687 | |
| 688 | SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask); |
| 689 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 690 | } else if (!is_mask) { |
| 691 | SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask); |
| 692 | } |
| 693 | |
| 694 | if (attrs & (1 << OVS_KEY_ATTR_IPV4)) { |
| 695 | const struct ovs_key_ipv4 *ipv4_key; |
| 696 | |
| 697 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); |
| 698 | if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) { |
| 699 | OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n", |
| 700 | ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX); |
| 701 | return -EINVAL; |
| 702 | } |
| 703 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 704 | ipv4_key->ipv4_proto, is_mask); |
| 705 | SW_FLOW_KEY_PUT(match, ip.tos, |
| 706 | ipv4_key->ipv4_tos, is_mask); |
| 707 | SW_FLOW_KEY_PUT(match, ip.ttl, |
| 708 | ipv4_key->ipv4_ttl, is_mask); |
| 709 | SW_FLOW_KEY_PUT(match, ip.frag, |
| 710 | ipv4_key->ipv4_frag, is_mask); |
| 711 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, |
| 712 | ipv4_key->ipv4_src, is_mask); |
| 713 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, |
| 714 | ipv4_key->ipv4_dst, is_mask); |
| 715 | attrs &= ~(1 << OVS_KEY_ATTR_IPV4); |
| 716 | } |
| 717 | |
| 718 | if (attrs & (1 << OVS_KEY_ATTR_IPV6)) { |
| 719 | const struct ovs_key_ipv6 *ipv6_key; |
| 720 | |
| 721 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); |
| 722 | if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) { |
| 723 | OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n", |
| 724 | ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX); |
| 725 | return -EINVAL; |
| 726 | } |
| 727 | SW_FLOW_KEY_PUT(match, ipv6.label, |
| 728 | ipv6_key->ipv6_label, is_mask); |
| 729 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 730 | ipv6_key->ipv6_proto, is_mask); |
| 731 | SW_FLOW_KEY_PUT(match, ip.tos, |
| 732 | ipv6_key->ipv6_tclass, is_mask); |
| 733 | SW_FLOW_KEY_PUT(match, ip.ttl, |
| 734 | ipv6_key->ipv6_hlimit, is_mask); |
| 735 | SW_FLOW_KEY_PUT(match, ip.frag, |
| 736 | ipv6_key->ipv6_frag, is_mask); |
| 737 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src, |
| 738 | ipv6_key->ipv6_src, |
| 739 | sizeof(match->key->ipv6.addr.src), |
| 740 | is_mask); |
| 741 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst, |
| 742 | ipv6_key->ipv6_dst, |
| 743 | sizeof(match->key->ipv6.addr.dst), |
| 744 | is_mask); |
| 745 | |
| 746 | attrs &= ~(1 << OVS_KEY_ATTR_IPV6); |
| 747 | } |
| 748 | |
| 749 | if (attrs & (1 << OVS_KEY_ATTR_ARP)) { |
| 750 | const struct ovs_key_arp *arp_key; |
| 751 | |
| 752 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); |
| 753 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
| 754 | OVS_NLERR("Unknown ARP opcode (opcode=%d).\n", |
| 755 | arp_key->arp_op); |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | |
| 759 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, |
| 760 | arp_key->arp_sip, is_mask); |
| 761 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, |
| 762 | arp_key->arp_tip, is_mask); |
| 763 | SW_FLOW_KEY_PUT(match, ip.proto, |
| 764 | ntohs(arp_key->arp_op), is_mask); |
| 765 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha, |
| 766 | arp_key->arp_sha, ETH_ALEN, is_mask); |
| 767 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha, |
| 768 | arp_key->arp_tha, ETH_ALEN, is_mask); |
| 769 | |
| 770 | attrs &= ~(1 << OVS_KEY_ATTR_ARP); |
| 771 | } |
| 772 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 773 | if (attrs & (1 << OVS_KEY_ATTR_MPLS)) { |
| 774 | const struct ovs_key_mpls *mpls_key; |
| 775 | |
| 776 | mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]); |
| 777 | SW_FLOW_KEY_PUT(match, mpls.top_lse, |
| 778 | mpls_key->mpls_lse, is_mask); |
| 779 | |
| 780 | attrs &= ~(1 << OVS_KEY_ATTR_MPLS); |
| 781 | } |
| 782 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 783 | if (attrs & (1 << OVS_KEY_ATTR_TCP)) { |
| 784 | const struct ovs_key_tcp *tcp_key; |
| 785 | |
| 786 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 787 | SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask); |
| 788 | SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 789 | attrs &= ~(1 << OVS_KEY_ATTR_TCP); |
| 790 | } |
| 791 | |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 792 | if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) { |
Joe Stringer | 1b760fb | 2014-09-07 22:11:08 -0700 | [diff] [blame] | 793 | SW_FLOW_KEY_PUT(match, tp.flags, |
| 794 | nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]), |
| 795 | is_mask); |
Jarno Rajahalme | 5eb26b1 | 2013-10-23 01:44:59 -0700 | [diff] [blame] | 796 | attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS); |
| 797 | } |
| 798 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 799 | if (attrs & (1 << OVS_KEY_ATTR_UDP)) { |
| 800 | const struct ovs_key_udp *udp_key; |
| 801 | |
| 802 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 803 | SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask); |
| 804 | SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 805 | attrs &= ~(1 << OVS_KEY_ATTR_UDP); |
| 806 | } |
| 807 | |
| 808 | if (attrs & (1 << OVS_KEY_ATTR_SCTP)) { |
| 809 | const struct ovs_key_sctp *sctp_key; |
| 810 | |
| 811 | sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 812 | SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask); |
| 813 | SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 814 | attrs &= ~(1 << OVS_KEY_ATTR_SCTP); |
| 815 | } |
| 816 | |
| 817 | if (attrs & (1 << OVS_KEY_ATTR_ICMP)) { |
| 818 | const struct ovs_key_icmp *icmp_key; |
| 819 | |
| 820 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 821 | SW_FLOW_KEY_PUT(match, tp.src, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 822 | htons(icmp_key->icmp_type), is_mask); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 823 | SW_FLOW_KEY_PUT(match, tp.dst, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 824 | htons(icmp_key->icmp_code), is_mask); |
| 825 | attrs &= ~(1 << OVS_KEY_ATTR_ICMP); |
| 826 | } |
| 827 | |
| 828 | if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) { |
| 829 | const struct ovs_key_icmpv6 *icmpv6_key; |
| 830 | |
| 831 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 832 | SW_FLOW_KEY_PUT(match, tp.src, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 833 | htons(icmpv6_key->icmpv6_type), is_mask); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 834 | SW_FLOW_KEY_PUT(match, tp.dst, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 835 | htons(icmpv6_key->icmpv6_code), is_mask); |
| 836 | attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6); |
| 837 | } |
| 838 | |
| 839 | if (attrs & (1 << OVS_KEY_ATTR_ND)) { |
| 840 | const struct ovs_key_nd *nd_key; |
| 841 | |
| 842 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); |
| 843 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target, |
| 844 | nd_key->nd_target, |
| 845 | sizeof(match->key->ipv6.nd.target), |
| 846 | is_mask); |
| 847 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll, |
| 848 | nd_key->nd_sll, ETH_ALEN, is_mask); |
| 849 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll, |
| 850 | nd_key->nd_tll, ETH_ALEN, is_mask); |
| 851 | attrs &= ~(1 << OVS_KEY_ATTR_ND); |
| 852 | } |
| 853 | |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 854 | if (attrs != 0) { |
| 855 | OVS_NLERR("Unknown key attributes (%llx).\n", |
| 856 | (unsigned long long)attrs); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 857 | return -EINVAL; |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 858 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 863 | static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 864 | { |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 865 | struct nlattr *nla; |
| 866 | int rem; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 867 | |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 868 | /* The nlattr stream should already have been validated */ |
| 869 | nla_for_each_nested(nla, attr, rem) { |
| 870 | /* We assume that ovs_key_lens[type] == -1 means that type is a |
| 871 | * nested attribute |
| 872 | */ |
| 873 | if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1) |
| 874 | nlattr_set(nla, val, false); |
| 875 | else |
| 876 | memset(nla_data(nla), val, nla_len(nla)); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | static void mask_set_nlattr(struct nlattr *attr, u8 val) |
| 881 | { |
| 882 | nlattr_set(attr, val, true); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
| 886 | * ovs_nla_get_match - parses Netlink attributes into a flow key and |
| 887 | * mask. In case the 'mask' is NULL, the flow is treated as exact match |
| 888 | * flow. Otherwise, it is treated as a wildcarded flow, except the mask |
| 889 | * does not include any don't care bit. |
| 890 | * @match: receives the extracted flow match information. |
| 891 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
| 892 | * sequence. The fields should of the packet that triggered the creation |
| 893 | * of this flow. |
| 894 | * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink |
| 895 | * attribute specifies the mask field of the wildcarded flow. |
| 896 | */ |
| 897 | int ovs_nla_get_match(struct sw_flow_match *match, |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 898 | const struct nlattr *nla_key, |
| 899 | const struct nlattr *nla_mask) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 900 | { |
| 901 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
| 902 | const struct nlattr *encap; |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 903 | struct nlattr *newmask = NULL; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 904 | u64 key_attrs = 0; |
| 905 | u64 mask_attrs = 0; |
| 906 | bool encap_valid = false; |
| 907 | int err; |
| 908 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 909 | err = parse_flow_nlattrs(nla_key, a, &key_attrs); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 910 | if (err) |
| 911 | return err; |
| 912 | |
| 913 | if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) && |
| 914 | (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) && |
| 915 | (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) { |
| 916 | __be16 tci; |
| 917 | |
| 918 | if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) && |
| 919 | (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) { |
| 920 | OVS_NLERR("Invalid Vlan frame.\n"); |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | |
| 924 | key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 925 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
| 926 | encap = a[OVS_KEY_ATTR_ENCAP]; |
| 927 | key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP); |
| 928 | encap_valid = true; |
| 929 | |
| 930 | if (tci & htons(VLAN_TAG_PRESENT)) { |
| 931 | err = parse_flow_nlattrs(encap, a, &key_attrs); |
| 932 | if (err) |
| 933 | return err; |
| 934 | } else if (!tci) { |
| 935 | /* Corner case for truncated 802.1Q header. */ |
| 936 | if (nla_len(encap)) { |
| 937 | OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n"); |
| 938 | return -EINVAL; |
| 939 | } |
| 940 | } else { |
| 941 | OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n"); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | } |
| 945 | |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 946 | err = ovs_key_from_nlattrs(match, key_attrs, a, false); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 947 | if (err) |
| 948 | return err; |
| 949 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 950 | if (match->mask) { |
| 951 | if (!nla_mask) { |
| 952 | /* Create an exact match mask. We need to set to 0xff |
| 953 | * all the 'match->mask' fields that have been touched |
| 954 | * in 'match->key'. We cannot simply memset |
| 955 | * 'match->mask', because padding bytes and fields not |
| 956 | * specified in 'match->key' should be left to 0. |
| 957 | * Instead, we use a stream of netlink attributes, |
| 958 | * copied from 'key' and set to 0xff. |
| 959 | * ovs_key_from_nlattrs() will take care of filling |
| 960 | * 'match->mask' appropriately. |
| 961 | */ |
| 962 | newmask = kmemdup(nla_key, |
| 963 | nla_total_size(nla_len(nla_key)), |
| 964 | GFP_KERNEL); |
| 965 | if (!newmask) |
| 966 | return -ENOMEM; |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 967 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 968 | mask_set_nlattr(newmask, 0xff); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 969 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 970 | /* The userspace does not send tunnel attributes that |
| 971 | * are 0, but we should not wildcard them nonetheless. |
| 972 | */ |
| 973 | if (match->key->tun_key.ipv4_dst) |
| 974 | SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, |
| 975 | 0xff, true); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 976 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 977 | nla_mask = newmask; |
| 978 | } |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 979 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 980 | err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 981 | if (err) |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 982 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 983 | |
Pravin B Shelar | a85311b | 2014-10-19 12:03:40 -0700 | [diff] [blame] | 984 | /* Always match on tci. */ |
| 985 | SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true); |
| 986 | |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 987 | if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 988 | __be16 eth_type = 0; |
| 989 | __be16 tci = 0; |
| 990 | |
| 991 | if (!encap_valid) { |
| 992 | OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n"); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 993 | err = -EINVAL; |
| 994 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP); |
| 998 | if (a[OVS_KEY_ATTR_ETHERTYPE]) |
| 999 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); |
| 1000 | |
| 1001 | if (eth_type == htons(0xffff)) { |
| 1002 | mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); |
| 1003 | encap = a[OVS_KEY_ATTR_ENCAP]; |
| 1004 | err = parse_flow_mask_nlattrs(encap, a, &mask_attrs); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1005 | if (err) |
| 1006 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n", |
| 1009 | ntohs(eth_type)); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1010 | err = -EINVAL; |
| 1011 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | if (a[OVS_KEY_ATTR_VLAN]) |
| 1015 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
| 1016 | |
| 1017 | if (!(tci & htons(VLAN_TAG_PRESENT))) { |
| 1018 | OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci)); |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1019 | err = -EINVAL; |
| 1020 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 1024 | err = ovs_key_from_nlattrs(match, mask_attrs, a, true); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1025 | if (err) |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1026 | goto free_newmask; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | if (!match_validate(match, key_attrs, mask_attrs)) |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1030 | err = -EINVAL; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1031 | |
Pravin B Shelar | f47de06 | 2014-10-16 21:55:45 -0700 | [diff] [blame] | 1032 | free_newmask: |
| 1033 | kfree(newmask); |
| 1034 | return err; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key. |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1039 | * @key: Receives extracted in_port, priority, tun_key and skb_mark. |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1040 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
| 1041 | * sequence. |
| 1042 | * |
| 1043 | * This parses a series of Netlink attributes that form a flow key, which must |
| 1044 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to |
| 1045 | * get the metadata, that is, the parts of the flow key that cannot be |
| 1046 | * extracted from the packet itself. |
| 1047 | */ |
| 1048 | |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1049 | int ovs_nla_get_flow_metadata(const struct nlattr *attr, |
| 1050 | struct sw_flow_key *key) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1051 | { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1052 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1053 | struct sw_flow_match match; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1054 | u64 attrs = 0; |
| 1055 | int err; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1056 | |
| 1057 | err = parse_flow_nlattrs(attr, a, &attrs); |
| 1058 | if (err) |
| 1059 | return -EINVAL; |
| 1060 | |
| 1061 | memset(&match, 0, sizeof(match)); |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1062 | match.key = key; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1063 | |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1064 | key->phy.in_port = DP_MAX_PORTS; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1065 | |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 1066 | return metadata_from_nlattrs(&match, &attrs, a, false); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | int ovs_nla_put_flow(const struct sw_flow_key *swkey, |
| 1070 | const struct sw_flow_key *output, struct sk_buff *skb) |
| 1071 | { |
| 1072 | struct ovs_key_ethernet *eth_key; |
| 1073 | struct nlattr *nla, *encap; |
| 1074 | bool is_mask = (swkey != output); |
| 1075 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 1076 | if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id)) |
| 1077 | goto nla_put_failure; |
| 1078 | |
| 1079 | if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash)) |
| 1080 | goto nla_put_failure; |
| 1081 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1082 | if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority)) |
| 1083 | goto nla_put_failure; |
| 1084 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1085 | if ((swkey->tun_key.ipv4_dst || is_mask)) { |
| 1086 | const struct geneve_opt *opts = NULL; |
| 1087 | |
| 1088 | if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT) |
| 1089 | opts = GENEVE_OPTS(output, swkey->tun_opts_len); |
| 1090 | |
| 1091 | if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts, |
| 1092 | swkey->tun_opts_len)) |
| 1093 | goto nla_put_failure; |
| 1094 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1095 | |
| 1096 | if (swkey->phy.in_port == DP_MAX_PORTS) { |
| 1097 | if (is_mask && (output->phy.in_port == 0xffff)) |
| 1098 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff)) |
| 1099 | goto nla_put_failure; |
| 1100 | } else { |
| 1101 | u16 upper_u16; |
| 1102 | upper_u16 = !is_mask ? 0 : 0xffff; |
| 1103 | |
| 1104 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, |
| 1105 | (upper_u16 << 16) | output->phy.in_port)) |
| 1106 | goto nla_put_failure; |
| 1107 | } |
| 1108 | |
| 1109 | if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark)) |
| 1110 | goto nla_put_failure; |
| 1111 | |
| 1112 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
| 1113 | if (!nla) |
| 1114 | goto nla_put_failure; |
| 1115 | |
| 1116 | eth_key = nla_data(nla); |
Joe Perches | 8c63ff0 | 2014-02-18 11:15:45 -0800 | [diff] [blame] | 1117 | ether_addr_copy(eth_key->eth_src, output->eth.src); |
| 1118 | ether_addr_copy(eth_key->eth_dst, output->eth.dst); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1119 | |
| 1120 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { |
| 1121 | __be16 eth_type; |
| 1122 | eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff); |
| 1123 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) || |
| 1124 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci)) |
| 1125 | goto nla_put_failure; |
| 1126 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); |
| 1127 | if (!swkey->eth.tci) |
| 1128 | goto unencap; |
| 1129 | } else |
| 1130 | encap = NULL; |
| 1131 | |
| 1132 | if (swkey->eth.type == htons(ETH_P_802_2)) { |
| 1133 | /* |
| 1134 | * Ethertype 802.2 is represented in the netlink with omitted |
| 1135 | * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and |
| 1136 | * 0xffff in the mask attribute. Ethertype can also |
| 1137 | * be wildcarded. |
| 1138 | */ |
| 1139 | if (is_mask && output->eth.type) |
| 1140 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, |
| 1141 | output->eth.type)) |
| 1142 | goto nla_put_failure; |
| 1143 | goto unencap; |
| 1144 | } |
| 1145 | |
| 1146 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type)) |
| 1147 | goto nla_put_failure; |
| 1148 | |
| 1149 | if (swkey->eth.type == htons(ETH_P_IP)) { |
| 1150 | struct ovs_key_ipv4 *ipv4_key; |
| 1151 | |
| 1152 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
| 1153 | if (!nla) |
| 1154 | goto nla_put_failure; |
| 1155 | ipv4_key = nla_data(nla); |
| 1156 | ipv4_key->ipv4_src = output->ipv4.addr.src; |
| 1157 | ipv4_key->ipv4_dst = output->ipv4.addr.dst; |
| 1158 | ipv4_key->ipv4_proto = output->ip.proto; |
| 1159 | ipv4_key->ipv4_tos = output->ip.tos; |
| 1160 | ipv4_key->ipv4_ttl = output->ip.ttl; |
| 1161 | ipv4_key->ipv4_frag = output->ip.frag; |
| 1162 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
| 1163 | struct ovs_key_ipv6 *ipv6_key; |
| 1164 | |
| 1165 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
| 1166 | if (!nla) |
| 1167 | goto nla_put_failure; |
| 1168 | ipv6_key = nla_data(nla); |
| 1169 | memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src, |
| 1170 | sizeof(ipv6_key->ipv6_src)); |
| 1171 | memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst, |
| 1172 | sizeof(ipv6_key->ipv6_dst)); |
| 1173 | ipv6_key->ipv6_label = output->ipv6.label; |
| 1174 | ipv6_key->ipv6_proto = output->ip.proto; |
| 1175 | ipv6_key->ipv6_tclass = output->ip.tos; |
| 1176 | ipv6_key->ipv6_hlimit = output->ip.ttl; |
| 1177 | ipv6_key->ipv6_frag = output->ip.frag; |
| 1178 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
| 1179 | swkey->eth.type == htons(ETH_P_RARP)) { |
| 1180 | struct ovs_key_arp *arp_key; |
| 1181 | |
| 1182 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
| 1183 | if (!nla) |
| 1184 | goto nla_put_failure; |
| 1185 | arp_key = nla_data(nla); |
| 1186 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
| 1187 | arp_key->arp_sip = output->ipv4.addr.src; |
| 1188 | arp_key->arp_tip = output->ipv4.addr.dst; |
| 1189 | arp_key->arp_op = htons(output->ip.proto); |
Joe Perches | 8c63ff0 | 2014-02-18 11:15:45 -0800 | [diff] [blame] | 1190 | ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha); |
| 1191 | ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1192 | } else if (eth_p_mpls(swkey->eth.type)) { |
| 1193 | struct ovs_key_mpls *mpls_key; |
| 1194 | |
| 1195 | nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key)); |
| 1196 | if (!nla) |
| 1197 | goto nla_put_failure; |
| 1198 | mpls_key = nla_data(nla); |
| 1199 | mpls_key->mpls_lse = output->mpls.top_lse; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | if ((swkey->eth.type == htons(ETH_P_IP) || |
| 1203 | swkey->eth.type == htons(ETH_P_IPV6)) && |
| 1204 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
| 1205 | |
| 1206 | if (swkey->ip.proto == IPPROTO_TCP) { |
| 1207 | struct ovs_key_tcp *tcp_key; |
| 1208 | |
| 1209 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
| 1210 | if (!nla) |
| 1211 | goto nla_put_failure; |
| 1212 | tcp_key = nla_data(nla); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1213 | tcp_key->tcp_src = output->tp.src; |
| 1214 | tcp_key->tcp_dst = output->tp.dst; |
| 1215 | if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS, |
| 1216 | output->tp.flags)) |
| 1217 | goto nla_put_failure; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1218 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
| 1219 | struct ovs_key_udp *udp_key; |
| 1220 | |
| 1221 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
| 1222 | if (!nla) |
| 1223 | goto nla_put_failure; |
| 1224 | udp_key = nla_data(nla); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1225 | udp_key->udp_src = output->tp.src; |
| 1226 | udp_key->udp_dst = output->tp.dst; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1227 | } else if (swkey->ip.proto == IPPROTO_SCTP) { |
| 1228 | struct ovs_key_sctp *sctp_key; |
| 1229 | |
| 1230 | nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key)); |
| 1231 | if (!nla) |
| 1232 | goto nla_put_failure; |
| 1233 | sctp_key = nla_data(nla); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1234 | sctp_key->sctp_src = output->tp.src; |
| 1235 | sctp_key->sctp_dst = output->tp.dst; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1236 | } else if (swkey->eth.type == htons(ETH_P_IP) && |
| 1237 | swkey->ip.proto == IPPROTO_ICMP) { |
| 1238 | struct ovs_key_icmp *icmp_key; |
| 1239 | |
| 1240 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
| 1241 | if (!nla) |
| 1242 | goto nla_put_failure; |
| 1243 | icmp_key = nla_data(nla); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1244 | icmp_key->icmp_type = ntohs(output->tp.src); |
| 1245 | icmp_key->icmp_code = ntohs(output->tp.dst); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1246 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && |
| 1247 | swkey->ip.proto == IPPROTO_ICMPV6) { |
| 1248 | struct ovs_key_icmpv6 *icmpv6_key; |
| 1249 | |
| 1250 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
| 1251 | sizeof(*icmpv6_key)); |
| 1252 | if (!nla) |
| 1253 | goto nla_put_failure; |
| 1254 | icmpv6_key = nla_data(nla); |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1255 | icmpv6_key->icmpv6_type = ntohs(output->tp.src); |
| 1256 | icmpv6_key->icmpv6_code = ntohs(output->tp.dst); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1257 | |
| 1258 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
| 1259 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { |
| 1260 | struct ovs_key_nd *nd_key; |
| 1261 | |
| 1262 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
| 1263 | if (!nla) |
| 1264 | goto nla_put_failure; |
| 1265 | nd_key = nla_data(nla); |
| 1266 | memcpy(nd_key->nd_target, &output->ipv6.nd.target, |
| 1267 | sizeof(nd_key->nd_target)); |
Joe Perches | 8c63ff0 | 2014-02-18 11:15:45 -0800 | [diff] [blame] | 1268 | ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll); |
| 1269 | ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | unencap: |
| 1275 | if (encap) |
| 1276 | nla_nest_end(skb, encap); |
| 1277 | |
| 1278 | return 0; |
| 1279 | |
| 1280 | nla_put_failure: |
| 1281 | return -EMSGSIZE; |
| 1282 | } |
| 1283 | |
| 1284 | #define MAX_ACTIONS_BUFSIZE (32 * 1024) |
| 1285 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1286 | static struct sw_flow_actions *nla_alloc_flow_actions(int size) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1287 | { |
| 1288 | struct sw_flow_actions *sfa; |
| 1289 | |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 1290 | if (size > MAX_ACTIONS_BUFSIZE) { |
| 1291 | OVS_NLERR("Flow action size (%u bytes) exceeds maximum", size); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1292 | return ERR_PTR(-EINVAL); |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 1293 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1294 | |
| 1295 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); |
| 1296 | if (!sfa) |
| 1297 | return ERR_PTR(-ENOMEM); |
| 1298 | |
| 1299 | sfa->actions_len = 0; |
| 1300 | return sfa; |
| 1301 | } |
| 1302 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1303 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. |
| 1304 | * The caller must hold rcu_read_lock for this to be sensible. */ |
| 1305 | void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts) |
| 1306 | { |
Daniel Borkmann | 11d6c461 | 2013-12-10 12:02:03 +0100 | [diff] [blame] | 1307 | kfree_rcu(sf_acts, rcu); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, |
| 1311 | int attr_len) |
| 1312 | { |
| 1313 | |
| 1314 | struct sw_flow_actions *acts; |
| 1315 | int new_acts_size; |
| 1316 | int req_size = NLA_ALIGN(attr_len); |
| 1317 | int next_offset = offsetof(struct sw_flow_actions, actions) + |
| 1318 | (*sfa)->actions_len; |
| 1319 | |
| 1320 | if (req_size <= (ksize(*sfa) - next_offset)) |
| 1321 | goto out; |
| 1322 | |
| 1323 | new_acts_size = ksize(*sfa) * 2; |
| 1324 | |
| 1325 | if (new_acts_size > MAX_ACTIONS_BUFSIZE) { |
| 1326 | if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) |
| 1327 | return ERR_PTR(-EMSGSIZE); |
| 1328 | new_acts_size = MAX_ACTIONS_BUFSIZE; |
| 1329 | } |
| 1330 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1331 | acts = nla_alloc_flow_actions(new_acts_size); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1332 | if (IS_ERR(acts)) |
| 1333 | return (void *)acts; |
| 1334 | |
| 1335 | memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len); |
| 1336 | acts->actions_len = (*sfa)->actions_len; |
| 1337 | kfree(*sfa); |
| 1338 | *sfa = acts; |
| 1339 | |
| 1340 | out: |
| 1341 | (*sfa)->actions_len += req_size; |
| 1342 | return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset); |
| 1343 | } |
| 1344 | |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1345 | static struct nlattr *__add_action(struct sw_flow_actions **sfa, |
| 1346 | int attrtype, void *data, int len) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1347 | { |
| 1348 | struct nlattr *a; |
| 1349 | |
| 1350 | a = reserve_sfa_size(sfa, nla_attr_size(len)); |
| 1351 | if (IS_ERR(a)) |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1352 | return a; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1353 | |
| 1354 | a->nla_type = attrtype; |
| 1355 | a->nla_len = nla_attr_size(len); |
| 1356 | |
| 1357 | if (data) |
| 1358 | memcpy(nla_data(a), data, len); |
| 1359 | memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len)); |
| 1360 | |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1361 | return a; |
| 1362 | } |
| 1363 | |
| 1364 | static int add_action(struct sw_flow_actions **sfa, int attrtype, |
| 1365 | void *data, int len) |
| 1366 | { |
| 1367 | struct nlattr *a; |
| 1368 | |
| 1369 | a = __add_action(sfa, attrtype, data, len); |
| 1370 | if (IS_ERR(a)) |
| 1371 | return PTR_ERR(a); |
| 1372 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1373 | return 0; |
| 1374 | } |
| 1375 | |
| 1376 | static inline int add_nested_action_start(struct sw_flow_actions **sfa, |
| 1377 | int attrtype) |
| 1378 | { |
| 1379 | int used = (*sfa)->actions_len; |
| 1380 | int err; |
| 1381 | |
| 1382 | err = add_action(sfa, attrtype, NULL, 0); |
| 1383 | if (err) |
| 1384 | return err; |
| 1385 | |
| 1386 | return used; |
| 1387 | } |
| 1388 | |
| 1389 | static inline void add_nested_action_end(struct sw_flow_actions *sfa, |
| 1390 | int st_offset) |
| 1391 | { |
| 1392 | struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + |
| 1393 | st_offset); |
| 1394 | |
| 1395 | a->nla_len = sfa->actions_len - st_offset; |
| 1396 | } |
| 1397 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1398 | static int __ovs_nla_copy_actions(const struct nlattr *attr, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1399 | const struct sw_flow_key *key, |
| 1400 | int depth, struct sw_flow_actions **sfa, |
| 1401 | __be16 eth_type, __be16 vlan_tci); |
| 1402 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1403 | static int validate_and_copy_sample(const struct nlattr *attr, |
| 1404 | const struct sw_flow_key *key, int depth, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1405 | struct sw_flow_actions **sfa, |
| 1406 | __be16 eth_type, __be16 vlan_tci) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1407 | { |
| 1408 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; |
| 1409 | const struct nlattr *probability, *actions; |
| 1410 | const struct nlattr *a; |
| 1411 | int rem, start, err, st_acts; |
| 1412 | |
| 1413 | memset(attrs, 0, sizeof(attrs)); |
| 1414 | nla_for_each_nested(a, attr, rem) { |
| 1415 | int type = nla_type(a); |
| 1416 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) |
| 1417 | return -EINVAL; |
| 1418 | attrs[type] = a; |
| 1419 | } |
| 1420 | if (rem) |
| 1421 | return -EINVAL; |
| 1422 | |
| 1423 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; |
| 1424 | if (!probability || nla_len(probability) != sizeof(u32)) |
| 1425 | return -EINVAL; |
| 1426 | |
| 1427 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; |
| 1428 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) |
| 1429 | return -EINVAL; |
| 1430 | |
| 1431 | /* validation done, copy sample action. */ |
| 1432 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE); |
| 1433 | if (start < 0) |
| 1434 | return start; |
| 1435 | err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, |
| 1436 | nla_data(probability), sizeof(u32)); |
| 1437 | if (err) |
| 1438 | return err; |
| 1439 | st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS); |
| 1440 | if (st_acts < 0) |
| 1441 | return st_acts; |
| 1442 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1443 | err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1444 | eth_type, vlan_tci); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1445 | if (err) |
| 1446 | return err; |
| 1447 | |
| 1448 | add_nested_action_end(*sfa, st_acts); |
| 1449 | add_nested_action_end(*sfa, start); |
| 1450 | |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1454 | static int validate_tp_port(const struct sw_flow_key *flow_key, |
| 1455 | __be16 eth_type) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1456 | { |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1457 | if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) && |
Jarno Rajahalme | 1139e24 | 2014-05-05 09:54:49 -0700 | [diff] [blame] | 1458 | (flow_key->tp.src || flow_key->tp.dst)) |
| 1459 | return 0; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1460 | |
| 1461 | return -EINVAL; |
| 1462 | } |
| 1463 | |
| 1464 | void ovs_match_init(struct sw_flow_match *match, |
| 1465 | struct sw_flow_key *key, |
| 1466 | struct sw_flow_mask *mask) |
| 1467 | { |
| 1468 | memset(match, 0, sizeof(*match)); |
| 1469 | match->key = key; |
| 1470 | match->mask = mask; |
| 1471 | |
| 1472 | memset(key, 0, sizeof(*key)); |
| 1473 | |
| 1474 | if (mask) { |
| 1475 | memset(&mask->key, 0, sizeof(mask->key)); |
| 1476 | mask->range.start = mask->range.end = 0; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static int validate_and_copy_set_tun(const struct nlattr *attr, |
| 1481 | struct sw_flow_actions **sfa) |
| 1482 | { |
| 1483 | struct sw_flow_match match; |
| 1484 | struct sw_flow_key key; |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1485 | struct ovs_tunnel_info *tun_info; |
| 1486 | struct nlattr *a; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1487 | int err, start; |
| 1488 | |
| 1489 | ovs_match_init(&match, &key, NULL); |
| 1490 | err = ipv4_tun_from_nlattr(nla_data(attr), &match, false); |
| 1491 | if (err) |
| 1492 | return err; |
| 1493 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1494 | if (key.tun_opts_len) { |
| 1495 | struct geneve_opt *option = GENEVE_OPTS(&key, |
| 1496 | key.tun_opts_len); |
| 1497 | int opts_len = key.tun_opts_len; |
| 1498 | bool crit_opt = false; |
| 1499 | |
| 1500 | while (opts_len > 0) { |
| 1501 | int len; |
| 1502 | |
| 1503 | if (opts_len < sizeof(*option)) |
| 1504 | return -EINVAL; |
| 1505 | |
| 1506 | len = sizeof(*option) + option->length * 4; |
| 1507 | if (len > opts_len) |
| 1508 | return -EINVAL; |
| 1509 | |
| 1510 | crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE); |
| 1511 | |
| 1512 | option = (struct geneve_opt *)((u8 *)option + len); |
| 1513 | opts_len -= len; |
| 1514 | }; |
| 1515 | |
| 1516 | key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0; |
| 1517 | }; |
| 1518 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1519 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET); |
| 1520 | if (start < 0) |
| 1521 | return start; |
| 1522 | |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1523 | a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL, |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1524 | sizeof(*tun_info) + key.tun_opts_len); |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1525 | if (IS_ERR(a)) |
| 1526 | return PTR_ERR(a); |
| 1527 | |
| 1528 | tun_info = nla_data(a); |
| 1529 | tun_info->tunnel = key.tun_key; |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1530 | tun_info->options_len = key.tun_opts_len; |
| 1531 | |
| 1532 | if (tun_info->options_len) { |
| 1533 | /* We need to store the options in the action itself since |
| 1534 | * everything else will go away after flow setup. We can append |
| 1535 | * it to tun_info and then point there. |
| 1536 | */ |
| 1537 | memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len), |
| 1538 | key.tun_opts_len); |
| 1539 | tun_info->options = (struct geneve_opt *)(tun_info + 1); |
| 1540 | } else { |
| 1541 | tun_info->options = NULL; |
| 1542 | } |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1543 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1544 | add_nested_action_end(*sfa, start); |
| 1545 | |
| 1546 | return err; |
| 1547 | } |
| 1548 | |
| 1549 | static int validate_set(const struct nlattr *a, |
| 1550 | const struct sw_flow_key *flow_key, |
| 1551 | struct sw_flow_actions **sfa, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1552 | bool *set_tun, __be16 eth_type) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1553 | { |
| 1554 | const struct nlattr *ovs_key = nla_data(a); |
| 1555 | int key_type = nla_type(ovs_key); |
| 1556 | |
| 1557 | /* There can be only one key in a action */ |
| 1558 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) |
| 1559 | return -EINVAL; |
| 1560 | |
| 1561 | if (key_type > OVS_KEY_ATTR_MAX || |
| 1562 | (ovs_key_lens[key_type] != nla_len(ovs_key) && |
| 1563 | ovs_key_lens[key_type] != -1)) |
| 1564 | return -EINVAL; |
| 1565 | |
| 1566 | switch (key_type) { |
| 1567 | const struct ovs_key_ipv4 *ipv4_key; |
| 1568 | const struct ovs_key_ipv6 *ipv6_key; |
| 1569 | int err; |
| 1570 | |
| 1571 | case OVS_KEY_ATTR_PRIORITY: |
| 1572 | case OVS_KEY_ATTR_SKB_MARK: |
| 1573 | case OVS_KEY_ATTR_ETHERNET: |
| 1574 | break; |
| 1575 | |
| 1576 | case OVS_KEY_ATTR_TUNNEL: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1577 | if (eth_p_mpls(eth_type)) |
| 1578 | return -EINVAL; |
| 1579 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1580 | *set_tun = true; |
| 1581 | err = validate_and_copy_set_tun(a, sfa); |
| 1582 | if (err) |
| 1583 | return err; |
| 1584 | break; |
| 1585 | |
| 1586 | case OVS_KEY_ATTR_IPV4: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1587 | if (eth_type != htons(ETH_P_IP)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1588 | return -EINVAL; |
| 1589 | |
| 1590 | if (!flow_key->ip.proto) |
| 1591 | return -EINVAL; |
| 1592 | |
| 1593 | ipv4_key = nla_data(ovs_key); |
| 1594 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) |
| 1595 | return -EINVAL; |
| 1596 | |
| 1597 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) |
| 1598 | return -EINVAL; |
| 1599 | |
| 1600 | break; |
| 1601 | |
| 1602 | case OVS_KEY_ATTR_IPV6: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1603 | if (eth_type != htons(ETH_P_IPV6)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1604 | return -EINVAL; |
| 1605 | |
| 1606 | if (!flow_key->ip.proto) |
| 1607 | return -EINVAL; |
| 1608 | |
| 1609 | ipv6_key = nla_data(ovs_key); |
| 1610 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) |
| 1611 | return -EINVAL; |
| 1612 | |
| 1613 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) |
| 1614 | return -EINVAL; |
| 1615 | |
| 1616 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) |
| 1617 | return -EINVAL; |
| 1618 | |
| 1619 | break; |
| 1620 | |
| 1621 | case OVS_KEY_ATTR_TCP: |
| 1622 | if (flow_key->ip.proto != IPPROTO_TCP) |
| 1623 | return -EINVAL; |
| 1624 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1625 | return validate_tp_port(flow_key, eth_type); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1626 | |
| 1627 | case OVS_KEY_ATTR_UDP: |
| 1628 | if (flow_key->ip.proto != IPPROTO_UDP) |
| 1629 | return -EINVAL; |
| 1630 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1631 | return validate_tp_port(flow_key, eth_type); |
| 1632 | |
| 1633 | case OVS_KEY_ATTR_MPLS: |
| 1634 | if (!eth_p_mpls(eth_type)) |
| 1635 | return -EINVAL; |
| 1636 | break; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1637 | |
| 1638 | case OVS_KEY_ATTR_SCTP: |
| 1639 | if (flow_key->ip.proto != IPPROTO_SCTP) |
| 1640 | return -EINVAL; |
| 1641 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1642 | return validate_tp_port(flow_key, eth_type); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1643 | |
| 1644 | default: |
| 1645 | return -EINVAL; |
| 1646 | } |
| 1647 | |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | static int validate_userspace(const struct nlattr *attr) |
| 1652 | { |
| 1653 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { |
| 1654 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, |
| 1655 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC }, |
| 1656 | }; |
| 1657 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; |
| 1658 | int error; |
| 1659 | |
| 1660 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, |
| 1661 | attr, userspace_policy); |
| 1662 | if (error) |
| 1663 | return error; |
| 1664 | |
| 1665 | if (!a[OVS_USERSPACE_ATTR_PID] || |
| 1666 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) |
| 1667 | return -EINVAL; |
| 1668 | |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | static int copy_action(const struct nlattr *from, |
| 1673 | struct sw_flow_actions **sfa) |
| 1674 | { |
| 1675 | int totlen = NLA_ALIGN(from->nla_len); |
| 1676 | struct nlattr *to; |
| 1677 | |
| 1678 | to = reserve_sfa_size(sfa, from->nla_len); |
| 1679 | if (IS_ERR(to)) |
| 1680 | return PTR_ERR(to); |
| 1681 | |
| 1682 | memcpy(to, from, totlen); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1686 | static int __ovs_nla_copy_actions(const struct nlattr *attr, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1687 | const struct sw_flow_key *key, |
| 1688 | int depth, struct sw_flow_actions **sfa, |
| 1689 | __be16 eth_type, __be16 vlan_tci) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1690 | { |
| 1691 | const struct nlattr *a; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1692 | bool out_tnl_port = false; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1693 | int rem, err; |
| 1694 | |
| 1695 | if (depth >= SAMPLE_ACTION_DEPTH) |
| 1696 | return -EOVERFLOW; |
| 1697 | |
| 1698 | nla_for_each_nested(a, attr, rem) { |
| 1699 | /* Expected argument lengths, (u32)-1 for variable length. */ |
| 1700 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { |
| 1701 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 1702 | [OVS_ACTION_ATTR_RECIRC] = sizeof(u32), |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1703 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1704 | [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls), |
| 1705 | [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16), |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1706 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), |
| 1707 | [OVS_ACTION_ATTR_POP_VLAN] = 0, |
| 1708 | [OVS_ACTION_ATTR_SET] = (u32)-1, |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 1709 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1, |
| 1710 | [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1711 | }; |
| 1712 | const struct ovs_action_push_vlan *vlan; |
| 1713 | int type = nla_type(a); |
| 1714 | bool skip_copy; |
| 1715 | |
| 1716 | if (type > OVS_ACTION_ATTR_MAX || |
| 1717 | (action_lens[type] != nla_len(a) && |
| 1718 | action_lens[type] != (u32)-1)) |
| 1719 | return -EINVAL; |
| 1720 | |
| 1721 | skip_copy = false; |
| 1722 | switch (type) { |
| 1723 | case OVS_ACTION_ATTR_UNSPEC: |
| 1724 | return -EINVAL; |
| 1725 | |
| 1726 | case OVS_ACTION_ATTR_USERSPACE: |
| 1727 | err = validate_userspace(a); |
| 1728 | if (err) |
| 1729 | return err; |
| 1730 | break; |
| 1731 | |
| 1732 | case OVS_ACTION_ATTR_OUTPUT: |
| 1733 | if (nla_get_u32(a) >= DP_MAX_PORTS) |
| 1734 | return -EINVAL; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1735 | out_tnl_port = false; |
| 1736 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1737 | break; |
| 1738 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 1739 | case OVS_ACTION_ATTR_HASH: { |
| 1740 | const struct ovs_action_hash *act_hash = nla_data(a); |
| 1741 | |
| 1742 | switch (act_hash->hash_alg) { |
| 1743 | case OVS_HASH_ALG_L4: |
| 1744 | break; |
| 1745 | default: |
| 1746 | return -EINVAL; |
| 1747 | } |
| 1748 | |
| 1749 | break; |
| 1750 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1751 | |
| 1752 | case OVS_ACTION_ATTR_POP_VLAN: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1753 | vlan_tci = htons(0); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1754 | break; |
| 1755 | |
| 1756 | case OVS_ACTION_ATTR_PUSH_VLAN: |
| 1757 | vlan = nla_data(a); |
| 1758 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) |
| 1759 | return -EINVAL; |
| 1760 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) |
| 1761 | return -EINVAL; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1762 | vlan_tci = vlan->vlan_tci; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 1765 | case OVS_ACTION_ATTR_RECIRC: |
| 1766 | break; |
| 1767 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1768 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
| 1769 | const struct ovs_action_push_mpls *mpls = nla_data(a); |
| 1770 | |
| 1771 | /* Networking stack do not allow simultaneous Tunnel |
| 1772 | * and MPLS GSO. |
| 1773 | */ |
| 1774 | if (out_tnl_port) |
| 1775 | return -EINVAL; |
| 1776 | |
| 1777 | if (!eth_p_mpls(mpls->mpls_ethertype)) |
| 1778 | return -EINVAL; |
| 1779 | /* Prohibit push MPLS other than to a white list |
| 1780 | * for packets that have a known tag order. |
| 1781 | */ |
| 1782 | if (vlan_tci & htons(VLAN_TAG_PRESENT) || |
| 1783 | (eth_type != htons(ETH_P_IP) && |
| 1784 | eth_type != htons(ETH_P_IPV6) && |
| 1785 | eth_type != htons(ETH_P_ARP) && |
| 1786 | eth_type != htons(ETH_P_RARP) && |
| 1787 | !eth_p_mpls(eth_type))) |
| 1788 | return -EINVAL; |
| 1789 | eth_type = mpls->mpls_ethertype; |
| 1790 | break; |
| 1791 | } |
| 1792 | |
| 1793 | case OVS_ACTION_ATTR_POP_MPLS: |
| 1794 | if (vlan_tci & htons(VLAN_TAG_PRESENT) || |
| 1795 | !eth_p_mpls(eth_type)) |
| 1796 | return -EINVAL; |
| 1797 | |
| 1798 | /* Disallow subsequent L2.5+ set and mpls_pop actions |
| 1799 | * as there is no check here to ensure that the new |
| 1800 | * eth_type is valid and thus set actions could |
| 1801 | * write off the end of the packet or otherwise |
| 1802 | * corrupt it. |
| 1803 | * |
| 1804 | * Support for these actions is planned using packet |
| 1805 | * recirculation. |
| 1806 | */ |
| 1807 | eth_type = htons(0); |
| 1808 | break; |
| 1809 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1810 | case OVS_ACTION_ATTR_SET: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1811 | err = validate_set(a, key, sfa, |
| 1812 | &out_tnl_port, eth_type); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1813 | if (err) |
| 1814 | return err; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1815 | |
| 1816 | skip_copy = out_tnl_port; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1817 | break; |
| 1818 | |
| 1819 | case OVS_ACTION_ATTR_SAMPLE: |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1820 | err = validate_and_copy_sample(a, key, depth, sfa, |
| 1821 | eth_type, vlan_tci); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1822 | if (err) |
| 1823 | return err; |
| 1824 | skip_copy = true; |
| 1825 | break; |
| 1826 | |
| 1827 | default: |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 1828 | OVS_NLERR("Unknown tunnel attribute (%d).\n", type); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1829 | return -EINVAL; |
| 1830 | } |
| 1831 | if (!skip_copy) { |
| 1832 | err = copy_action(a, sfa); |
| 1833 | if (err) |
| 1834 | return err; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | if (rem > 0) |
| 1839 | return -EINVAL; |
| 1840 | |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1844 | int ovs_nla_copy_actions(const struct nlattr *attr, |
| 1845 | const struct sw_flow_key *key, |
| 1846 | struct sw_flow_actions **sfa) |
| 1847 | { |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1848 | int err; |
| 1849 | |
| 1850 | *sfa = nla_alloc_flow_actions(nla_len(attr)); |
| 1851 | if (IS_ERR(*sfa)) |
| 1852 | return PTR_ERR(*sfa); |
| 1853 | |
| 1854 | err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type, |
| 1855 | key->eth.tci); |
| 1856 | if (err) |
| 1857 | kfree(*sfa); |
| 1858 | |
| 1859 | return err; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1862 | static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb) |
| 1863 | { |
| 1864 | const struct nlattr *a; |
| 1865 | struct nlattr *start; |
| 1866 | int err = 0, rem; |
| 1867 | |
| 1868 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE); |
| 1869 | if (!start) |
| 1870 | return -EMSGSIZE; |
| 1871 | |
| 1872 | nla_for_each_nested(a, attr, rem) { |
| 1873 | int type = nla_type(a); |
| 1874 | struct nlattr *st_sample; |
| 1875 | |
| 1876 | switch (type) { |
| 1877 | case OVS_SAMPLE_ATTR_PROBABILITY: |
| 1878 | if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, |
| 1879 | sizeof(u32), nla_data(a))) |
| 1880 | return -EMSGSIZE; |
| 1881 | break; |
| 1882 | case OVS_SAMPLE_ATTR_ACTIONS: |
| 1883 | st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS); |
| 1884 | if (!st_sample) |
| 1885 | return -EMSGSIZE; |
| 1886 | err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb); |
| 1887 | if (err) |
| 1888 | return err; |
| 1889 | nla_nest_end(skb, st_sample); |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | nla_nest_end(skb, start); |
| 1895 | return err; |
| 1896 | } |
| 1897 | |
| 1898 | static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb) |
| 1899 | { |
| 1900 | const struct nlattr *ovs_key = nla_data(a); |
| 1901 | int key_type = nla_type(ovs_key); |
| 1902 | struct nlattr *start; |
| 1903 | int err; |
| 1904 | |
| 1905 | switch (key_type) { |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1906 | case OVS_KEY_ATTR_TUNNEL_INFO: { |
| 1907 | struct ovs_tunnel_info *tun_info = nla_data(ovs_key); |
| 1908 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1909 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SET); |
| 1910 | if (!start) |
| 1911 | return -EMSGSIZE; |
| 1912 | |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1913 | err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel, |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 1914 | tun_info->options_len ? |
| 1915 | tun_info->options : NULL, |
| 1916 | tun_info->options_len); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1917 | if (err) |
| 1918 | return err; |
| 1919 | nla_nest_end(skb, start); |
| 1920 | break; |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 1921 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1922 | default: |
| 1923 | if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key)) |
| 1924 | return -EMSGSIZE; |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb) |
| 1932 | { |
| 1933 | const struct nlattr *a; |
| 1934 | int rem, err; |
| 1935 | |
| 1936 | nla_for_each_attr(a, attr, len, rem) { |
| 1937 | int type = nla_type(a); |
| 1938 | |
| 1939 | switch (type) { |
| 1940 | case OVS_ACTION_ATTR_SET: |
| 1941 | err = set_action_to_attr(a, skb); |
| 1942 | if (err) |
| 1943 | return err; |
| 1944 | break; |
| 1945 | |
| 1946 | case OVS_ACTION_ATTR_SAMPLE: |
| 1947 | err = sample_action_to_attr(a, skb); |
| 1948 | if (err) |
| 1949 | return err; |
| 1950 | break; |
| 1951 | default: |
| 1952 | if (nla_put(skb, type, nla_len(a), nla_data(a))) |
| 1953 | return -EMSGSIZE; |
| 1954 | break; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | return 0; |
| 1959 | } |