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