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