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