blob: 7da4230627f51489707fc199c4333702c344282e [file] [log] [blame]
Thomas Gleixnerc9422992019-05-29 07:12:43 -07001// SPDX-License-Identifier: GPL-2.0-only
Pravin B Shelare6445712013-10-03 18:16:47 -07002/*
andy zhou798c1662017-03-20 16:32:29 -07003 * Copyright (c) 2007-2017 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07004 */
5
Joe Perches2235ad12014-02-03 17:18:21 -08006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Pravin B Shelare6445712013-10-03 18:16:47 -07008#include "flow.h"
9#include "datapath.h"
10#include <linux/uaccess.h>
11#include <linux/netdevice.h>
12#include <linux/etherdevice.h>
13#include <linux/if_ether.h>
14#include <linux/if_vlan.h>
15#include <net/llc_pdu.h>
16#include <linux/kernel.h>
17#include <linux/jhash.h>
18#include <linux/jiffies.h>
19#include <linux/llc.h>
20#include <linux/module.h>
21#include <linux/in.h>
22#include <linux/rcupdate.h>
23#include <linux/if_arp.h>
24#include <linux/ip.h>
25#include <linux/ipv6.h>
26#include <linux/sctp.h>
27#include <linux/tcp.h>
28#include <linux/udp.h>
29#include <linux/icmp.h>
30#include <linux/icmpv6.h>
31#include <linux/rculist.h>
Jesse Grossf5796682014-10-03 15:35:33 -070032#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070033#include <net/ip.h>
34#include <net/ipv6.h>
35#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070036#include <net/mpls.h>
Thomas Graf614732e2015-07-21 10:44:06 +020037#include <net/vxlan.h>
Yi Yangb2d0f5d2017-11-07 21:07:02 +080038#include <net/tun_proto.h>
William Tufc1372f2018-01-25 13:20:11 -080039#include <net/erspan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070040
41#include "flow_netlink.h"
42
Thomas Graf81bfe3c32015-01-15 03:53:58 +010043struct ovs_len_tbl {
44 int len;
45 const struct ovs_len_tbl *next;
46};
47
48#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070049#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c32015-01-15 03:53:58 +010050
andy zhou798c1662017-03-20 16:32:29 -070051static bool actions_may_change_flow(const struct nlattr *actions)
52{
53 struct nlattr *nla;
54 int rem;
55
56 nla_for_each_nested(nla, actions, rem) {
57 u16 action = nla_type(nla);
58
59 switch (action) {
60 case OVS_ACTION_ATTR_OUTPUT:
61 case OVS_ACTION_ATTR_RECIRC:
62 case OVS_ACTION_ATTR_TRUNC:
63 case OVS_ACTION_ATTR_USERSPACE:
64 break;
65
66 case OVS_ACTION_ATTR_CT:
Eric Garverb8226962017-10-10 16:54:44 -040067 case OVS_ACTION_ATTR_CT_CLEAR:
andy zhou798c1662017-03-20 16:32:29 -070068 case OVS_ACTION_ATTR_HASH:
69 case OVS_ACTION_ATTR_POP_ETH:
70 case OVS_ACTION_ATTR_POP_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080071 case OVS_ACTION_ATTR_POP_NSH:
andy zhou798c1662017-03-20 16:32:29 -070072 case OVS_ACTION_ATTR_POP_VLAN:
73 case OVS_ACTION_ATTR_PUSH_ETH:
74 case OVS_ACTION_ATTR_PUSH_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080075 case OVS_ACTION_ATTR_PUSH_NSH:
andy zhou798c1662017-03-20 16:32:29 -070076 case OVS_ACTION_ATTR_PUSH_VLAN:
77 case OVS_ACTION_ATTR_SAMPLE:
78 case OVS_ACTION_ATTR_SET:
79 case OVS_ACTION_ATTR_SET_MASKED:
Andy Zhoucd8a6c32017-11-10 12:09:43 -080080 case OVS_ACTION_ATTR_METER:
Numan Siddique4d5ec892019-03-26 06:13:46 +053081 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
Martin Varghesef66b53f2019-12-21 08:50:46 +053082 case OVS_ACTION_ATTR_ADD_MPLS:
andy zhou798c1662017-03-20 16:32:29 -070083 default:
84 return true;
85 }
86 }
87 return false;
88}
89
Pravin B Shelara85311b2014-10-19 12:03:40 -070090static void update_range(struct sw_flow_match *match,
91 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070092{
Pravin B Shelara85311b2014-10-19 12:03:40 -070093 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070094 size_t start = rounddown(offset, sizeof(long));
95 size_t end = roundup(offset + size, sizeof(long));
96
97 if (!is_mask)
98 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -070099 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700100 range = &match->mask->range;
101
Pravin B Shelare6445712013-10-03 18:16:47 -0700102 if (range->start == range->end) {
103 range->start = start;
104 range->end = end;
105 return;
106 }
107
108 if (range->start > start)
109 range->start = start;
110
111 if (range->end < end)
112 range->end = end;
113}
114
115#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
116 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700117 update_range(match, offsetof(struct sw_flow_key, field), \
118 sizeof((match)->key->field), is_mask); \
119 if (is_mask) \
120 (match)->mask->key.field = value; \
121 else \
Pravin B Shelare6445712013-10-03 18:16:47 -0700122 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700123 } while (0)
124
Jesse Grossf5796682014-10-03 15:35:33 -0700125#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
126 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700127 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700128 if (is_mask) \
129 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700130 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700131 else \
132 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700133 } while (0)
134
Jesse Grossf5796682014-10-03 15:35:33 -0700135#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
136 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
137 value_p, len, is_mask)
138
Pravin B Shelara85311b2014-10-19 12:03:40 -0700139#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
140 do { \
141 update_range(match, offsetof(struct sw_flow_key, field), \
142 sizeof((match)->key->field), is_mask); \
143 if (is_mask) \
144 memset((u8 *)&(match)->mask->key.field, value, \
145 sizeof((match)->mask->key.field)); \
146 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700147 memset((u8 *)&(match)->key->field, value, \
148 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700149 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700150
151static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800152 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700153{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100154 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700155 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
156
157 /* The following mask attributes allowed only if they
158 * pass the validation tests. */
159 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800160 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700161 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800162 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700163 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700164 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700165 | (1 << OVS_KEY_ATTR_UDP)
166 | (1 << OVS_KEY_ATTR_SCTP)
167 | (1 << OVS_KEY_ATTR_ICMP)
168 | (1 << OVS_KEY_ATTR_ICMPV6)
169 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700170 | (1 << OVS_KEY_ATTR_ND)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800171 | (1 << OVS_KEY_ATTR_MPLS)
172 | (1 << OVS_KEY_ATTR_NSH));
Pravin B Shelare6445712013-10-03 18:16:47 -0700173
174 /* Always allowed mask fields. */
175 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
176 | (1 << OVS_KEY_ATTR_IN_PORT)
177 | (1 << OVS_KEY_ATTR_ETHERTYPE));
178
179 /* Check key attributes. */
180 if (match->key->eth.type == htons(ETH_P_ARP)
181 || match->key->eth.type == htons(ETH_P_RARP)) {
182 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800183 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700184 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
185 }
186
Simon Horman25cd9ba2014-10-06 05:05:13 -0700187 if (eth_p_mpls(match->key->eth.type)) {
188 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
189 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
190 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
191 }
192
Pravin B Shelare6445712013-10-03 18:16:47 -0700193 if (match->key->eth.type == htons(ETH_P_IP)) {
194 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -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_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800197 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
198 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700199
200 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
201 if (match->key->ip.proto == IPPROTO_UDP) {
202 key_expected |= 1 << OVS_KEY_ATTR_UDP;
203 if (match->mask && (match->mask->key.ip.proto == 0xff))
204 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
205 }
206
207 if (match->key->ip.proto == IPPROTO_SCTP) {
208 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_TCP) {
214 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700215 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
216 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700217 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700218 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
219 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700220 }
221
222 if (match->key->ip.proto == IPPROTO_ICMP) {
223 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
224 if (match->mask && (match->mask->key.ip.proto == 0xff))
225 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
226 }
227 }
228 }
229
230 if (match->key->eth.type == htons(ETH_P_IPV6)) {
231 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800232 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700233 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800234 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
235 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700236
237 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
238 if (match->key->ip.proto == IPPROTO_UDP) {
239 key_expected |= 1 << OVS_KEY_ATTR_UDP;
240 if (match->mask && (match->mask->key.ip.proto == 0xff))
241 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
242 }
243
244 if (match->key->ip.proto == IPPROTO_SCTP) {
245 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
246 if (match->mask && (match->mask->key.ip.proto == 0xff))
247 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
248 }
249
250 if (match->key->ip.proto == IPPROTO_TCP) {
251 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700252 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
253 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700254 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700255 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
256 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700257 }
258
259 if (match->key->ip.proto == IPPROTO_ICMPV6) {
260 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
261 if (match->mask && (match->mask->key.ip.proto == 0xff))
262 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
263
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700264 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700265 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700266 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700267 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800268 /* Original direction conntrack tuple
269 * uses the same space as the ND fields
270 * in the key, so both are not allowed
271 * at the same time.
272 */
273 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800274 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700275 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
276 }
277 }
278 }
279 }
280
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800281 if (match->key->eth.type == htons(ETH_P_NSH)) {
282 key_expected |= 1 << OVS_KEY_ATTR_NSH;
283 if (match->mask &&
284 match->mask->key.eth.type == htons(0xffff)) {
285 mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
286 }
287 }
288
Pravin B Shelare6445712013-10-03 18:16:47 -0700289 if ((key_attrs & key_expected) != key_expected) {
290 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800291 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
292 (unsigned long long)key_attrs,
293 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700294 return false;
295 }
296
297 if ((mask_attrs & mask_allowed) != mask_attrs) {
298 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800299 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
300 (unsigned long long)mask_attrs,
301 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700302 return false;
303 }
304
305 return true;
306}
307
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800308size_t ovs_tun_key_attr_size(void)
309{
310 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
311 * updating this function.
312 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200313 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200314 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
315 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800316 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
317 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
318 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
319 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
320 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
321 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
William Tufc1372f2018-01-25 13:20:11 -0800322 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
323 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
Thomas Graf1dd144c2015-01-15 03:53:59 +0100324 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
325 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800326 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
William Tu95a33202018-01-12 12:29:22 -0800327 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800328}
329
Wei Yongjun06c23512017-11-14 06:27:03 +0000330static size_t ovs_nsh_key_attr_size(void)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800331{
332 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
333 * updating this function.
334 */
335 return nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
336 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
337 * mutually exclusive, so the bigger one can cover
338 * the small one.
339 */
340 + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
341}
342
Joe Stringer41af73e2014-10-18 16:14:14 -0700343size_t ovs_key_attr_size(void)
344{
345 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
346 * updating this function.
347 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800348 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
Joe Stringer41af73e2014-10-18 16:14:14 -0700349
350 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
351 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800352 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700353 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
354 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
355 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
356 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700357 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700358 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700359 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700360 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800361 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800362 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
363 + ovs_nsh_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700364 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
365 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
366 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
367 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
368 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
369 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
371 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
372}
373
Jesse Gross982b5272015-09-11 18:38:28 -0700374static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
375 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
376};
377
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100378static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
379 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
380 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
381 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
382 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
383 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
384 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
385 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
386 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
387 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
388 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700389 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
390 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
391 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200392 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
393 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
William Tufc1372f2018-01-25 13:20:11 -0800394 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_VARIABLE },
wenxu18b6f712019-03-28 12:43:23 +0800395 [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE] = { .len = 0 },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100396};
397
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800398static const struct ovs_len_tbl
399ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
400 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
401 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
402 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
403};
404
Pravin B Shelare6445712013-10-03 18:16:47 -0700405/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100406static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
407 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
408 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
409 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
410 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
411 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
412 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
413 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
414 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
415 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
416 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
417 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
418 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
419 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
420 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
421 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
422 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
423 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
424 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
425 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
426 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
427 .next = ovs_tunnel_key_lens, },
Martin Varghesefbdcdd72019-11-04 07:27:44 +0530428 [OVS_KEY_ATTR_MPLS] = { .len = OVS_ATTR_VARIABLE },
Joe Stringerfbccce52015-10-06 11:00:00 -0700429 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700430 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700431 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700432 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800433 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
434 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
435 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
436 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800437 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
438 .next = ovs_nsh_key_attr_lens, },
Pravin B Shelare6445712013-10-03 18:16:47 -0700439};
440
Jesse Gross982b5272015-09-11 18:38:28 -0700441static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
442{
443 return expected_len == attr_len ||
444 expected_len == OVS_ATTR_NESTED ||
445 expected_len == OVS_ATTR_VARIABLE;
446}
447
Pravin B Shelare6445712013-10-03 18:16:47 -0700448static bool is_all_zero(const u8 *fp, size_t size)
449{
450 int i;
451
452 if (!fp)
453 return false;
454
455 for (i = 0; i < size; i++)
456 if (fp[i])
457 return false;
458
459 return true;
460}
461
462static int __parse_flow_nlattrs(const struct nlattr *attr,
463 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800464 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700465{
466 const struct nlattr *nla;
467 u64 attrs;
468 int rem;
469
470 attrs = *attrsp;
471 nla_for_each_nested(nla, attr, rem) {
472 u16 type = nla_type(nla);
473 int expected_len;
474
475 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800476 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700477 type, OVS_KEY_ATTR_MAX);
478 return -EINVAL;
479 }
480
481 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800482 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700483 return -EINVAL;
484 }
485
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100486 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700487 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800488 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
489 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700490 return -EINVAL;
491 }
492
Ross Lagerwall04a4af32019-01-14 09:16:56 +0000493 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700494 attrs |= 1 << type;
495 a[type] = nla;
496 }
497 }
498 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800499 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700500 return -EINVAL;
501 }
502
503 *attrsp = attrs;
504 return 0;
505}
506
507static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800508 const struct nlattr *a[], u64 *attrsp,
509 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700510{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800511 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700512}
513
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800514int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
515 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700516{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800517 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
518}
519
520static int genev_tun_opt_from_nlattr(const struct nlattr *a,
521 struct sw_flow_match *match, bool is_mask,
522 bool log)
523{
524 unsigned long opt_key_offset;
525
526 if (nla_len(a) > sizeof(match->key->tun_opts)) {
527 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
528 nla_len(a), sizeof(match->key->tun_opts));
529 return -EINVAL;
530 }
531
532 if (nla_len(a) % 4 != 0) {
533 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
534 nla_len(a));
535 return -EINVAL;
536 }
537
538 /* We need to record the length of the options passed
539 * down, otherwise packets with the same format but
540 * additional options will be silently matched.
541 */
542 if (!is_mask) {
543 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
544 false);
545 } else {
546 /* This is somewhat unusual because it looks at
547 * both the key and mask while parsing the
548 * attributes (and by extension assumes the key
549 * is parsed first). Normally, we would verify
550 * that each is the correct length and that the
551 * attributes line up in the validate function.
552 * However, that is difficult because this is
553 * variable length and we won't have the
554 * information later.
555 */
556 if (match->key->tun_opts_len != nla_len(a)) {
557 OVS_NLERR(log, "Geneve option len %d != mask len %d",
558 match->key->tun_opts_len, nla_len(a));
559 return -EINVAL;
560 }
561
562 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
563 }
564
Thomas Grafd91641d2015-01-15 03:53:57 +0100565 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800566 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
567 nla_len(a), is_mask);
568 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700569}
570
Jesse Gross982b5272015-09-11 18:38:28 -0700571static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100572 struct sw_flow_match *match, bool is_mask,
573 bool log)
574{
Jesse Gross982b5272015-09-11 18:38:28 -0700575 struct nlattr *a;
576 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100577 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200578 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100579
580 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
581
Thomas Graf1dd144c2015-01-15 03:53:59 +0100582 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700583 nla_for_each_nested(a, attr, rem) {
584 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100585
Jesse Gross982b5272015-09-11 18:38:28 -0700586 if (type > OVS_VXLAN_EXT_MAX) {
587 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
588 type, OVS_VXLAN_EXT_MAX);
589 return -EINVAL;
590 }
591
592 if (!check_attr_len(nla_len(a),
593 ovs_vxlan_ext_key_lens[type].len)) {
594 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
595 type, nla_len(a),
596 ovs_vxlan_ext_key_lens[type].len);
597 return -EINVAL;
598 }
599
600 switch (type) {
601 case OVS_VXLAN_EXT_GBP:
602 opts.gbp = nla_get_u32(a);
603 break;
604 default:
605 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
606 type);
607 return -EINVAL;
608 }
609 }
610 if (rem) {
611 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
612 rem);
613 return -EINVAL;
614 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100615
616 if (!is_mask)
617 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
618 else
619 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
620
621 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
622 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
623 is_mask);
624 return 0;
625}
626
William Tufc1372f2018-01-25 13:20:11 -0800627static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
628 struct sw_flow_match *match, bool is_mask,
629 bool log)
630{
631 unsigned long opt_key_offset;
632
633 BUILD_BUG_ON(sizeof(struct erspan_metadata) >
634 sizeof(match->key->tun_opts));
635
636 if (nla_len(a) > sizeof(match->key->tun_opts)) {
637 OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
638 nla_len(a), sizeof(match->key->tun_opts));
639 return -EINVAL;
640 }
641
642 if (!is_mask)
643 SW_FLOW_KEY_PUT(match, tun_opts_len,
644 sizeof(struct erspan_metadata), false);
645 else
646 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
647
648 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
649 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
650 nla_len(a), is_mask);
651 return 0;
652}
653
Jiri Benc6b26ba32015-10-05 13:09:47 +0200654static int ip_tun_from_nlattr(const struct nlattr *attr,
655 struct sw_flow_match *match, bool is_mask,
656 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700657{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700658 bool ttl = false, ipv4 = false, ipv6 = false;
wenxu18b6f712019-03-28 12:43:23 +0800659 bool info_bridge_mode = false;
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700660 __be16 tun_flags = 0;
661 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700662 struct nlattr *a;
663 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700664
665 nla_for_each_nested(a, attr, rem) {
666 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800667 int err;
668
Pravin B Shelare6445712013-10-03 18:16:47 -0700669 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800670 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
671 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700672 return -EINVAL;
673 }
674
Jesse Gross982b5272015-09-11 18:38:28 -0700675 if (!check_attr_len(nla_len(a),
676 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800677 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100678 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700679 return -EINVAL;
680 }
681
682 switch (type) {
683 case OVS_TUNNEL_KEY_ATTR_ID:
684 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
685 nla_get_be64(a), is_mask);
686 tun_flags |= TUNNEL_KEY;
687 break;
688 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200689 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200690 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200691 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700692 break;
693 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200694 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200695 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200696 ipv4 = true;
697 break;
698 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200699 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200700 nla_get_in6_addr(a), is_mask);
701 ipv6 = true;
702 break;
703 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
704 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
705 nla_get_in6_addr(a), is_mask);
706 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700707 break;
708 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200709 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700710 nla_get_u8(a), is_mask);
711 break;
712 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200713 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700714 nla_get_u8(a), is_mask);
715 ttl = true;
716 break;
717 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
718 tun_flags |= TUNNEL_DONT_FRAGMENT;
719 break;
720 case OVS_TUNNEL_KEY_ATTR_CSUM:
721 tun_flags |= TUNNEL_CSUM;
722 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800723 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
724 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
725 nla_get_be16(a), is_mask);
726 break;
727 case OVS_TUNNEL_KEY_ATTR_TP_DST:
728 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
729 nla_get_be16(a), is_mask);
730 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700731 case OVS_TUNNEL_KEY_ATTR_OAM:
732 tun_flags |= TUNNEL_OAM;
733 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700734 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100735 if (opts_type) {
736 OVS_NLERR(log, "Multiple metadata blocks provided");
737 return -EINVAL;
738 }
739
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800740 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
741 if (err)
742 return err;
743
Thomas Graf1dd144c2015-01-15 03:53:59 +0100744 tun_flags |= TUNNEL_GENEVE_OPT;
745 opts_type = type;
746 break;
747 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
748 if (opts_type) {
749 OVS_NLERR(log, "Multiple metadata blocks provided");
750 return -EINVAL;
751 }
752
753 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
754 if (err)
755 return err;
756
757 tun_flags |= TUNNEL_VXLAN_OPT;
758 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700759 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500760 case OVS_TUNNEL_KEY_ATTR_PAD:
761 break;
William Tufc1372f2018-01-25 13:20:11 -0800762 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
763 if (opts_type) {
764 OVS_NLERR(log, "Multiple metadata blocks provided");
765 return -EINVAL;
766 }
767
768 err = erspan_tun_opt_from_nlattr(a, match, is_mask,
769 log);
770 if (err)
771 return err;
772
773 tun_flags |= TUNNEL_ERSPAN_OPT;
774 opts_type = type;
775 break;
wenxu18b6f712019-03-28 12:43:23 +0800776 case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE:
777 info_bridge_mode = true;
778 ipv4 = true;
779 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700780 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200781 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700782 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700783 return -EINVAL;
784 }
785 }
786
787 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200788 if (is_mask)
789 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
790 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200791 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
792 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700793
794 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200795 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800796 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700797 return -EINVAL;
798 }
799
Jiri Benc6b26ba32015-10-05 13:09:47 +0200800 if (ipv4 && ipv6) {
801 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
802 return -EINVAL;
803 }
804
Pravin B Shelare6445712013-10-03 18:16:47 -0700805 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200806 if (!ipv4 && !ipv6) {
807 OVS_NLERR(log, "IP tunnel dst address not specified");
808 return -EINVAL;
809 }
wenxu18b6f712019-03-28 12:43:23 +0800810 if (ipv4) {
811 if (info_bridge_mode) {
812 if (match->key->tun_key.u.ipv4.src ||
813 match->key->tun_key.u.ipv4.dst ||
814 match->key->tun_key.tp_src ||
815 match->key->tun_key.tp_dst ||
816 match->key->tun_key.ttl ||
817 match->key->tun_key.tos ||
818 tun_flags & ~TUNNEL_KEY) {
819 OVS_NLERR(log, "IPv4 tun info is not correct");
820 return -EINVAL;
821 }
822 } else if (!match->key->tun_key.u.ipv4.dst) {
823 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
824 return -EINVAL;
825 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700826 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200827 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
828 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
829 return -EINVAL;
830 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700831
wenxu18b6f712019-03-28 12:43:23 +0800832 if (!ttl && !info_bridge_mode) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200833 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700834 return -EINVAL;
835 }
836 }
837
Thomas Graf1dd144c2015-01-15 03:53:59 +0100838 return opts_type;
839}
840
841static int vxlan_opt_to_nlattr(struct sk_buff *skb,
842 const void *tun_opts, int swkey_tun_opts_len)
843{
Thomas Graf614732e2015-07-21 10:44:06 +0200844 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100845 struct nlattr *nla;
846
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200847 nla = nla_nest_start_noflag(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100848 if (!nla)
849 return -EMSGSIZE;
850
851 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
852 return -EMSGSIZE;
853
854 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700855 return 0;
856}
857
Jiri Benc6b26ba32015-10-05 13:09:47 +0200858static int __ip_tun_to_nlattr(struct sk_buff *skb,
859 const struct ip_tunnel_key *output,
860 const void *tun_opts, int swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800861 unsigned short tun_proto, u8 mode)
Pravin B Shelare6445712013-10-03 18:16:47 -0700862{
Pravin B Shelare6445712013-10-03 18:16:47 -0700863 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200864 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
865 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700866 return -EMSGSIZE;
wenxu18b6f712019-03-28 12:43:23 +0800867
868 if (mode & IP_TUNNEL_INFO_BRIDGE)
869 return nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE)
870 ? -EMSGSIZE : 0;
871
Jiri Benc6b26ba32015-10-05 13:09:47 +0200872 switch (tun_proto) {
873 case AF_INET:
874 if (output->u.ipv4.src &&
875 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
876 output->u.ipv4.src))
877 return -EMSGSIZE;
878 if (output->u.ipv4.dst &&
879 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
880 output->u.ipv4.dst))
881 return -EMSGSIZE;
882 break;
883 case AF_INET6:
884 if (!ipv6_addr_any(&output->u.ipv6.src) &&
885 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
886 &output->u.ipv6.src))
887 return -EMSGSIZE;
888 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
889 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
890 &output->u.ipv6.dst))
891 return -EMSGSIZE;
892 break;
893 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200894 if (output->tos &&
895 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700896 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200897 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700898 return -EMSGSIZE;
899 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700900 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700901 return -EMSGSIZE;
902 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700903 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
904 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800905 if (output->tp_src &&
906 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
907 return -EMSGSIZE;
908 if (output->tp_dst &&
909 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
910 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700911 if ((output->tun_flags & TUNNEL_OAM) &&
912 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700913 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700914 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100915 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
916 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
917 swkey_tun_opts_len, tun_opts))
918 return -EMSGSIZE;
919 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
920 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
921 return -EMSGSIZE;
William Tufc1372f2018-01-25 13:20:11 -0800922 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
923 nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
924 swkey_tun_opts_len, tun_opts))
925 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100926 }
Jesse Grossf5796682014-10-03 15:35:33 -0700927
928 return 0;
929}
930
Jiri Benc6b26ba32015-10-05 13:09:47 +0200931static int ip_tun_to_nlattr(struct sk_buff *skb,
932 const struct ip_tunnel_key *output,
933 const void *tun_opts, int swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800934 unsigned short tun_proto, u8 mode)
Jesse Grossf5796682014-10-03 15:35:33 -0700935{
936 struct nlattr *nla;
937 int err;
938
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200939 nla = nla_nest_start_noflag(skb, OVS_KEY_ATTR_TUNNEL);
Jesse Grossf5796682014-10-03 15:35:33 -0700940 if (!nla)
941 return -EMSGSIZE;
942
Jiri Benc6b26ba32015-10-05 13:09:47 +0200943 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
wenxu18b6f712019-03-28 12:43:23 +0800944 tun_proto, mode);
Jesse Grossf5796682014-10-03 15:35:33 -0700945 if (err)
946 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700947
948 nla_nest_end(skb, nla);
949 return 0;
950}
951
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700952int ovs_nla_put_tunnel_info(struct sk_buff *skb,
953 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800954{
David S. Millerba3e2082015-10-24 06:54:12 -0700955 return __ip_tun_to_nlattr(skb, &tun_info->key,
956 ip_tunnel_info_opts(tun_info),
957 tun_info->options_len,
wenxu18b6f712019-03-28 12:43:23 +0800958 ip_tunnel_info_af(tun_info), tun_info->mode);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800959}
960
Eric Garver018c1dd2016-09-07 12:56:59 -0400961static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
962 const struct nlattr *a[],
963 bool is_mask, bool inner)
964{
965 __be16 tci = 0;
966 __be16 tpid = 0;
967
968 if (a[OVS_KEY_ATTR_VLAN])
969 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
970
971 if (a[OVS_KEY_ATTR_ETHERTYPE])
972 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
973
974 if (likely(!inner)) {
975 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
976 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
977 } else {
978 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
979 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
980 }
981 return 0;
982}
983
984static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
985 u64 key_attrs, bool inner,
986 const struct nlattr **a, bool log)
987{
988 __be16 tci = 0;
989
990 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
991 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
992 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
993 /* Not a VLAN. */
994 return 0;
995 }
996
997 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
998 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
999 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
1000 return -EINVAL;
1001 }
1002
1003 if (a[OVS_KEY_ATTR_VLAN])
1004 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1005
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001006 if (!(tci & htons(VLAN_CFI_MASK))) {
Eric Garver018c1dd2016-09-07 12:56:59 -04001007 if (tci) {
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001008 OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
Eric Garver018c1dd2016-09-07 12:56:59 -04001009 (inner) ? "C-VLAN" : "VLAN");
1010 return -EINVAL;
1011 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
1012 /* Corner case for truncated VLAN header. */
1013 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
1014 (inner) ? "C-VLAN" : "VLAN");
1015 return -EINVAL;
1016 }
1017 }
1018
1019 return 1;
1020}
1021
1022static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
1023 u64 key_attrs, bool inner,
1024 const struct nlattr **a, bool log)
1025{
1026 __be16 tci = 0;
1027 __be16 tpid = 0;
1028 bool encap_valid = !!(match->key->eth.vlan.tci &
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001029 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001030 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001031 htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001032
1033 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
1034 /* Not a VLAN. */
1035 return 0;
1036 }
1037
1038 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
1039 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
1040 (inner) ? "C-VLAN" : "VLAN");
1041 return -EINVAL;
1042 }
1043
1044 if (a[OVS_KEY_ATTR_VLAN])
1045 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1046
1047 if (a[OVS_KEY_ATTR_ETHERTYPE])
1048 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1049
1050 if (tpid != htons(0xffff)) {
1051 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1052 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1053 return -EINVAL;
1054 }
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001055 if (!(tci & htons(VLAN_CFI_MASK))) {
1056 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
Eric Garver018c1dd2016-09-07 12:56:59 -04001057 (inner) ? "C-VLAN" : "VLAN");
1058 return -EINVAL;
1059 }
1060
1061 return 1;
1062}
1063
1064static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1065 u64 *key_attrs, bool inner,
1066 const struct nlattr **a, bool is_mask,
1067 bool log)
1068{
1069 int err;
1070 const struct nlattr *encap;
1071
1072 if (!is_mask)
1073 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1074 a, log);
1075 else
1076 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1077 a, log);
1078 if (err <= 0)
1079 return err;
1080
1081 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1082 if (err)
1083 return err;
1084
1085 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1086 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1087 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1088
1089 encap = a[OVS_KEY_ATTR_ENCAP];
1090
1091 if (!is_mask)
1092 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1093 else
1094 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1095
1096 return err;
1097}
1098
1099static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1100 u64 *key_attrs, const struct nlattr **a,
1101 bool is_mask, bool log)
1102{
1103 int err;
1104 bool encap_valid = false;
1105
1106 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1107 is_mask, log);
1108 if (err)
1109 return err;
1110
Michał Mirosław9df46ae2018-11-08 18:44:50 +01001111 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
Eric Garver018c1dd2016-09-07 12:56:59 -04001112 if (encap_valid) {
1113 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1114 is_mask, log);
1115 if (err)
1116 return err;
1117 }
1118
1119 return 0;
1120}
1121
Jiri Benc0a6410f2016-11-10 16:28:22 +01001122static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1123 u64 *attrs, const struct nlattr **a,
1124 bool is_mask, bool log)
1125{
1126 __be16 eth_type;
1127
1128 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1129 if (is_mask) {
1130 /* Always exact match EtherType. */
1131 eth_type = htons(0xffff);
1132 } else if (!eth_proto_is_802_3(eth_type)) {
1133 OVS_NLERR(log, "EtherType %x is less than min %x",
1134 ntohs(eth_type), ETH_P_802_3_MIN);
1135 return -EINVAL;
1136 }
1137
1138 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1139 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1140 return 0;
1141}
1142
Joe Stringerc2ac6672015-08-26 11:31:52 -07001143static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1144 u64 *attrs, const struct nlattr **a,
1145 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001146{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001147 u8 mac_proto = MAC_PROTO_ETHERNET;
1148
Andy Zhou971427f32014-09-15 19:37:25 -07001149 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1150 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1151
1152 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1153 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1154 }
1155
1156 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1157 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1158
1159 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1160 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1161 }
1162
Pravin B Shelare6445712013-10-03 18:16:47 -07001163 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1164 SW_FLOW_KEY_PUT(match, phy.priority,
1165 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1166 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1167 }
1168
1169 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1170 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1171
Jesse Gross426cda52014-10-06 05:08:38 -07001172 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001173 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001174 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001175 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001176 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001177 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001178 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001179
1180 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1181 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1182 } else if (!is_mask) {
1183 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1184 }
1185
1186 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1187 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1188
1189 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1190 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1191 }
1192 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001193 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1194 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001195 return -EINVAL;
1196 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1197 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001198
1199 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001200 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001201 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001202
Joe Stringer9e384712015-10-19 19:18:57 -07001203 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001204 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001205 ct_state);
1206 return -EINVAL;
1207 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001208
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001209 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001210 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1211 }
1212 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001213 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001214 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1215
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001216 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001217 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1218 }
Joe Stringer182e3042015-08-26 11:31:49 -07001219 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001220 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001221 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1222
1223 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1224 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1225 }
Joe Stringer33db4122015-10-01 15:00:37 -07001226 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1227 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1228 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001229
Joe Stringer33db4122015-10-01 15:00:37 -07001230 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1231 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001232 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001233 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001234 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001235 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1236 const struct ovs_key_ct_tuple_ipv4 *ct;
1237
1238 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1239
1240 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1241 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1242 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1243 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001244 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001245 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1246 }
1247 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1248 const struct ovs_key_ct_tuple_ipv6 *ct;
1249
1250 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1251
1252 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1253 sizeof(match->key->ipv6.ct_orig.src),
1254 is_mask);
1255 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1256 sizeof(match->key->ipv6.ct_orig.dst),
1257 is_mask);
1258 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1259 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001260 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001261 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1262 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001263
Jiri Benc0a6410f2016-11-10 16:28:22 +01001264 /* For layer 3 packets the Ethernet type is provided
1265 * and treated as metadata but no MAC addresses are provided.
1266 */
1267 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1268 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1269 mac_proto = MAC_PROTO_NONE;
1270
Jiri Benc329f45b2016-11-10 16:28:18 +01001271 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001272 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1273
1274 if (mac_proto == MAC_PROTO_NONE)
1275 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1276 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001277
Pravin B Shelare6445712013-10-03 18:16:47 -07001278 return 0;
1279}
1280
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001281int nsh_hdr_from_nlattr(const struct nlattr *attr,
1282 struct nshhdr *nh, size_t size)
1283{
1284 struct nlattr *a;
1285 int rem;
1286 u8 flags = 0;
1287 u8 ttl = 0;
1288 int mdlen = 0;
1289
1290 /* validate_nsh has check this, so we needn't do duplicate check here
1291 */
1292 if (size < NSH_BASE_HDR_LEN)
1293 return -ENOBUFS;
1294
1295 nla_for_each_nested(a, attr, rem) {
1296 int type = nla_type(a);
1297
1298 switch (type) {
1299 case OVS_NSH_KEY_ATTR_BASE: {
1300 const struct ovs_nsh_key_base *base = nla_data(a);
1301
1302 flags = base->flags;
1303 ttl = base->ttl;
1304 nh->np = base->np;
1305 nh->mdtype = base->mdtype;
1306 nh->path_hdr = base->path_hdr;
1307 break;
1308 }
1309 case OVS_NSH_KEY_ATTR_MD1:
1310 mdlen = nla_len(a);
1311 if (mdlen > size - NSH_BASE_HDR_LEN)
1312 return -ENOBUFS;
1313 memcpy(&nh->md1, nla_data(a), mdlen);
1314 break;
1315
1316 case OVS_NSH_KEY_ATTR_MD2:
1317 mdlen = nla_len(a);
1318 if (mdlen > size - NSH_BASE_HDR_LEN)
1319 return -ENOBUFS;
1320 memcpy(&nh->md2, nla_data(a), mdlen);
1321 break;
1322
1323 default:
1324 return -EINVAL;
1325 }
1326 }
1327
1328 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1329 nh->ver_flags_ttl_len = 0;
1330 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1331
1332 return 0;
1333}
1334
1335int nsh_key_from_nlattr(const struct nlattr *attr,
1336 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1337{
1338 struct nlattr *a;
1339 int rem;
1340
1341 /* validate_nsh has check this, so we needn't do duplicate check here
1342 */
1343 nla_for_each_nested(a, attr, rem) {
1344 int type = nla_type(a);
1345
1346 switch (type) {
1347 case OVS_NSH_KEY_ATTR_BASE: {
1348 const struct ovs_nsh_key_base *base = nla_data(a);
1349 const struct ovs_nsh_key_base *base_mask = base + 1;
1350
1351 nsh->base = *base;
1352 nsh_mask->base = *base_mask;
1353 break;
1354 }
1355 case OVS_NSH_KEY_ATTR_MD1: {
1356 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1357 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1358
1359 memcpy(nsh->context, md1->context, sizeof(*md1));
1360 memcpy(nsh_mask->context, md1_mask->context,
1361 sizeof(*md1_mask));
1362 break;
1363 }
1364 case OVS_NSH_KEY_ATTR_MD2:
1365 /* Not supported yet */
1366 return -ENOTSUPP;
1367 default:
1368 return -EINVAL;
1369 }
1370 }
1371
1372 return 0;
1373}
1374
1375static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1376 struct sw_flow_match *match, bool is_mask,
1377 bool is_push_nsh, bool log)
1378{
1379 struct nlattr *a;
1380 int rem;
1381 bool has_base = false;
1382 bool has_md1 = false;
1383 bool has_md2 = false;
1384 u8 mdtype = 0;
1385 int mdlen = 0;
1386
1387 if (WARN_ON(is_push_nsh && is_mask))
1388 return -EINVAL;
1389
1390 nla_for_each_nested(a, attr, rem) {
1391 int type = nla_type(a);
1392 int i;
1393
1394 if (type > OVS_NSH_KEY_ATTR_MAX) {
1395 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1396 type, OVS_NSH_KEY_ATTR_MAX);
1397 return -EINVAL;
1398 }
1399
1400 if (!check_attr_len(nla_len(a),
1401 ovs_nsh_key_attr_lens[type].len)) {
1402 OVS_NLERR(
1403 log,
1404 "nsh attr %d has unexpected len %d expected %d",
1405 type,
1406 nla_len(a),
1407 ovs_nsh_key_attr_lens[type].len
1408 );
1409 return -EINVAL;
1410 }
1411
1412 switch (type) {
1413 case OVS_NSH_KEY_ATTR_BASE: {
1414 const struct ovs_nsh_key_base *base = nla_data(a);
1415
1416 has_base = true;
1417 mdtype = base->mdtype;
1418 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1419 base->flags, is_mask);
1420 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1421 base->ttl, is_mask);
1422 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1423 base->mdtype, is_mask);
1424 SW_FLOW_KEY_PUT(match, nsh.base.np,
1425 base->np, is_mask);
1426 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1427 base->path_hdr, is_mask);
1428 break;
1429 }
1430 case OVS_NSH_KEY_ATTR_MD1: {
1431 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1432
1433 has_md1 = true;
1434 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1435 SW_FLOW_KEY_PUT(match, nsh.context[i],
1436 md1->context[i], is_mask);
1437 break;
1438 }
1439 case OVS_NSH_KEY_ATTR_MD2:
1440 if (!is_push_nsh) /* Not supported MD type 2 yet */
1441 return -ENOTSUPP;
1442
1443 has_md2 = true;
1444 mdlen = nla_len(a);
1445 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1446 OVS_NLERR(
1447 log,
1448 "Invalid MD length %d for MD type %d",
1449 mdlen,
1450 mdtype
1451 );
1452 return -EINVAL;
1453 }
1454 break;
1455 default:
1456 OVS_NLERR(log, "Unknown nsh attribute %d",
1457 type);
1458 return -EINVAL;
1459 }
1460 }
1461
1462 if (rem > 0) {
1463 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1464 return -EINVAL;
1465 }
1466
1467 if (has_md1 && has_md2) {
1468 OVS_NLERR(
1469 1,
1470 "invalid nsh attribute: md1 and md2 are exclusive."
1471 );
1472 return -EINVAL;
1473 }
1474
1475 if (!is_mask) {
1476 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1477 (has_md2 && mdtype != NSH_M_TYPE2)) {
1478 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1479 mdtype);
1480 return -EINVAL;
1481 }
1482
1483 if (is_push_nsh &&
1484 (!has_base || (!has_md1 && !has_md2))) {
1485 OVS_NLERR(
1486 1,
1487 "push_nsh: missing base or metadata attributes"
1488 );
1489 return -EINVAL;
1490 }
1491 }
1492
1493 return 0;
1494}
1495
Joe Stringerc2ac6672015-08-26 11:31:52 -07001496static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1497 u64 attrs, const struct nlattr **a,
1498 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001499{
1500 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001501
Joe Stringerc2ac6672015-08-26 11:31:52 -07001502 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001503 if (err)
1504 return err;
1505
1506 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1507 const struct ovs_key_ethernet *eth_key;
1508
1509 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1510 SW_FLOW_KEY_MEMCPY(match, eth.src,
1511 eth_key->eth_src, ETH_ALEN, is_mask);
1512 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1513 eth_key->eth_dst, ETH_ALEN, is_mask);
1514 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001515
Jiri Benc0a6410f2016-11-10 16:28:22 +01001516 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1517 /* VLAN attribute is always parsed before getting here since it
1518 * may occur multiple times.
1519 */
1520 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001521 return -EINVAL;
1522 }
1523
Jiri Benc0a6410f2016-11-10 16:28:22 +01001524 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1525 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1526 log);
1527 if (err)
1528 return err;
1529 } else if (!is_mask) {
1530 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1531 }
1532 } else if (!match->key->eth.type) {
1533 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1534 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001535 }
1536
1537 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1538 const struct ovs_key_ipv4 *ipv4_key;
1539
1540 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1541 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001542 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1543 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001544 return -EINVAL;
1545 }
1546 SW_FLOW_KEY_PUT(match, ip.proto,
1547 ipv4_key->ipv4_proto, is_mask);
1548 SW_FLOW_KEY_PUT(match, ip.tos,
1549 ipv4_key->ipv4_tos, is_mask);
1550 SW_FLOW_KEY_PUT(match, ip.ttl,
1551 ipv4_key->ipv4_ttl, is_mask);
1552 SW_FLOW_KEY_PUT(match, ip.frag,
1553 ipv4_key->ipv4_frag, is_mask);
1554 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1555 ipv4_key->ipv4_src, is_mask);
1556 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1557 ipv4_key->ipv4_dst, is_mask);
1558 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1559 }
1560
1561 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1562 const struct ovs_key_ipv6 *ipv6_key;
1563
1564 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1565 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001566 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1567 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001568 return -EINVAL;
1569 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001570
Joe Stringerd3052bb2014-11-19 13:54:49 -08001571 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001572 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001573 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1574 return -EINVAL;
1575 }
1576
Pravin B Shelare6445712013-10-03 18:16:47 -07001577 SW_FLOW_KEY_PUT(match, ipv6.label,
1578 ipv6_key->ipv6_label, is_mask);
1579 SW_FLOW_KEY_PUT(match, ip.proto,
1580 ipv6_key->ipv6_proto, is_mask);
1581 SW_FLOW_KEY_PUT(match, ip.tos,
1582 ipv6_key->ipv6_tclass, is_mask);
1583 SW_FLOW_KEY_PUT(match, ip.ttl,
1584 ipv6_key->ipv6_hlimit, is_mask);
1585 SW_FLOW_KEY_PUT(match, ip.frag,
1586 ipv6_key->ipv6_frag, is_mask);
1587 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1588 ipv6_key->ipv6_src,
1589 sizeof(match->key->ipv6.addr.src),
1590 is_mask);
1591 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1592 ipv6_key->ipv6_dst,
1593 sizeof(match->key->ipv6.addr.dst),
1594 is_mask);
1595
1596 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1597 }
1598
1599 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1600 const struct ovs_key_arp *arp_key;
1601
1602 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1603 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001604 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001605 arp_key->arp_op);
1606 return -EINVAL;
1607 }
1608
1609 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1610 arp_key->arp_sip, is_mask);
1611 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1612 arp_key->arp_tip, is_mask);
1613 SW_FLOW_KEY_PUT(match, ip.proto,
1614 ntohs(arp_key->arp_op), is_mask);
1615 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1616 arp_key->arp_sha, ETH_ALEN, is_mask);
1617 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1618 arp_key->arp_tha, ETH_ALEN, is_mask);
1619
1620 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1621 }
1622
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001623 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1624 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1625 is_mask, false, log) < 0)
1626 return -EINVAL;
1627 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1628 }
1629
Simon Horman25cd9ba2014-10-06 05:05:13 -07001630 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1631 const struct ovs_key_mpls *mpls_key;
Martin Varghesefbdcdd72019-11-04 07:27:44 +05301632 u32 hdr_len;
1633 u32 label_count, label_count_mask, i;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001634
1635 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
Martin Varghesefbdcdd72019-11-04 07:27:44 +05301636 hdr_len = nla_len(a[OVS_KEY_ATTR_MPLS]);
1637 label_count = hdr_len / sizeof(struct ovs_key_mpls);
1638
1639 if (label_count == 0 || label_count > MPLS_LABEL_DEPTH ||
1640 hdr_len % sizeof(struct ovs_key_mpls))
1641 return -EINVAL;
1642
1643 label_count_mask = GENMASK(label_count - 1, 0);
1644
1645 for (i = 0 ; i < label_count; i++)
1646 SW_FLOW_KEY_PUT(match, mpls.lse[i],
1647 mpls_key[i].mpls_lse, is_mask);
1648
1649 SW_FLOW_KEY_PUT(match, mpls.num_labels_mask,
1650 label_count_mask, is_mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001651
1652 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1653 }
1654
Pravin B Shelare6445712013-10-03 18:16:47 -07001655 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1656 const struct ovs_key_tcp *tcp_key;
1657
1658 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001659 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1660 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001661 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1662 }
1663
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001664 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001665 SW_FLOW_KEY_PUT(match, tp.flags,
1666 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1667 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001668 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1669 }
1670
Pravin B Shelare6445712013-10-03 18:16:47 -07001671 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1672 const struct ovs_key_udp *udp_key;
1673
1674 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001675 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1676 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001677 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1678 }
1679
1680 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1681 const struct ovs_key_sctp *sctp_key;
1682
1683 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001684 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1685 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001686 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1687 }
1688
1689 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1690 const struct ovs_key_icmp *icmp_key;
1691
1692 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001693 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001694 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001695 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001696 htons(icmp_key->icmp_code), is_mask);
1697 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1698 }
1699
1700 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1701 const struct ovs_key_icmpv6 *icmpv6_key;
1702
1703 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001704 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001705 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001706 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001707 htons(icmpv6_key->icmpv6_code), is_mask);
1708 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1709 }
1710
1711 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1712 const struct ovs_key_nd *nd_key;
1713
1714 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1715 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1716 nd_key->nd_target,
1717 sizeof(match->key->ipv6.nd.target),
1718 is_mask);
1719 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1720 nd_key->nd_sll, ETH_ALEN, is_mask);
1721 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1722 nd_key->nd_tll, ETH_ALEN, is_mask);
1723 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1724 }
1725
Jesse Gross426cda52014-10-06 05:08:38 -07001726 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001727 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001728 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001729 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001730 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001731
1732 return 0;
1733}
1734
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001735static void nlattr_set(struct nlattr *attr, u8 val,
1736 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001737{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001738 struct nlattr *nla;
1739 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001740
Pravin B Shelarf47de062014-10-16 21:55:45 -07001741 /* The nlattr stream should already have been validated */
1742 nla_for_each_nested(nla, attr, rem) {
Stefano Brivio72f17ba2018-05-03 18:13:25 +02001743 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1744 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1745 else
Pravin B Shelarf47de062014-10-16 21:55:45 -07001746 memset(nla_data(nla), val, nla_len(nla));
Joe Stringer9e384712015-10-19 19:18:57 -07001747
1748 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1749 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001750 }
1751}
1752
1753static void mask_set_nlattr(struct nlattr *attr, u8 val)
1754{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001755 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001756}
1757
1758/**
1759 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1760 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1761 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1762 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001763 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001764 * @match: receives the extracted flow match information.
1765 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1766 * sequence. The fields should of the packet that triggered the creation
1767 * of this flow.
1768 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1769 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001770 * @log: Boolean to allow kernel error logging. Normally true, but when
1771 * probing for feature compatibility this should be passed in as false to
1772 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001773 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001774int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001775 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001776 const struct nlattr *nla_mask,
1777 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001778{
1779 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001780 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001781 u64 key_attrs = 0;
1782 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001783 int err;
1784
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001785 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001786 if (err)
1787 return err;
1788
Eric Garver018c1dd2016-09-07 12:56:59 -04001789 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1790 if (err)
1791 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001792
Joe Stringerc2ac6672015-08-26 11:31:52 -07001793 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001794 if (err)
1795 return err;
1796
Pravin B Shelara85311b2014-10-19 12:03:40 -07001797 if (match->mask) {
1798 if (!nla_mask) {
1799 /* Create an exact match mask. We need to set to 0xff
1800 * all the 'match->mask' fields that have been touched
1801 * in 'match->key'. We cannot simply memset
1802 * 'match->mask', because padding bytes and fields not
1803 * specified in 'match->key' should be left to 0.
1804 * Instead, we use a stream of netlink attributes,
1805 * copied from 'key' and set to 0xff.
1806 * ovs_key_from_nlattrs() will take care of filling
1807 * 'match->mask' appropriately.
1808 */
1809 newmask = kmemdup(nla_key,
1810 nla_total_size(nla_len(nla_key)),
1811 GFP_KERNEL);
1812 if (!newmask)
1813 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001814
Pravin B Shelara85311b2014-10-19 12:03:40 -07001815 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001816
Pravin B Shelara85311b2014-10-19 12:03:40 -07001817 /* The userspace does not send tunnel attributes that
1818 * are 0, but we should not wildcard them nonetheless.
1819 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001820 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001821 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1822 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001823
Pravin B Shelara85311b2014-10-19 12:03:40 -07001824 nla_mask = newmask;
1825 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001826
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001827 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001828 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001829 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001830
Pravin B Shelara85311b2014-10-19 12:03:40 -07001831 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001832 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1833 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001834
Eric Garver018c1dd2016-09-07 12:56:59 -04001835 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1836 if (err)
1837 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001838
Joe Stringerc2ac6672015-08-26 11:31:52 -07001839 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1840 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001841 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001842 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001843 }
1844
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001845 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001846 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001847
Pravin B Shelarf47de062014-10-16 21:55:45 -07001848free_newmask:
1849 kfree(newmask);
1850 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001851}
1852
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001853static size_t get_ufid_len(const struct nlattr *attr, bool log)
1854{
1855 size_t len;
1856
1857 if (!attr)
1858 return 0;
1859
1860 len = nla_len(attr);
1861 if (len < 1 || len > MAX_UFID_LENGTH) {
1862 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1863 nla_len(attr), MAX_UFID_LENGTH);
1864 return 0;
1865 }
1866
1867 return len;
1868}
1869
1870/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1871 * or false otherwise.
1872 */
1873bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1874 bool log)
1875{
1876 sfid->ufid_len = get_ufid_len(attr, log);
1877 if (sfid->ufid_len)
1878 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1879
1880 return sfid->ufid_len;
1881}
1882
1883int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1884 const struct sw_flow_key *key, bool log)
1885{
1886 struct sw_flow_key *new_key;
1887
1888 if (ovs_nla_get_ufid(sfid, ufid, log))
1889 return 0;
1890
1891 /* If UFID was not provided, use unmasked key. */
1892 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1893 if (!new_key)
1894 return -ENOMEM;
1895 memcpy(new_key, key, sizeof(*key));
1896 sfid->unmasked_key = new_key;
1897
1898 return 0;
1899}
1900
1901u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1902{
1903 return attr ? nla_get_u32(attr) : 0;
1904}
1905
Pravin B Shelare6445712013-10-03 18:16:47 -07001906/**
1907 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001908 * @net: Network namespace.
1909 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1910 * metadata.
1911 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1912 * attributes.
1913 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001914 * @log: Boolean to allow kernel error logging. Normally true, but when
1915 * probing for feature compatibility this should be passed in as false to
1916 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001917 *
1918 * This parses a series of Netlink attributes that form a flow key, which must
1919 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1920 * get the metadata, that is, the parts of the flow key that cannot be
1921 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001922 *
1923 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001924 */
1925
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001926int ovs_nla_get_flow_metadata(struct net *net,
1927 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1928 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001929{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001930 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001931
1932 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001933 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001934
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001935 key->ct_state = 0;
1936 key->ct_zone = 0;
1937 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001938 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001939 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1940 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1941
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001942 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001943
Joe Stringerc2ac6672015-08-26 11:31:52 -07001944 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001945}
1946
Eric Garver018c1dd2016-09-07 12:56:59 -04001947static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1948 bool is_mask)
1949{
1950 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1951
1952 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1953 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1954 return -EMSGSIZE;
1955 return 0;
1956}
1957
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001958static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1959 struct sk_buff *skb)
1960{
1961 struct nlattr *start;
1962
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001963 start = nla_nest_start_noflag(skb, OVS_KEY_ATTR_NSH);
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001964 if (!start)
1965 return -EMSGSIZE;
1966
1967 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1968 goto nla_put_failure;
1969
1970 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1971 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1972 sizeof(nsh->context), nsh->context))
1973 goto nla_put_failure;
1974 }
1975
1976 /* Don't support MD type 2 yet */
1977
1978 nla_nest_end(skb, start);
1979
1980 return 0;
1981
1982nla_put_failure:
1983 return -EMSGSIZE;
1984}
1985
Joe Stringer5b4237b2015-01-21 16:42:48 -08001986static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1987 const struct sw_flow_key *output, bool is_mask,
1988 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001989{
1990 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001991 struct nlattr *nla;
1992 struct nlattr *encap = NULL;
1993 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001994
Andy Zhou971427f32014-09-15 19:37:25 -07001995 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1996 goto nla_put_failure;
1997
1998 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1999 goto nla_put_failure;
2000
Pravin B Shelare6445712013-10-03 18:16:47 -07002001 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
2002 goto nla_put_failure;
2003
Jiri Benc00a93ba2015-10-05 13:09:46 +02002004 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01002005 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07002006
2007 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01002008 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07002009
Jiri Benc6b26ba32015-10-05 13:09:47 +02002010 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
wenxu18b6f712019-03-28 12:43:23 +08002011 swkey->tun_opts_len, swkey->tun_proto, 0))
Jesse Grossf5796682014-10-03 15:35:33 -07002012 goto nla_put_failure;
2013 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002014
2015 if (swkey->phy.in_port == DP_MAX_PORTS) {
2016 if (is_mask && (output->phy.in_port == 0xffff))
2017 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
2018 goto nla_put_failure;
2019 } else {
2020 u16 upper_u16;
2021 upper_u16 = !is_mask ? 0 : 0xffff;
2022
2023 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
2024 (upper_u16 << 16) | output->phy.in_port))
2025 goto nla_put_failure;
2026 }
2027
2028 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
2029 goto nla_put_failure;
2030
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08002031 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07002032 goto nla_put_failure;
2033
Jiri Benc0a6410f2016-11-10 16:28:22 +01002034 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
2035 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
2036 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07002037 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04002038
Jiri Benc0a6410f2016-11-10 16:28:22 +01002039 eth_key = nla_data(nla);
2040 ether_addr_copy(eth_key->eth_src, output->eth.src);
2041 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
2042
2043 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
2044 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04002045 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002046 encap = nla_nest_start_noflag(skb, OVS_KEY_ATTR_ENCAP);
Jiri Benc0a6410f2016-11-10 16:28:22 +01002047 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04002048 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07002049
Jiri Benc0a6410f2016-11-10 16:28:22 +01002050 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
2051 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
2052 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002053 in_encap = nla_nest_start_noflag(skb,
2054 OVS_KEY_ATTR_ENCAP);
Jiri Benc0a6410f2016-11-10 16:28:22 +01002055 if (!swkey->eth.cvlan.tci)
2056 goto unencap;
2057 }
2058 }
2059
2060 if (swkey->eth.type == htons(ETH_P_802_2)) {
2061 /*
2062 * Ethertype 802.2 is represented in the netlink with omitted
2063 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2064 * 0xffff in the mask attribute. Ethertype can also
2065 * be wildcarded.
2066 */
2067 if (is_mask && output->eth.type)
2068 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
2069 output->eth.type))
2070 goto nla_put_failure;
2071 goto unencap;
2072 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002073 }
2074
2075 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2076 goto nla_put_failure;
2077
Eric Garver018c1dd2016-09-07 12:56:59 -04002078 if (eth_type_vlan(swkey->eth.type)) {
2079 /* There are 3 VLAN tags, we don't know anything about the rest
2080 * of the packet, so truncate here.
2081 */
2082 WARN_ON_ONCE(!(encap && in_encap));
2083 goto unencap;
2084 }
2085
Pravin B Shelare6445712013-10-03 18:16:47 -07002086 if (swkey->eth.type == htons(ETH_P_IP)) {
2087 struct ovs_key_ipv4 *ipv4_key;
2088
2089 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2090 if (!nla)
2091 goto nla_put_failure;
2092 ipv4_key = nla_data(nla);
2093 ipv4_key->ipv4_src = output->ipv4.addr.src;
2094 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2095 ipv4_key->ipv4_proto = output->ip.proto;
2096 ipv4_key->ipv4_tos = output->ip.tos;
2097 ipv4_key->ipv4_ttl = output->ip.ttl;
2098 ipv4_key->ipv4_frag = output->ip.frag;
2099 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2100 struct ovs_key_ipv6 *ipv6_key;
2101
2102 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2103 if (!nla)
2104 goto nla_put_failure;
2105 ipv6_key = nla_data(nla);
2106 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2107 sizeof(ipv6_key->ipv6_src));
2108 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2109 sizeof(ipv6_key->ipv6_dst));
2110 ipv6_key->ipv6_label = output->ipv6.label;
2111 ipv6_key->ipv6_proto = output->ip.proto;
2112 ipv6_key->ipv6_tclass = output->ip.tos;
2113 ipv6_key->ipv6_hlimit = output->ip.ttl;
2114 ipv6_key->ipv6_frag = output->ip.frag;
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002115 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2116 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2117 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002118 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2119 swkey->eth.type == htons(ETH_P_RARP)) {
2120 struct ovs_key_arp *arp_key;
2121
2122 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2123 if (!nla)
2124 goto nla_put_failure;
2125 arp_key = nla_data(nla);
2126 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2127 arp_key->arp_sip = output->ipv4.addr.src;
2128 arp_key->arp_tip = output->ipv4.addr.dst;
2129 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08002130 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2131 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002132 } else if (eth_p_mpls(swkey->eth.type)) {
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302133 u8 i, num_labels;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002134 struct ovs_key_mpls *mpls_key;
2135
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302136 num_labels = hweight_long(output->mpls.num_labels_mask);
2137 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS,
2138 num_labels * sizeof(*mpls_key));
Simon Horman25cd9ba2014-10-06 05:05:13 -07002139 if (!nla)
2140 goto nla_put_failure;
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302141
Simon Horman25cd9ba2014-10-06 05:05:13 -07002142 mpls_key = nla_data(nla);
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302143 for (i = 0; i < num_labels; i++)
2144 mpls_key[i].mpls_lse = output->mpls.lse[i];
Pravin B Shelare6445712013-10-03 18:16:47 -07002145 }
2146
2147 if ((swkey->eth.type == htons(ETH_P_IP) ||
2148 swkey->eth.type == htons(ETH_P_IPV6)) &&
2149 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2150
2151 if (swkey->ip.proto == IPPROTO_TCP) {
2152 struct ovs_key_tcp *tcp_key;
2153
2154 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2155 if (!nla)
2156 goto nla_put_failure;
2157 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002158 tcp_key->tcp_src = output->tp.src;
2159 tcp_key->tcp_dst = output->tp.dst;
2160 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2161 output->tp.flags))
2162 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002163 } else if (swkey->ip.proto == IPPROTO_UDP) {
2164 struct ovs_key_udp *udp_key;
2165
2166 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2167 if (!nla)
2168 goto nla_put_failure;
2169 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002170 udp_key->udp_src = output->tp.src;
2171 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002172 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2173 struct ovs_key_sctp *sctp_key;
2174
2175 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2176 if (!nla)
2177 goto nla_put_failure;
2178 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002179 sctp_key->sctp_src = output->tp.src;
2180 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002181 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2182 swkey->ip.proto == IPPROTO_ICMP) {
2183 struct ovs_key_icmp *icmp_key;
2184
2185 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2186 if (!nla)
2187 goto nla_put_failure;
2188 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002189 icmp_key->icmp_type = ntohs(output->tp.src);
2190 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002191 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2192 swkey->ip.proto == IPPROTO_ICMPV6) {
2193 struct ovs_key_icmpv6 *icmpv6_key;
2194
2195 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2196 sizeof(*icmpv6_key));
2197 if (!nla)
2198 goto nla_put_failure;
2199 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002200 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2201 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002202
2203 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
2204 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
2205 struct ovs_key_nd *nd_key;
2206
2207 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2208 if (!nla)
2209 goto nla_put_failure;
2210 nd_key = nla_data(nla);
2211 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2212 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08002213 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2214 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07002215 }
2216 }
2217 }
2218
2219unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04002220 if (in_encap)
2221 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07002222 if (encap)
2223 nla_nest_end(skb, encap);
2224
2225 return 0;
2226
2227nla_put_failure:
2228 return -EMSGSIZE;
2229}
2230
Joe Stringer5b4237b2015-01-21 16:42:48 -08002231int ovs_nla_put_key(const struct sw_flow_key *swkey,
2232 const struct sw_flow_key *output, int attr, bool is_mask,
2233 struct sk_buff *skb)
2234{
2235 int err;
2236 struct nlattr *nla;
2237
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002238 nla = nla_nest_start_noflag(skb, attr);
Joe Stringer5b4237b2015-01-21 16:42:48 -08002239 if (!nla)
2240 return -EMSGSIZE;
2241 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2242 if (err)
2243 return err;
2244 nla_nest_end(skb, nla);
2245
2246 return 0;
2247}
2248
2249/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002250int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08002251{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002252 if (ovs_identifier_is_ufid(&flow->id))
2253 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2254 flow->id.ufid);
2255
2256 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2257 OVS_FLOW_ATTR_KEY, false, skb);
2258}
2259
2260/* Called with ovs_mutex or RCU read lock. */
2261int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2262{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08002263 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08002264 OVS_FLOW_ATTR_KEY, false, skb);
2265}
2266
2267/* Called with ovs_mutex or RCU read lock. */
2268int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2269{
2270 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2271 OVS_FLOW_ATTR_MASK, true, skb);
2272}
2273
Pravin B Shelare6445712013-10-03 18:16:47 -07002274#define MAX_ACTIONS_BUFSIZE (32 * 1024)
2275
zhangliping67c8d222017-11-25 22:02:12 +08002276static struct sw_flow_actions *nla_alloc_flow_actions(int size)
Pravin B Shelare6445712013-10-03 18:16:47 -07002277{
2278 struct sw_flow_actions *sfa;
2279
zhangliping67c8d222017-11-25 22:02:12 +08002280 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002281
2282 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2283 if (!sfa)
2284 return ERR_PTR(-ENOMEM);
2285
2286 sfa->actions_len = 0;
2287 return sfa;
2288}
2289
Thomas Graf34ae9322015-07-21 10:44:03 +02002290static void ovs_nla_free_set_action(const struct nlattr *a)
2291{
2292 const struct nlattr *ovs_key = nla_data(a);
2293 struct ovs_tunnel_info *ovs_tun;
2294
2295 switch (nla_type(ovs_key)) {
2296 case OVS_KEY_ATTR_TUNNEL_INFO:
2297 ovs_tun = nla_data(ovs_key);
2298 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2299 break;
2300 }
2301}
2302
Pravin B Shelare6445712013-10-03 18:16:47 -07002303void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2304{
Thomas Graf34ae9322015-07-21 10:44:03 +02002305 const struct nlattr *a;
2306 int rem;
2307
2308 if (!sf_acts)
2309 return;
2310
2311 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
2312 switch (nla_type(a)) {
2313 case OVS_ACTION_ATTR_SET:
2314 ovs_nla_free_set_action(a);
2315 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002316 case OVS_ACTION_ATTR_CT:
2317 ovs_ct_free_action(a);
2318 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02002319 }
2320 }
2321
2322 kfree(sf_acts);
2323}
2324
2325static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2326{
2327 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2328}
2329
2330/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2331 * The caller must hold rcu_read_lock for this to be sensible. */
2332void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2333{
2334 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07002335}
2336
2337static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002338 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002339{
2340
2341 struct sw_flow_actions *acts;
2342 int new_acts_size;
Andrea Righif28cd2a2019-03-28 07:36:00 +01002343 size_t req_size = NLA_ALIGN(attr_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002344 int next_offset = offsetof(struct sw_flow_actions, actions) +
2345 (*sfa)->actions_len;
2346
2347 if (req_size <= (ksize(*sfa) - next_offset))
2348 goto out;
2349
Andrea Righif28cd2a2019-03-28 07:36:00 +01002350 new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
Pravin B Shelare6445712013-10-03 18:16:47 -07002351
2352 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
zhangliping67c8d222017-11-25 22:02:12 +08002353 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
2354 OVS_NLERR(log, "Flow action size exceeds max %u",
2355 MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002356 return ERR_PTR(-EMSGSIZE);
zhangliping67c8d222017-11-25 22:02:12 +08002357 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002358 new_acts_size = MAX_ACTIONS_BUFSIZE;
2359 }
2360
zhangliping67c8d222017-11-25 22:02:12 +08002361 acts = nla_alloc_flow_actions(new_acts_size);
Pravin B Shelare6445712013-10-03 18:16:47 -07002362 if (IS_ERR(acts))
2363 return (void *)acts;
2364
2365 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2366 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07002367 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002368 kfree(*sfa);
2369 *sfa = acts;
2370
2371out:
2372 (*sfa)->actions_len += req_size;
2373 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2374}
2375
Jesse Grossf0b128c2014-10-03 15:35:31 -07002376static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002377 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002378{
2379 struct nlattr *a;
2380
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002381 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002382 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002383 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002384
2385 a->nla_type = attrtype;
2386 a->nla_len = nla_attr_size(len);
2387
2388 if (data)
2389 memcpy(nla_data(a), data, len);
2390 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2391
Jesse Grossf0b128c2014-10-03 15:35:31 -07002392 return a;
2393}
2394
Joe Stringer7f8a4362015-08-26 11:31:48 -07002395int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2396 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002397{
2398 struct nlattr *a;
2399
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002400 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002401
Fabian Frederickf35423c12014-11-14 19:32:58 +01002402 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002403}
2404
2405static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002406 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002407{
2408 int used = (*sfa)->actions_len;
2409 int err;
2410
Joe Stringer7f8a4362015-08-26 11:31:48 -07002411 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002412 if (err)
2413 return err;
2414
2415 return used;
2416}
2417
2418static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2419 int st_offset)
2420{
2421 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2422 st_offset);
2423
2424 a->nla_len = sfa->actions_len - st_offset;
2425}
2426
Joe Stringer7f8a4362015-08-26 11:31:48 -07002427static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002428 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002429 struct sw_flow_actions **sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302430 __be16 eth_type, __be16 vlan_tci,
2431 u32 mpls_label_count, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002432
Joe Stringer7f8a4362015-08-26 11:31:48 -07002433static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002434 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002435 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002436 __be16 eth_type, __be16 vlan_tci,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302437 u32 mpls_label_count, bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002438{
2439 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2440 const struct nlattr *probability, *actions;
2441 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002442 int rem, start, err;
2443 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002444
2445 memset(attrs, 0, sizeof(attrs));
2446 nla_for_each_nested(a, attr, rem) {
2447 int type = nla_type(a);
2448 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2449 return -EINVAL;
2450 attrs[type] = a;
2451 }
2452 if (rem)
2453 return -EINVAL;
2454
2455 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2456 if (!probability || nla_len(probability) != sizeof(u32))
2457 return -EINVAL;
2458
2459 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2460 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2461 return -EINVAL;
2462
2463 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002464 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002465 if (start < 0)
2466 return start;
andy zhou798c1662017-03-20 16:32:29 -07002467
2468 /* When both skb and flow may be changed, put the sample
2469 * into a deferred fifo. On the other hand, if only skb
2470 * may be modified, the actions can be executed in place.
2471 *
2472 * Do this analysis at the flow installation time.
2473 * Set 'clone_action->exec' to true if the actions can be
2474 * executed without being deferred.
2475 *
2476 * If the sample is the last action, it can always be excuted
2477 * rather than deferred.
2478 */
2479 arg.exec = last || !actions_may_change_flow(actions);
2480 arg.probability = nla_get_u32(probability);
2481
2482 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2483 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002484 if (err)
2485 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002486
andy zhou798c1662017-03-20 16:32:29 -07002487 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302488 eth_type, vlan_tci, mpls_label_count, log);
andy zhou798c1662017-03-20 16:32:29 -07002489
Pravin B Shelare6445712013-10-03 18:16:47 -07002490 if (err)
2491 return err;
2492
Pravin B Shelare6445712013-10-03 18:16:47 -07002493 add_nested_action_end(*sfa, start);
2494
2495 return 0;
2496}
2497
Yifeng Sunb2335042018-07-02 08:18:03 -07002498static int validate_and_copy_clone(struct net *net,
2499 const struct nlattr *attr,
2500 const struct sw_flow_key *key,
2501 struct sw_flow_actions **sfa,
2502 __be16 eth_type, __be16 vlan_tci,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302503 u32 mpls_label_count, bool log, bool last)
Yifeng Sunb2335042018-07-02 08:18:03 -07002504{
2505 int start, err;
2506 u32 exec;
2507
2508 if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
2509 return -EINVAL;
2510
2511 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
2512 if (start < 0)
2513 return start;
2514
2515 exec = last || !actions_may_change_flow(attr);
2516
2517 err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
2518 sizeof(exec), log);
2519 if (err)
2520 return err;
2521
2522 err = __ovs_nla_copy_actions(net, attr, key, sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302523 eth_type, vlan_tci, mpls_label_count, log);
Yifeng Sunb2335042018-07-02 08:18:03 -07002524 if (err)
2525 return err;
2526
2527 add_nested_action_end(*sfa, start);
2528
2529 return 0;
2530}
2531
Pravin B Shelare6445712013-10-03 18:16:47 -07002532void ovs_match_init(struct sw_flow_match *match,
2533 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002534 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002535 struct sw_flow_mask *mask)
2536{
2537 memset(match, 0, sizeof(*match));
2538 match->key = key;
2539 match->mask = mask;
2540
pravin shelar22799942016-09-19 13:51:00 -07002541 if (reset_key)
2542 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002543
2544 if (mask) {
2545 memset(&mask->key, 0, sizeof(mask->key));
2546 mask->range.start = mask->range.end = 0;
2547 }
2548}
2549
Thomas Grafd91641d2015-01-15 03:53:57 +01002550static int validate_geneve_opts(struct sw_flow_key *key)
2551{
2552 struct geneve_opt *option;
2553 int opts_len = key->tun_opts_len;
2554 bool crit_opt = false;
2555
2556 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2557 while (opts_len > 0) {
2558 int len;
2559
2560 if (opts_len < sizeof(*option))
2561 return -EINVAL;
2562
2563 len = sizeof(*option) + option->length * 4;
2564 if (len > opts_len)
2565 return -EINVAL;
2566
2567 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2568
2569 option = (struct geneve_opt *)((u8 *)option + len);
2570 opts_len -= len;
Christopher Díaz Riveros89290b82018-01-17 16:10:28 -05002571 }
Thomas Grafd91641d2015-01-15 03:53:57 +01002572
2573 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2574
2575 return 0;
2576}
2577
Pravin B Shelare6445712013-10-03 18:16:47 -07002578static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002579 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002580{
2581 struct sw_flow_match match;
2582 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002583 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002584 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002585 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002586 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002587 int err = 0, start, opts_type;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002588 __be16 dst_opt_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002589
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002590 dst_opt_type = 0;
pravin shelar22799942016-09-19 13:51:00 -07002591 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002592 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002593 if (opts_type < 0)
2594 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002595
Jesse Grossf5796682014-10-03 15:35:33 -07002596 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002597 switch (opts_type) {
2598 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2599 err = validate_geneve_opts(&key);
2600 if (err < 0)
2601 return err;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002602 dst_opt_type = TUNNEL_GENEVE_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002603 break;
2604 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002605 dst_opt_type = TUNNEL_VXLAN_OPT;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002606 break;
William Tufc1372f2018-01-25 13:20:11 -08002607 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002608 dst_opt_type = TUNNEL_ERSPAN_OPT;
William Tufc1372f2018-01-25 13:20:11 -08002609 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002610 }
Christopher Díaz Riveros89290b82018-01-17 16:10:28 -05002611 }
Jesse Grossf5796682014-10-03 15:35:33 -07002612
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002613 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002614 if (start < 0)
2615 return start;
2616
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002617 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2618 GFP_KERNEL);
2619
Thomas Graf34ae9322015-07-21 10:44:03 +02002620 if (!tun_dst)
2621 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002622
Paolo Abenid71785f2016-02-12 15:43:57 +01002623 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2624 if (err) {
2625 dst_release((struct dst_entry *)tun_dst);
2626 return err;
2627 }
2628
Thomas Graf34ae9322015-07-21 10:44:03 +02002629 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2630 sizeof(*ovs_tun), log);
2631 if (IS_ERR(a)) {
2632 dst_release((struct dst_entry *)tun_dst);
2633 return PTR_ERR(a);
2634 }
2635
2636 ovs_tun = nla_data(a);
2637 ovs_tun->tun_dst = tun_dst;
2638
2639 tun_info = &tun_dst->u.tun_info;
2640 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002641 if (key.tun_proto == AF_INET6)
2642 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
wenxu18b6f712019-03-28 12:43:23 +08002643 else if (key.tun_proto == AF_INET && key.tun_key.u.ipv4.dst == 0)
2644 tun_info->mode |= IP_TUNNEL_INFO_BRIDGE;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002645 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002646
Pravin B Shelar4c222792015-08-30 18:09:38 -07002647 /* We need to store the options in the action itself since
2648 * everything else will go away after flow setup. We can append
2649 * it to tun_info and then point there.
2650 */
2651 ip_tunnel_info_opts_set(tun_info,
2652 TUN_METADATA_OPTS(&key, key.tun_opts_len),
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -07002653 key.tun_opts_len, dst_opt_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002654 add_nested_action_end(*sfa, start);
2655
2656 return err;
2657}
2658
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002659static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2660 bool is_push_nsh, bool log)
2661{
2662 struct sw_flow_match match;
2663 struct sw_flow_key key;
2664 int ret = 0;
2665
2666 ovs_match_init(&match, &key, true, NULL);
2667 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2668 is_push_nsh, log);
2669 return !ret;
2670}
2671
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002672/* Return false if there are any non-masked bits set.
2673 * Mask follows data immediately, before any netlink padding.
2674 */
2675static bool validate_masked(u8 *data, int len)
2676{
2677 u8 *mask = data + len;
2678
2679 while (len--)
2680 if (*data++ & ~*mask++)
2681 return false;
2682
2683 return true;
2684}
2685
Pravin B Shelare6445712013-10-03 18:16:47 -07002686static int validate_set(const struct nlattr *a,
2687 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002688 struct sw_flow_actions **sfa, bool *skip_copy,
2689 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002690{
2691 const struct nlattr *ovs_key = nla_data(a);
2692 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002693 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002694
2695 /* There can be only one key in a action */
2696 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2697 return -EINVAL;
2698
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002699 key_len = nla_len(ovs_key);
2700 if (masked)
2701 key_len /= 2;
2702
Pravin B Shelare6445712013-10-03 18:16:47 -07002703 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002704 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002705 return -EINVAL;
2706
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002707 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2708 return -EINVAL;
2709
Pravin B Shelare6445712013-10-03 18:16:47 -07002710 switch (key_type) {
2711 const struct ovs_key_ipv4 *ipv4_key;
2712 const struct ovs_key_ipv6 *ipv6_key;
2713 int err;
2714
2715 case OVS_KEY_ATTR_PRIORITY:
2716 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002717 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002718 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002719 break;
2720
Jiri Benc0a6410f2016-11-10 16:28:22 +01002721 case OVS_KEY_ATTR_ETHERNET:
2722 if (mac_proto != MAC_PROTO_ETHERNET)
2723 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002724 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002725
Pravin B Shelare6445712013-10-03 18:16:47 -07002726 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002727 if (masked)
2728 return -EINVAL; /* Masked tunnel set not supported. */
2729
2730 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002731 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002732 if (err)
2733 return err;
2734 break;
2735
2736 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002737 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002738 return -EINVAL;
2739
Pravin B Shelare6445712013-10-03 18:16:47 -07002740 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002741
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002742 if (masked) {
2743 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002744
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002745 /* Non-writeable fields. */
2746 if (mask->ipv4_proto || mask->ipv4_frag)
2747 return -EINVAL;
2748 } else {
2749 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2750 return -EINVAL;
2751
2752 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2753 return -EINVAL;
2754 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002755 break;
2756
2757 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002758 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002759 return -EINVAL;
2760
Pravin B Shelare6445712013-10-03 18:16:47 -07002761 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002762
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002763 if (masked) {
2764 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002765
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002766 /* Non-writeable fields. */
2767 if (mask->ipv6_proto || mask->ipv6_frag)
2768 return -EINVAL;
2769
2770 /* Invalid bits in the flow label mask? */
2771 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2772 return -EINVAL;
2773 } else {
2774 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2775 return -EINVAL;
2776
2777 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2778 return -EINVAL;
2779 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002780 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2781 return -EINVAL;
2782
2783 break;
2784
2785 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002786 if ((eth_type != htons(ETH_P_IP) &&
2787 eth_type != htons(ETH_P_IPV6)) ||
2788 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002789 return -EINVAL;
2790
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002791 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002792
2793 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002794 if ((eth_type != htons(ETH_P_IP) &&
2795 eth_type != htons(ETH_P_IPV6)) ||
2796 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002797 return -EINVAL;
2798
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002799 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002800
2801 case OVS_KEY_ATTR_MPLS:
2802 if (!eth_p_mpls(eth_type))
2803 return -EINVAL;
2804 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002805
2806 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002807 if ((eth_type != htons(ETH_P_IP) &&
2808 eth_type != htons(ETH_P_IPV6)) ||
2809 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002810 return -EINVAL;
2811
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002812 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002813
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002814 case OVS_KEY_ATTR_NSH:
2815 if (eth_type != htons(ETH_P_NSH))
2816 return -EINVAL;
2817 if (!validate_nsh(nla_data(a), masked, false, log))
2818 return -EINVAL;
2819 break;
2820
Pravin B Shelare6445712013-10-03 18:16:47 -07002821 default:
2822 return -EINVAL;
2823 }
2824
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002825 /* Convert non-masked non-tunnel set actions to masked set actions. */
2826 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2827 int start, len = key_len * 2;
2828 struct nlattr *at;
2829
2830 *skip_copy = true;
2831
2832 start = add_nested_action_start(sfa,
2833 OVS_ACTION_ATTR_SET_TO_MASKED,
2834 log);
2835 if (start < 0)
2836 return start;
2837
2838 at = __add_action(sfa, key_type, NULL, len, log);
2839 if (IS_ERR(at))
2840 return PTR_ERR(at);
2841
2842 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2843 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2844 /* Clear non-writeable bits from otherwise writeable fields. */
2845 if (key_type == OVS_KEY_ATTR_IPV6) {
2846 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2847
2848 mask->ipv6_label &= htonl(0x000FFFFF);
2849 }
2850 add_nested_action_end(*sfa, start);
2851 }
2852
Pravin B Shelare6445712013-10-03 18:16:47 -07002853 return 0;
2854}
2855
2856static int validate_userspace(const struct nlattr *attr)
2857{
2858 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2859 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2860 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002861 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002862 };
2863 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2864 int error;
2865
Johannes Berg8cb08172019-04-26 14:07:28 +02002866 error = nla_parse_nested_deprecated(a, OVS_USERSPACE_ATTR_MAX, attr,
2867 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002868 if (error)
2869 return error;
2870
2871 if (!a[OVS_USERSPACE_ATTR_PID] ||
2872 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2873 return -EINVAL;
2874
2875 return 0;
2876}
2877
Numan Siddique4d5ec892019-03-26 06:13:46 +05302878static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
2879 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
2880 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
2881 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
2882};
2883
2884static int validate_and_copy_check_pkt_len(struct net *net,
2885 const struct nlattr *attr,
2886 const struct sw_flow_key *key,
2887 struct sw_flow_actions **sfa,
2888 __be16 eth_type, __be16 vlan_tci,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302889 u32 mpls_label_count,
Numan Siddique4d5ec892019-03-26 06:13:46 +05302890 bool log, bool last)
2891{
2892 const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
2893 struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
2894 struct check_pkt_len_arg arg;
2895 int nested_acts_start;
2896 int start, err;
2897
Johannes Berg8cb08172019-04-26 14:07:28 +02002898 err = nla_parse_deprecated_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX,
2899 nla_data(attr), nla_len(attr),
2900 cpl_policy, NULL);
Numan Siddique4d5ec892019-03-26 06:13:46 +05302901 if (err)
2902 return err;
2903
2904 if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
2905 !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
2906 return -EINVAL;
2907
2908 acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
2909 acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
2910
2911 /* Both the nested action should be present. */
2912 if (!acts_if_greater || !acts_if_lesser_eq)
2913 return -EINVAL;
2914
2915 /* validation done, copy the nested actions. */
2916 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
2917 log);
2918 if (start < 0)
2919 return start;
2920
2921 arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
2922 arg.exec_for_lesser_equal =
2923 last || !actions_may_change_flow(acts_if_lesser_eq);
2924 arg.exec_for_greater =
2925 last || !actions_may_change_flow(acts_if_greater);
2926
2927 err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
2928 sizeof(arg), log);
2929 if (err)
2930 return err;
2931
2932 nested_acts_start = add_nested_action_start(sfa,
2933 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
2934 if (nested_acts_start < 0)
2935 return nested_acts_start;
2936
2937 err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302938 eth_type, vlan_tci, mpls_label_count, log);
Numan Siddique4d5ec892019-03-26 06:13:46 +05302939
2940 if (err)
2941 return err;
2942
2943 add_nested_action_end(*sfa, nested_acts_start);
2944
2945 nested_acts_start = add_nested_action_start(sfa,
2946 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
2947 if (nested_acts_start < 0)
2948 return nested_acts_start;
2949
2950 err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302951 eth_type, vlan_tci, mpls_label_count, log);
Numan Siddique4d5ec892019-03-26 06:13:46 +05302952
2953 if (err)
2954 return err;
2955
2956 add_nested_action_end(*sfa, nested_acts_start);
2957 add_nested_action_end(*sfa, start);
2958 return 0;
2959}
2960
Pravin B Shelare6445712013-10-03 18:16:47 -07002961static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002962 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002963{
2964 int totlen = NLA_ALIGN(from->nla_len);
2965 struct nlattr *to;
2966
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002967 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002968 if (IS_ERR(to))
2969 return PTR_ERR(to);
2970
2971 memcpy(to, from, totlen);
2972 return 0;
2973}
2974
Joe Stringer7f8a4362015-08-26 11:31:48 -07002975static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002976 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002977 struct sw_flow_actions **sfa,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05302978 __be16 eth_type, __be16 vlan_tci,
2979 u32 mpls_label_count, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002980{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002981 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002982 const struct nlattr *a;
2983 int rem, err;
2984
Pravin B Shelare6445712013-10-03 18:16:47 -07002985 nla_for_each_nested(a, attr, rem) {
2986 /* Expected argument lengths, (u32)-1 for variable length. */
2987 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2988 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002989 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002990 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002991 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2992 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002993 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2994 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2995 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002996 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002997 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002998 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2999 [OVS_ACTION_ATTR_CT] = (u32)-1,
Eric Garverb8226962017-10-10 16:54:44 -04003000 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
William Tuf2a4d082016-06-10 11:49:33 -07003001 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01003002 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
3003 [OVS_ACTION_ATTR_POP_ETH] = 0,
Yi Yangb2d0f5d2017-11-07 21:07:02 +08003004 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
3005 [OVS_ACTION_ATTR_POP_NSH] = 0,
Andy Zhoucd8a6c32017-11-10 12:09:43 -08003006 [OVS_ACTION_ATTR_METER] = sizeof(u32),
Yifeng Sunb2335042018-07-02 08:18:03 -07003007 [OVS_ACTION_ATTR_CLONE] = (u32)-1,
Numan Siddique4d5ec892019-03-26 06:13:46 +05303008 [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
Martin Varghesef66b53f2019-12-21 08:50:46 +05303009 [OVS_ACTION_ATTR_ADD_MPLS] = sizeof(struct ovs_action_add_mpls),
Pravin B Shelare6445712013-10-03 18:16:47 -07003010 };
3011 const struct ovs_action_push_vlan *vlan;
3012 int type = nla_type(a);
3013 bool skip_copy;
3014
3015 if (type > OVS_ACTION_ATTR_MAX ||
3016 (action_lens[type] != nla_len(a) &&
3017 action_lens[type] != (u32)-1))
3018 return -EINVAL;
3019
3020 skip_copy = false;
3021 switch (type) {
3022 case OVS_ACTION_ATTR_UNSPEC:
3023 return -EINVAL;
3024
3025 case OVS_ACTION_ATTR_USERSPACE:
3026 err = validate_userspace(a);
3027 if (err)
3028 return err;
3029 break;
3030
3031 case OVS_ACTION_ATTR_OUTPUT:
3032 if (nla_get_u32(a) >= DP_MAX_PORTS)
3033 return -EINVAL;
3034 break;
3035
William Tuf2a4d082016-06-10 11:49:33 -07003036 case OVS_ACTION_ATTR_TRUNC: {
3037 const struct ovs_action_trunc *trunc = nla_data(a);
3038
3039 if (trunc->max_len < ETH_HLEN)
3040 return -EINVAL;
3041 break;
3042 }
3043
Andy Zhou971427f32014-09-15 19:37:25 -07003044 case OVS_ACTION_ATTR_HASH: {
3045 const struct ovs_action_hash *act_hash = nla_data(a);
3046
3047 switch (act_hash->hash_alg) {
3048 case OVS_HASH_ALG_L4:
3049 break;
3050 default:
3051 return -EINVAL;
3052 }
3053
3054 break;
3055 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003056
3057 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003058 if (mac_proto != MAC_PROTO_ETHERNET)
3059 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003060 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07003061 break;
3062
3063 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01003064 if (mac_proto != MAC_PROTO_ETHERNET)
3065 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07003066 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04003067 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07003068 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003069 if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
Pravin B Shelare6445712013-10-03 18:16:47 -07003070 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003071 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07003072 break;
3073
Andy Zhou971427f32014-09-15 19:37:25 -07003074 case OVS_ACTION_ATTR_RECIRC:
3075 break;
3076
Martin Varghesef66b53f2019-12-21 08:50:46 +05303077 case OVS_ACTION_ATTR_ADD_MPLS: {
3078 const struct ovs_action_add_mpls *mpls = nla_data(a);
3079
3080 if (!eth_p_mpls(mpls->mpls_ethertype))
3081 return -EINVAL;
3082
3083 if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK) {
3084 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3085 (eth_type != htons(ETH_P_IP) &&
3086 eth_type != htons(ETH_P_IPV6) &&
3087 eth_type != htons(ETH_P_ARP) &&
3088 eth_type != htons(ETH_P_RARP) &&
3089 !eth_p_mpls(eth_type)))
3090 return -EINVAL;
3091 mpls_label_count++;
3092 } else {
3093 if (mac_proto == MAC_PROTO_ETHERNET) {
3094 mpls_label_count = 1;
3095 mac_proto = MAC_PROTO_NONE;
3096 } else {
3097 mpls_label_count++;
3098 }
3099 }
3100 eth_type = mpls->mpls_ethertype;
3101 break;
3102 }
3103
Simon Horman25cd9ba2014-10-06 05:05:13 -07003104 case OVS_ACTION_ATTR_PUSH_MPLS: {
3105 const struct ovs_action_push_mpls *mpls = nla_data(a);
3106
Simon Horman25cd9ba2014-10-06 05:05:13 -07003107 if (!eth_p_mpls(mpls->mpls_ethertype))
3108 return -EINVAL;
3109 /* Prohibit push MPLS other than to a white list
3110 * for packets that have a known tag order.
3111 */
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003112 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003113 (eth_type != htons(ETH_P_IP) &&
3114 eth_type != htons(ETH_P_IPV6) &&
3115 eth_type != htons(ETH_P_ARP) &&
3116 eth_type != htons(ETH_P_RARP) &&
3117 !eth_p_mpls(eth_type)))
3118 return -EINVAL;
3119 eth_type = mpls->mpls_ethertype;
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303120 mpls_label_count++;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003121 break;
3122 }
3123
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303124 case OVS_ACTION_ATTR_POP_MPLS: {
3125 __be16 proto;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003126 if (vlan_tci & htons(VLAN_CFI_MASK) ||
Simon Horman25cd9ba2014-10-06 05:05:13 -07003127 !eth_p_mpls(eth_type))
3128 return -EINVAL;
3129
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303130 /* Disallow subsequent L2.5+ set actions and mpls_pop
3131 * actions once the last MPLS label in the packet is
3132 * is popped as there is no check here to ensure that
3133 * the new eth type is valid and thus set actions could
3134 * write off the end of the packet or otherwise corrupt
3135 * it.
Simon Horman25cd9ba2014-10-06 05:05:13 -07003136 *
3137 * Support for these actions is planned using packet
3138 * recirculation.
3139 */
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303140 proto = nla_get_be16(a);
Martin Varghesef66b53f2019-12-21 08:50:46 +05303141
3142 if (proto == htons(ETH_P_TEB) &&
3143 mac_proto != MAC_PROTO_NONE)
3144 return -EINVAL;
3145
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303146 mpls_label_count--;
3147
3148 if (!eth_p_mpls(proto) || !mpls_label_count)
3149 eth_type = htons(0);
3150 else
3151 eth_type = proto;
3152
Simon Horman25cd9ba2014-10-06 05:05:13 -07003153 break;
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303154 }
Simon Horman25cd9ba2014-10-06 05:05:13 -07003155
Pravin B Shelare6445712013-10-03 18:16:47 -07003156 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07003157 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003158 &skip_copy, mac_proto, eth_type,
3159 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003160 if (err)
3161 return err;
3162 break;
3163
3164 case OVS_ACTION_ATTR_SET_MASKED:
3165 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01003166 &skip_copy, mac_proto, eth_type,
3167 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003168 if (err)
3169 return err;
3170 break;
3171
andy zhou798c1662017-03-20 16:32:29 -07003172 case OVS_ACTION_ATTR_SAMPLE: {
3173 bool last = nla_is_last(a, rem);
3174
3175 err = validate_and_copy_sample(net, a, key, sfa,
3176 eth_type, vlan_tci,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303177 mpls_label_count,
andy zhou798c1662017-03-20 16:32:29 -07003178 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07003179 if (err)
3180 return err;
3181 skip_copy = true;
3182 break;
andy zhou798c1662017-03-20 16:32:29 -07003183 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003184
Joe Stringer7f8a4362015-08-26 11:31:48 -07003185 case OVS_ACTION_ATTR_CT:
3186 err = ovs_ct_copy_action(net, a, key, sfa, log);
3187 if (err)
3188 return err;
3189 skip_copy = true;
3190 break;
3191
Eric Garverb8226962017-10-10 16:54:44 -04003192 case OVS_ACTION_ATTR_CT_CLEAR:
3193 break;
3194
Jiri Benc91820da2016-11-10 16:28:23 +01003195 case OVS_ACTION_ATTR_PUSH_ETH:
3196 /* Disallow pushing an Ethernet header if one
3197 * is already present */
3198 if (mac_proto != MAC_PROTO_NONE)
3199 return -EINVAL;
Jaime Caamaño Ruiz46ebe282018-10-31 18:52:03 +01003200 mac_proto = MAC_PROTO_ETHERNET;
Jiri Benc91820da2016-11-10 16:28:23 +01003201 break;
3202
3203 case OVS_ACTION_ATTR_POP_ETH:
3204 if (mac_proto != MAC_PROTO_ETHERNET)
3205 return -EINVAL;
Michał Mirosław9df46ae2018-11-08 18:44:50 +01003206 if (vlan_tci & htons(VLAN_CFI_MASK))
Jiri Benc91820da2016-11-10 16:28:23 +01003207 return -EINVAL;
Jaime Caamaño Ruiz46ebe282018-10-31 18:52:03 +01003208 mac_proto = MAC_PROTO_NONE;
Jiri Benc91820da2016-11-10 16:28:23 +01003209 break;
3210
Yi Yangb2d0f5d2017-11-07 21:07:02 +08003211 case OVS_ACTION_ATTR_PUSH_NSH:
3212 if (mac_proto != MAC_PROTO_ETHERNET) {
3213 u8 next_proto;
3214
3215 next_proto = tun_p_from_eth_p(eth_type);
3216 if (!next_proto)
3217 return -EINVAL;
3218 }
3219 mac_proto = MAC_PROTO_NONE;
3220 if (!validate_nsh(nla_data(a), false, true, true))
3221 return -EINVAL;
3222 break;
3223
3224 case OVS_ACTION_ATTR_POP_NSH: {
3225 __be16 inner_proto;
3226
3227 if (eth_type != htons(ETH_P_NSH))
3228 return -EINVAL;
3229 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
3230 if (!inner_proto)
3231 return -EINVAL;
3232 if (key->nsh.base.np == TUN_P_ETHERNET)
3233 mac_proto = MAC_PROTO_ETHERNET;
3234 else
3235 mac_proto = MAC_PROTO_NONE;
3236 break;
3237 }
3238
Andy Zhoucd8a6c32017-11-10 12:09:43 -08003239 case OVS_ACTION_ATTR_METER:
3240 /* Non-existent meters are simply ignored. */
3241 break;
3242
Yifeng Sunb2335042018-07-02 08:18:03 -07003243 case OVS_ACTION_ATTR_CLONE: {
3244 bool last = nla_is_last(a, rem);
3245
3246 err = validate_and_copy_clone(net, a, key, sfa,
3247 eth_type, vlan_tci,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303248 mpls_label_count,
Yifeng Sunb2335042018-07-02 08:18:03 -07003249 log, last);
3250 if (err)
3251 return err;
3252 skip_copy = true;
3253 break;
3254 }
3255
Numan Siddique4d5ec892019-03-26 06:13:46 +05303256 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
3257 bool last = nla_is_last(a, rem);
3258
3259 err = validate_and_copy_check_pkt_len(net, a, key, sfa,
3260 eth_type,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303261 vlan_tci,
3262 mpls_label_count,
3263 log, last);
Numan Siddique4d5ec892019-03-26 06:13:46 +05303264 if (err)
3265 return err;
3266 skip_copy = true;
3267 break;
3268 }
3269
Pravin B Shelare6445712013-10-03 18:16:47 -07003270 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003271 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07003272 return -EINVAL;
3273 }
3274 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003275 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003276 if (err)
3277 return err;
3278 }
3279 }
3280
3281 if (rem > 0)
3282 return -EINVAL;
3283
3284 return 0;
3285}
3286
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003287/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07003288int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07003289 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003290 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07003291{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003292 int err;
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303293 u32 mpls_label_count = 0;
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003294
zhangliping67c8d222017-11-25 22:02:12 +08003295 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003296 if (IS_ERR(*sfa))
3297 return PTR_ERR(*sfa);
3298
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303299 if (eth_p_mpls(key->eth.type))
3300 mpls_label_count = hweight_long(key->mpls.num_labels_mask);
3301
Joe Stringer8e2fed12015-08-26 11:31:44 -07003302 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07003303 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Martin Varghesefbdcdd72019-11-04 07:27:44 +05303304 key->eth.vlan.tci, mpls_label_count, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003305 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02003306 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003307
3308 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003309}
3310
andy zhou798c1662017-03-20 16:32:29 -07003311static int sample_action_to_attr(const struct nlattr *attr,
3312 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07003313{
andy zhou798c1662017-03-20 16:32:29 -07003314 struct nlattr *start, *ac_start = NULL, *sample_arg;
3315 int err = 0, rem = nla_len(attr);
3316 const struct sample_arg *arg;
3317 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07003318
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003319 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SAMPLE);
Pravin B Shelare6445712013-10-03 18:16:47 -07003320 if (!start)
3321 return -EMSGSIZE;
3322
andy zhou798c1662017-03-20 16:32:29 -07003323 sample_arg = nla_data(attr);
3324 arg = nla_data(sample_arg);
3325 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07003326
andy zhou798c1662017-03-20 16:32:29 -07003327 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3328 err = -EMSGSIZE;
3329 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07003330 }
3331
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003332 ac_start = nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS);
andy zhou798c1662017-03-20 16:32:29 -07003333 if (!ac_start) {
3334 err = -EMSGSIZE;
3335 goto out;
3336 }
3337
3338 err = ovs_nla_put_actions(actions, rem, skb);
3339
3340out:
3341 if (err) {
3342 nla_nest_cancel(skb, ac_start);
3343 nla_nest_cancel(skb, start);
3344 } else {
3345 nla_nest_end(skb, ac_start);
3346 nla_nest_end(skb, start);
3347 }
3348
Pravin B Shelare6445712013-10-03 18:16:47 -07003349 return err;
3350}
3351
Yifeng Sunb2335042018-07-02 08:18:03 -07003352static int clone_action_to_attr(const struct nlattr *attr,
3353 struct sk_buff *skb)
3354{
3355 struct nlattr *start;
3356 int err = 0, rem = nla_len(attr);
3357
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003358 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CLONE);
Yifeng Sunb2335042018-07-02 08:18:03 -07003359 if (!start)
3360 return -EMSGSIZE;
3361
3362 err = ovs_nla_put_actions(nla_data(attr), rem, skb);
3363
3364 if (err)
3365 nla_nest_cancel(skb, start);
3366 else
3367 nla_nest_end(skb, start);
3368
3369 return err;
3370}
3371
Numan Siddique4d5ec892019-03-26 06:13:46 +05303372static int check_pkt_len_action_to_attr(const struct nlattr *attr,
3373 struct sk_buff *skb)
3374{
3375 struct nlattr *start, *ac_start = NULL;
3376 const struct check_pkt_len_arg *arg;
3377 const struct nlattr *a, *cpl_arg;
3378 int err = 0, rem = nla_len(attr);
3379
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003380 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
Numan Siddique4d5ec892019-03-26 06:13:46 +05303381 if (!start)
3382 return -EMSGSIZE;
3383
3384 /* The first nested attribute in 'attr' is always
3385 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3386 */
3387 cpl_arg = nla_data(attr);
3388 arg = nla_data(cpl_arg);
3389
3390 if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
3391 err = -EMSGSIZE;
3392 goto out;
3393 }
3394
3395 /* Second nested attribute in 'attr' is always
3396 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3397 */
3398 a = nla_next(cpl_arg, &rem);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003399 ac_start = nla_nest_start_noflag(skb,
3400 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
Numan Siddique4d5ec892019-03-26 06:13:46 +05303401 if (!ac_start) {
3402 err = -EMSGSIZE;
3403 goto out;
3404 }
3405
3406 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3407 if (err) {
3408 nla_nest_cancel(skb, ac_start);
3409 goto out;
3410 } else {
3411 nla_nest_end(skb, ac_start);
3412 }
3413
3414 /* Third nested attribute in 'attr' is always
3415 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3416 */
3417 a = nla_next(a, &rem);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003418 ac_start = nla_nest_start_noflag(skb,
3419 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
Numan Siddique4d5ec892019-03-26 06:13:46 +05303420 if (!ac_start) {
3421 err = -EMSGSIZE;
3422 goto out;
3423 }
3424
3425 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3426 if (err) {
3427 nla_nest_cancel(skb, ac_start);
3428 goto out;
3429 } else {
3430 nla_nest_end(skb, ac_start);
3431 }
3432
3433 nla_nest_end(skb, start);
3434 return 0;
3435
3436out:
3437 nla_nest_cancel(skb, start);
3438 return err;
3439}
3440
Pravin B Shelare6445712013-10-03 18:16:47 -07003441static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3442{
3443 const struct nlattr *ovs_key = nla_data(a);
3444 int key_type = nla_type(ovs_key);
3445 struct nlattr *start;
3446 int err;
3447
3448 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07003449 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02003450 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3451 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003452
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003453 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
Pravin B Shelare6445712013-10-03 18:16:47 -07003454 if (!start)
3455 return -EMSGSIZE;
3456
Simon Hormane905eab2015-12-18 19:43:15 +09003457 err = ip_tun_to_nlattr(skb, &tun_info->key,
3458 ip_tunnel_info_opts(tun_info),
3459 tun_info->options_len,
wenxu18b6f712019-03-28 12:43:23 +08003460 ip_tunnel_info_af(tun_info), tun_info->mode);
Pravin B Shelare6445712013-10-03 18:16:47 -07003461 if (err)
3462 return err;
3463 nla_nest_end(skb, start);
3464 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003465 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003466 default:
3467 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3468 return -EMSGSIZE;
3469 break;
3470 }
3471
3472 return 0;
3473}
3474
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003475static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3476 struct sk_buff *skb)
3477{
3478 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08003479 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003480 size_t key_len = nla_len(ovs_key) / 2;
3481
3482 /* Revert the conversion we did from a non-masked set action to
3483 * masked set action.
3484 */
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003485 nla = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
Joe Stringerf4f8e732015-03-02 18:49:56 -08003486 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003487 return -EMSGSIZE;
3488
Joe Stringerf4f8e732015-03-02 18:49:56 -08003489 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3490 return -EMSGSIZE;
3491
3492 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003493 return 0;
3494}
3495
Pravin B Shelare6445712013-10-03 18:16:47 -07003496int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3497{
3498 const struct nlattr *a;
3499 int rem, err;
3500
3501 nla_for_each_attr(a, attr, len, rem) {
3502 int type = nla_type(a);
3503
3504 switch (type) {
3505 case OVS_ACTION_ATTR_SET:
3506 err = set_action_to_attr(a, skb);
3507 if (err)
3508 return err;
3509 break;
3510
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003511 case OVS_ACTION_ATTR_SET_TO_MASKED:
3512 err = masked_set_action_to_set_action_attr(a, skb);
3513 if (err)
3514 return err;
3515 break;
3516
Pravin B Shelare6445712013-10-03 18:16:47 -07003517 case OVS_ACTION_ATTR_SAMPLE:
3518 err = sample_action_to_attr(a, skb);
3519 if (err)
3520 return err;
3521 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07003522
3523 case OVS_ACTION_ATTR_CT:
3524 err = ovs_ct_action_to_attr(nla_data(a), skb);
3525 if (err)
3526 return err;
3527 break;
3528
Yifeng Sunb2335042018-07-02 08:18:03 -07003529 case OVS_ACTION_ATTR_CLONE:
3530 err = clone_action_to_attr(a, skb);
3531 if (err)
3532 return err;
3533 break;
3534
Numan Siddique4d5ec892019-03-26 06:13:46 +05303535 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
3536 err = check_pkt_len_action_to_attr(a, skb);
3537 if (err)
3538 return err;
3539 break;
3540
Pravin B Shelare6445712013-10-03 18:16:47 -07003541 default:
3542 if (nla_put(skb, type, nla_len(a), nla_data(a)))
3543 return -EMSGSIZE;
3544 break;
3545 }
3546 }
3547
3548 return 0;
3549}