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