blob: bd019058fc6fb80113f6716011558be5daacbb2e [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 },
wenxu18b6f712019-03-28 12:43:23 +0800407 [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE] = { .len = 0 },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100408};
409
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800410static const struct ovs_len_tbl
411ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
412 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
413 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
414 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
415};
416
Pravin B Shelare6445712013-10-03 18:16:47 -0700417/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100418static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
419 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
420 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
421 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
422 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
423 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
424 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
425 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
426 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
427 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
428 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
429 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
430 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
431 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
432 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
433 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
434 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
435 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
436 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
437 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
438 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
439 .next = ovs_tunnel_key_lens, },
440 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700441 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700442 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700443 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700444 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800445 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
446 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
447 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
448 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800449 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
450 .next = ovs_nsh_key_attr_lens, },
Pravin B Shelare6445712013-10-03 18:16:47 -0700451};
452
Jesse Gross982b5272015-09-11 18:38:28 -0700453static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
454{
455 return expected_len == attr_len ||
456 expected_len == OVS_ATTR_NESTED ||
457 expected_len == OVS_ATTR_VARIABLE;
458}
459
Pravin B Shelare6445712013-10-03 18:16:47 -0700460static bool is_all_zero(const u8 *fp, size_t size)
461{
462 int i;
463
464 if (!fp)
465 return false;
466
467 for (i = 0; i < size; i++)
468 if (fp[i])
469 return false;
470
471 return true;
472}
473
474static int __parse_flow_nlattrs(const struct nlattr *attr,
475 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800476 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700477{
478 const struct nlattr *nla;
479 u64 attrs;
480 int rem;
481
482 attrs = *attrsp;
483 nla_for_each_nested(nla, attr, rem) {
484 u16 type = nla_type(nla);
485 int expected_len;
486
487 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800488 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700489 type, OVS_KEY_ATTR_MAX);
490 return -EINVAL;
491 }
492
493 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800494 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700495 return -EINVAL;
496 }
497
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100498 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700499 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800500 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
501 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700502 return -EINVAL;
503 }
504
Ross Lagerwall04a4af32019-01-14 09:16:56 +0000505 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700506 attrs |= 1 << type;
507 a[type] = nla;
508 }
509 }
510 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800511 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700512 return -EINVAL;
513 }
514
515 *attrsp = attrs;
516 return 0;
517}
518
519static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800520 const struct nlattr *a[], u64 *attrsp,
521 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700522{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800523 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700524}
525
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800526int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
527 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700528{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800529 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
530}
531
532static int genev_tun_opt_from_nlattr(const struct nlattr *a,
533 struct sw_flow_match *match, bool is_mask,
534 bool log)
535{
536 unsigned long opt_key_offset;
537
538 if (nla_len(a) > sizeof(match->key->tun_opts)) {
539 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
540 nla_len(a), sizeof(match->key->tun_opts));
541 return -EINVAL;
542 }
543
544 if (nla_len(a) % 4 != 0) {
545 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
546 nla_len(a));
547 return -EINVAL;
548 }
549
550 /* We need to record the length of the options passed
551 * down, otherwise packets with the same format but
552 * additional options will be silently matched.
553 */
554 if (!is_mask) {
555 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
556 false);
557 } else {
558 /* This is somewhat unusual because it looks at
559 * both the key and mask while parsing the
560 * attributes (and by extension assumes the key
561 * is parsed first). Normally, we would verify
562 * that each is the correct length and that the
563 * attributes line up in the validate function.
564 * However, that is difficult because this is
565 * variable length and we won't have the
566 * information later.
567 */
568 if (match->key->tun_opts_len != nla_len(a)) {
569 OVS_NLERR(log, "Geneve option len %d != mask len %d",
570 match->key->tun_opts_len, nla_len(a));
571 return -EINVAL;
572 }
573
574 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
575 }
576
Thomas Grafd91641d2015-01-15 03:53:57 +0100577 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800578 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
579 nla_len(a), is_mask);
580 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700581}
582
Jesse Gross982b5272015-09-11 18:38:28 -0700583static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100584 struct sw_flow_match *match, bool is_mask,
585 bool log)
586{
Jesse Gross982b5272015-09-11 18:38:28 -0700587 struct nlattr *a;
588 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100589 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200590 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100591
592 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
593
Thomas Graf1dd144c2015-01-15 03:53:59 +0100594 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700595 nla_for_each_nested(a, attr, rem) {
596 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100597
Jesse Gross982b5272015-09-11 18:38:28 -0700598 if (type > OVS_VXLAN_EXT_MAX) {
599 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
600 type, OVS_VXLAN_EXT_MAX);
601 return -EINVAL;
602 }
603
604 if (!check_attr_len(nla_len(a),
605 ovs_vxlan_ext_key_lens[type].len)) {
606 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
607 type, nla_len(a),
608 ovs_vxlan_ext_key_lens[type].len);
609 return -EINVAL;
610 }
611
612 switch (type) {
613 case OVS_VXLAN_EXT_GBP:
614 opts.gbp = nla_get_u32(a);
615 break;
616 default:
617 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
618 type);
619 return -EINVAL;
620 }
621 }
622 if (rem) {
623 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
624 rem);
625 return -EINVAL;
626 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100627
628 if (!is_mask)
629 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
630 else
631 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
632
633 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
634 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
635 is_mask);
636 return 0;
637}
638
William Tufc1372f2018-01-25 13:20:11 -0800639static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
640 struct sw_flow_match *match, bool is_mask,
641 bool log)
642{
643 unsigned long opt_key_offset;
644
645 BUILD_BUG_ON(sizeof(struct erspan_metadata) >
646 sizeof(match->key->tun_opts));
647
648 if (nla_len(a) > sizeof(match->key->tun_opts)) {
649 OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
650 nla_len(a), sizeof(match->key->tun_opts));
651 return -EINVAL;
652 }
653
654 if (!is_mask)
655 SW_FLOW_KEY_PUT(match, tun_opts_len,
656 sizeof(struct erspan_metadata), false);
657 else
658 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
659
660 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
661 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
662 nla_len(a), is_mask);
663 return 0;
664}
665
Jiri Benc6b26ba32015-10-05 13:09:47 +0200666static int ip_tun_from_nlattr(const struct nlattr *attr,
667 struct sw_flow_match *match, bool is_mask,
668 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700669{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700670 bool ttl = false, ipv4 = false, ipv6 = false;
wenxu18b6f712019-03-28 12:43:23 +0800671 bool info_bridge_mode = false;
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700672 __be16 tun_flags = 0;
673 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700674 struct nlattr *a;
675 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700676
677 nla_for_each_nested(a, attr, rem) {
678 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800679 int err;
680
Pravin B Shelare6445712013-10-03 18:16:47 -0700681 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800682 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
683 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700684 return -EINVAL;
685 }
686
Jesse Gross982b5272015-09-11 18:38:28 -0700687 if (!check_attr_len(nla_len(a),
688 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800689 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100690 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700691 return -EINVAL;
692 }
693
694 switch (type) {
695 case OVS_TUNNEL_KEY_ATTR_ID:
696 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
697 nla_get_be64(a), is_mask);
698 tun_flags |= TUNNEL_KEY;
699 break;
700 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200701 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200702 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200703 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700704 break;
705 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200706 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200707 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200708 ipv4 = true;
709 break;
710 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200711 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200712 nla_get_in6_addr(a), is_mask);
713 ipv6 = true;
714 break;
715 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
716 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
717 nla_get_in6_addr(a), is_mask);
718 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700719 break;
720 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200721 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700722 nla_get_u8(a), is_mask);
723 break;
724 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200725 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700726 nla_get_u8(a), is_mask);
727 ttl = true;
728 break;
729 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
730 tun_flags |= TUNNEL_DONT_FRAGMENT;
731 break;
732 case OVS_TUNNEL_KEY_ATTR_CSUM:
733 tun_flags |= TUNNEL_CSUM;
734 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800735 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
736 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
737 nla_get_be16(a), is_mask);
738 break;
739 case OVS_TUNNEL_KEY_ATTR_TP_DST:
740 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
741 nla_get_be16(a), is_mask);
742 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700743 case OVS_TUNNEL_KEY_ATTR_OAM:
744 tun_flags |= TUNNEL_OAM;
745 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700746 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100747 if (opts_type) {
748 OVS_NLERR(log, "Multiple metadata blocks provided");
749 return -EINVAL;
750 }
751
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800752 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
753 if (err)
754 return err;
755
Thomas Graf1dd144c2015-01-15 03:53:59 +0100756 tun_flags |= TUNNEL_GENEVE_OPT;
757 opts_type = type;
758 break;
759 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
760 if (opts_type) {
761 OVS_NLERR(log, "Multiple metadata blocks provided");
762 return -EINVAL;
763 }
764
765 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
766 if (err)
767 return err;
768
769 tun_flags |= TUNNEL_VXLAN_OPT;
770 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700771 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500772 case OVS_TUNNEL_KEY_ATTR_PAD:
773 break;
William Tufc1372f2018-01-25 13:20:11 -0800774 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
775 if (opts_type) {
776 OVS_NLERR(log, "Multiple metadata blocks provided");
777 return -EINVAL;
778 }
779
780 err = erspan_tun_opt_from_nlattr(a, match, is_mask,
781 log);
782 if (err)
783 return err;
784
785 tun_flags |= TUNNEL_ERSPAN_OPT;
786 opts_type = type;
787 break;
wenxu18b6f712019-03-28 12:43:23 +0800788 case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE:
789 info_bridge_mode = true;
790 ipv4 = true;
791 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700792 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200793 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700794 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700795 return -EINVAL;
796 }
797 }
798
799 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200800 if (is_mask)
801 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
802 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200803 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
804 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700805
806 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200807 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800808 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700809 return -EINVAL;
810 }
811
Jiri Benc6b26ba32015-10-05 13:09:47 +0200812 if (ipv4 && ipv6) {
813 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
814 return -EINVAL;
815 }
816
Pravin B Shelare6445712013-10-03 18:16:47 -0700817 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200818 if (!ipv4 && !ipv6) {
819 OVS_NLERR(log, "IP tunnel dst address not specified");
820 return -EINVAL;
821 }
wenxu18b6f712019-03-28 12:43:23 +0800822 if (ipv4) {
823 if (info_bridge_mode) {
824 if (match->key->tun_key.u.ipv4.src ||
825 match->key->tun_key.u.ipv4.dst ||
826 match->key->tun_key.tp_src ||
827 match->key->tun_key.tp_dst ||
828 match->key->tun_key.ttl ||
829 match->key->tun_key.tos ||
830 tun_flags & ~TUNNEL_KEY) {
831 OVS_NLERR(log, "IPv4 tun info is not correct");
832 return -EINVAL;
833 }
834 } else if (!match->key->tun_key.u.ipv4.dst) {
835 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
836 return -EINVAL;
837 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700838 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200839 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
840 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
841 return -EINVAL;
842 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700843
wenxu18b6f712019-03-28 12:43:23 +0800844 if (!ttl && !info_bridge_mode) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200845 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700846 return -EINVAL;
847 }
848 }
849
Thomas Graf1dd144c2015-01-15 03:53:59 +0100850 return opts_type;
851}
852
853static int vxlan_opt_to_nlattr(struct sk_buff *skb,
854 const void *tun_opts, int swkey_tun_opts_len)
855{
Thomas Graf614732e2015-07-21 10:44:06 +0200856 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100857 struct nlattr *nla;
858
859 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
860 if (!nla)
861 return -EMSGSIZE;
862
863 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
864 return -EMSGSIZE;
865
866 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700867 return 0;
868}
869
Jiri Benc6b26ba32015-10-05 13:09:47 +0200870static int __ip_tun_to_nlattr(struct sk_buff *skb,
871 const struct ip_tunnel_key *output,
872 const void *tun_opts, int swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800873 unsigned short tun_proto, u8 mode)
Pravin B Shelare6445712013-10-03 18:16:47 -0700874{
Pravin B Shelare6445712013-10-03 18:16:47 -0700875 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200876 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
877 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700878 return -EMSGSIZE;
wenxu18b6f712019-03-28 12:43:23 +0800879
880 if (mode & IP_TUNNEL_INFO_BRIDGE)
881 return nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE)
882 ? -EMSGSIZE : 0;
883
Jiri Benc6b26ba32015-10-05 13:09:47 +0200884 switch (tun_proto) {
885 case AF_INET:
886 if (output->u.ipv4.src &&
887 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
888 output->u.ipv4.src))
889 return -EMSGSIZE;
890 if (output->u.ipv4.dst &&
891 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
892 output->u.ipv4.dst))
893 return -EMSGSIZE;
894 break;
895 case AF_INET6:
896 if (!ipv6_addr_any(&output->u.ipv6.src) &&
897 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
898 &output->u.ipv6.src))
899 return -EMSGSIZE;
900 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
901 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
902 &output->u.ipv6.dst))
903 return -EMSGSIZE;
904 break;
905 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200906 if (output->tos &&
907 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700908 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200909 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700910 return -EMSGSIZE;
911 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700912 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700913 return -EMSGSIZE;
914 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700915 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
916 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800917 if (output->tp_src &&
918 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
919 return -EMSGSIZE;
920 if (output->tp_dst &&
921 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
922 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700923 if ((output->tun_flags & TUNNEL_OAM) &&
924 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700925 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700926 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100927 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
928 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
929 swkey_tun_opts_len, tun_opts))
930 return -EMSGSIZE;
931 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
932 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
933 return -EMSGSIZE;
William Tufc1372f2018-01-25 13:20:11 -0800934 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
935 nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
936 swkey_tun_opts_len, tun_opts))
937 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100938 }
Jesse Grossf5796682014-10-03 15:35:33 -0700939
940 return 0;
941}
942
Jiri Benc6b26ba32015-10-05 13:09:47 +0200943static int ip_tun_to_nlattr(struct sk_buff *skb,
944 const struct ip_tunnel_key *output,
945 const void *tun_opts, int swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800946 unsigned short tun_proto, u8 mode)
Jesse Grossf5796682014-10-03 15:35:33 -0700947{
948 struct nlattr *nla;
949 int err;
950
951 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
952 if (!nla)
953 return -EMSGSIZE;
954
Jiri Benc6b26ba32015-10-05 13:09:47 +0200955 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800956 tun_proto, mode);
Jesse Grossf5796682014-10-03 15:35:33 -0700957 if (err)
958 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700959
960 nla_nest_end(skb, nla);
961 return 0;
962}
963
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700964int ovs_nla_put_tunnel_info(struct sk_buff *skb,
965 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800966{
David S. Millerba3e2082015-10-24 06:54:12 -0700967 return __ip_tun_to_nlattr(skb, &tun_info->key,
968 ip_tunnel_info_opts(tun_info),
969 tun_info->options_len,
wenxu18b6f712019-03-28 12:43:23 +0800970 ip_tunnel_info_af(tun_info), tun_info->mode);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800971}
972
Eric Garver018c1dd2016-09-07 12:56:59 -0400973static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
974 const struct nlattr *a[],
975 bool is_mask, bool inner)
976{
977 __be16 tci = 0;
978 __be16 tpid = 0;
979
980 if (a[OVS_KEY_ATTR_VLAN])
981 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
982
983 if (a[OVS_KEY_ATTR_ETHERTYPE])
984 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
985
986 if (likely(!inner)) {
987 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
988 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
989 } else {
990 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
991 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
992 }
993 return 0;
994}
995
996static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
997 u64 key_attrs, bool inner,
998 const struct nlattr **a, bool log)
999{
1000 __be16 tci = 0;
1001
1002 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1003 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1004 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
1005 /* Not a VLAN. */
1006 return 0;
1007 }
1008
1009 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1010 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1011 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
1012 return -EINVAL;
1013 }
1014
1015 if (a[OVS_KEY_ATTR_VLAN])
1016 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1017
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001018 if (!(tci & htons(VLAN_CFI_MASK))) {
Eric Garver018c1dd2016-09-07 12:56:59 -04001019 if (tci) {
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001020 OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
Eric Garver018c1dd2016-09-07 12:56:59 -04001021 (inner) ? "C-VLAN" : "VLAN");
1022 return -EINVAL;
1023 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
1024 /* Corner case for truncated VLAN header. */
1025 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
1026 (inner) ? "C-VLAN" : "VLAN");
1027 return -EINVAL;
1028 }
1029 }
1030
1031 return 1;
1032}
1033
1034static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
1035 u64 key_attrs, bool inner,
1036 const struct nlattr **a, bool log)
1037{
1038 __be16 tci = 0;
1039 __be16 tpid = 0;
1040 bool encap_valid = !!(match->key->eth.vlan.tci &
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001041 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001042 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001043 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001044
1045 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
1046 /* Not a VLAN. */
1047 return 0;
1048 }
1049
1050 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
1051 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
1052 (inner) ? "C-VLAN" : "VLAN");
1053 return -EINVAL;
1054 }
1055
1056 if (a[OVS_KEY_ATTR_VLAN])
1057 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1058
1059 if (a[OVS_KEY_ATTR_ETHERTYPE])
1060 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1061
1062 if (tpid != htons(0xffff)) {
1063 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1064 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1065 return -EINVAL;
1066 }
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001067 if (!(tci & htons(VLAN_CFI_MASK))) {
1068 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
Eric Garver018c1dd2016-09-07 12:56:59 -04001069 (inner) ? "C-VLAN" : "VLAN");
1070 return -EINVAL;
1071 }
1072
1073 return 1;
1074}
1075
1076static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1077 u64 *key_attrs, bool inner,
1078 const struct nlattr **a, bool is_mask,
1079 bool log)
1080{
1081 int err;
1082 const struct nlattr *encap;
1083
1084 if (!is_mask)
1085 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1086 a, log);
1087 else
1088 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1089 a, log);
1090 if (err <= 0)
1091 return err;
1092
1093 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1094 if (err)
1095 return err;
1096
1097 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1098 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1099 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1100
1101 encap = a[OVS_KEY_ATTR_ENCAP];
1102
1103 if (!is_mask)
1104 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1105 else
1106 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1107
1108 return err;
1109}
1110
1111static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1112 u64 *key_attrs, const struct nlattr **a,
1113 bool is_mask, bool log)
1114{
1115 int err;
1116 bool encap_valid = false;
1117
1118 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1119 is_mask, log);
1120 if (err)
1121 return err;
1122
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001123 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001124 if (encap_valid) {
1125 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1126 is_mask, log);
1127 if (err)
1128 return err;
1129 }
1130
1131 return 0;
1132}
1133
Jiri Benc0a6410f2016-11-10 16:28:22 +01001134static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1135 u64 *attrs, const struct nlattr **a,
1136 bool is_mask, bool log)
1137{
1138 __be16 eth_type;
1139
1140 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1141 if (is_mask) {
1142 /* Always exact match EtherType. */
1143 eth_type = htons(0xffff);
1144 } else if (!eth_proto_is_802_3(eth_type)) {
1145 OVS_NLERR(log, "EtherType %x is less than min %x",
1146 ntohs(eth_type), ETH_P_802_3_MIN);
1147 return -EINVAL;
1148 }
1149
1150 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1151 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1152 return 0;
1153}
1154
Joe Stringerc2ac6672015-08-26 11:31:52 -07001155static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1156 u64 *attrs, const struct nlattr **a,
1157 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001158{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001159 u8 mac_proto = MAC_PROTO_ETHERNET;
1160
Andy Zhou971427f32014-09-15 19:37:25 -07001161 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1162 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1163
1164 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1165 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1166 }
1167
1168 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1169 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1170
1171 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1172 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1173 }
1174
Pravin B Shelare6445712013-10-03 18:16:47 -07001175 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1176 SW_FLOW_KEY_PUT(match, phy.priority,
1177 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1178 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1179 }
1180
1181 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1182 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1183
Jesse Gross426cda52014-10-06 05:08:38 -07001184 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001185 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001186 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001187 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001188 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001189 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001190 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001191
1192 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1193 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1194 } else if (!is_mask) {
1195 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1196 }
1197
1198 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1199 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1200
1201 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1202 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1203 }
1204 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001205 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1206 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001207 return -EINVAL;
1208 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1209 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001210
1211 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001212 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001213 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001214
Joe Stringer9e384712015-10-19 19:18:57 -07001215 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001216 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001217 ct_state);
1218 return -EINVAL;
1219 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001220
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001221 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001222 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1223 }
1224 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001225 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001226 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1227
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001228 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001229 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1230 }
Joe Stringer182e3042015-08-26 11:31:49 -07001231 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001232 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001233 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1234
1235 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1236 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1237 }
Joe Stringer33db4122015-10-01 15:00:37 -07001238 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1239 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1240 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001241
Joe Stringer33db4122015-10-01 15:00:37 -07001242 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1243 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001244 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001245 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001246 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001247 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1248 const struct ovs_key_ct_tuple_ipv4 *ct;
1249
1250 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1251
1252 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1253 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1254 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1255 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001256 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001257 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1258 }
1259 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1260 const struct ovs_key_ct_tuple_ipv6 *ct;
1261
1262 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1263
1264 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1265 sizeof(match->key->ipv6.ct_orig.src),
1266 is_mask);
1267 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1268 sizeof(match->key->ipv6.ct_orig.dst),
1269 is_mask);
1270 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1271 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001272 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001273 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1274 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001275
Jiri Benc0a6410f2016-11-10 16:28:22 +01001276 /* For layer 3 packets the Ethernet type is provided
1277 * and treated as metadata but no MAC addresses are provided.
1278 */
1279 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1280 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1281 mac_proto = MAC_PROTO_NONE;
1282
Jiri Benc329f45b2016-11-10 16:28:18 +01001283 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001284 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1285
1286 if (mac_proto == MAC_PROTO_NONE)
1287 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1288 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001289
Pravin B Shelare6445712013-10-03 18:16:47 -07001290 return 0;
1291}
1292
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001293int nsh_hdr_from_nlattr(const struct nlattr *attr,
1294 struct nshhdr *nh, size_t size)
1295{
1296 struct nlattr *a;
1297 int rem;
1298 u8 flags = 0;
1299 u8 ttl = 0;
1300 int mdlen = 0;
1301
1302 /* validate_nsh has check this, so we needn't do duplicate check here
1303 */
1304 if (size < NSH_BASE_HDR_LEN)
1305 return -ENOBUFS;
1306
1307 nla_for_each_nested(a, attr, rem) {
1308 int type = nla_type(a);
1309
1310 switch (type) {
1311 case OVS_NSH_KEY_ATTR_BASE: {
1312 const struct ovs_nsh_key_base *base = nla_data(a);
1313
1314 flags = base->flags;
1315 ttl = base->ttl;
1316 nh->np = base->np;
1317 nh->mdtype = base->mdtype;
1318 nh->path_hdr = base->path_hdr;
1319 break;
1320 }
1321 case OVS_NSH_KEY_ATTR_MD1:
1322 mdlen = nla_len(a);
1323 if (mdlen > size - NSH_BASE_HDR_LEN)
1324 return -ENOBUFS;
1325 memcpy(&nh->md1, nla_data(a), mdlen);
1326 break;
1327
1328 case OVS_NSH_KEY_ATTR_MD2:
1329 mdlen = nla_len(a);
1330 if (mdlen > size - NSH_BASE_HDR_LEN)
1331 return -ENOBUFS;
1332 memcpy(&nh->md2, nla_data(a), mdlen);
1333 break;
1334
1335 default:
1336 return -EINVAL;
1337 }
1338 }
1339
1340 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1341 nh->ver_flags_ttl_len = 0;
1342 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1343
1344 return 0;
1345}
1346
1347int nsh_key_from_nlattr(const struct nlattr *attr,
1348 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1349{
1350 struct nlattr *a;
1351 int rem;
1352
1353 /* validate_nsh has check this, so we needn't do duplicate check here
1354 */
1355 nla_for_each_nested(a, attr, rem) {
1356 int type = nla_type(a);
1357
1358 switch (type) {
1359 case OVS_NSH_KEY_ATTR_BASE: {
1360 const struct ovs_nsh_key_base *base = nla_data(a);
1361 const struct ovs_nsh_key_base *base_mask = base + 1;
1362
1363 nsh->base = *base;
1364 nsh_mask->base = *base_mask;
1365 break;
1366 }
1367 case OVS_NSH_KEY_ATTR_MD1: {
1368 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1369 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1370
1371 memcpy(nsh->context, md1->context, sizeof(*md1));
1372 memcpy(nsh_mask->context, md1_mask->context,
1373 sizeof(*md1_mask));
1374 break;
1375 }
1376 case OVS_NSH_KEY_ATTR_MD2:
1377 /* Not supported yet */
1378 return -ENOTSUPP;
1379 default:
1380 return -EINVAL;
1381 }
1382 }
1383
1384 return 0;
1385}
1386
1387static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1388 struct sw_flow_match *match, bool is_mask,
1389 bool is_push_nsh, bool log)
1390{
1391 struct nlattr *a;
1392 int rem;
1393 bool has_base = false;
1394 bool has_md1 = false;
1395 bool has_md2 = false;
1396 u8 mdtype = 0;
1397 int mdlen = 0;
1398
1399 if (WARN_ON(is_push_nsh && is_mask))
1400 return -EINVAL;
1401
1402 nla_for_each_nested(a, attr, rem) {
1403 int type = nla_type(a);
1404 int i;
1405
1406 if (type > OVS_NSH_KEY_ATTR_MAX) {
1407 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1408 type, OVS_NSH_KEY_ATTR_MAX);
1409 return -EINVAL;
1410 }
1411
1412 if (!check_attr_len(nla_len(a),
1413 ovs_nsh_key_attr_lens[type].len)) {
1414 OVS_NLERR(
1415 log,
1416 "nsh attr %d has unexpected len %d expected %d",
1417 type,
1418 nla_len(a),
1419 ovs_nsh_key_attr_lens[type].len
1420 );
1421 return -EINVAL;
1422 }
1423
1424 switch (type) {
1425 case OVS_NSH_KEY_ATTR_BASE: {
1426 const struct ovs_nsh_key_base *base = nla_data(a);
1427
1428 has_base = true;
1429 mdtype = base->mdtype;
1430 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1431 base->flags, is_mask);
1432 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1433 base->ttl, is_mask);
1434 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1435 base->mdtype, is_mask);
1436 SW_FLOW_KEY_PUT(match, nsh.base.np,
1437 base->np, is_mask);
1438 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1439 base->path_hdr, is_mask);
1440 break;
1441 }
1442 case OVS_NSH_KEY_ATTR_MD1: {
1443 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1444
1445 has_md1 = true;
1446 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1447 SW_FLOW_KEY_PUT(match, nsh.context[i],
1448 md1->context[i], is_mask);
1449 break;
1450 }
1451 case OVS_NSH_KEY_ATTR_MD2:
1452 if (!is_push_nsh) /* Not supported MD type 2 yet */
1453 return -ENOTSUPP;
1454
1455 has_md2 = true;
1456 mdlen = nla_len(a);
1457 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1458 OVS_NLERR(
1459 log,
1460 "Invalid MD length %d for MD type %d",
1461 mdlen,
1462 mdtype
1463 );
1464 return -EINVAL;
1465 }
1466 break;
1467 default:
1468 OVS_NLERR(log, "Unknown nsh attribute %d",
1469 type);
1470 return -EINVAL;
1471 }
1472 }
1473
1474 if (rem > 0) {
1475 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1476 return -EINVAL;
1477 }
1478
1479 if (has_md1 && has_md2) {
1480 OVS_NLERR(
1481 1,
1482 "invalid nsh attribute: md1 and md2 are exclusive."
1483 );
1484 return -EINVAL;
1485 }
1486
1487 if (!is_mask) {
1488 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1489 (has_md2 && mdtype != NSH_M_TYPE2)) {
1490 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1491 mdtype);
1492 return -EINVAL;
1493 }
1494
1495 if (is_push_nsh &&
1496 (!has_base || (!has_md1 && !has_md2))) {
1497 OVS_NLERR(
1498 1,
1499 "push_nsh: missing base or metadata attributes"
1500 );
1501 return -EINVAL;
1502 }
1503 }
1504
1505 return 0;
1506}
1507
Joe Stringerc2ac6672015-08-26 11:31:52 -07001508static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1509 u64 attrs, const struct nlattr **a,
1510 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001511{
1512 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001513
Joe Stringerc2ac6672015-08-26 11:31:52 -07001514 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001515 if (err)
1516 return err;
1517
1518 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1519 const struct ovs_key_ethernet *eth_key;
1520
1521 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1522 SW_FLOW_KEY_MEMCPY(match, eth.src,
1523 eth_key->eth_src, ETH_ALEN, is_mask);
1524 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1525 eth_key->eth_dst, ETH_ALEN, is_mask);
1526 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001527
Jiri Benc0a6410f2016-11-10 16:28:22 +01001528 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1529 /* VLAN attribute is always parsed before getting here since it
1530 * may occur multiple times.
1531 */
1532 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001533 return -EINVAL;
1534 }
1535
Jiri Benc0a6410f2016-11-10 16:28:22 +01001536 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1537 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1538 log);
1539 if (err)
1540 return err;
1541 } else if (!is_mask) {
1542 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1543 }
1544 } else if (!match->key->eth.type) {
1545 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1546 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001547 }
1548
1549 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1550 const struct ovs_key_ipv4 *ipv4_key;
1551
1552 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1553 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001554 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1555 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001556 return -EINVAL;
1557 }
1558 SW_FLOW_KEY_PUT(match, ip.proto,
1559 ipv4_key->ipv4_proto, is_mask);
1560 SW_FLOW_KEY_PUT(match, ip.tos,
1561 ipv4_key->ipv4_tos, is_mask);
1562 SW_FLOW_KEY_PUT(match, ip.ttl,
1563 ipv4_key->ipv4_ttl, is_mask);
1564 SW_FLOW_KEY_PUT(match, ip.frag,
1565 ipv4_key->ipv4_frag, is_mask);
1566 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1567 ipv4_key->ipv4_src, is_mask);
1568 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1569 ipv4_key->ipv4_dst, is_mask);
1570 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1571 }
1572
1573 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1574 const struct ovs_key_ipv6 *ipv6_key;
1575
1576 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1577 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001578 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1579 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001580 return -EINVAL;
1581 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001582
Joe Stringerd3052bb2014-11-19 13:54:49 -08001583 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001584 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001585 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1586 return -EINVAL;
1587 }
1588
Pravin B Shelare6445712013-10-03 18:16:47 -07001589 SW_FLOW_KEY_PUT(match, ipv6.label,
1590 ipv6_key->ipv6_label, is_mask);
1591 SW_FLOW_KEY_PUT(match, ip.proto,
1592 ipv6_key->ipv6_proto, is_mask);
1593 SW_FLOW_KEY_PUT(match, ip.tos,
1594 ipv6_key->ipv6_tclass, is_mask);
1595 SW_FLOW_KEY_PUT(match, ip.ttl,
1596 ipv6_key->ipv6_hlimit, is_mask);
1597 SW_FLOW_KEY_PUT(match, ip.frag,
1598 ipv6_key->ipv6_frag, is_mask);
1599 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1600 ipv6_key->ipv6_src,
1601 sizeof(match->key->ipv6.addr.src),
1602 is_mask);
1603 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1604 ipv6_key->ipv6_dst,
1605 sizeof(match->key->ipv6.addr.dst),
1606 is_mask);
1607
1608 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1609 }
1610
1611 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1612 const struct ovs_key_arp *arp_key;
1613
1614 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1615 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001616 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001617 arp_key->arp_op);
1618 return -EINVAL;
1619 }
1620
1621 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1622 arp_key->arp_sip, is_mask);
1623 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1624 arp_key->arp_tip, is_mask);
1625 SW_FLOW_KEY_PUT(match, ip.proto,
1626 ntohs(arp_key->arp_op), is_mask);
1627 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1628 arp_key->arp_sha, ETH_ALEN, is_mask);
1629 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1630 arp_key->arp_tha, ETH_ALEN, is_mask);
1631
1632 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1633 }
1634
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001635 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1636 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1637 is_mask, false, log) < 0)
1638 return -EINVAL;
1639 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1640 }
1641
Simon Horman25cd9ba2014-10-06 05:05:13 -07001642 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1643 const struct ovs_key_mpls *mpls_key;
1644
1645 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1646 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1647 mpls_key->mpls_lse, is_mask);
1648
1649 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1650 }
1651
Pravin B Shelare6445712013-10-03 18:16:47 -07001652 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1653 const struct ovs_key_tcp *tcp_key;
1654
1655 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001656 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1657 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001658 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1659 }
1660
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001661 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001662 SW_FLOW_KEY_PUT(match, tp.flags,
1663 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1664 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001665 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1666 }
1667
Pravin B Shelare6445712013-10-03 18:16:47 -07001668 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1669 const struct ovs_key_udp *udp_key;
1670
1671 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001672 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1673 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001674 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1675 }
1676
1677 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1678 const struct ovs_key_sctp *sctp_key;
1679
1680 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001681 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1682 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001683 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1684 }
1685
1686 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1687 const struct ovs_key_icmp *icmp_key;
1688
1689 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001690 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001691 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001692 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001693 htons(icmp_key->icmp_code), is_mask);
1694 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1695 }
1696
1697 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1698 const struct ovs_key_icmpv6 *icmpv6_key;
1699
1700 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001701 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001702 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001703 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001704 htons(icmpv6_key->icmpv6_code), is_mask);
1705 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1706 }
1707
1708 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1709 const struct ovs_key_nd *nd_key;
1710
1711 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1712 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1713 nd_key->nd_target,
1714 sizeof(match->key->ipv6.nd.target),
1715 is_mask);
1716 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1717 nd_key->nd_sll, ETH_ALEN, is_mask);
1718 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1719 nd_key->nd_tll, ETH_ALEN, is_mask);
1720 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1721 }
1722
Jesse Gross426cda52014-10-06 05:08:38 -07001723 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001724 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001725 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001726 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001727 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001728
1729 return 0;
1730}
1731
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001732static void nlattr_set(struct nlattr *attr, u8 val,
1733 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001734{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001735 struct nlattr *nla;
1736 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001737
Pravin B Shelarf47de062014-10-16 21:55:45 -07001738 /* The nlattr stream should already have been validated */
1739 nla_for_each_nested(nla, attr, rem) {
Stefano Brivio72f17ba2018-05-03 18:13:25 +02001740 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1741 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1742 else
Pravin B Shelarf47de062014-10-16 21:55:45 -07001743 memset(nla_data(nla), val, nla_len(nla));
Joe Stringer9e384712015-10-19 19:18:57 -07001744
1745 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1746 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001747 }
1748}
1749
1750static void mask_set_nlattr(struct nlattr *attr, u8 val)
1751{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001752 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001753}
1754
1755/**
1756 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1757 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1758 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1759 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001760 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001761 * @match: receives the extracted flow match information.
1762 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1763 * sequence. The fields should of the packet that triggered the creation
1764 * of this flow.
1765 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1766 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001767 * @log: Boolean to allow kernel error logging. Normally true, but when
1768 * probing for feature compatibility this should be passed in as false to
1769 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001770 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001771int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001772 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001773 const struct nlattr *nla_mask,
1774 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001775{
1776 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001777 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001778 u64 key_attrs = 0;
1779 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001780 int err;
1781
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001782 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001783 if (err)
1784 return err;
1785
Eric Garver018c1dd2016-09-07 12:56:59 -04001786 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1787 if (err)
1788 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001789
Joe Stringerc2ac6672015-08-26 11:31:52 -07001790 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001791 if (err)
1792 return err;
1793
Pravin B Shelara85311b2014-10-19 12:03:40 -07001794 if (match->mask) {
1795 if (!nla_mask) {
1796 /* Create an exact match mask. We need to set to 0xff
1797 * all the 'match->mask' fields that have been touched
1798 * in 'match->key'. We cannot simply memset
1799 * 'match->mask', because padding bytes and fields not
1800 * specified in 'match->key' should be left to 0.
1801 * Instead, we use a stream of netlink attributes,
1802 * copied from 'key' and set to 0xff.
1803 * ovs_key_from_nlattrs() will take care of filling
1804 * 'match->mask' appropriately.
1805 */
1806 newmask = kmemdup(nla_key,
1807 nla_total_size(nla_len(nla_key)),
1808 GFP_KERNEL);
1809 if (!newmask)
1810 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001811
Pravin B Shelara85311b2014-10-19 12:03:40 -07001812 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001813
Pravin B Shelara85311b2014-10-19 12:03:40 -07001814 /* The userspace does not send tunnel attributes that
1815 * are 0, but we should not wildcard them nonetheless.
1816 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001817 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001818 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1819 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001820
Pravin B Shelara85311b2014-10-19 12:03:40 -07001821 nla_mask = newmask;
1822 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001823
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001824 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001825 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001826 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001827
Pravin B Shelara85311b2014-10-19 12:03:40 -07001828 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001829 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1830 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001831
Eric Garver018c1dd2016-09-07 12:56:59 -04001832 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1833 if (err)
1834 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001835
Joe Stringerc2ac6672015-08-26 11:31:52 -07001836 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1837 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001838 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001839 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001840 }
1841
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001842 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001843 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001844
Pravin B Shelarf47de062014-10-16 21:55:45 -07001845free_newmask:
1846 kfree(newmask);
1847 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001848}
1849
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001850static size_t get_ufid_len(const struct nlattr *attr, bool log)
1851{
1852 size_t len;
1853
1854 if (!attr)
1855 return 0;
1856
1857 len = nla_len(attr);
1858 if (len < 1 || len > MAX_UFID_LENGTH) {
1859 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1860 nla_len(attr), MAX_UFID_LENGTH);
1861 return 0;
1862 }
1863
1864 return len;
1865}
1866
1867/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1868 * or false otherwise.
1869 */
1870bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1871 bool log)
1872{
1873 sfid->ufid_len = get_ufid_len(attr, log);
1874 if (sfid->ufid_len)
1875 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1876
1877 return sfid->ufid_len;
1878}
1879
1880int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1881 const struct sw_flow_key *key, bool log)
1882{
1883 struct sw_flow_key *new_key;
1884
1885 if (ovs_nla_get_ufid(sfid, ufid, log))
1886 return 0;
1887
1888 /* If UFID was not provided, use unmasked key. */
1889 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1890 if (!new_key)
1891 return -ENOMEM;
1892 memcpy(new_key, key, sizeof(*key));
1893 sfid->unmasked_key = new_key;
1894
1895 return 0;
1896}
1897
1898u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1899{
1900 return attr ? nla_get_u32(attr) : 0;
1901}
1902
Pravin B Shelare6445712013-10-03 18:16:47 -07001903/**
1904 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001905 * @net: Network namespace.
1906 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1907 * metadata.
1908 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1909 * attributes.
1910 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001911 * @log: Boolean to allow kernel error logging. Normally true, but when
1912 * probing for feature compatibility this should be passed in as false to
1913 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001914 *
1915 * This parses a series of Netlink attributes that form a flow key, which must
1916 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1917 * get the metadata, that is, the parts of the flow key that cannot be
1918 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001919 *
1920 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001921 */
1922
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001923int ovs_nla_get_flow_metadata(struct net *net,
1924 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1925 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001926{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001927 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001928
1929 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001930 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001931
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001932 key->ct_state = 0;
1933 key->ct_zone = 0;
1934 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001935 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001936 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1937 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1938
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001939 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001940
Joe Stringerc2ac6672015-08-26 11:31:52 -07001941 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001942}
1943
Eric Garver018c1dd2016-09-07 12:56:59 -04001944static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1945 bool is_mask)
1946{
1947 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1948
1949 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1950 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1951 return -EMSGSIZE;
1952 return 0;
1953}
1954
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001955static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1956 struct sk_buff *skb)
1957{
1958 struct nlattr *start;
1959
1960 start = nla_nest_start(skb, OVS_KEY_ATTR_NSH);
1961 if (!start)
1962 return -EMSGSIZE;
1963
1964 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1965 goto nla_put_failure;
1966
1967 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1968 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1969 sizeof(nsh->context), nsh->context))
1970 goto nla_put_failure;
1971 }
1972
1973 /* Don't support MD type 2 yet */
1974
1975 nla_nest_end(skb, start);
1976
1977 return 0;
1978
1979nla_put_failure:
1980 return -EMSGSIZE;
1981}
1982
Joe Stringer5b4237b2015-01-21 16:42:48 -08001983static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1984 const struct sw_flow_key *output, bool is_mask,
1985 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001986{
1987 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001988 struct nlattr *nla;
1989 struct nlattr *encap = NULL;
1990 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001991
Andy Zhou971427f32014-09-15 19:37:25 -07001992 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1993 goto nla_put_failure;
1994
1995 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1996 goto nla_put_failure;
1997
Pravin B Shelare6445712013-10-03 18:16:47 -07001998 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1999 goto nla_put_failure;
2000
Jiri Benc00a93ba2015-10-05 13:09:46 +02002001 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01002002 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07002003
2004 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01002005 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07002006
Jiri Benc6b26ba32015-10-05 13:09:47 +02002007 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
wenxu18b6f712019-03-28 12:43:23 +08002008 swkey->tun_opts_len, swkey->tun_proto, 0))
Jesse Grossf5796682014-10-03 15:35:33 -07002009 goto nla_put_failure;
2010 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002011
2012 if (swkey->phy.in_port == DP_MAX_PORTS) {
2013 if (is_mask && (output->phy.in_port == 0xffff))
2014 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
2015 goto nla_put_failure;
2016 } else {
2017 u16 upper_u16;
2018 upper_u16 = !is_mask ? 0 : 0xffff;
2019
2020 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
2021 (upper_u16 << 16) | output->phy.in_port))
2022 goto nla_put_failure;
2023 }
2024
2025 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
2026 goto nla_put_failure;
2027
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08002028 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07002029 goto nla_put_failure;
2030
Jiri Benc0a6410f2016-11-10 16:28:22 +01002031 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
2032 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
2033 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07002034 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04002035
Jiri Benc0a6410f2016-11-10 16:28:22 +01002036 eth_key = nla_data(nla);
2037 ether_addr_copy(eth_key->eth_src, output->eth.src);
2038 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
2039
2040 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
2041 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04002042 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002043 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
2044 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04002045 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07002046
Jiri Benc0a6410f2016-11-10 16:28:22 +01002047 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
2048 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
2049 goto nla_put_failure;
2050 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
2051 if (!swkey->eth.cvlan.tci)
2052 goto unencap;
2053 }
2054 }
2055
2056 if (swkey->eth.type == htons(ETH_P_802_2)) {
2057 /*
2058 * Ethertype 802.2 is represented in the netlink with omitted
2059 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2060 * 0xffff in the mask attribute. Ethertype can also
2061 * be wildcarded.
2062 */
2063 if (is_mask && output->eth.type)
2064 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
2065 output->eth.type))
2066 goto nla_put_failure;
2067 goto unencap;
2068 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002069 }
2070
2071 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2072 goto nla_put_failure;
2073
Eric Garver018c1dd2016-09-07 12:56:59 -04002074 if (eth_type_vlan(swkey->eth.type)) {
2075 /* There are 3 VLAN tags, we don't know anything about the rest
2076 * of the packet, so truncate here.
2077 */
2078 WARN_ON_ONCE(!(encap && in_encap));
2079 goto unencap;
2080 }
2081
Pravin B Shelare6445712013-10-03 18:16:47 -07002082 if (swkey->eth.type == htons(ETH_P_IP)) {
2083 struct ovs_key_ipv4 *ipv4_key;
2084
2085 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2086 if (!nla)
2087 goto nla_put_failure;
2088 ipv4_key = nla_data(nla);
2089 ipv4_key->ipv4_src = output->ipv4.addr.src;
2090 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2091 ipv4_key->ipv4_proto = output->ip.proto;
2092 ipv4_key->ipv4_tos = output->ip.tos;
2093 ipv4_key->ipv4_ttl = output->ip.ttl;
2094 ipv4_key->ipv4_frag = output->ip.frag;
2095 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2096 struct ovs_key_ipv6 *ipv6_key;
2097
2098 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2099 if (!nla)
2100 goto nla_put_failure;
2101 ipv6_key = nla_data(nla);
2102 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2103 sizeof(ipv6_key->ipv6_src));
2104 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2105 sizeof(ipv6_key->ipv6_dst));
2106 ipv6_key->ipv6_label = output->ipv6.label;
2107 ipv6_key->ipv6_proto = output->ip.proto;
2108 ipv6_key->ipv6_tclass = output->ip.tos;
2109 ipv6_key->ipv6_hlimit = output->ip.ttl;
2110 ipv6_key->ipv6_frag = output->ip.frag;
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002111 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2112 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2113 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002114 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2115 swkey->eth.type == htons(ETH_P_RARP)) {
2116 struct ovs_key_arp *arp_key;
2117
2118 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2119 if (!nla)
2120 goto nla_put_failure;
2121 arp_key = nla_data(nla);
2122 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2123 arp_key->arp_sip = output->ipv4.addr.src;
2124 arp_key->arp_tip = output->ipv4.addr.dst;
2125 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08002126 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2127 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002128 } else if (eth_p_mpls(swkey->eth.type)) {
2129 struct ovs_key_mpls *mpls_key;
2130
2131 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
2132 if (!nla)
2133 goto nla_put_failure;
2134 mpls_key = nla_data(nla);
2135 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07002136 }
2137
2138 if ((swkey->eth.type == htons(ETH_P_IP) ||
2139 swkey->eth.type == htons(ETH_P_IPV6)) &&
2140 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2141
2142 if (swkey->ip.proto == IPPROTO_TCP) {
2143 struct ovs_key_tcp *tcp_key;
2144
2145 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2146 if (!nla)
2147 goto nla_put_failure;
2148 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002149 tcp_key->tcp_src = output->tp.src;
2150 tcp_key->tcp_dst = output->tp.dst;
2151 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2152 output->tp.flags))
2153 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002154 } else if (swkey->ip.proto == IPPROTO_UDP) {
2155 struct ovs_key_udp *udp_key;
2156
2157 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2158 if (!nla)
2159 goto nla_put_failure;
2160 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002161 udp_key->udp_src = output->tp.src;
2162 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002163 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2164 struct ovs_key_sctp *sctp_key;
2165
2166 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2167 if (!nla)
2168 goto nla_put_failure;
2169 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002170 sctp_key->sctp_src = output->tp.src;
2171 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002172 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2173 swkey->ip.proto == IPPROTO_ICMP) {
2174 struct ovs_key_icmp *icmp_key;
2175
2176 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2177 if (!nla)
2178 goto nla_put_failure;
2179 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002180 icmp_key->icmp_type = ntohs(output->tp.src);
2181 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002182 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2183 swkey->ip.proto == IPPROTO_ICMPV6) {
2184 struct ovs_key_icmpv6 *icmpv6_key;
2185
2186 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2187 sizeof(*icmpv6_key));
2188 if (!nla)
2189 goto nla_put_failure;
2190 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002191 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2192 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002193
2194 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
2195 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
2196 struct ovs_key_nd *nd_key;
2197
2198 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2199 if (!nla)
2200 goto nla_put_failure;
2201 nd_key = nla_data(nla);
2202 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2203 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08002204 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2205 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07002206 }
2207 }
2208 }
2209
2210unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04002211 if (in_encap)
2212 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07002213 if (encap)
2214 nla_nest_end(skb, encap);
2215
2216 return 0;
2217
2218nla_put_failure:
2219 return -EMSGSIZE;
2220}
2221
Joe Stringer5b4237b2015-01-21 16:42:48 -08002222int ovs_nla_put_key(const struct sw_flow_key *swkey,
2223 const struct sw_flow_key *output, int attr, bool is_mask,
2224 struct sk_buff *skb)
2225{
2226 int err;
2227 struct nlattr *nla;
2228
2229 nla = nla_nest_start(skb, attr);
2230 if (!nla)
2231 return -EMSGSIZE;
2232 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2233 if (err)
2234 return err;
2235 nla_nest_end(skb, nla);
2236
2237 return 0;
2238}
2239
2240/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002241int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08002242{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002243 if (ovs_identifier_is_ufid(&flow->id))
2244 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2245 flow->id.ufid);
2246
2247 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2248 OVS_FLOW_ATTR_KEY, false, skb);
2249}
2250
2251/* Called with ovs_mutex or RCU read lock. */
2252int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2253{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08002254 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08002255 OVS_FLOW_ATTR_KEY, false, skb);
2256}
2257
2258/* Called with ovs_mutex or RCU read lock. */
2259int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2260{
2261 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2262 OVS_FLOW_ATTR_MASK, true, skb);
2263}
2264
Pravin B Shelare6445712013-10-03 18:16:47 -07002265#define MAX_ACTIONS_BUFSIZE (32 * 1024)
2266
zhangliping67c8d222017-11-25 22:02:12 +08002267static struct sw_flow_actions *nla_alloc_flow_actions(int size)
Pravin B Shelare6445712013-10-03 18:16:47 -07002268{
2269 struct sw_flow_actions *sfa;
2270
zhangliping67c8d222017-11-25 22:02:12 +08002271 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002272
2273 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2274 if (!sfa)
2275 return ERR_PTR(-ENOMEM);
2276
2277 sfa->actions_len = 0;
2278 return sfa;
2279}
2280
Thomas Graf34ae9322015-07-21 10:44:03 +02002281static void ovs_nla_free_set_action(const struct nlattr *a)
2282{
2283 const struct nlattr *ovs_key = nla_data(a);
2284 struct ovs_tunnel_info *ovs_tun;
2285
2286 switch (nla_type(ovs_key)) {
2287 case OVS_KEY_ATTR_TUNNEL_INFO:
2288 ovs_tun = nla_data(ovs_key);
2289 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2290 break;
2291 }
2292}
2293
Pravin B Shelare6445712013-10-03 18:16:47 -07002294void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2295{
Thomas Graf34ae9322015-07-21 10:44:03 +02002296 const struct nlattr *a;
2297 int rem;
2298
2299 if (!sf_acts)
2300 return;
2301
2302 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
2303 switch (nla_type(a)) {
2304 case OVS_ACTION_ATTR_SET:
2305 ovs_nla_free_set_action(a);
2306 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002307 case OVS_ACTION_ATTR_CT:
2308 ovs_ct_free_action(a);
2309 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02002310 }
2311 }
2312
2313 kfree(sf_acts);
2314}
2315
2316static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2317{
2318 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2319}
2320
2321/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2322 * The caller must hold rcu_read_lock for this to be sensible. */
2323void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2324{
2325 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07002326}
2327
2328static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002329 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002330{
2331
2332 struct sw_flow_actions *acts;
2333 int new_acts_size;
2334 int req_size = NLA_ALIGN(attr_len);
2335 int next_offset = offsetof(struct sw_flow_actions, actions) +
2336 (*sfa)->actions_len;
2337
2338 if (req_size <= (ksize(*sfa) - next_offset))
2339 goto out;
2340
2341 new_acts_size = ksize(*sfa) * 2;
2342
2343 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
zhangliping67c8d222017-11-25 22:02:12 +08002344 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
2345 OVS_NLERR(log, "Flow action size exceeds max %u",
2346 MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002347 return ERR_PTR(-EMSGSIZE);
zhangliping67c8d222017-11-25 22:02:12 +08002348 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002349 new_acts_size = MAX_ACTIONS_BUFSIZE;
2350 }
2351
zhangliping67c8d222017-11-25 22:02:12 +08002352 acts = nla_alloc_flow_actions(new_acts_size);
Pravin B Shelare6445712013-10-03 18:16:47 -07002353 if (IS_ERR(acts))
2354 return (void *)acts;
2355
2356 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2357 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07002358 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002359 kfree(*sfa);
2360 *sfa = acts;
2361
2362out:
2363 (*sfa)->actions_len += req_size;
2364 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2365}
2366
Jesse Grossf0b128c2014-10-03 15:35:31 -07002367static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002368 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002369{
2370 struct nlattr *a;
2371
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002372 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002373 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002374 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002375
2376 a->nla_type = attrtype;
2377 a->nla_len = nla_attr_size(len);
2378
2379 if (data)
2380 memcpy(nla_data(a), data, len);
2381 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2382
Jesse Grossf0b128c2014-10-03 15:35:31 -07002383 return a;
2384}
2385
Joe Stringer7f8a4362015-08-26 11:31:48 -07002386int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2387 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002388{
2389 struct nlattr *a;
2390
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002391 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002392
Fabian Frederickf35423c12014-11-14 19:32:58 +01002393 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002394}
2395
2396static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002397 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002398{
2399 int used = (*sfa)->actions_len;
2400 int err;
2401
Joe Stringer7f8a4362015-08-26 11:31:48 -07002402 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002403 if (err)
2404 return err;
2405
2406 return used;
2407}
2408
2409static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2410 int st_offset)
2411{
2412 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2413 st_offset);
2414
2415 a->nla_len = sfa->actions_len - st_offset;
2416}
2417
Joe Stringer7f8a4362015-08-26 11:31:48 -07002418static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002419 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002420 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002421 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002422
Joe Stringer7f8a4362015-08-26 11:31:48 -07002423static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002424 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002425 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002426 __be16 eth_type, __be16 vlan_tci,
2427 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002428{
2429 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2430 const struct nlattr *probability, *actions;
2431 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002432 int rem, start, err;
2433 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002434
2435 memset(attrs, 0, sizeof(attrs));
2436 nla_for_each_nested(a, attr, rem) {
2437 int type = nla_type(a);
2438 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2439 return -EINVAL;
2440 attrs[type] = a;
2441 }
2442 if (rem)
2443 return -EINVAL;
2444
2445 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2446 if (!probability || nla_len(probability) != sizeof(u32))
2447 return -EINVAL;
2448
2449 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2450 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2451 return -EINVAL;
2452
2453 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002454 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002455 if (start < 0)
2456 return start;
andy zhou798c1662017-03-20 16:32:29 -07002457
2458 /* When both skb and flow may be changed, put the sample
2459 * into a deferred fifo. On the other hand, if only skb
2460 * may be modified, the actions can be executed in place.
2461 *
2462 * Do this analysis at the flow installation time.
2463 * Set 'clone_action->exec' to true if the actions can be
2464 * executed without being deferred.
2465 *
2466 * If the sample is the last action, it can always be excuted
2467 * rather than deferred.
2468 */
2469 arg.exec = last || !actions_may_change_flow(actions);
2470 arg.probability = nla_get_u32(probability);
2471
2472 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2473 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002474 if (err)
2475 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002476
andy zhou798c1662017-03-20 16:32:29 -07002477 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002478 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002479
Pravin B Shelare6445712013-10-03 18:16:47 -07002480 if (err)
2481 return err;
2482
Pravin B Shelare6445712013-10-03 18:16:47 -07002483 add_nested_action_end(*sfa, start);
2484
2485 return 0;
2486}
2487
Yifeng Sunb2335042018-07-02 08:18:03 -07002488static int validate_and_copy_clone(struct net *net,
2489 const struct nlattr *attr,
2490 const struct sw_flow_key *key,
2491 struct sw_flow_actions **sfa,
2492 __be16 eth_type, __be16 vlan_tci,
2493 bool log, bool last)
2494{
2495 int start, err;
2496 u32 exec;
2497
2498 if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
2499 return -EINVAL;
2500
2501 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
2502 if (start < 0)
2503 return start;
2504
2505 exec = last || !actions_may_change_flow(attr);
2506
2507 err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
2508 sizeof(exec), log);
2509 if (err)
2510 return err;
2511
2512 err = __ovs_nla_copy_actions(net, attr, key, sfa,
2513 eth_type, vlan_tci, log);
2514 if (err)
2515 return err;
2516
2517 add_nested_action_end(*sfa, start);
2518
2519 return 0;
2520}
2521
Pravin B Shelare6445712013-10-03 18:16:47 -07002522void ovs_match_init(struct sw_flow_match *match,
2523 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002524 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002525 struct sw_flow_mask *mask)
2526{
2527 memset(match, 0, sizeof(*match));
2528 match->key = key;
2529 match->mask = mask;
2530
pravin shelar22799942016-09-19 13:51:00 -07002531 if (reset_key)
2532 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002533
2534 if (mask) {
2535 memset(&mask->key, 0, sizeof(mask->key));
2536 mask->range.start = mask->range.end = 0;
2537 }
2538}
2539
Thomas Grafd91641d2015-01-15 03:53:57 +01002540static int validate_geneve_opts(struct sw_flow_key *key)
2541{
2542 struct geneve_opt *option;
2543 int opts_len = key->tun_opts_len;
2544 bool crit_opt = false;
2545
2546 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2547 while (opts_len > 0) {
2548 int len;
2549
2550 if (opts_len < sizeof(*option))
2551 return -EINVAL;
2552
2553 len = sizeof(*option) + option->length * 4;
2554 if (len > opts_len)
2555 return -EINVAL;
2556
2557 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2558
2559 option = (struct geneve_opt *)((u8 *)option + len);
2560 opts_len -= len;
Christopher Díaz Riveros89290b82018-01-17 16:10:28 -05002561 }
Thomas Grafd91641d2015-01-15 03:53:57 +01002562
2563 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2564
2565 return 0;
2566}
2567
Pravin B Shelare6445712013-10-03 18:16:47 -07002568static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002569 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002570{
2571 struct sw_flow_match match;
2572 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002573 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002574 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002575 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002576 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002577 int err = 0, start, opts_type;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002578 __be16 dst_opt_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002579
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002580 dst_opt_type = 0;
pravin shelar22799942016-09-19 13:51:00 -07002581 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002582 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002583 if (opts_type < 0)
2584 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002585
Jesse Grossf5796682014-10-03 15:35:33 -07002586 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002587 switch (opts_type) {
2588 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2589 err = validate_geneve_opts(&key);
2590 if (err < 0)
2591 return err;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002592 dst_opt_type = TUNNEL_GENEVE_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002593 break;
2594 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002595 dst_opt_type = TUNNEL_VXLAN_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002596 break;
William Tufc1372f2018-01-25 13:20:11 -08002597 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002598 dst_opt_type = TUNNEL_ERSPAN_OPT;
William Tufc1372f2018-01-25 13:20:11 -08002599 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002600 }
Christopher Díaz Riveros89290b82018-01-17 16:10:28 -05002601 }
Jesse Grossf5796682014-10-03 15:35:33 -07002602
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002603 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002604 if (start < 0)
2605 return start;
2606
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002607 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2608 GFP_KERNEL);
2609
Thomas Graf34ae9322015-07-21 10:44:03 +02002610 if (!tun_dst)
2611 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002612
Paolo Abenid71785f2016-02-12 15:43:57 +01002613 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2614 if (err) {
2615 dst_release((struct dst_entry *)tun_dst);
2616 return err;
2617 }
2618
Thomas Graf34ae9322015-07-21 10:44:03 +02002619 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2620 sizeof(*ovs_tun), log);
2621 if (IS_ERR(a)) {
2622 dst_release((struct dst_entry *)tun_dst);
2623 return PTR_ERR(a);
2624 }
2625
2626 ovs_tun = nla_data(a);
2627 ovs_tun->tun_dst = tun_dst;
2628
2629 tun_info = &tun_dst->u.tun_info;
2630 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002631 if (key.tun_proto == AF_INET6)
2632 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
wenxu18b6f712019-03-28 12:43:23 +08002633 else if (key.tun_proto == AF_INET && key.tun_key.u.ipv4.dst == 0)
2634 tun_info->mode |= IP_TUNNEL_INFO_BRIDGE;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002635 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002636
Pravin B Shelar4c222792015-08-30 18:09:38 -07002637 /* We need to store the options in the action itself since
2638 * everything else will go away after flow setup. We can append
2639 * it to tun_info and then point there.
2640 */
2641 ip_tunnel_info_opts_set(tun_info,
2642 TUN_METADATA_OPTS(&key, key.tun_opts_len),
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002643 key.tun_opts_len, dst_opt_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002644 add_nested_action_end(*sfa, start);
2645
2646 return err;
2647}
2648
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002649static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2650 bool is_push_nsh, bool log)
2651{
2652 struct sw_flow_match match;
2653 struct sw_flow_key key;
2654 int ret = 0;
2655
2656 ovs_match_init(&match, &key, true, NULL);
2657 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2658 is_push_nsh, log);
2659 return !ret;
2660}
2661
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002662/* Return false if there are any non-masked bits set.
2663 * Mask follows data immediately, before any netlink padding.
2664 */
2665static bool validate_masked(u8 *data, int len)
2666{
2667 u8 *mask = data + len;
2668
2669 while (len--)
2670 if (*data++ & ~*mask++)
2671 return false;
2672
2673 return true;
2674}
2675
Pravin B Shelare6445712013-10-03 18:16:47 -07002676static int validate_set(const struct nlattr *a,
2677 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002678 struct sw_flow_actions **sfa, bool *skip_copy,
2679 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002680{
2681 const struct nlattr *ovs_key = nla_data(a);
2682 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002683 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002684
2685 /* There can be only one key in a action */
2686 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2687 return -EINVAL;
2688
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002689 key_len = nla_len(ovs_key);
2690 if (masked)
2691 key_len /= 2;
2692
Pravin B Shelare6445712013-10-03 18:16:47 -07002693 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002694 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002695 return -EINVAL;
2696
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002697 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2698 return -EINVAL;
2699
Pravin B Shelare6445712013-10-03 18:16:47 -07002700 switch (key_type) {
2701 const struct ovs_key_ipv4 *ipv4_key;
2702 const struct ovs_key_ipv6 *ipv6_key;
2703 int err;
2704
2705 case OVS_KEY_ATTR_PRIORITY:
2706 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002707 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002708 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002709 break;
2710
Jiri Benc0a6410f2016-11-10 16:28:22 +01002711 case OVS_KEY_ATTR_ETHERNET:
2712 if (mac_proto != MAC_PROTO_ETHERNET)
2713 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002714 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002715
Pravin B Shelare6445712013-10-03 18:16:47 -07002716 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002717 if (masked)
2718 return -EINVAL; /* Masked tunnel set not supported. */
2719
2720 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002721 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002722 if (err)
2723 return err;
2724 break;
2725
2726 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002727 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002728 return -EINVAL;
2729
Pravin B Shelare6445712013-10-03 18:16:47 -07002730 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002731
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002732 if (masked) {
2733 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002734
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002735 /* Non-writeable fields. */
2736 if (mask->ipv4_proto || mask->ipv4_frag)
2737 return -EINVAL;
2738 } else {
2739 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2740 return -EINVAL;
2741
2742 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2743 return -EINVAL;
2744 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002745 break;
2746
2747 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002748 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002749 return -EINVAL;
2750
Pravin B Shelare6445712013-10-03 18:16:47 -07002751 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002752
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002753 if (masked) {
2754 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002755
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002756 /* Non-writeable fields. */
2757 if (mask->ipv6_proto || mask->ipv6_frag)
2758 return -EINVAL;
2759
2760 /* Invalid bits in the flow label mask? */
2761 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2762 return -EINVAL;
2763 } else {
2764 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2765 return -EINVAL;
2766
2767 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2768 return -EINVAL;
2769 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002770 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2771 return -EINVAL;
2772
2773 break;
2774
2775 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002776 if ((eth_type != htons(ETH_P_IP) &&
2777 eth_type != htons(ETH_P_IPV6)) ||
2778 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002779 return -EINVAL;
2780
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002781 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002782
2783 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002784 if ((eth_type != htons(ETH_P_IP) &&
2785 eth_type != htons(ETH_P_IPV6)) ||
2786 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002787 return -EINVAL;
2788
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002789 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002790
2791 case OVS_KEY_ATTR_MPLS:
2792 if (!eth_p_mpls(eth_type))
2793 return -EINVAL;
2794 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002795
2796 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002797 if ((eth_type != htons(ETH_P_IP) &&
2798 eth_type != htons(ETH_P_IPV6)) ||
2799 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002800 return -EINVAL;
2801
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002802 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002803
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002804 case OVS_KEY_ATTR_NSH:
2805 if (eth_type != htons(ETH_P_NSH))
2806 return -EINVAL;
2807 if (!validate_nsh(nla_data(a), masked, false, log))
2808 return -EINVAL;
2809 break;
2810
Pravin B Shelare6445712013-10-03 18:16:47 -07002811 default:
2812 return -EINVAL;
2813 }
2814
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002815 /* Convert non-masked non-tunnel set actions to masked set actions. */
2816 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2817 int start, len = key_len * 2;
2818 struct nlattr *at;
2819
2820 *skip_copy = true;
2821
2822 start = add_nested_action_start(sfa,
2823 OVS_ACTION_ATTR_SET_TO_MASKED,
2824 log);
2825 if (start < 0)
2826 return start;
2827
2828 at = __add_action(sfa, key_type, NULL, len, log);
2829 if (IS_ERR(at))
2830 return PTR_ERR(at);
2831
2832 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2833 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2834 /* Clear non-writeable bits from otherwise writeable fields. */
2835 if (key_type == OVS_KEY_ATTR_IPV6) {
2836 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2837
2838 mask->ipv6_label &= htonl(0x000FFFFF);
2839 }
2840 add_nested_action_end(*sfa, start);
2841 }
2842
Pravin B Shelare6445712013-10-03 18:16:47 -07002843 return 0;
2844}
2845
2846static int validate_userspace(const struct nlattr *attr)
2847{
2848 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2849 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2850 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002851 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002852 };
2853 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2854 int error;
2855
Johannes Bergfceb6432017-04-12 14:34:07 +02002856 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2857 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002858 if (error)
2859 return error;
2860
2861 if (!a[OVS_USERSPACE_ATTR_PID] ||
2862 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2863 return -EINVAL;
2864
2865 return 0;
2866}
2867
Numan Siddique4d5ec892019-03-26 06:13:46 +05302868static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
2869 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
2870 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
2871 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
2872};
2873
2874static int validate_and_copy_check_pkt_len(struct net *net,
2875 const struct nlattr *attr,
2876 const struct sw_flow_key *key,
2877 struct sw_flow_actions **sfa,
2878 __be16 eth_type, __be16 vlan_tci,
2879 bool log, bool last)
2880{
2881 const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
2882 struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
2883 struct check_pkt_len_arg arg;
2884 int nested_acts_start;
2885 int start, err;
2886
2887 err = nla_parse_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX, nla_data(attr),
2888 nla_len(attr), cpl_policy, NULL);
2889 if (err)
2890 return err;
2891
2892 if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
2893 !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
2894 return -EINVAL;
2895
2896 acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
2897 acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
2898
2899 /* Both the nested action should be present. */
2900 if (!acts_if_greater || !acts_if_lesser_eq)
2901 return -EINVAL;
2902
2903 /* validation done, copy the nested actions. */
2904 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
2905 log);
2906 if (start < 0)
2907 return start;
2908
2909 arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
2910 arg.exec_for_lesser_equal =
2911 last || !actions_may_change_flow(acts_if_lesser_eq);
2912 arg.exec_for_greater =
2913 last || !actions_may_change_flow(acts_if_greater);
2914
2915 err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
2916 sizeof(arg), log);
2917 if (err)
2918 return err;
2919
2920 nested_acts_start = add_nested_action_start(sfa,
2921 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
2922 if (nested_acts_start < 0)
2923 return nested_acts_start;
2924
2925 err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
2926 eth_type, vlan_tci, log);
2927
2928 if (err)
2929 return err;
2930
2931 add_nested_action_end(*sfa, nested_acts_start);
2932
2933 nested_acts_start = add_nested_action_start(sfa,
2934 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
2935 if (nested_acts_start < 0)
2936 return nested_acts_start;
2937
2938 err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
2939 eth_type, vlan_tci, log);
2940
2941 if (err)
2942 return err;
2943
2944 add_nested_action_end(*sfa, nested_acts_start);
2945 add_nested_action_end(*sfa, start);
2946 return 0;
2947}
2948
Pravin B Shelare6445712013-10-03 18:16:47 -07002949static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002950 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002951{
2952 int totlen = NLA_ALIGN(from->nla_len);
2953 struct nlattr *to;
2954
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002955 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002956 if (IS_ERR(to))
2957 return PTR_ERR(to);
2958
2959 memcpy(to, from, totlen);
2960 return 0;
2961}
2962
Joe Stringer7f8a4362015-08-26 11:31:48 -07002963static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002964 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002965 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002966 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002967{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002968 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002969 const struct nlattr *a;
2970 int rem, err;
2971
Pravin B Shelare6445712013-10-03 18:16:47 -07002972 nla_for_each_nested(a, attr, rem) {
2973 /* Expected argument lengths, (u32)-1 for variable length. */
2974 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2975 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002976 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002977 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002978 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2979 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002980 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2981 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2982 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002983 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002984 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002985 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2986 [OVS_ACTION_ATTR_CT] = (u32)-1,
Eric Garverb8226962017-10-10 16:54:44 -04002987 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
William Tuf2a4d082016-06-10 11:49:33 -07002988 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002989 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2990 [OVS_ACTION_ATTR_POP_ETH] = 0,
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002991 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
2992 [OVS_ACTION_ATTR_POP_NSH] = 0,
Andy Zhoucd8a6c32017-11-10 12:09:43 -08002993 [OVS_ACTION_ATTR_METER] = sizeof(u32),
Yifeng Sunb2335042018-07-02 08:18:03 -07002994 [OVS_ACTION_ATTR_CLONE] = (u32)-1,
Numan Siddique4d5ec892019-03-26 06:13:46 +05302995 [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002996 };
2997 const struct ovs_action_push_vlan *vlan;
2998 int type = nla_type(a);
2999 bool skip_copy;
3000
3001 if (type > OVS_ACTION_ATTR_MAX ||
3002 (action_lens[type] != nla_len(a) &&
3003 action_lens[type] != (u32)-1))
3004 return -EINVAL;
3005
3006 skip_copy = false;
3007 switch (type) {
3008 case OVS_ACTION_ATTR_UNSPEC:
3009 return -EINVAL;
3010
3011 case OVS_ACTION_ATTR_USERSPACE:
3012 err = validate_userspace(a);
3013 if (err)
3014 return err;
3015 break;
3016
3017 case OVS_ACTION_ATTR_OUTPUT:
3018 if (nla_get_u32(a) >= DP_MAX_PORTS)
3019 return -EINVAL;
3020 break;
3021
William Tuf2a4d082016-06-10 11:49:33 -07003022 case OVS_ACTION_ATTR_TRUNC: {
3023 const struct ovs_action_trunc *trunc = nla_data(a);
3024
3025 if (trunc->max_len < ETH_HLEN)
3026 return -EINVAL;
3027 break;
3028 }
3029
Andy Zhou971427f32014-09-15 19:37:25 -07003030 case OVS_ACTION_ATTR_HASH: {
3031 const struct ovs_action_hash *act_hash = nla_data(a);
3032
3033 switch (act_hash->hash_alg) {
3034 case OVS_HASH_ALG_L4:
3035 break;
3036 default:
3037 return -EINVAL;
3038 }
3039
3040 break;
3041 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003042
3043 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003044 if (mac_proto != MAC_PROTO_ETHERNET)
3045 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003046 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07003047 break;
3048
3049 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003050 if (mac_proto != MAC_PROTO_ETHERNET)
3051 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07003052 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04003053 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07003054 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003055 if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
Pravin B Shelare6445712013-10-03 18:16:47 -07003056 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003057 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07003058 break;
3059
Andy Zhou971427f32014-09-15 19:37:25 -07003060 case OVS_ACTION_ATTR_RECIRC:
3061 break;
3062
Simon Horman25cd9ba2014-10-06 05:05:13 -07003063 case OVS_ACTION_ATTR_PUSH_MPLS: {
3064 const struct ovs_action_push_mpls *mpls = nla_data(a);
3065
Simon Horman25cd9ba2014-10-06 05:05:13 -07003066 if (!eth_p_mpls(mpls->mpls_ethertype))
3067 return -EINVAL;
3068 /* Prohibit push MPLS other than to a white list
3069 * for packets that have a known tag order.
3070 */
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003071 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003072 (eth_type != htons(ETH_P_IP) &&
3073 eth_type != htons(ETH_P_IPV6) &&
3074 eth_type != htons(ETH_P_ARP) &&
3075 eth_type != htons(ETH_P_RARP) &&
3076 !eth_p_mpls(eth_type)))
3077 return -EINVAL;
3078 eth_type = mpls->mpls_ethertype;
3079 break;
3080 }
3081
3082 case OVS_ACTION_ATTR_POP_MPLS:
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003083 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003084 !eth_p_mpls(eth_type))
3085 return -EINVAL;
3086
3087 /* Disallow subsequent L2.5+ set and mpls_pop actions
3088 * as there is no check here to ensure that the new
3089 * eth_type is valid and thus set actions could
3090 * write off the end of the packet or otherwise
3091 * corrupt it.
3092 *
3093 * Support for these actions is planned using packet
3094 * recirculation.
3095 */
3096 eth_type = htons(0);
3097 break;
3098
Pravin B Shelare6445712013-10-03 18:16:47 -07003099 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07003100 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003101 &skip_copy, mac_proto, eth_type,
3102 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003103 if (err)
3104 return err;
3105 break;
3106
3107 case OVS_ACTION_ATTR_SET_MASKED:
3108 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003109 &skip_copy, mac_proto, eth_type,
3110 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003111 if (err)
3112 return err;
3113 break;
3114
andy zhou798c1662017-03-20 16:32:29 -07003115 case OVS_ACTION_ATTR_SAMPLE: {
3116 bool last = nla_is_last(a, rem);
3117
3118 err = validate_and_copy_sample(net, a, key, sfa,
3119 eth_type, vlan_tci,
3120 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07003121 if (err)
3122 return err;
3123 skip_copy = true;
3124 break;
andy zhou798c1662017-03-20 16:32:29 -07003125 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003126
Joe Stringer7f8a4362015-08-26 11:31:48 -07003127 case OVS_ACTION_ATTR_CT:
3128 err = ovs_ct_copy_action(net, a, key, sfa, log);
3129 if (err)
3130 return err;
3131 skip_copy = true;
3132 break;
3133
Eric Garverb8226962017-10-10 16:54:44 -04003134 case OVS_ACTION_ATTR_CT_CLEAR:
3135 break;
3136
Jiri Benc91820da2016-11-10 16:28:23 +01003137 case OVS_ACTION_ATTR_PUSH_ETH:
3138 /* Disallow pushing an Ethernet header if one
3139 * is already present */
3140 if (mac_proto != MAC_PROTO_NONE)
3141 return -EINVAL;
Jaime Caamaño Ruiz46ebe282018-10-31 18:52:03 +01003142 mac_proto = MAC_PROTO_ETHERNET;
Jiri Benc91820da2016-11-10 16:28:23 +01003143 break;
3144
3145 case OVS_ACTION_ATTR_POP_ETH:
3146 if (mac_proto != MAC_PROTO_ETHERNET)
3147 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003148 if (vlan_tci & htons(VLAN_CFI_MASK))
Jiri Benc91820da2016-11-10 16:28:23 +01003149 return -EINVAL;
Jaime Caamaño Ruiz46ebe282018-10-31 18:52:03 +01003150 mac_proto = MAC_PROTO_NONE;
Jiri Benc91820da2016-11-10 16:28:23 +01003151 break;
3152
Yi Yangb2d0f5d2017-11-07 21:07:02 +08003153 case OVS_ACTION_ATTR_PUSH_NSH:
3154 if (mac_proto != MAC_PROTO_ETHERNET) {
3155 u8 next_proto;
3156
3157 next_proto = tun_p_from_eth_p(eth_type);
3158 if (!next_proto)
3159 return -EINVAL;
3160 }
3161 mac_proto = MAC_PROTO_NONE;
3162 if (!validate_nsh(nla_data(a), false, true, true))
3163 return -EINVAL;
3164 break;
3165
3166 case OVS_ACTION_ATTR_POP_NSH: {
3167 __be16 inner_proto;
3168
3169 if (eth_type != htons(ETH_P_NSH))
3170 return -EINVAL;
3171 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
3172 if (!inner_proto)
3173 return -EINVAL;
3174 if (key->nsh.base.np == TUN_P_ETHERNET)
3175 mac_proto = MAC_PROTO_ETHERNET;
3176 else
3177 mac_proto = MAC_PROTO_NONE;
3178 break;
3179 }
3180
Andy Zhoucd8a6c32017-11-10 12:09:43 -08003181 case OVS_ACTION_ATTR_METER:
3182 /* Non-existent meters are simply ignored. */
3183 break;
3184
Yifeng Sunb2335042018-07-02 08:18:03 -07003185 case OVS_ACTION_ATTR_CLONE: {
3186 bool last = nla_is_last(a, rem);
3187
3188 err = validate_and_copy_clone(net, a, key, sfa,
3189 eth_type, vlan_tci,
3190 log, last);
3191 if (err)
3192 return err;
3193 skip_copy = true;
3194 break;
3195 }
3196
Numan Siddique4d5ec892019-03-26 06:13:46 +05303197 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
3198 bool last = nla_is_last(a, rem);
3199
3200 err = validate_and_copy_check_pkt_len(net, a, key, sfa,
3201 eth_type,
3202 vlan_tci, log,
3203 last);
3204 if (err)
3205 return err;
3206 skip_copy = true;
3207 break;
3208 }
3209
Pravin B Shelare6445712013-10-03 18:16:47 -07003210 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003211 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07003212 return -EINVAL;
3213 }
3214 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003215 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003216 if (err)
3217 return err;
3218 }
3219 }
3220
3221 if (rem > 0)
3222 return -EINVAL;
3223
3224 return 0;
3225}
3226
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003227/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07003228int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07003229 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003230 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07003231{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003232 int err;
3233
zhangliping67c8d222017-11-25 22:02:12 +08003234 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003235 if (IS_ERR(*sfa))
3236 return PTR_ERR(*sfa);
3237
Joe Stringer8e2fed12015-08-26 11:31:44 -07003238 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07003239 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04003240 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003241 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02003242 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003243
3244 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003245}
3246
andy zhou798c1662017-03-20 16:32:29 -07003247static int sample_action_to_attr(const struct nlattr *attr,
3248 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07003249{
andy zhou798c1662017-03-20 16:32:29 -07003250 struct nlattr *start, *ac_start = NULL, *sample_arg;
3251 int err = 0, rem = nla_len(attr);
3252 const struct sample_arg *arg;
3253 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07003254
3255 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
3256 if (!start)
3257 return -EMSGSIZE;
3258
andy zhou798c1662017-03-20 16:32:29 -07003259 sample_arg = nla_data(attr);
3260 arg = nla_data(sample_arg);
3261 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07003262
andy zhou798c1662017-03-20 16:32:29 -07003263 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3264 err = -EMSGSIZE;
3265 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07003266 }
3267
andy zhou798c1662017-03-20 16:32:29 -07003268 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
3269 if (!ac_start) {
3270 err = -EMSGSIZE;
3271 goto out;
3272 }
3273
3274 err = ovs_nla_put_actions(actions, rem, skb);
3275
3276out:
3277 if (err) {
3278 nla_nest_cancel(skb, ac_start);
3279 nla_nest_cancel(skb, start);
3280 } else {
3281 nla_nest_end(skb, ac_start);
3282 nla_nest_end(skb, start);
3283 }
3284
Pravin B Shelare6445712013-10-03 18:16:47 -07003285 return err;
3286}
3287
Yifeng Sunb2335042018-07-02 08:18:03 -07003288static int clone_action_to_attr(const struct nlattr *attr,
3289 struct sk_buff *skb)
3290{
3291 struct nlattr *start;
3292 int err = 0, rem = nla_len(attr);
3293
3294 start = nla_nest_start(skb, OVS_ACTION_ATTR_CLONE);
3295 if (!start)
3296 return -EMSGSIZE;
3297
3298 err = ovs_nla_put_actions(nla_data(attr), rem, skb);
3299
3300 if (err)
3301 nla_nest_cancel(skb, start);
3302 else
3303 nla_nest_end(skb, start);
3304
3305 return err;
3306}
3307
Numan Siddique4d5ec892019-03-26 06:13:46 +05303308static int check_pkt_len_action_to_attr(const struct nlattr *attr,
3309 struct sk_buff *skb)
3310{
3311 struct nlattr *start, *ac_start = NULL;
3312 const struct check_pkt_len_arg *arg;
3313 const struct nlattr *a, *cpl_arg;
3314 int err = 0, rem = nla_len(attr);
3315
3316 start = nla_nest_start(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
3317 if (!start)
3318 return -EMSGSIZE;
3319
3320 /* The first nested attribute in 'attr' is always
3321 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3322 */
3323 cpl_arg = nla_data(attr);
3324 arg = nla_data(cpl_arg);
3325
3326 if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
3327 err = -EMSGSIZE;
3328 goto out;
3329 }
3330
3331 /* Second nested attribute in 'attr' is always
3332 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3333 */
3334 a = nla_next(cpl_arg, &rem);
3335 ac_start = nla_nest_start(skb,
3336 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
3337 if (!ac_start) {
3338 err = -EMSGSIZE;
3339 goto out;
3340 }
3341
3342 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3343 if (err) {
3344 nla_nest_cancel(skb, ac_start);
3345 goto out;
3346 } else {
3347 nla_nest_end(skb, ac_start);
3348 }
3349
3350 /* Third nested attribute in 'attr' is always
3351 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3352 */
3353 a = nla_next(a, &rem);
3354 ac_start = nla_nest_start(skb,
3355 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
3356 if (!ac_start) {
3357 err = -EMSGSIZE;
3358 goto out;
3359 }
3360
3361 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3362 if (err) {
3363 nla_nest_cancel(skb, ac_start);
3364 goto out;
3365 } else {
3366 nla_nest_end(skb, ac_start);
3367 }
3368
3369 nla_nest_end(skb, start);
3370 return 0;
3371
3372out:
3373 nla_nest_cancel(skb, start);
3374 return err;
3375}
3376
Pravin B Shelare6445712013-10-03 18:16:47 -07003377static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3378{
3379 const struct nlattr *ovs_key = nla_data(a);
3380 int key_type = nla_type(ovs_key);
3381 struct nlattr *start;
3382 int err;
3383
3384 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07003385 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02003386 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3387 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003388
Pravin B Shelare6445712013-10-03 18:16:47 -07003389 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3390 if (!start)
3391 return -EMSGSIZE;
3392
Simon Hormane905eab2015-12-18 19:43:15 +09003393 err = ip_tun_to_nlattr(skb, &tun_info->key,
3394 ip_tunnel_info_opts(tun_info),
3395 tun_info->options_len,
wenxu18b6f712019-03-28 12:43:23 +08003396 ip_tunnel_info_af(tun_info), tun_info->mode);
Pravin B Shelare6445712013-10-03 18:16:47 -07003397 if (err)
3398 return err;
3399 nla_nest_end(skb, start);
3400 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003401 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003402 default:
3403 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3404 return -EMSGSIZE;
3405 break;
3406 }
3407
3408 return 0;
3409}
3410
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003411static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3412 struct sk_buff *skb)
3413{
3414 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08003415 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003416 size_t key_len = nla_len(ovs_key) / 2;
3417
3418 /* Revert the conversion we did from a non-masked set action to
3419 * masked set action.
3420 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08003421 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3422 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003423 return -EMSGSIZE;
3424
Joe Stringerf4f8e732015-03-02 18:49:56 -08003425 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3426 return -EMSGSIZE;
3427
3428 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003429 return 0;
3430}
3431
Pravin B Shelare6445712013-10-03 18:16:47 -07003432int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3433{
3434 const struct nlattr *a;
3435 int rem, err;
3436
3437 nla_for_each_attr(a, attr, len, rem) {
3438 int type = nla_type(a);
3439
3440 switch (type) {
3441 case OVS_ACTION_ATTR_SET:
3442 err = set_action_to_attr(a, skb);
3443 if (err)
3444 return err;
3445 break;
3446
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003447 case OVS_ACTION_ATTR_SET_TO_MASKED:
3448 err = masked_set_action_to_set_action_attr(a, skb);
3449 if (err)
3450 return err;
3451 break;
3452
Pravin B Shelare6445712013-10-03 18:16:47 -07003453 case OVS_ACTION_ATTR_SAMPLE:
3454 err = sample_action_to_attr(a, skb);
3455 if (err)
3456 return err;
3457 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07003458
3459 case OVS_ACTION_ATTR_CT:
3460 err = ovs_ct_action_to_attr(nla_data(a), skb);
3461 if (err)
3462 return err;
3463 break;
3464
Yifeng Sunb2335042018-07-02 08:18:03 -07003465 case OVS_ACTION_ATTR_CLONE:
3466 err = clone_action_to_attr(a, skb);
3467 if (err)
3468 return err;
3469 break;
3470
Numan Siddique4d5ec892019-03-26 06:13:46 +05303471 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
3472 err = check_pkt_len_action_to_attr(a, skb);
3473 if (err)
3474 return err;
3475 break;
3476
Pravin B Shelare6445712013-10-03 18:16:47 -07003477 default:
3478 if (nla_put(skb, type, nla_len(a), nla_data(a)))
3479 return -EMSGSIZE;
3480 break;
3481 }
3482 }
3483
3484 return 0;
3485}