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