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