blob: b7543700db87eaffd5868da8bf6880f932749a16 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
andy zhou798c1662017-03-20 16:32:29 -07002 * Copyright (c) 2007-2017 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
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 Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#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 Grossf5796682014-10-03 15:35:33 -070045#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070046#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070049#include <net/mpls.h>
Thomas Graf614732e2015-07-21 10:44:06 +020050#include <net/vxlan.h>
Yi Yangb2d0f5d2017-11-07 21:07:02 +080051#include <net/tun_proto.h>
William Tufc1372f2018-01-25 13:20:11 -080052#include <net/erspan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070053
54#include "flow_netlink.h"
55
Thomas Graf81bfe3c32015-01-15 03:53:58 +010056struct ovs_len_tbl {
57 int len;
58 const struct ovs_len_tbl *next;
59};
60
61#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070062#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c32015-01-15 03:53:58 +010063
andy zhou798c1662017-03-20 16:32:29 -070064static 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 Garverb8226962017-10-10 16:54:44 -040080 case OVS_ACTION_ATTR_CT_CLEAR:
andy zhou798c1662017-03-20 16:32:29 -070081 case OVS_ACTION_ATTR_HASH:
82 case OVS_ACTION_ATTR_POP_ETH:
83 case OVS_ACTION_ATTR_POP_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080084 case OVS_ACTION_ATTR_POP_NSH:
andy zhou798c1662017-03-20 16:32:29 -070085 case OVS_ACTION_ATTR_POP_VLAN:
86 case OVS_ACTION_ATTR_PUSH_ETH:
87 case OVS_ACTION_ATTR_PUSH_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080088 case OVS_ACTION_ATTR_PUSH_NSH:
andy zhou798c1662017-03-20 16:32:29 -070089 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 Zhoucd8a6c32017-11-10 12:09:43 -080093 case OVS_ACTION_ATTR_METER:
Numan Siddique4d5ec892019-03-26 06:13:46 +053094 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
andy zhou798c1662017-03-20 16:32:29 -070095 default:
96 return true;
97 }
98 }
99 return false;
100}
101
Pravin B Shelara85311b2014-10-19 12:03:40 -0700102static void update_range(struct sw_flow_match *match,
103 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700104{
Pravin B Shelara85311b2014-10-19 12:03:40 -0700105 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -0700106 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 Shelara85311b2014-10-19 12:03:40 -0700111 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700112 range = &match->mask->range;
113
Pravin B Shelare6445712013-10-03 18:16:47 -0700114 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 Shelara85311b2014-10-19 12:03:40 -0700129 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 Shelare6445712013-10-03 18:16:47 -0700134 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700135 } while (0)
136
Jesse Grossf5796682014-10-03 15:35:33 -0700137#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
138 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700139 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700140 if (is_mask) \
141 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700142 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700143 else \
144 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700145 } while (0)
146
Jesse Grossf5796682014-10-03 15:35:33 -0700147#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 Shelara85311b2014-10-19 12:03:40 -0700151#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 Shelarf47de062014-10-16 21:55:45 -0700159 memset((u8 *)&(match)->key->field, value, \
160 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700161 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700162
163static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800164 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700165{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100166 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700167 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 Rajahalme9dd7f892017-02-09 11:21:59 -0800172 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700173 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800174 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700175 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700176 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700177 | (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 Horman25cd9ba2014-10-06 05:05:13 -0700182 | (1 << OVS_KEY_ATTR_ND)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800183 | (1 << OVS_KEY_ATTR_MPLS)
184 | (1 << OVS_KEY_ATTR_NSH));
Pravin B Shelare6445712013-10-03 18:16:47 -0700185
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 Shelarf2a015172014-11-30 23:04:17 -0800195 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700196 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
197 }
198
Simon Horman25cd9ba2014-10-06 05:05:13 -0700199 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 Shelare6445712013-10-03 18:16:47 -0700205 if (match->key->eth.type == htons(ETH_P_IP)) {
206 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800207 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700208 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800209 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
210 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700211
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 Rajahalme5eb26b12013-10-23 01:44:59 -0700227 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
228 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700229 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700230 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
231 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700232 }
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 Rajahalme9dd7f892017-02-09 11:21:59 -0800244 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700245 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800246 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
247 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700248
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 Rajahalme5eb26b12013-10-23 01:44:59 -0700264 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
265 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700266 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700267 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
268 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700269 }
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 Rajahalme1139e242014-05-05 09:54:49 -0700276 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700277 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700278 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700279 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800280 /* 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 Shelarf2a015172014-11-30 23:04:17 -0800286 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700287 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
288 }
289 }
290 }
291 }
292
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800293 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 Shelare6445712013-10-03 18:16:47 -0700301 if ((key_attrs & key_expected) != key_expected) {
302 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800303 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
304 (unsigned long long)key_attrs,
305 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700306 return false;
307 }
308
309 if ((mask_attrs & mask_allowed) != mask_attrs) {
310 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800311 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
312 (unsigned long long)mask_attrs,
313 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700314 return false;
315 }
316
317 return true;
318}
319
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800320size_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 Dichtelb46f6de2016-04-22 17:31:18 +0200325 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200326 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
327 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800328 + 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 Tufc1372f2018-01-25 13:20:11 -0800334 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
335 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
Thomas Graf1dd144c2015-01-15 03:53:59 +0100336 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
337 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800338 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
William Tu95a33202018-01-12 12:29:22 -0800339 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800340}
341
Wei Yongjun06c23512017-11-14 06:27:03 +0000342static size_t ovs_nsh_key_attr_size(void)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800343{
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 Stringer41af73e2014-10-18 16:14:14 -0700355size_t ovs_key_attr_size(void)
356{
357 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
358 * updating this function.
359 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800360 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
Joe Stringer41af73e2014-10-18 16:14:14 -0700361
362 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
363 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800364 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700365 + 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 Stringerfbccce52015-10-06 11:00:00 -0700369 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700370 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700371 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700372 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800373 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800374 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
375 + ovs_nsh_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700376 + 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 Gross982b5272015-09-11 18:38:28 -0700386static 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 Graf81bfe3c32015-01-15 03:53:58 +0100390static 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 Gross982b5272015-09-11 18:38:28 -0700401 [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 Benc6b26ba32015-10-05 13:09:47 +0200404 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
405 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
William Tufc1372f2018-01-25 13:20:11 -0800406 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_VARIABLE },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100407};
408
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800409static const struct ovs_len_tbl
410ovs_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 Shelare6445712013-10-03 18:16:47 -0700416/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100417static 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 Stringerfbccce52015-10-06 11:00:00 -0700440 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700441 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700442 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700443 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800444 [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 Yangb2d0f5d2017-11-07 21:07:02 +0800448 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
449 .next = ovs_nsh_key_attr_lens, },
Pravin B Shelare6445712013-10-03 18:16:47 -0700450};
451
Jesse Gross982b5272015-09-11 18:38:28 -0700452static 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 Shelare6445712013-10-03 18:16:47 -0700459static 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
473static int __parse_flow_nlattrs(const struct nlattr *attr,
474 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800475 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700476{
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 Rajahalme05da5892014-11-06 07:03:05 -0800487 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700488 type, OVS_KEY_ATTR_MAX);
489 return -EINVAL;
490 }
491
492 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800493 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700494 return -EINVAL;
495 }
496
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100497 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700498 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800499 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
500 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700501 return -EINVAL;
502 }
503
Ross Lagerwall04a4af32019-01-14 09:16:56 +0000504 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700505 attrs |= 1 << type;
506 a[type] = nla;
507 }
508 }
509 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800510 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700511 return -EINVAL;
512 }
513
514 *attrsp = attrs;
515 return 0;
516}
517
518static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800519 const struct nlattr *a[], u64 *attrsp,
520 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700521{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800522 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700523}
524
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800525int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
526 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700527{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800528 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
529}
530
531static 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 Grafd91641d2015-01-15 03:53:57 +0100576 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800577 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
578 nla_len(a), is_mask);
579 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700580}
581
Jesse Gross982b5272015-09-11 18:38:28 -0700582static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100583 struct sw_flow_match *match, bool is_mask,
584 bool log)
585{
Jesse Gross982b5272015-09-11 18:38:28 -0700586 struct nlattr *a;
587 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100588 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200589 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100590
591 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
592
Thomas Graf1dd144c2015-01-15 03:53:59 +0100593 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700594 nla_for_each_nested(a, attr, rem) {
595 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100596
Jesse Gross982b5272015-09-11 18:38:28 -0700597 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 Graf1dd144c2015-01-15 03:53:59 +0100626
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 Tufc1372f2018-01-25 13:20:11 -0800638static 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 Benc6b26ba32015-10-05 13:09:47 +0200665static int ip_tun_from_nlattr(const struct nlattr *attr,
666 struct sw_flow_match *match, bool is_mask,
667 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700668{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700669 bool ttl = false, ipv4 = false, ipv6 = false;
670 __be16 tun_flags = 0;
671 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700672 struct nlattr *a;
673 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700674
675 nla_for_each_nested(a, attr, rem) {
676 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800677 int err;
678
Pravin B Shelare6445712013-10-03 18:16:47 -0700679 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800680 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
681 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700682 return -EINVAL;
683 }
684
Jesse Gross982b5272015-09-11 18:38:28 -0700685 if (!check_attr_len(nla_len(a),
686 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800687 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100688 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700689 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 Bencc1ea5d62015-08-20 13:56:23 +0200699 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200700 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200701 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700702 break;
703 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200704 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200705 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200706 ipv4 = true;
707 break;
708 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200709 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200710 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 Shelare6445712013-10-03 18:16:47 -0700717 break;
718 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200719 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700720 nla_get_u8(a), is_mask);
721 break;
722 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200723 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700724 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 Zhang8f0aad62014-11-06 06:51:24 -0800733 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 Gross67fa0342014-10-03 15:35:30 -0700741 case OVS_TUNNEL_KEY_ATTR_OAM:
742 tun_flags |= TUNNEL_OAM;
743 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700744 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100745 if (opts_type) {
746 OVS_NLERR(log, "Multiple metadata blocks provided");
747 return -EINVAL;
748 }
749
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800750 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
751 if (err)
752 return err;
753
Thomas Graf1dd144c2015-01-15 03:53:59 +0100754 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 Grossf5796682014-10-03 15:35:33 -0700769 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500770 case OVS_TUNNEL_KEY_ATTR_PAD:
771 break;
William Tufc1372f2018-01-25 13:20:11 -0800772 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 Shelare6445712013-10-03 18:16:47 -0700786 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200787 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700788 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700789 return -EINVAL;
790 }
791 }
792
793 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200794 if (is_mask)
795 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
796 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200797 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
798 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700799
800 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200801 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800802 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700803 return -EINVAL;
804 }
805
Jiri Benc6b26ba32015-10-05 13:09:47 +0200806 if (ipv4 && ipv6) {
807 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
808 return -EINVAL;
809 }
810
Pravin B Shelare6445712013-10-03 18:16:47 -0700811 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200812 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 Rajahalme05da5892014-11-06 07:03:05 -0800817 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700818 return -EINVAL;
819 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200820 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 Shelare6445712013-10-03 18:16:47 -0700824
825 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200826 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700827 return -EINVAL;
828 }
829 }
830
Thomas Graf1dd144c2015-01-15 03:53:59 +0100831 return opts_type;
832}
833
834static int vxlan_opt_to_nlattr(struct sk_buff *skb,
835 const void *tun_opts, int swkey_tun_opts_len)
836{
Thomas Graf614732e2015-07-21 10:44:06 +0200837 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100838 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 Shelare6445712013-10-03 18:16:47 -0700848 return 0;
849}
850
Jiri Benc6b26ba32015-10-05 13:09:47 +0200851static 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 Shelare6445712013-10-03 18:16:47 -0700855{
Pravin B Shelare6445712013-10-03 18:16:47 -0700856 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200857 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
858 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700859 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200860 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 Benc7c383fb2015-08-20 13:56:24 +0200882 if (output->tos &&
883 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700884 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200885 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700886 return -EMSGSIZE;
887 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700888 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700889 return -EMSGSIZE;
890 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700891 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
892 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800893 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 Gross67fa0342014-10-03 15:35:30 -0700899 if ((output->tun_flags & TUNNEL_OAM) &&
900 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700901 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700902 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100903 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 Tufc1372f2018-01-25 13:20:11 -0800910 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 Graf1dd144c2015-01-15 03:53:59 +0100914 }
Jesse Grossf5796682014-10-03 15:35:33 -0700915
916 return 0;
917}
918
Jiri Benc6b26ba32015-10-05 13:09:47 +0200919static 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 Grossf5796682014-10-03 15:35:33 -0700923{
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 Benc6b26ba32015-10-05 13:09:47 +0200931 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
932 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700933 if (err)
934 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700935
936 nla_nest_end(skb, nla);
937 return 0;
938}
939
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700940int ovs_nla_put_tunnel_info(struct sk_buff *skb,
941 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800942{
David S. Millerba3e2082015-10-24 06:54:12 -0700943 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 Zhang8f0aad62014-11-06 06:51:24 -0800947}
948
Eric Garver018c1dd2016-09-07 12:56:59 -0400949static 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
972static 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ław9df46ae2018-11-08 18:44:50 +0100994 if (!(tci & htons(VLAN_CFI_MASK))) {
Eric Garver018c1dd2016-09-07 12:56:59 -0400995 if (tci) {
Michał Mirosław9df46ae2018-11-08 18:44:50 +0100996 OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
Eric Garver018c1dd2016-09-07 12:56:59 -0400997 (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
1010static 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ław9df46ae2018-11-08 18:44:50 +01001017 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001018 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001019 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001020
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ław9df46ae2018-11-08 18:44:50 +01001043 if (!(tci & htons(VLAN_CFI_MASK))) {
1044 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
Eric Garver018c1dd2016-09-07 12:56:59 -04001045 (inner) ? "C-VLAN" : "VLAN");
1046 return -EINVAL;
1047 }
1048
1049 return 1;
1050}
1051
1052static 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
1087static 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ław9df46ae2018-11-08 18:44:50 +01001099 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001100 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 Benc0a6410f2016-11-10 16:28:22 +01001110static 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 Stringerc2ac6672015-08-26 11:31:52 -07001131static 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 Shelare6445712013-10-03 18:16:47 -07001134{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001135 u8 mac_proto = MAC_PROTO_ETHERNET;
1136
Andy Zhou971427f32014-09-15 19:37:25 -07001137 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 Shelare6445712013-10-03 18:16:47 -07001151 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 Gross426cda52014-10-06 05:08:38 -07001160 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001161 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001162 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001163 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001164 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001165 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001166 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001167
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 Benc6b26ba32015-10-05 13:09:47 +02001181 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1182 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001183 return -EINVAL;
1184 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1185 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001186
1187 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001188 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001189 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001190
Joe Stringer9e384712015-10-19 19:18:57 -07001191 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001192 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001193 ct_state);
1194 return -EINVAL;
1195 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001196
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001197 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001198 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1199 }
1200 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001201 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001202 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1203
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001204 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001205 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1206 }
Joe Stringer182e3042015-08-26 11:31:49 -07001207 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001208 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001209 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 Stringer33db4122015-10-01 15:00:37 -07001214 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 Stringerc2ac6672015-08-26 11:31:52 -07001217
Joe Stringer33db4122015-10-01 15:00:37 -07001218 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1219 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001220 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001221 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001222 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001223 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 Rajahalme316d4d72017-02-09 11:22:01 -08001232 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001233 *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 Rajahalme316d4d72017-02-09 11:22:01 -08001248 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001249 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1250 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001251
Jiri Benc0a6410f2016-11-10 16:28:22 +01001252 /* 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 Benc329f45b2016-11-10 16:28:18 +01001259 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001260 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 Benc329f45b2016-11-10 16:28:18 +01001265
Pravin B Shelare6445712013-10-03 18:16:47 -07001266 return 0;
1267}
1268
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001269int 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
1323int 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
1363static 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 Stringerc2ac6672015-08-26 11:31:52 -07001484static 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 Shelare6445712013-10-03 18:16:47 -07001487{
1488 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001489
Joe Stringerc2ac6672015-08-26 11:31:52 -07001490 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001491 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 Shelare6445712013-10-03 18:16:47 -07001503
Jiri Benc0a6410f2016-11-10 16:28:22 +01001504 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 Shelare6445712013-10-03 18:16:47 -07001509 return -EINVAL;
1510 }
1511
Jiri Benc0a6410f2016-11-10 16:28:22 +01001512 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 Shelare6445712013-10-03 18:16:47 -07001523 }
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 Rajahalme05da5892014-11-06 07:03:05 -08001530 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1531 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001532 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 Rajahalme05da5892014-11-06 07:03:05 -08001554 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1555 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001556 return -EINVAL;
1557 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001558
Joe Stringerd3052bb2014-11-19 13:54:49 -08001559 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001560 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001561 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1562 return -EINVAL;
1563 }
1564
Pravin B Shelare6445712013-10-03 18:16:47 -07001565 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 Rajahalme05da5892014-11-06 07:03:05 -08001592 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001593 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 Yangb2d0f5d2017-11-07 21:07:02 +08001611 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 Horman25cd9ba2014-10-06 05:05:13 -07001618 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 Shelare6445712013-10-03 18:16:47 -07001628 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 Rajahalme1139e242014-05-05 09:54:49 -07001632 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 Shelare6445712013-10-03 18:16:47 -07001634 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1635 }
1636
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001637 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001638 SW_FLOW_KEY_PUT(match, tp.flags,
1639 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1640 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001641 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1642 }
1643
Pravin B Shelare6445712013-10-03 18:16:47 -07001644 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 Rajahalme1139e242014-05-05 09:54:49 -07001648 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 Shelare6445712013-10-03 18:16:47 -07001650 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 Rajahalme1139e242014-05-05 09:54:49 -07001657 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 Shelare6445712013-10-03 18:16:47 -07001659 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 Rajahalme1139e242014-05-05 09:54:49 -07001666 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001667 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001668 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001669 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 Rajahalme1139e242014-05-05 09:54:49 -07001677 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001678 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001679 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001680 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 Gross426cda52014-10-06 05:08:38 -07001699 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001700 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001701 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001702 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001703 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001704
1705 return 0;
1706}
1707
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001708static void nlattr_set(struct nlattr *attr, u8 val,
1709 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001710{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001711 struct nlattr *nla;
1712 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001713
Pravin B Shelarf47de062014-10-16 21:55:45 -07001714 /* The nlattr stream should already have been validated */
1715 nla_for_each_nested(nla, attr, rem) {
Stefano Brivio72f17ba2018-05-03 18:13:25 +02001716 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1717 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1718 else
Pravin B Shelarf47de062014-10-16 21:55:45 -07001719 memset(nla_data(nla), val, nla_len(nla));
Joe Stringer9e384712015-10-19 19:18:57 -07001720
1721 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1722 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001723 }
1724}
1725
1726static void mask_set_nlattr(struct nlattr *attr, u8 val)
1727{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001728 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001729}
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 Stringerc2ac6672015-08-26 11:31:52 -07001736 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001737 * @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 Rajahalme05da5892014-11-06 07:03:05 -08001743 * @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 Shelare6445712013-10-03 18:16:47 -07001746 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001747int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001748 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001749 const struct nlattr *nla_mask,
1750 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001751{
1752 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001753 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001754 u64 key_attrs = 0;
1755 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001756 int err;
1757
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001758 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001759 if (err)
1760 return err;
1761
Eric Garver018c1dd2016-09-07 12:56:59 -04001762 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1763 if (err)
1764 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001765
Joe Stringerc2ac6672015-08-26 11:31:52 -07001766 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001767 if (err)
1768 return err;
1769
Pravin B Shelara85311b2014-10-19 12:03:40 -07001770 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 Shelarf47de062014-10-16 21:55:45 -07001787
Pravin B Shelara85311b2014-10-19 12:03:40 -07001788 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001789
Pravin B Shelara85311b2014-10-19 12:03:40 -07001790 /* The userspace does not send tunnel attributes that
1791 * are 0, but we should not wildcard them nonetheless.
1792 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001793 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001794 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1795 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001796
Pravin B Shelara85311b2014-10-19 12:03:40 -07001797 nla_mask = newmask;
1798 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001799
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001800 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001801 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001802 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001803
Pravin B Shelara85311b2014-10-19 12:03:40 -07001804 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001805 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 Shelara85311b2014-10-19 12:03:40 -07001807
Eric Garver018c1dd2016-09-07 12:56:59 -04001808 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1809 if (err)
1810 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001811
Joe Stringerc2ac6672015-08-26 11:31:52 -07001812 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1813 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001814 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001815 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001816 }
1817
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001818 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001819 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001820
Pravin B Shelarf47de062014-10-16 21:55:45 -07001821free_newmask:
1822 kfree(newmask);
1823 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001824}
1825
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001826static 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 */
1846bool 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
1856int 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
1874u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1875{
1876 return attr ? nla_get_u32(attr) : 0;
1877}
1878
Pravin B Shelare6445712013-10-03 18:16:47 -07001879/**
1880 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001881 * @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 Rajahalme05da5892014-11-06 07:03:05 -08001887 * @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 Shelare6445712013-10-03 18:16:47 -07001890 *
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 Rajahalme9dd7f892017-02-09 11:21:59 -08001895 *
1896 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001897 */
1898
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001899int 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 Shelare6445712013-10-03 18:16:47 -07001902{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001903 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001904
1905 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001906 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001907
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001908 key->ct_state = 0;
1909 key->ct_zone = 0;
1910 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001911 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001912 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 Shelar83c8df22014-09-15 19:20:31 -07001915 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001916
Joe Stringerc2ac6672015-08-26 11:31:52 -07001917 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001918}
1919
Eric Garver018c1dd2016-09-07 12:56:59 -04001920static 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 Yangb2d0f5d2017-11-07 21:07:02 +08001931static 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
1955nla_put_failure:
1956 return -EMSGSIZE;
1957}
1958
Joe Stringer5b4237b2015-01-21 16:42:48 -08001959static 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 Shelare6445712013-10-03 18:16:47 -07001962{
1963 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001964 struct nlattr *nla;
1965 struct nlattr *encap = NULL;
1966 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001967
Andy Zhou971427f32014-09-15 19:37:25 -07001968 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 Shelare6445712013-10-03 18:16:47 -07001974 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1975 goto nla_put_failure;
1976
Jiri Benc00a93ba2015-10-05 13:09:46 +02001977 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001978 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001979
1980 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001981 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001982
Jiri Benc6b26ba32015-10-05 13:09:47 +02001983 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1984 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001985 goto nla_put_failure;
1986 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001987
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 Rajahalme9dd7f892017-02-09 11:21:59 -08002004 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07002005 goto nla_put_failure;
2006
Jiri Benc0a6410f2016-11-10 16:28:22 +01002007 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 Shelare6445712013-10-03 18:16:47 -07002010 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04002011
Jiri Benc0a6410f2016-11-10 16:28:22 +01002012 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 Garver018c1dd2016-09-07 12:56:59 -04002018 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002019 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
2020 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04002021 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07002022
Jiri Benc0a6410f2016-11-10 16:28:22 +01002023 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 Shelare6445712013-10-03 18:16:47 -07002045 }
2046
2047 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2048 goto nla_put_failure;
2049
Eric Garver018c1dd2016-09-07 12:56:59 -04002050 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 Shelare6445712013-10-03 18:16:47 -07002058 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 Yangb2d0f5d2017-11-07 21:07:02 +08002087 } 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 Shelare6445712013-10-03 18:16:47 -07002090 } 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 Perches8c63ff02014-02-18 11:15:45 -08002102 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2103 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002104 } 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 Shelare6445712013-10-03 18:16:47 -07002112 }
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 Rajahalme1139e242014-05-05 09:54:49 -07002125 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 Shelare6445712013-10-03 18:16:47 -07002130 } 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 Rajahalme1139e242014-05-05 09:54:49 -07002137 udp_key->udp_src = output->tp.src;
2138 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002139 } 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 Rajahalme1139e242014-05-05 09:54:49 -07002146 sctp_key->sctp_src = output->tp.src;
2147 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002148 } 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 Rajahalme1139e242014-05-05 09:54:49 -07002156 icmp_key->icmp_type = ntohs(output->tp.src);
2157 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002158 } 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 Rajahalme1139e242014-05-05 09:54:49 -07002167 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2168 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002169
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 Perches8c63ff02014-02-18 11:15:45 -08002180 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 Shelare6445712013-10-03 18:16:47 -07002182 }
2183 }
2184 }
2185
2186unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04002187 if (in_encap)
2188 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07002189 if (encap)
2190 nla_nest_end(skb, encap);
2191
2192 return 0;
2193
2194nla_put_failure:
2195 return -EMSGSIZE;
2196}
2197
Joe Stringer5b4237b2015-01-21 16:42:48 -08002198int 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 Stringer74ed7ab2015-01-21 16:42:52 -08002217int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08002218{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002219 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. */
2228int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2229{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08002230 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08002231 OVS_FLOW_ATTR_KEY, false, skb);
2232}
2233
2234/* Called with ovs_mutex or RCU read lock. */
2235int 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 Shelare6445712013-10-03 18:16:47 -07002241#define MAX_ACTIONS_BUFSIZE (32 * 1024)
2242
zhangliping67c8d222017-11-25 22:02:12 +08002243static struct sw_flow_actions *nla_alloc_flow_actions(int size)
Pravin B Shelare6445712013-10-03 18:16:47 -07002244{
2245 struct sw_flow_actions *sfa;
2246
zhangliping67c8d222017-11-25 22:02:12 +08002247 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002248
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 Graf34ae9322015-07-21 10:44:03 +02002257static 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 Shelare6445712013-10-03 18:16:47 -07002270void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2271{
Thomas Graf34ae9322015-07-21 10:44:03 +02002272 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 Stringer7f8a4362015-08-26 11:31:48 -07002283 case OVS_ACTION_ATTR_CT:
2284 ovs_ct_free_action(a);
2285 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02002286 }
2287 }
2288
2289 kfree(sf_acts);
2290}
2291
2292static 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. */
2299void 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 Shelare6445712013-10-03 18:16:47 -07002302}
2303
2304static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002305 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002306{
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) {
zhangliping67c8d222017-11-25 22:02:12 +08002320 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
2321 OVS_NLERR(log, "Flow action size exceeds max %u",
2322 MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002323 return ERR_PTR(-EMSGSIZE);
zhangliping67c8d222017-11-25 22:02:12 +08002324 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002325 new_acts_size = MAX_ACTIONS_BUFSIZE;
2326 }
2327
zhangliping67c8d222017-11-25 22:02:12 +08002328 acts = nla_alloc_flow_actions(new_acts_size);
Pravin B Shelare6445712013-10-03 18:16:47 -07002329 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 Stringer8e2fed12015-08-26 11:31:44 -07002334 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002335 kfree(*sfa);
2336 *sfa = acts;
2337
2338out:
2339 (*sfa)->actions_len += req_size;
2340 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2341}
2342
Jesse Grossf0b128c2014-10-03 15:35:31 -07002343static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002344 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002345{
2346 struct nlattr *a;
2347
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002348 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002349 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002350 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002351
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 Grossf0b128c2014-10-03 15:35:31 -07002359 return a;
2360}
2361
Joe Stringer7f8a4362015-08-26 11:31:48 -07002362int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2363 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002364{
2365 struct nlattr *a;
2366
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002367 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002368
Fabian Frederickf35423c12014-11-14 19:32:58 +01002369 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002370}
2371
2372static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002373 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002374{
2375 int used = (*sfa)->actions_len;
2376 int err;
2377
Joe Stringer7f8a4362015-08-26 11:31:48 -07002378 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002379 if (err)
2380 return err;
2381
2382 return used;
2383}
2384
2385static 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 Stringer7f8a4362015-08-26 11:31:48 -07002394static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002395 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002396 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002397 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002398
Joe Stringer7f8a4362015-08-26 11:31:48 -07002399static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002400 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002401 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002402 __be16 eth_type, __be16 vlan_tci,
2403 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002404{
2405 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2406 const struct nlattr *probability, *actions;
2407 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002408 int rem, start, err;
2409 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002410
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 Rajahalme05da5892014-11-06 07:03:05 -08002430 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002431 if (start < 0)
2432 return start;
andy zhou798c1662017-03-20 16:32:29 -07002433
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 Shelare6445712013-10-03 18:16:47 -07002450 if (err)
2451 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002452
andy zhou798c1662017-03-20 16:32:29 -07002453 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002454 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002455
Pravin B Shelare6445712013-10-03 18:16:47 -07002456 if (err)
2457 return err;
2458
Pravin B Shelare6445712013-10-03 18:16:47 -07002459 add_nested_action_end(*sfa, start);
2460
2461 return 0;
2462}
2463
Yifeng Sunb2335042018-07-02 08:18:03 -07002464static 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 Shelare6445712013-10-03 18:16:47 -07002498void ovs_match_init(struct sw_flow_match *match,
2499 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002500 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002501 struct sw_flow_mask *mask)
2502{
2503 memset(match, 0, sizeof(*match));
2504 match->key = key;
2505 match->mask = mask;
2506
pravin shelar22799942016-09-19 13:51:00 -07002507 if (reset_key)
2508 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002509
2510 if (mask) {
2511 memset(&mask->key, 0, sizeof(mask->key));
2512 mask->range.start = mask->range.end = 0;
2513 }
2514}
2515
Thomas Grafd91641d2015-01-15 03:53:57 +01002516static 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 Riveros89290b82018-01-17 16:10:28 -05002537 }
Thomas Grafd91641d2015-01-15 03:53:57 +01002538
2539 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2540
2541 return 0;
2542}
2543
Pravin B Shelare6445712013-10-03 18:16:47 -07002544static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002545 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002546{
2547 struct sw_flow_match match;
2548 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002549 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002550 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002551 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002552 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002553 int err = 0, start, opts_type;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002554 __be16 dst_opt_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002555
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002556 dst_opt_type = 0;
pravin shelar22799942016-09-19 13:51:00 -07002557 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002558 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002559 if (opts_type < 0)
2560 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002561
Jesse Grossf5796682014-10-03 15:35:33 -07002562 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002563 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 Vuuren256c87c2018-06-26 21:39:36 -07002568 dst_opt_type = TUNNEL_GENEVE_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002569 break;
2570 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002571 dst_opt_type = TUNNEL_VXLAN_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002572 break;
William Tufc1372f2018-01-25 13:20:11 -08002573 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002574 dst_opt_type = TUNNEL_ERSPAN_OPT;
William Tufc1372f2018-01-25 13:20:11 -08002575 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002576 }
Christopher Díaz Riveros89290b82018-01-17 16:10:28 -05002577 }
Jesse Grossf5796682014-10-03 15:35:33 -07002578
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002579 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002580 if (start < 0)
2581 return start;
2582
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002583 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2584 GFP_KERNEL);
2585
Thomas Graf34ae9322015-07-21 10:44:03 +02002586 if (!tun_dst)
2587 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002588
Paolo Abenid71785f2016-02-12 15:43:57 +01002589 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 Graf34ae9322015-07-21 10:44:03 +02002595 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 Benc00a93ba2015-10-05 13:09:46 +02002607 if (key.tun_proto == AF_INET6)
2608 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002609 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002610
Pravin B Shelar4c222792015-08-30 18:09:38 -07002611 /* 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 Vuuren256c87c2018-06-26 21:39:36 -07002617 key.tun_opts_len, dst_opt_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002618 add_nested_action_end(*sfa, start);
2619
2620 return err;
2621}
2622
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002623static 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 Rajahalme83d2b9b2015-02-05 13:40:49 -08002636/* Return false if there are any non-masked bits set.
2637 * Mask follows data immediately, before any netlink padding.
2638 */
2639static 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 Shelare6445712013-10-03 18:16:47 -07002650static int validate_set(const struct nlattr *a,
2651 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002652 struct sw_flow_actions **sfa, bool *skip_copy,
2653 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002654{
2655 const struct nlattr *ovs_key = nla_data(a);
2656 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002657 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002658
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 Rajahalme83d2b9b2015-02-05 13:40:49 -08002663 key_len = nla_len(ovs_key);
2664 if (masked)
2665 key_len /= 2;
2666
Pravin B Shelare6445712013-10-03 18:16:47 -07002667 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002668 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002669 return -EINVAL;
2670
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002671 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2672 return -EINVAL;
2673
Pravin B Shelare6445712013-10-03 18:16:47 -07002674 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 Stringer182e3042015-08-26 11:31:49 -07002681 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002682 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002683 break;
2684
Jiri Benc0a6410f2016-11-10 16:28:22 +01002685 case OVS_KEY_ATTR_ETHERNET:
2686 if (mac_proto != MAC_PROTO_ETHERNET)
2687 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002688 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002689
Pravin B Shelare6445712013-10-03 18:16:47 -07002690 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002691 if (masked)
2692 return -EINVAL; /* Masked tunnel set not supported. */
2693
2694 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002695 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002696 if (err)
2697 return err;
2698 break;
2699
2700 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002701 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002702 return -EINVAL;
2703
Pravin B Shelare6445712013-10-03 18:16:47 -07002704 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002705
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002706 if (masked) {
2707 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002708
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002709 /* 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 Shelare6445712013-10-03 18:16:47 -07002719 break;
2720
2721 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002722 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002723 return -EINVAL;
2724
Pravin B Shelare6445712013-10-03 18:16:47 -07002725 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002726
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002727 if (masked) {
2728 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002729
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002730 /* 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 Shelare6445712013-10-03 18:16:47 -07002744 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2745 return -EINVAL;
2746
2747 break;
2748
2749 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002750 if ((eth_type != htons(ETH_P_IP) &&
2751 eth_type != htons(ETH_P_IPV6)) ||
2752 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002753 return -EINVAL;
2754
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002755 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002756
2757 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002758 if ((eth_type != htons(ETH_P_IP) &&
2759 eth_type != htons(ETH_P_IPV6)) ||
2760 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002761 return -EINVAL;
2762
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002763 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002764
2765 case OVS_KEY_ATTR_MPLS:
2766 if (!eth_p_mpls(eth_type))
2767 return -EINVAL;
2768 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002769
2770 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002771 if ((eth_type != htons(ETH_P_IP) &&
2772 eth_type != htons(ETH_P_IPV6)) ||
2773 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002774 return -EINVAL;
2775
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002776 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002777
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002778 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 Shelare6445712013-10-03 18:16:47 -07002785 default:
2786 return -EINVAL;
2787 }
2788
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002789 /* 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 Shelare6445712013-10-03 18:16:47 -07002817 return 0;
2818}
2819
2820static 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 Zhang8f0aad62014-11-06 06:51:24 -08002825 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002826 };
2827 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2828 int error;
2829
Johannes Bergfceb6432017-04-12 14:34:07 +02002830 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2831 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002832 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 Siddique4d5ec892019-03-26 06:13:46 +05302842static 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
2848static 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 Shelare6445712013-10-03 18:16:47 -07002923static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002924 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002925{
2926 int totlen = NLA_ALIGN(from->nla_len);
2927 struct nlattr *to;
2928
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002929 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002930 if (IS_ERR(to))
2931 return PTR_ERR(to);
2932
2933 memcpy(to, from, totlen);
2934 return 0;
2935}
2936
Joe Stringer7f8a4362015-08-26 11:31:48 -07002937static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002938 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002939 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002940 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002941{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002942 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002943 const struct nlattr *a;
2944 int rem, err;
2945
Pravin B Shelare6445712013-10-03 18:16:47 -07002946 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 Zhou971427f32014-09-15 19:37:25 -07002950 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002951 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002952 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2953 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002954 [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 Rajahalme83d2b9b2015-02-05 13:40:49 -08002957 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002958 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002959 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2960 [OVS_ACTION_ATTR_CT] = (u32)-1,
Eric Garverb8226962017-10-10 16:54:44 -04002961 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
William Tuf2a4d082016-06-10 11:49:33 -07002962 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002963 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2964 [OVS_ACTION_ATTR_POP_ETH] = 0,
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002965 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
2966 [OVS_ACTION_ATTR_POP_NSH] = 0,
Andy Zhoucd8a6c32017-11-10 12:09:43 -08002967 [OVS_ACTION_ATTR_METER] = sizeof(u32),
Yifeng Sunb2335042018-07-02 08:18:03 -07002968 [OVS_ACTION_ATTR_CLONE] = (u32)-1,
Numan Siddique4d5ec892019-03-26 06:13:46 +05302969 [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002970 };
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 Tuf2a4d082016-06-10 11:49:33 -07002996 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 Zhou971427f32014-09-15 19:37:25 -07003004 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 Shelare6445712013-10-03 18:16:47 -07003016
3017 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003018 if (mac_proto != MAC_PROTO_ETHERNET)
3019 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003020 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07003021 break;
3022
3023 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003024 if (mac_proto != MAC_PROTO_ETHERNET)
3025 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07003026 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04003027 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07003028 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003029 if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
Pravin B Shelare6445712013-10-03 18:16:47 -07003030 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003031 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07003032 break;
3033
Andy Zhou971427f32014-09-15 19:37:25 -07003034 case OVS_ACTION_ATTR_RECIRC:
3035 break;
3036
Simon Horman25cd9ba2014-10-06 05:05:13 -07003037 case OVS_ACTION_ATTR_PUSH_MPLS: {
3038 const struct ovs_action_push_mpls *mpls = nla_data(a);
3039
Simon Horman25cd9ba2014-10-06 05:05:13 -07003040 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ław9df46ae2018-11-08 18:44:50 +01003045 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003046 (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ław9df46ae2018-11-08 18:44:50 +01003057 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003058 !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 Shelare6445712013-10-03 18:16:47 -07003073 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07003074 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003075 &skip_copy, mac_proto, eth_type,
3076 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003077 if (err)
3078 return err;
3079 break;
3080
3081 case OVS_ACTION_ATTR_SET_MASKED:
3082 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003083 &skip_copy, mac_proto, eth_type,
3084 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003085 if (err)
3086 return err;
3087 break;
3088
andy zhou798c1662017-03-20 16:32:29 -07003089 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 Shelare6445712013-10-03 18:16:47 -07003095 if (err)
3096 return err;
3097 skip_copy = true;
3098 break;
andy zhou798c1662017-03-20 16:32:29 -07003099 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003100
Joe Stringer7f8a4362015-08-26 11:31:48 -07003101 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 Garverb8226962017-10-10 16:54:44 -04003108 case OVS_ACTION_ATTR_CT_CLEAR:
3109 break;
3110
Jiri Benc91820da2016-11-10 16:28:23 +01003111 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 Ruiz46ebe282018-10-31 18:52:03 +01003116 mac_proto = MAC_PROTO_ETHERNET;
Jiri Benc91820da2016-11-10 16:28:23 +01003117 break;
3118
3119 case OVS_ACTION_ATTR_POP_ETH:
3120 if (mac_proto != MAC_PROTO_ETHERNET)
3121 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003122 if (vlan_tci & htons(VLAN_CFI_MASK))
Jiri Benc91820da2016-11-10 16:28:23 +01003123 return -EINVAL;
Jaime Caamaño Ruiz46ebe282018-10-31 18:52:03 +01003124 mac_proto = MAC_PROTO_NONE;
Jiri Benc91820da2016-11-10 16:28:23 +01003125 break;
3126
Yi Yangb2d0f5d2017-11-07 21:07:02 +08003127 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 Zhoucd8a6c32017-11-10 12:09:43 -08003155 case OVS_ACTION_ATTR_METER:
3156 /* Non-existent meters are simply ignored. */
3157 break;
3158
Yifeng Sunb2335042018-07-02 08:18:03 -07003159 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 Siddique4d5ec892019-03-26 06:13:46 +05303171 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 Shelare6445712013-10-03 18:16:47 -07003184 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003185 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07003186 return -EINVAL;
3187 }
3188 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003189 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003190 if (err)
3191 return err;
3192 }
3193 }
3194
3195 if (rem > 0)
3196 return -EINVAL;
3197
3198 return 0;
3199}
3200
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003201/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07003202int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07003203 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003204 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07003205{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003206 int err;
3207
zhangliping67c8d222017-11-25 22:02:12 +08003208 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003209 if (IS_ERR(*sfa))
3210 return PTR_ERR(*sfa);
3211
Joe Stringer8e2fed12015-08-26 11:31:44 -07003212 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07003213 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04003214 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003215 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02003216 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003217
3218 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003219}
3220
andy zhou798c1662017-03-20 16:32:29 -07003221static int sample_action_to_attr(const struct nlattr *attr,
3222 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07003223{
andy zhou798c1662017-03-20 16:32:29 -07003224 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 Shelare6445712013-10-03 18:16:47 -07003228
3229 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
3230 if (!start)
3231 return -EMSGSIZE;
3232
andy zhou798c1662017-03-20 16:32:29 -07003233 sample_arg = nla_data(attr);
3234 arg = nla_data(sample_arg);
3235 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07003236
andy zhou798c1662017-03-20 16:32:29 -07003237 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3238 err = -EMSGSIZE;
3239 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07003240 }
3241
andy zhou798c1662017-03-20 16:32:29 -07003242 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
3250out:
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 Shelare6445712013-10-03 18:16:47 -07003259 return err;
3260}
3261
Yifeng Sunb2335042018-07-02 08:18:03 -07003262static 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 Siddique4d5ec892019-03-26 06:13:46 +05303282static 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
3346out:
3347 nla_nest_cancel(skb, start);
3348 return err;
3349}
3350
Pravin B Shelare6445712013-10-03 18:16:47 -07003351static 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 Grossf0b128c2014-10-03 15:35:31 -07003359 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02003360 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 Grossf0b128c2014-10-03 15:35:31 -07003362
Pravin B Shelare6445712013-10-03 18:16:47 -07003363 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3364 if (!start)
3365 return -EMSGSIZE;
3366
Simon Hormane905eab2015-12-18 19:43:15 +09003367 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 Shelare6445712013-10-03 18:16:47 -07003371 if (err)
3372 return err;
3373 nla_nest_end(skb, start);
3374 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003375 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003376 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 Rajahalme83d2b9b2015-02-05 13:40:49 -08003385static 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 Stringerf4f8e732015-03-02 18:49:56 -08003389 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003390 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 Stringerf4f8e732015-03-02 18:49:56 -08003395 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3396 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003397 return -EMSGSIZE;
3398
Joe Stringerf4f8e732015-03-02 18:49:56 -08003399 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 Rajahalme83d2b9b2015-02-05 13:40:49 -08003403 return 0;
3404}
3405
Pravin B Shelare6445712013-10-03 18:16:47 -07003406int 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 Rajahalme83d2b9b2015-02-05 13:40:49 -08003421 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 Shelare6445712013-10-03 18:16:47 -07003427 case OVS_ACTION_ATTR_SAMPLE:
3428 err = sample_action_to_attr(a, skb);
3429 if (err)
3430 return err;
3431 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07003432
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 Sunb2335042018-07-02 08:18:03 -07003439 case OVS_ACTION_ATTR_CLONE:
3440 err = clone_action_to_attr(a, skb);
3441 if (err)
3442 return err;
3443 break;
3444
Numan Siddique4d5ec892019-03-26 06:13:46 +05303445 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 Shelare6445712013-10-03 18:16:47 -07003451 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}