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