blob: 624ea74353dd3ba403ccedc822f1c9574982e709 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
andy zhou798c1662017-03-20 16:32:29 -07002 * Copyright (c) 2007-2017 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
Joe Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#include "flow.h"
22#include "datapath.h"
23#include <linux/uaccess.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <net/llc_pdu.h>
29#include <linux/kernel.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/llc.h>
33#include <linux/module.h>
34#include <linux/in.h>
35#include <linux/rcupdate.h>
36#include <linux/if_arp.h>
37#include <linux/ip.h>
38#include <linux/ipv6.h>
39#include <linux/sctp.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
43#include <linux/icmpv6.h>
44#include <linux/rculist.h>
Jesse Grossf5796682014-10-03 15:35:33 -070045#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070046#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070049#include <net/mpls.h>
Thomas Graf614732e2015-07-21 10:44:06 +020050#include <net/vxlan.h>
Yi Yangb2d0f5d2017-11-07 21:07:02 +080051#include <net/tun_proto.h>
William Tuceaa0012017-10-04 17:03:12 -070052#include <net/erspan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070053
54#include "flow_netlink.h"
55
Thomas Graf81bfe3c2015-01-15 03:53:58 +010056struct ovs_len_tbl {
57 int len;
58 const struct ovs_len_tbl *next;
59};
60
61#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070062#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c2015-01-15 03:53:58 +010063
andy zhou798c1662017-03-20 16:32:29 -070064static bool actions_may_change_flow(const struct nlattr *actions)
65{
66 struct nlattr *nla;
67 int rem;
68
69 nla_for_each_nested(nla, actions, rem) {
70 u16 action = nla_type(nla);
71
72 switch (action) {
73 case OVS_ACTION_ATTR_OUTPUT:
74 case OVS_ACTION_ATTR_RECIRC:
75 case OVS_ACTION_ATTR_TRUNC:
76 case OVS_ACTION_ATTR_USERSPACE:
77 break;
78
79 case OVS_ACTION_ATTR_CT:
Eric Garverb8226962017-10-10 16:54:44 -040080 case OVS_ACTION_ATTR_CT_CLEAR:
andy zhou798c1662017-03-20 16:32:29 -070081 case OVS_ACTION_ATTR_HASH:
82 case OVS_ACTION_ATTR_POP_ETH:
83 case OVS_ACTION_ATTR_POP_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080084 case OVS_ACTION_ATTR_POP_NSH:
andy zhou798c1662017-03-20 16:32:29 -070085 case OVS_ACTION_ATTR_POP_VLAN:
86 case OVS_ACTION_ATTR_PUSH_ETH:
87 case OVS_ACTION_ATTR_PUSH_MPLS:
Yi Yangb2d0f5d2017-11-07 21:07:02 +080088 case OVS_ACTION_ATTR_PUSH_NSH:
andy zhou798c1662017-03-20 16:32:29 -070089 case OVS_ACTION_ATTR_PUSH_VLAN:
90 case OVS_ACTION_ATTR_SAMPLE:
91 case OVS_ACTION_ATTR_SET:
92 case OVS_ACTION_ATTR_SET_MASKED:
Andy Zhoucd8a6c32017-11-10 12:09:43 -080093 case OVS_ACTION_ATTR_METER:
andy zhou798c1662017-03-20 16:32:29 -070094 default:
95 return true;
96 }
97 }
98 return false;
99}
100
Pravin B Shelara85311b2014-10-19 12:03:40 -0700101static void update_range(struct sw_flow_match *match,
102 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700103{
Pravin B Shelara85311b2014-10-19 12:03:40 -0700104 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -0700105 size_t start = rounddown(offset, sizeof(long));
106 size_t end = roundup(offset + size, sizeof(long));
107
108 if (!is_mask)
109 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -0700110 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700111 range = &match->mask->range;
112
Pravin B Shelare6445712013-10-03 18:16:47 -0700113 if (range->start == range->end) {
114 range->start = start;
115 range->end = end;
116 return;
117 }
118
119 if (range->start > start)
120 range->start = start;
121
122 if (range->end < end)
123 range->end = end;
124}
125
126#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
127 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700128 update_range(match, offsetof(struct sw_flow_key, field), \
129 sizeof((match)->key->field), is_mask); \
130 if (is_mask) \
131 (match)->mask->key.field = value; \
132 else \
Pravin B Shelare6445712013-10-03 18:16:47 -0700133 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700134 } while (0)
135
Jesse Grossf5796682014-10-03 15:35:33 -0700136#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
137 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700138 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700139 if (is_mask) \
140 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700141 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700142 else \
143 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700144 } while (0)
145
Jesse Grossf5796682014-10-03 15:35:33 -0700146#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
147 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
148 value_p, len, is_mask)
149
Pravin B Shelara85311b2014-10-19 12:03:40 -0700150#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
151 do { \
152 update_range(match, offsetof(struct sw_flow_key, field), \
153 sizeof((match)->key->field), is_mask); \
154 if (is_mask) \
155 memset((u8 *)&(match)->mask->key.field, value, \
156 sizeof((match)->mask->key.field)); \
157 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700158 memset((u8 *)&(match)->key->field, value, \
159 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700160 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700161
162static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800163 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700164{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100165 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700166 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
167
168 /* The following mask attributes allowed only if they
169 * pass the validation tests. */
170 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800171 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700172 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800173 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700174 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700175 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700176 | (1 << OVS_KEY_ATTR_UDP)
177 | (1 << OVS_KEY_ATTR_SCTP)
178 | (1 << OVS_KEY_ATTR_ICMP)
179 | (1 << OVS_KEY_ATTR_ICMPV6)
180 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700181 | (1 << OVS_KEY_ATTR_ND)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800182 | (1 << OVS_KEY_ATTR_MPLS)
183 | (1 << OVS_KEY_ATTR_NSH));
Pravin B Shelare6445712013-10-03 18:16:47 -0700184
185 /* Always allowed mask fields. */
186 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
187 | (1 << OVS_KEY_ATTR_IN_PORT)
188 | (1 << OVS_KEY_ATTR_ETHERTYPE));
189
190 /* Check key attributes. */
191 if (match->key->eth.type == htons(ETH_P_ARP)
192 || match->key->eth.type == htons(ETH_P_RARP)) {
193 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800194 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700195 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
196 }
197
Simon Horman25cd9ba2014-10-06 05:05:13 -0700198 if (eth_p_mpls(match->key->eth.type)) {
199 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
200 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
201 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
202 }
203
Pravin B Shelare6445712013-10-03 18:16:47 -0700204 if (match->key->eth.type == htons(ETH_P_IP)) {
205 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800206 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700207 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800208 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
209 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700210
211 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
212 if (match->key->ip.proto == IPPROTO_UDP) {
213 key_expected |= 1 << OVS_KEY_ATTR_UDP;
214 if (match->mask && (match->mask->key.ip.proto == 0xff))
215 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
216 }
217
218 if (match->key->ip.proto == IPPROTO_SCTP) {
219 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
220 if (match->mask && (match->mask->key.ip.proto == 0xff))
221 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
222 }
223
224 if (match->key->ip.proto == IPPROTO_TCP) {
225 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700226 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
227 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700228 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700229 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
230 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700231 }
232
233 if (match->key->ip.proto == IPPROTO_ICMP) {
234 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
235 if (match->mask && (match->mask->key.ip.proto == 0xff))
236 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
237 }
238 }
239 }
240
241 if (match->key->eth.type == htons(ETH_P_IPV6)) {
242 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800243 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700244 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800245 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
246 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700247
248 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
249 if (match->key->ip.proto == IPPROTO_UDP) {
250 key_expected |= 1 << OVS_KEY_ATTR_UDP;
251 if (match->mask && (match->mask->key.ip.proto == 0xff))
252 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
253 }
254
255 if (match->key->ip.proto == IPPROTO_SCTP) {
256 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
257 if (match->mask && (match->mask->key.ip.proto == 0xff))
258 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
259 }
260
261 if (match->key->ip.proto == IPPROTO_TCP) {
262 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700263 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
264 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700265 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700266 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
267 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700268 }
269
270 if (match->key->ip.proto == IPPROTO_ICMPV6) {
271 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
272 if (match->mask && (match->mask->key.ip.proto == 0xff))
273 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
274
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700275 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700276 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700277 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700278 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800279 /* Original direction conntrack tuple
280 * uses the same space as the ND fields
281 * in the key, so both are not allowed
282 * at the same time.
283 */
284 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800285 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700286 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
287 }
288 }
289 }
290 }
291
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800292 if (match->key->eth.type == htons(ETH_P_NSH)) {
293 key_expected |= 1 << OVS_KEY_ATTR_NSH;
294 if (match->mask &&
295 match->mask->key.eth.type == htons(0xffff)) {
296 mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
297 }
298 }
299
Pravin B Shelare6445712013-10-03 18:16:47 -0700300 if ((key_attrs & key_expected) != key_expected) {
301 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800302 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
303 (unsigned long long)key_attrs,
304 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700305 return false;
306 }
307
308 if ((mask_attrs & mask_allowed) != mask_attrs) {
309 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800310 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
311 (unsigned long long)mask_attrs,
312 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700313 return false;
314 }
315
316 return true;
317}
318
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800319size_t ovs_tun_key_attr_size(void)
320{
321 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
322 * updating this function.
323 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200324 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200325 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
326 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800327 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
328 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
329 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
330 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
331 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
332 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100333 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
334 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
335 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800336 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
William Tuceaa0012017-10-04 17:03:12 -0700337 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_DST */
338 + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800339}
340
Wei Yongjun06c23512017-11-14 06:27:03 +0000341static size_t ovs_nsh_key_attr_size(void)
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800342{
343 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
344 * updating this function.
345 */
346 return nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
347 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
348 * mutually exclusive, so the bigger one can cover
349 * the small one.
350 */
351 + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
352}
353
Joe Stringer41af73e2014-10-18 16:14:14 -0700354size_t ovs_key_attr_size(void)
355{
356 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
357 * updating this function.
358 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800359 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
Joe Stringer41af73e2014-10-18 16:14:14 -0700360
361 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
362 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800363 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700364 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
365 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
366 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
367 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700368 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700369 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700370 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700371 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800372 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800373 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
374 + ovs_nsh_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700375 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
376 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
377 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
378 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
382 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
383}
384
Jesse Gross982b5272015-09-11 18:38:28 -0700385static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
386 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
387};
388
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100389static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
390 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
391 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
392 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
393 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
394 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
395 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
396 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
397 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
398 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
399 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700400 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
401 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
402 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200403 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
404 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
William Tuceaa0012017-10-04 17:03:12 -0700405 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = sizeof(u32) },
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100406};
407
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800408static const struct ovs_len_tbl
409ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
410 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
411 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
412 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
413};
414
Pravin B Shelare6445712013-10-03 18:16:47 -0700415/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100416static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
417 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
418 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
419 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
420 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
421 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
422 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
423 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
424 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
425 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
426 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
427 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
428 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
429 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
430 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
431 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
432 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
433 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
434 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
435 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
436 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
437 .next = ovs_tunnel_key_lens, },
438 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700439 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700440 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700441 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700442 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800443 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
444 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
445 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
446 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Yi Yangb2d0f5d2017-11-07 21:07:02 +0800447 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
448 .next = ovs_nsh_key_attr_lens, },
Pravin B Shelare6445712013-10-03 18:16:47 -0700449};
450
Jesse Gross982b5272015-09-11 18:38:28 -0700451static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
452{
453 return expected_len == attr_len ||
454 expected_len == OVS_ATTR_NESTED ||
455 expected_len == OVS_ATTR_VARIABLE;
456}
457
Pravin B Shelare6445712013-10-03 18:16:47 -0700458static bool is_all_zero(const u8 *fp, size_t size)
459{
460 int i;
461
462 if (!fp)
463 return false;
464
465 for (i = 0; i < size; i++)
466 if (fp[i])
467 return false;
468
469 return true;
470}
471
472static int __parse_flow_nlattrs(const struct nlattr *attr,
473 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800474 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700475{
476 const struct nlattr *nla;
477 u64 attrs;
478 int rem;
479
480 attrs = *attrsp;
481 nla_for_each_nested(nla, attr, rem) {
482 u16 type = nla_type(nla);
483 int expected_len;
484
485 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800486 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700487 type, OVS_KEY_ATTR_MAX);
488 return -EINVAL;
489 }
490
491 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800492 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700493 return -EINVAL;
494 }
495
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100496 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700497 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800498 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
499 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700500 return -EINVAL;
501 }
502
503 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
504 attrs |= 1 << type;
505 a[type] = nla;
506 }
507 }
508 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800509 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700510 return -EINVAL;
511 }
512
513 *attrsp = attrs;
514 return 0;
515}
516
517static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800518 const struct nlattr *a[], u64 *attrsp,
519 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700520{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800521 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700522}
523
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800524int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
525 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700526{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800527 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
528}
529
530static int genev_tun_opt_from_nlattr(const struct nlattr *a,
531 struct sw_flow_match *match, bool is_mask,
532 bool log)
533{
534 unsigned long opt_key_offset;
535
536 if (nla_len(a) > sizeof(match->key->tun_opts)) {
537 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
538 nla_len(a), sizeof(match->key->tun_opts));
539 return -EINVAL;
540 }
541
542 if (nla_len(a) % 4 != 0) {
543 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
544 nla_len(a));
545 return -EINVAL;
546 }
547
548 /* We need to record the length of the options passed
549 * down, otherwise packets with the same format but
550 * additional options will be silently matched.
551 */
552 if (!is_mask) {
553 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
554 false);
555 } else {
556 /* This is somewhat unusual because it looks at
557 * both the key and mask while parsing the
558 * attributes (and by extension assumes the key
559 * is parsed first). Normally, we would verify
560 * that each is the correct length and that the
561 * attributes line up in the validate function.
562 * However, that is difficult because this is
563 * variable length and we won't have the
564 * information later.
565 */
566 if (match->key->tun_opts_len != nla_len(a)) {
567 OVS_NLERR(log, "Geneve option len %d != mask len %d",
568 match->key->tun_opts_len, nla_len(a));
569 return -EINVAL;
570 }
571
572 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
573 }
574
Thomas Grafd91641d2015-01-15 03:53:57 +0100575 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800576 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
577 nla_len(a), is_mask);
578 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700579}
580
Jesse Gross982b5272015-09-11 18:38:28 -0700581static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100582 struct sw_flow_match *match, bool is_mask,
583 bool log)
584{
Jesse Gross982b5272015-09-11 18:38:28 -0700585 struct nlattr *a;
586 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100587 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200588 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100589
590 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
591
Thomas Graf1dd144c2015-01-15 03:53:59 +0100592 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700593 nla_for_each_nested(a, attr, rem) {
594 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100595
Jesse Gross982b5272015-09-11 18:38:28 -0700596 if (type > OVS_VXLAN_EXT_MAX) {
597 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
598 type, OVS_VXLAN_EXT_MAX);
599 return -EINVAL;
600 }
601
602 if (!check_attr_len(nla_len(a),
603 ovs_vxlan_ext_key_lens[type].len)) {
604 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
605 type, nla_len(a),
606 ovs_vxlan_ext_key_lens[type].len);
607 return -EINVAL;
608 }
609
610 switch (type) {
611 case OVS_VXLAN_EXT_GBP:
612 opts.gbp = nla_get_u32(a);
613 break;
614 default:
615 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
616 type);
617 return -EINVAL;
618 }
619 }
620 if (rem) {
621 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
622 rem);
623 return -EINVAL;
624 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100625
626 if (!is_mask)
627 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
628 else
629 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
630
631 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
632 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
633 is_mask);
634 return 0;
635}
636
William Tuceaa0012017-10-04 17:03:12 -0700637static int erspan_tun_opt_from_nlattr(const struct nlattr *attr,
638 struct sw_flow_match *match, bool is_mask,
639 bool log)
640{
641 unsigned long opt_key_offset;
642 struct erspan_metadata opts;
643
644 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
645
646 memset(&opts, 0, sizeof(opts));
647 opts.index = nla_get_be32(attr);
648
649 /* Index has only 20-bit */
650 if (ntohl(opts.index) & ~INDEX_MASK) {
651 OVS_NLERR(log, "ERSPAN index number %x too large.",
652 ntohl(opts.index));
653 return -EINVAL;
654 }
655
656 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), is_mask);
657 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
658 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
659 is_mask);
660
661 return 0;
662}
663
Jiri Benc6b26ba32015-10-05 13:09:47 +0200664static int ip_tun_from_nlattr(const struct nlattr *attr,
665 struct sw_flow_match *match, bool is_mask,
666 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700667{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700668 bool ttl = false, ipv4 = false, ipv6 = false;
669 __be16 tun_flags = 0;
670 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700671 struct nlattr *a;
672 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700673
674 nla_for_each_nested(a, attr, rem) {
675 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800676 int err;
677
Pravin B Shelare6445712013-10-03 18:16:47 -0700678 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800679 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
680 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700681 return -EINVAL;
682 }
683
Jesse Gross982b5272015-09-11 18:38:28 -0700684 if (!check_attr_len(nla_len(a),
685 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800686 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100687 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700688 return -EINVAL;
689 }
690
691 switch (type) {
692 case OVS_TUNNEL_KEY_ATTR_ID:
693 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
694 nla_get_be64(a), is_mask);
695 tun_flags |= TUNNEL_KEY;
696 break;
697 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200698 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200699 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200700 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 break;
702 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200703 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200704 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200705 ipv4 = true;
706 break;
707 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200708 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200709 nla_get_in6_addr(a), is_mask);
710 ipv6 = true;
711 break;
712 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
713 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
714 nla_get_in6_addr(a), is_mask);
715 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700716 break;
717 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200718 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700719 nla_get_u8(a), is_mask);
720 break;
721 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200722 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700723 nla_get_u8(a), is_mask);
724 ttl = true;
725 break;
726 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
727 tun_flags |= TUNNEL_DONT_FRAGMENT;
728 break;
729 case OVS_TUNNEL_KEY_ATTR_CSUM:
730 tun_flags |= TUNNEL_CSUM;
731 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800732 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
733 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
734 nla_get_be16(a), is_mask);
735 break;
736 case OVS_TUNNEL_KEY_ATTR_TP_DST:
737 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
738 nla_get_be16(a), is_mask);
739 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700740 case OVS_TUNNEL_KEY_ATTR_OAM:
741 tun_flags |= TUNNEL_OAM;
742 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700743 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100744 if (opts_type) {
745 OVS_NLERR(log, "Multiple metadata blocks provided");
746 return -EINVAL;
747 }
748
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800749 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
750 if (err)
751 return err;
752
Thomas Graf1dd144c2015-01-15 03:53:59 +0100753 tun_flags |= TUNNEL_GENEVE_OPT;
754 opts_type = type;
755 break;
756 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
757 if (opts_type) {
758 OVS_NLERR(log, "Multiple metadata blocks provided");
759 return -EINVAL;
760 }
761
762 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
763 if (err)
764 return err;
765
766 tun_flags |= TUNNEL_VXLAN_OPT;
767 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700768 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500769 case OVS_TUNNEL_KEY_ATTR_PAD:
770 break;
William Tuceaa0012017-10-04 17:03:12 -0700771 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
772 if (opts_type) {
773 OVS_NLERR(log, "Multiple metadata blocks provided");
774 return -EINVAL;
775 }
776
777 err = erspan_tun_opt_from_nlattr(a, match, is_mask, log);
778 if (err)
779 return err;
780
781 tun_flags |= TUNNEL_ERSPAN_OPT;
782 opts_type = type;
783 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700784 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200785 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700786 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700787 return -EINVAL;
788 }
789 }
790
791 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200792 if (is_mask)
793 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
794 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200795 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
796 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700797
798 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200799 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800800 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700801 return -EINVAL;
802 }
803
Jiri Benc6b26ba32015-10-05 13:09:47 +0200804 if (ipv4 && ipv6) {
805 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
806 return -EINVAL;
807 }
808
Pravin B Shelare6445712013-10-03 18:16:47 -0700809 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200810 if (!ipv4 && !ipv6) {
811 OVS_NLERR(log, "IP tunnel dst address not specified");
812 return -EINVAL;
813 }
814 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800815 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700816 return -EINVAL;
817 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200818 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
819 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
820 return -EINVAL;
821 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700822
823 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200824 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700825 return -EINVAL;
826 }
827 }
828
Thomas Graf1dd144c2015-01-15 03:53:59 +0100829 return opts_type;
830}
831
832static int vxlan_opt_to_nlattr(struct sk_buff *skb,
833 const void *tun_opts, int swkey_tun_opts_len)
834{
Thomas Graf614732e2015-07-21 10:44:06 +0200835 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100836 struct nlattr *nla;
837
838 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
839 if (!nla)
840 return -EMSGSIZE;
841
842 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
843 return -EMSGSIZE;
844
845 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700846 return 0;
847}
848
Jiri Benc6b26ba32015-10-05 13:09:47 +0200849static int __ip_tun_to_nlattr(struct sk_buff *skb,
850 const struct ip_tunnel_key *output,
851 const void *tun_opts, int swkey_tun_opts_len,
852 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700853{
Pravin B Shelare6445712013-10-03 18:16:47 -0700854 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200855 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
856 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700857 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200858 switch (tun_proto) {
859 case AF_INET:
860 if (output->u.ipv4.src &&
861 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
862 output->u.ipv4.src))
863 return -EMSGSIZE;
864 if (output->u.ipv4.dst &&
865 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
866 output->u.ipv4.dst))
867 return -EMSGSIZE;
868 break;
869 case AF_INET6:
870 if (!ipv6_addr_any(&output->u.ipv6.src) &&
871 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
872 &output->u.ipv6.src))
873 return -EMSGSIZE;
874 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
875 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
876 &output->u.ipv6.dst))
877 return -EMSGSIZE;
878 break;
879 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200880 if (output->tos &&
881 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700882 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200883 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700884 return -EMSGSIZE;
885 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700886 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700887 return -EMSGSIZE;
888 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700889 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
890 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800891 if (output->tp_src &&
892 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
893 return -EMSGSIZE;
894 if (output->tp_dst &&
895 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
896 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700897 if ((output->tun_flags & TUNNEL_OAM) &&
898 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700899 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700900 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100901 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
902 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
903 swkey_tun_opts_len, tun_opts))
904 return -EMSGSIZE;
905 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
906 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
907 return -EMSGSIZE;
William Tuceaa0012017-10-04 17:03:12 -0700908 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
909 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
910 ((struct erspan_metadata *)tun_opts)->index))
911 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100912 }
Jesse Grossf5796682014-10-03 15:35:33 -0700913
914 return 0;
915}
916
Jiri Benc6b26ba32015-10-05 13:09:47 +0200917static int ip_tun_to_nlattr(struct sk_buff *skb,
918 const struct ip_tunnel_key *output,
919 const void *tun_opts, int swkey_tun_opts_len,
920 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700921{
922 struct nlattr *nla;
923 int err;
924
925 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
926 if (!nla)
927 return -EMSGSIZE;
928
Jiri Benc6b26ba32015-10-05 13:09:47 +0200929 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
930 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700931 if (err)
932 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700933
934 nla_nest_end(skb, nla);
935 return 0;
936}
937
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700938int ovs_nla_put_tunnel_info(struct sk_buff *skb,
939 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800940{
David S. Millerba3e2082015-10-24 06:54:12 -0700941 return __ip_tun_to_nlattr(skb, &tun_info->key,
942 ip_tunnel_info_opts(tun_info),
943 tun_info->options_len,
944 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800945}
946
Eric Garver018c1dd2016-09-07 12:56:59 -0400947static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
948 const struct nlattr *a[],
949 bool is_mask, bool inner)
950{
951 __be16 tci = 0;
952 __be16 tpid = 0;
953
954 if (a[OVS_KEY_ATTR_VLAN])
955 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
956
957 if (a[OVS_KEY_ATTR_ETHERTYPE])
958 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
959
960 if (likely(!inner)) {
961 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
962 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
963 } else {
964 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
965 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
966 }
967 return 0;
968}
969
970static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
971 u64 key_attrs, bool inner,
972 const struct nlattr **a, bool log)
973{
974 __be16 tci = 0;
975
976 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
977 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
978 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
979 /* Not a VLAN. */
980 return 0;
981 }
982
983 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
984 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
985 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
986 return -EINVAL;
987 }
988
989 if (a[OVS_KEY_ATTR_VLAN])
990 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
991
992 if (!(tci & htons(VLAN_TAG_PRESENT))) {
993 if (tci) {
994 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
995 (inner) ? "C-VLAN" : "VLAN");
996 return -EINVAL;
997 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
998 /* Corner case for truncated VLAN header. */
999 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
1000 (inner) ? "C-VLAN" : "VLAN");
1001 return -EINVAL;
1002 }
1003 }
1004
1005 return 1;
1006}
1007
1008static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
1009 u64 key_attrs, bool inner,
1010 const struct nlattr **a, bool log)
1011{
1012 __be16 tci = 0;
1013 __be16 tpid = 0;
1014 bool encap_valid = !!(match->key->eth.vlan.tci &
1015 htons(VLAN_TAG_PRESENT));
1016 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
1017 htons(VLAN_TAG_PRESENT));
1018
1019 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
1020 /* Not a VLAN. */
1021 return 0;
1022 }
1023
1024 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
1025 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
1026 (inner) ? "C-VLAN" : "VLAN");
1027 return -EINVAL;
1028 }
1029
1030 if (a[OVS_KEY_ATTR_VLAN])
1031 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1032
1033 if (a[OVS_KEY_ATTR_ETHERTYPE])
1034 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1035
1036 if (tpid != htons(0xffff)) {
1037 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1038 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1039 return -EINVAL;
1040 }
1041 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1042 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
1043 (inner) ? "C-VLAN" : "VLAN");
1044 return -EINVAL;
1045 }
1046
1047 return 1;
1048}
1049
1050static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1051 u64 *key_attrs, bool inner,
1052 const struct nlattr **a, bool is_mask,
1053 bool log)
1054{
1055 int err;
1056 const struct nlattr *encap;
1057
1058 if (!is_mask)
1059 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1060 a, log);
1061 else
1062 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1063 a, log);
1064 if (err <= 0)
1065 return err;
1066
1067 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1068 if (err)
1069 return err;
1070
1071 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1072 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1073 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1074
1075 encap = a[OVS_KEY_ATTR_ENCAP];
1076
1077 if (!is_mask)
1078 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1079 else
1080 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1081
1082 return err;
1083}
1084
1085static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1086 u64 *key_attrs, const struct nlattr **a,
1087 bool is_mask, bool log)
1088{
1089 int err;
1090 bool encap_valid = false;
1091
1092 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1093 is_mask, log);
1094 if (err)
1095 return err;
1096
1097 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1098 if (encap_valid) {
1099 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1100 is_mask, log);
1101 if (err)
1102 return err;
1103 }
1104
1105 return 0;
1106}
1107
Jiri Benc0a6410f2016-11-10 16:28:22 +01001108static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1109 u64 *attrs, const struct nlattr **a,
1110 bool is_mask, bool log)
1111{
1112 __be16 eth_type;
1113
1114 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1115 if (is_mask) {
1116 /* Always exact match EtherType. */
1117 eth_type = htons(0xffff);
1118 } else if (!eth_proto_is_802_3(eth_type)) {
1119 OVS_NLERR(log, "EtherType %x is less than min %x",
1120 ntohs(eth_type), ETH_P_802_3_MIN);
1121 return -EINVAL;
1122 }
1123
1124 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1125 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1126 return 0;
1127}
1128
Joe Stringerc2ac6672015-08-26 11:31:52 -07001129static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1130 u64 *attrs, const struct nlattr **a,
1131 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001132{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001133 u8 mac_proto = MAC_PROTO_ETHERNET;
1134
Andy Zhou971427f32014-09-15 19:37:25 -07001135 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1136 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1137
1138 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1139 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1140 }
1141
1142 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1143 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1144
1145 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1146 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1147 }
1148
Pravin B Shelare6445712013-10-03 18:16:47 -07001149 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1150 SW_FLOW_KEY_PUT(match, phy.priority,
1151 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1152 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1153 }
1154
1155 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1156 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1157
Jesse Gross426cda52014-10-06 05:08:38 -07001158 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001159 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001160 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001161 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001162 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001163 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001164 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001165
1166 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1167 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1168 } else if (!is_mask) {
1169 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1170 }
1171
1172 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1173 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1174
1175 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1176 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1177 }
1178 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001179 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1180 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001181 return -EINVAL;
1182 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1183 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001184
1185 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001186 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001187 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001188
Joe Stringer9e384712015-10-19 19:18:57 -07001189 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001190 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001191 ct_state);
1192 return -EINVAL;
1193 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001194
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001195 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001196 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1197 }
1198 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001199 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001200 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1201
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001202 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001203 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1204 }
Joe Stringer182e3042015-08-26 11:31:49 -07001205 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001206 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001207 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1208
1209 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1210 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1211 }
Joe Stringer33db4122015-10-01 15:00:37 -07001212 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1213 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1214 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001215
Joe Stringer33db4122015-10-01 15:00:37 -07001216 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1217 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001218 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001219 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001220 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001221 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1222 const struct ovs_key_ct_tuple_ipv4 *ct;
1223
1224 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1225
1226 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1227 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1228 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1229 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001230 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001231 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1232 }
1233 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1234 const struct ovs_key_ct_tuple_ipv6 *ct;
1235
1236 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1237
1238 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1239 sizeof(match->key->ipv6.ct_orig.src),
1240 is_mask);
1241 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1242 sizeof(match->key->ipv6.ct_orig.dst),
1243 is_mask);
1244 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1245 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001246 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001247 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1248 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001249
Jiri Benc0a6410f2016-11-10 16:28:22 +01001250 /* For layer 3 packets the Ethernet type is provided
1251 * and treated as metadata but no MAC addresses are provided.
1252 */
1253 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1254 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1255 mac_proto = MAC_PROTO_NONE;
1256
Jiri Benc329f45b2016-11-10 16:28:18 +01001257 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001258 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1259
1260 if (mac_proto == MAC_PROTO_NONE)
1261 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1262 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001263
Pravin B Shelare6445712013-10-03 18:16:47 -07001264 return 0;
1265}
1266
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001267int nsh_hdr_from_nlattr(const struct nlattr *attr,
1268 struct nshhdr *nh, size_t size)
1269{
1270 struct nlattr *a;
1271 int rem;
1272 u8 flags = 0;
1273 u8 ttl = 0;
1274 int mdlen = 0;
1275
1276 /* validate_nsh has check this, so we needn't do duplicate check here
1277 */
1278 if (size < NSH_BASE_HDR_LEN)
1279 return -ENOBUFS;
1280
1281 nla_for_each_nested(a, attr, rem) {
1282 int type = nla_type(a);
1283
1284 switch (type) {
1285 case OVS_NSH_KEY_ATTR_BASE: {
1286 const struct ovs_nsh_key_base *base = nla_data(a);
1287
1288 flags = base->flags;
1289 ttl = base->ttl;
1290 nh->np = base->np;
1291 nh->mdtype = base->mdtype;
1292 nh->path_hdr = base->path_hdr;
1293 break;
1294 }
1295 case OVS_NSH_KEY_ATTR_MD1:
1296 mdlen = nla_len(a);
1297 if (mdlen > size - NSH_BASE_HDR_LEN)
1298 return -ENOBUFS;
1299 memcpy(&nh->md1, nla_data(a), mdlen);
1300 break;
1301
1302 case OVS_NSH_KEY_ATTR_MD2:
1303 mdlen = nla_len(a);
1304 if (mdlen > size - NSH_BASE_HDR_LEN)
1305 return -ENOBUFS;
1306 memcpy(&nh->md2, nla_data(a), mdlen);
1307 break;
1308
1309 default:
1310 return -EINVAL;
1311 }
1312 }
1313
1314 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1315 nh->ver_flags_ttl_len = 0;
1316 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1317
1318 return 0;
1319}
1320
1321int nsh_key_from_nlattr(const struct nlattr *attr,
1322 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1323{
1324 struct nlattr *a;
1325 int rem;
1326
1327 /* validate_nsh has check this, so we needn't do duplicate check here
1328 */
1329 nla_for_each_nested(a, attr, rem) {
1330 int type = nla_type(a);
1331
1332 switch (type) {
1333 case OVS_NSH_KEY_ATTR_BASE: {
1334 const struct ovs_nsh_key_base *base = nla_data(a);
1335 const struct ovs_nsh_key_base *base_mask = base + 1;
1336
1337 nsh->base = *base;
1338 nsh_mask->base = *base_mask;
1339 break;
1340 }
1341 case OVS_NSH_KEY_ATTR_MD1: {
1342 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1343 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1344
1345 memcpy(nsh->context, md1->context, sizeof(*md1));
1346 memcpy(nsh_mask->context, md1_mask->context,
1347 sizeof(*md1_mask));
1348 break;
1349 }
1350 case OVS_NSH_KEY_ATTR_MD2:
1351 /* Not supported yet */
1352 return -ENOTSUPP;
1353 default:
1354 return -EINVAL;
1355 }
1356 }
1357
1358 return 0;
1359}
1360
1361static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1362 struct sw_flow_match *match, bool is_mask,
1363 bool is_push_nsh, bool log)
1364{
1365 struct nlattr *a;
1366 int rem;
1367 bool has_base = false;
1368 bool has_md1 = false;
1369 bool has_md2 = false;
1370 u8 mdtype = 0;
1371 int mdlen = 0;
1372
1373 if (WARN_ON(is_push_nsh && is_mask))
1374 return -EINVAL;
1375
1376 nla_for_each_nested(a, attr, rem) {
1377 int type = nla_type(a);
1378 int i;
1379
1380 if (type > OVS_NSH_KEY_ATTR_MAX) {
1381 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1382 type, OVS_NSH_KEY_ATTR_MAX);
1383 return -EINVAL;
1384 }
1385
1386 if (!check_attr_len(nla_len(a),
1387 ovs_nsh_key_attr_lens[type].len)) {
1388 OVS_NLERR(
1389 log,
1390 "nsh attr %d has unexpected len %d expected %d",
1391 type,
1392 nla_len(a),
1393 ovs_nsh_key_attr_lens[type].len
1394 );
1395 return -EINVAL;
1396 }
1397
1398 switch (type) {
1399 case OVS_NSH_KEY_ATTR_BASE: {
1400 const struct ovs_nsh_key_base *base = nla_data(a);
1401
1402 has_base = true;
1403 mdtype = base->mdtype;
1404 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1405 base->flags, is_mask);
1406 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1407 base->ttl, is_mask);
1408 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1409 base->mdtype, is_mask);
1410 SW_FLOW_KEY_PUT(match, nsh.base.np,
1411 base->np, is_mask);
1412 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1413 base->path_hdr, is_mask);
1414 break;
1415 }
1416 case OVS_NSH_KEY_ATTR_MD1: {
1417 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1418
1419 has_md1 = true;
1420 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1421 SW_FLOW_KEY_PUT(match, nsh.context[i],
1422 md1->context[i], is_mask);
1423 break;
1424 }
1425 case OVS_NSH_KEY_ATTR_MD2:
1426 if (!is_push_nsh) /* Not supported MD type 2 yet */
1427 return -ENOTSUPP;
1428
1429 has_md2 = true;
1430 mdlen = nla_len(a);
1431 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1432 OVS_NLERR(
1433 log,
1434 "Invalid MD length %d for MD type %d",
1435 mdlen,
1436 mdtype
1437 );
1438 return -EINVAL;
1439 }
1440 break;
1441 default:
1442 OVS_NLERR(log, "Unknown nsh attribute %d",
1443 type);
1444 return -EINVAL;
1445 }
1446 }
1447
1448 if (rem > 0) {
1449 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1450 return -EINVAL;
1451 }
1452
1453 if (has_md1 && has_md2) {
1454 OVS_NLERR(
1455 1,
1456 "invalid nsh attribute: md1 and md2 are exclusive."
1457 );
1458 return -EINVAL;
1459 }
1460
1461 if (!is_mask) {
1462 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1463 (has_md2 && mdtype != NSH_M_TYPE2)) {
1464 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1465 mdtype);
1466 return -EINVAL;
1467 }
1468
1469 if (is_push_nsh &&
1470 (!has_base || (!has_md1 && !has_md2))) {
1471 OVS_NLERR(
1472 1,
1473 "push_nsh: missing base or metadata attributes"
1474 );
1475 return -EINVAL;
1476 }
1477 }
1478
1479 return 0;
1480}
1481
Joe Stringerc2ac6672015-08-26 11:31:52 -07001482static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1483 u64 attrs, const struct nlattr **a,
1484 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001485{
1486 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001487
Joe Stringerc2ac6672015-08-26 11:31:52 -07001488 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001489 if (err)
1490 return err;
1491
1492 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1493 const struct ovs_key_ethernet *eth_key;
1494
1495 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1496 SW_FLOW_KEY_MEMCPY(match, eth.src,
1497 eth_key->eth_src, ETH_ALEN, is_mask);
1498 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1499 eth_key->eth_dst, ETH_ALEN, is_mask);
1500 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001501
Jiri Benc0a6410f2016-11-10 16:28:22 +01001502 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1503 /* VLAN attribute is always parsed before getting here since it
1504 * may occur multiple times.
1505 */
1506 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001507 return -EINVAL;
1508 }
1509
Jiri Benc0a6410f2016-11-10 16:28:22 +01001510 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1511 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1512 log);
1513 if (err)
1514 return err;
1515 } else if (!is_mask) {
1516 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1517 }
1518 } else if (!match->key->eth.type) {
1519 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1520 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001521 }
1522
1523 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1524 const struct ovs_key_ipv4 *ipv4_key;
1525
1526 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1527 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001528 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1529 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001530 return -EINVAL;
1531 }
1532 SW_FLOW_KEY_PUT(match, ip.proto,
1533 ipv4_key->ipv4_proto, is_mask);
1534 SW_FLOW_KEY_PUT(match, ip.tos,
1535 ipv4_key->ipv4_tos, is_mask);
1536 SW_FLOW_KEY_PUT(match, ip.ttl,
1537 ipv4_key->ipv4_ttl, is_mask);
1538 SW_FLOW_KEY_PUT(match, ip.frag,
1539 ipv4_key->ipv4_frag, is_mask);
1540 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1541 ipv4_key->ipv4_src, is_mask);
1542 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1543 ipv4_key->ipv4_dst, is_mask);
1544 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1545 }
1546
1547 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1548 const struct ovs_key_ipv6 *ipv6_key;
1549
1550 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1551 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001552 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1553 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001554 return -EINVAL;
1555 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001556
Joe Stringerd3052bb2014-11-19 13:54:49 -08001557 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001558 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001559 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1560 return -EINVAL;
1561 }
1562
Pravin B Shelare6445712013-10-03 18:16:47 -07001563 SW_FLOW_KEY_PUT(match, ipv6.label,
1564 ipv6_key->ipv6_label, is_mask);
1565 SW_FLOW_KEY_PUT(match, ip.proto,
1566 ipv6_key->ipv6_proto, is_mask);
1567 SW_FLOW_KEY_PUT(match, ip.tos,
1568 ipv6_key->ipv6_tclass, is_mask);
1569 SW_FLOW_KEY_PUT(match, ip.ttl,
1570 ipv6_key->ipv6_hlimit, is_mask);
1571 SW_FLOW_KEY_PUT(match, ip.frag,
1572 ipv6_key->ipv6_frag, is_mask);
1573 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1574 ipv6_key->ipv6_src,
1575 sizeof(match->key->ipv6.addr.src),
1576 is_mask);
1577 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1578 ipv6_key->ipv6_dst,
1579 sizeof(match->key->ipv6.addr.dst),
1580 is_mask);
1581
1582 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1583 }
1584
1585 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1586 const struct ovs_key_arp *arp_key;
1587
1588 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1589 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001590 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001591 arp_key->arp_op);
1592 return -EINVAL;
1593 }
1594
1595 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1596 arp_key->arp_sip, is_mask);
1597 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1598 arp_key->arp_tip, is_mask);
1599 SW_FLOW_KEY_PUT(match, ip.proto,
1600 ntohs(arp_key->arp_op), is_mask);
1601 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1602 arp_key->arp_sha, ETH_ALEN, is_mask);
1603 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1604 arp_key->arp_tha, ETH_ALEN, is_mask);
1605
1606 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1607 }
1608
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001609 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1610 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1611 is_mask, false, log) < 0)
1612 return -EINVAL;
1613 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1614 }
1615
Simon Horman25cd9ba2014-10-06 05:05:13 -07001616 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1617 const struct ovs_key_mpls *mpls_key;
1618
1619 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1620 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1621 mpls_key->mpls_lse, is_mask);
1622
1623 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1624 }
1625
Pravin B Shelare6445712013-10-03 18:16:47 -07001626 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1627 const struct ovs_key_tcp *tcp_key;
1628
1629 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001630 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1631 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001632 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1633 }
1634
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001635 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001636 SW_FLOW_KEY_PUT(match, tp.flags,
1637 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1638 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001639 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1640 }
1641
Pravin B Shelare6445712013-10-03 18:16:47 -07001642 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1643 const struct ovs_key_udp *udp_key;
1644
1645 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001646 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1647 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001648 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1649 }
1650
1651 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1652 const struct ovs_key_sctp *sctp_key;
1653
1654 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001655 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1656 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001657 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1658 }
1659
1660 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1661 const struct ovs_key_icmp *icmp_key;
1662
1663 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001664 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001665 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001666 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001667 htons(icmp_key->icmp_code), is_mask);
1668 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1669 }
1670
1671 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1672 const struct ovs_key_icmpv6 *icmpv6_key;
1673
1674 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001675 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001676 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001677 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001678 htons(icmpv6_key->icmpv6_code), is_mask);
1679 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1680 }
1681
1682 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1683 const struct ovs_key_nd *nd_key;
1684
1685 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1686 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1687 nd_key->nd_target,
1688 sizeof(match->key->ipv6.nd.target),
1689 is_mask);
1690 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1691 nd_key->nd_sll, ETH_ALEN, is_mask);
1692 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1693 nd_key->nd_tll, ETH_ALEN, is_mask);
1694 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1695 }
1696
Jesse Gross426cda52014-10-06 05:08:38 -07001697 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001698 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001699 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001700 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001701 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001702
1703 return 0;
1704}
1705
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001706static void nlattr_set(struct nlattr *attr, u8 val,
1707 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001708{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001709 struct nlattr *nla;
1710 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001711
Pravin B Shelarf47de062014-10-16 21:55:45 -07001712 /* The nlattr stream should already have been validated */
1713 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001714 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1715 if (tbl[nla_type(nla)].next)
1716 tbl = tbl[nla_type(nla)].next;
1717 nlattr_set(nla, val, tbl);
1718 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001719 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001720 }
Joe Stringer9e384712015-10-19 19:18:57 -07001721
1722 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1723 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001724 }
1725}
1726
1727static void mask_set_nlattr(struct nlattr *attr, u8 val)
1728{
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001729 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001730}
1731
1732/**
1733 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1734 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1735 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1736 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001737 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001738 * @match: receives the extracted flow match information.
1739 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1740 * sequence. The fields should of the packet that triggered the creation
1741 * of this flow.
1742 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1743 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001744 * @log: Boolean to allow kernel error logging. Normally true, but when
1745 * probing for feature compatibility this should be passed in as false to
1746 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001747 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001748int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001749 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001750 const struct nlattr *nla_mask,
1751 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001752{
1753 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001754 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001755 u64 key_attrs = 0;
1756 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001757 int err;
1758
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001759 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001760 if (err)
1761 return err;
1762
Eric Garver018c1dd2016-09-07 12:56:59 -04001763 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1764 if (err)
1765 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001766
Joe Stringerc2ac6672015-08-26 11:31:52 -07001767 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001768 if (err)
1769 return err;
1770
Pravin B Shelara85311b2014-10-19 12:03:40 -07001771 if (match->mask) {
1772 if (!nla_mask) {
1773 /* Create an exact match mask. We need to set to 0xff
1774 * all the 'match->mask' fields that have been touched
1775 * in 'match->key'. We cannot simply memset
1776 * 'match->mask', because padding bytes and fields not
1777 * specified in 'match->key' should be left to 0.
1778 * Instead, we use a stream of netlink attributes,
1779 * copied from 'key' and set to 0xff.
1780 * ovs_key_from_nlattrs() will take care of filling
1781 * 'match->mask' appropriately.
1782 */
1783 newmask = kmemdup(nla_key,
1784 nla_total_size(nla_len(nla_key)),
1785 GFP_KERNEL);
1786 if (!newmask)
1787 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001788
Pravin B Shelara85311b2014-10-19 12:03:40 -07001789 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001790
Pravin B Shelara85311b2014-10-19 12:03:40 -07001791 /* The userspace does not send tunnel attributes that
1792 * are 0, but we should not wildcard them nonetheless.
1793 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001794 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001795 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1796 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001797
Pravin B Shelara85311b2014-10-19 12:03:40 -07001798 nla_mask = newmask;
1799 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001800
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001801 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001802 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001803 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001804
Pravin B Shelara85311b2014-10-19 12:03:40 -07001805 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001806 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1807 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001808
Eric Garver018c1dd2016-09-07 12:56:59 -04001809 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1810 if (err)
1811 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001812
Joe Stringerc2ac6672015-08-26 11:31:52 -07001813 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1814 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001815 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001816 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001817 }
1818
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001819 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001820 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001821
Pravin B Shelarf47de062014-10-16 21:55:45 -07001822free_newmask:
1823 kfree(newmask);
1824 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001825}
1826
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001827static size_t get_ufid_len(const struct nlattr *attr, bool log)
1828{
1829 size_t len;
1830
1831 if (!attr)
1832 return 0;
1833
1834 len = nla_len(attr);
1835 if (len < 1 || len > MAX_UFID_LENGTH) {
1836 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1837 nla_len(attr), MAX_UFID_LENGTH);
1838 return 0;
1839 }
1840
1841 return len;
1842}
1843
1844/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1845 * or false otherwise.
1846 */
1847bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1848 bool log)
1849{
1850 sfid->ufid_len = get_ufid_len(attr, log);
1851 if (sfid->ufid_len)
1852 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1853
1854 return sfid->ufid_len;
1855}
1856
1857int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1858 const struct sw_flow_key *key, bool log)
1859{
1860 struct sw_flow_key *new_key;
1861
1862 if (ovs_nla_get_ufid(sfid, ufid, log))
1863 return 0;
1864
1865 /* If UFID was not provided, use unmasked key. */
1866 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1867 if (!new_key)
1868 return -ENOMEM;
1869 memcpy(new_key, key, sizeof(*key));
1870 sfid->unmasked_key = new_key;
1871
1872 return 0;
1873}
1874
1875u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1876{
1877 return attr ? nla_get_u32(attr) : 0;
1878}
1879
Pravin B Shelare6445712013-10-03 18:16:47 -07001880/**
1881 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001882 * @net: Network namespace.
1883 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1884 * metadata.
1885 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1886 * attributes.
1887 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001888 * @log: Boolean to allow kernel error logging. Normally true, but when
1889 * probing for feature compatibility this should be passed in as false to
1890 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001891 *
1892 * This parses a series of Netlink attributes that form a flow key, which must
1893 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1894 * get the metadata, that is, the parts of the flow key that cannot be
1895 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001896 *
1897 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001898 */
1899
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001900int ovs_nla_get_flow_metadata(struct net *net,
1901 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1902 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001903{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001904 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001905
1906 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001907 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001908
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001909 key->ct_state = 0;
1910 key->ct_zone = 0;
1911 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001912 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001913 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1914 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1915
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001916 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001917
Joe Stringerc2ac6672015-08-26 11:31:52 -07001918 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001919}
1920
Eric Garver018c1dd2016-09-07 12:56:59 -04001921static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1922 bool is_mask)
1923{
1924 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1925
1926 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1927 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1928 return -EMSGSIZE;
1929 return 0;
1930}
1931
Yi Yangb2d0f5d2017-11-07 21:07:02 +08001932static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1933 struct sk_buff *skb)
1934{
1935 struct nlattr *start;
1936
1937 start = nla_nest_start(skb, OVS_KEY_ATTR_NSH);
1938 if (!start)
1939 return -EMSGSIZE;
1940
1941 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1942 goto nla_put_failure;
1943
1944 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1945 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1946 sizeof(nsh->context), nsh->context))
1947 goto nla_put_failure;
1948 }
1949
1950 /* Don't support MD type 2 yet */
1951
1952 nla_nest_end(skb, start);
1953
1954 return 0;
1955
1956nla_put_failure:
1957 return -EMSGSIZE;
1958}
1959
Joe Stringer5b4237b2015-01-21 16:42:48 -08001960static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1961 const struct sw_flow_key *output, bool is_mask,
1962 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001963{
1964 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001965 struct nlattr *nla;
1966 struct nlattr *encap = NULL;
1967 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001968
Andy Zhou971427f32014-09-15 19:37:25 -07001969 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1970 goto nla_put_failure;
1971
1972 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1973 goto nla_put_failure;
1974
Pravin B Shelare6445712013-10-03 18:16:47 -07001975 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1976 goto nla_put_failure;
1977
Jiri Benc00a93ba2015-10-05 13:09:46 +02001978 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001979 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001980
1981 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001982 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001983
Jiri Benc6b26ba32015-10-05 13:09:47 +02001984 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1985 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001986 goto nla_put_failure;
1987 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001988
1989 if (swkey->phy.in_port == DP_MAX_PORTS) {
1990 if (is_mask && (output->phy.in_port == 0xffff))
1991 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1992 goto nla_put_failure;
1993 } else {
1994 u16 upper_u16;
1995 upper_u16 = !is_mask ? 0 : 0xffff;
1996
1997 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1998 (upper_u16 << 16) | output->phy.in_port))
1999 goto nla_put_failure;
2000 }
2001
2002 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
2003 goto nla_put_failure;
2004
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08002005 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07002006 goto nla_put_failure;
2007
Jiri Benc0a6410f2016-11-10 16:28:22 +01002008 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
2009 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
2010 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07002011 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04002012
Jiri Benc0a6410f2016-11-10 16:28:22 +01002013 eth_key = nla_data(nla);
2014 ether_addr_copy(eth_key->eth_src, output->eth.src);
2015 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
2016
2017 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
2018 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04002019 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002020 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
2021 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04002022 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07002023
Jiri Benc0a6410f2016-11-10 16:28:22 +01002024 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
2025 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
2026 goto nla_put_failure;
2027 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
2028 if (!swkey->eth.cvlan.tci)
2029 goto unencap;
2030 }
2031 }
2032
2033 if (swkey->eth.type == htons(ETH_P_802_2)) {
2034 /*
2035 * Ethertype 802.2 is represented in the netlink with omitted
2036 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2037 * 0xffff in the mask attribute. Ethertype can also
2038 * be wildcarded.
2039 */
2040 if (is_mask && output->eth.type)
2041 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
2042 output->eth.type))
2043 goto nla_put_failure;
2044 goto unencap;
2045 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002046 }
2047
2048 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2049 goto nla_put_failure;
2050
Eric Garver018c1dd2016-09-07 12:56:59 -04002051 if (eth_type_vlan(swkey->eth.type)) {
2052 /* There are 3 VLAN tags, we don't know anything about the rest
2053 * of the packet, so truncate here.
2054 */
2055 WARN_ON_ONCE(!(encap && in_encap));
2056 goto unencap;
2057 }
2058
Pravin B Shelare6445712013-10-03 18:16:47 -07002059 if (swkey->eth.type == htons(ETH_P_IP)) {
2060 struct ovs_key_ipv4 *ipv4_key;
2061
2062 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2063 if (!nla)
2064 goto nla_put_failure;
2065 ipv4_key = nla_data(nla);
2066 ipv4_key->ipv4_src = output->ipv4.addr.src;
2067 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2068 ipv4_key->ipv4_proto = output->ip.proto;
2069 ipv4_key->ipv4_tos = output->ip.tos;
2070 ipv4_key->ipv4_ttl = output->ip.ttl;
2071 ipv4_key->ipv4_frag = output->ip.frag;
2072 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2073 struct ovs_key_ipv6 *ipv6_key;
2074
2075 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2076 if (!nla)
2077 goto nla_put_failure;
2078 ipv6_key = nla_data(nla);
2079 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2080 sizeof(ipv6_key->ipv6_src));
2081 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2082 sizeof(ipv6_key->ipv6_dst));
2083 ipv6_key->ipv6_label = output->ipv6.label;
2084 ipv6_key->ipv6_proto = output->ip.proto;
2085 ipv6_key->ipv6_tclass = output->ip.tos;
2086 ipv6_key->ipv6_hlimit = output->ip.ttl;
2087 ipv6_key->ipv6_frag = output->ip.frag;
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002088 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2089 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2090 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002091 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2092 swkey->eth.type == htons(ETH_P_RARP)) {
2093 struct ovs_key_arp *arp_key;
2094
2095 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2096 if (!nla)
2097 goto nla_put_failure;
2098 arp_key = nla_data(nla);
2099 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2100 arp_key->arp_sip = output->ipv4.addr.src;
2101 arp_key->arp_tip = output->ipv4.addr.dst;
2102 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08002103 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2104 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002105 } else if (eth_p_mpls(swkey->eth.type)) {
2106 struct ovs_key_mpls *mpls_key;
2107
2108 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
2109 if (!nla)
2110 goto nla_put_failure;
2111 mpls_key = nla_data(nla);
2112 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07002113 }
2114
2115 if ((swkey->eth.type == htons(ETH_P_IP) ||
2116 swkey->eth.type == htons(ETH_P_IPV6)) &&
2117 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2118
2119 if (swkey->ip.proto == IPPROTO_TCP) {
2120 struct ovs_key_tcp *tcp_key;
2121
2122 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2123 if (!nla)
2124 goto nla_put_failure;
2125 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002126 tcp_key->tcp_src = output->tp.src;
2127 tcp_key->tcp_dst = output->tp.dst;
2128 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2129 output->tp.flags))
2130 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07002131 } else if (swkey->ip.proto == IPPROTO_UDP) {
2132 struct ovs_key_udp *udp_key;
2133
2134 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2135 if (!nla)
2136 goto nla_put_failure;
2137 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002138 udp_key->udp_src = output->tp.src;
2139 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002140 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2141 struct ovs_key_sctp *sctp_key;
2142
2143 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2144 if (!nla)
2145 goto nla_put_failure;
2146 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002147 sctp_key->sctp_src = output->tp.src;
2148 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07002149 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2150 swkey->ip.proto == IPPROTO_ICMP) {
2151 struct ovs_key_icmp *icmp_key;
2152
2153 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2154 if (!nla)
2155 goto nla_put_failure;
2156 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002157 icmp_key->icmp_type = ntohs(output->tp.src);
2158 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002159 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2160 swkey->ip.proto == IPPROTO_ICMPV6) {
2161 struct ovs_key_icmpv6 *icmpv6_key;
2162
2163 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2164 sizeof(*icmpv6_key));
2165 if (!nla)
2166 goto nla_put_failure;
2167 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07002168 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2169 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07002170
2171 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
2172 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
2173 struct ovs_key_nd *nd_key;
2174
2175 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2176 if (!nla)
2177 goto nla_put_failure;
2178 nd_key = nla_data(nla);
2179 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2180 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08002181 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2182 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07002183 }
2184 }
2185 }
2186
2187unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04002188 if (in_encap)
2189 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07002190 if (encap)
2191 nla_nest_end(skb, encap);
2192
2193 return 0;
2194
2195nla_put_failure:
2196 return -EMSGSIZE;
2197}
2198
Joe Stringer5b4237b2015-01-21 16:42:48 -08002199int ovs_nla_put_key(const struct sw_flow_key *swkey,
2200 const struct sw_flow_key *output, int attr, bool is_mask,
2201 struct sk_buff *skb)
2202{
2203 int err;
2204 struct nlattr *nla;
2205
2206 nla = nla_nest_start(skb, attr);
2207 if (!nla)
2208 return -EMSGSIZE;
2209 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2210 if (err)
2211 return err;
2212 nla_nest_end(skb, nla);
2213
2214 return 0;
2215}
2216
2217/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002218int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08002219{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08002220 if (ovs_identifier_is_ufid(&flow->id))
2221 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2222 flow->id.ufid);
2223
2224 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2225 OVS_FLOW_ATTR_KEY, false, skb);
2226}
2227
2228/* Called with ovs_mutex or RCU read lock. */
2229int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2230{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08002231 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08002232 OVS_FLOW_ATTR_KEY, false, skb);
2233}
2234
2235/* Called with ovs_mutex or RCU read lock. */
2236int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2237{
2238 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2239 OVS_FLOW_ATTR_MASK, true, skb);
2240}
2241
Pravin B Shelare6445712013-10-03 18:16:47 -07002242#define MAX_ACTIONS_BUFSIZE (32 * 1024)
2243
zhangliping67c8d222017-11-25 22:02:12 +08002244static struct sw_flow_actions *nla_alloc_flow_actions(int size)
Pravin B Shelare6445712013-10-03 18:16:47 -07002245{
2246 struct sw_flow_actions *sfa;
2247
zhangliping67c8d222017-11-25 22:02:12 +08002248 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002249
2250 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2251 if (!sfa)
2252 return ERR_PTR(-ENOMEM);
2253
2254 sfa->actions_len = 0;
2255 return sfa;
2256}
2257
Thomas Graf34ae9322015-07-21 10:44:03 +02002258static void ovs_nla_free_set_action(const struct nlattr *a)
2259{
2260 const struct nlattr *ovs_key = nla_data(a);
2261 struct ovs_tunnel_info *ovs_tun;
2262
2263 switch (nla_type(ovs_key)) {
2264 case OVS_KEY_ATTR_TUNNEL_INFO:
2265 ovs_tun = nla_data(ovs_key);
2266 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2267 break;
2268 }
2269}
2270
Pravin B Shelare6445712013-10-03 18:16:47 -07002271void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2272{
Thomas Graf34ae9322015-07-21 10:44:03 +02002273 const struct nlattr *a;
2274 int rem;
2275
2276 if (!sf_acts)
2277 return;
2278
2279 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
2280 switch (nla_type(a)) {
2281 case OVS_ACTION_ATTR_SET:
2282 ovs_nla_free_set_action(a);
2283 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002284 case OVS_ACTION_ATTR_CT:
2285 ovs_ct_free_action(a);
2286 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02002287 }
2288 }
2289
2290 kfree(sf_acts);
2291}
2292
2293static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2294{
2295 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2296}
2297
2298/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2299 * The caller must hold rcu_read_lock for this to be sensible. */
2300void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2301{
2302 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07002303}
2304
2305static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002306 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002307{
2308
2309 struct sw_flow_actions *acts;
2310 int new_acts_size;
2311 int req_size = NLA_ALIGN(attr_len);
2312 int next_offset = offsetof(struct sw_flow_actions, actions) +
2313 (*sfa)->actions_len;
2314
2315 if (req_size <= (ksize(*sfa) - next_offset))
2316 goto out;
2317
2318 new_acts_size = ksize(*sfa) * 2;
2319
2320 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
zhangliping67c8d222017-11-25 22:02:12 +08002321 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
2322 OVS_NLERR(log, "Flow action size exceeds max %u",
2323 MAX_ACTIONS_BUFSIZE);
Pravin B Shelare6445712013-10-03 18:16:47 -07002324 return ERR_PTR(-EMSGSIZE);
zhangliping67c8d222017-11-25 22:02:12 +08002325 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002326 new_acts_size = MAX_ACTIONS_BUFSIZE;
2327 }
2328
zhangliping67c8d222017-11-25 22:02:12 +08002329 acts = nla_alloc_flow_actions(new_acts_size);
Pravin B Shelare6445712013-10-03 18:16:47 -07002330 if (IS_ERR(acts))
2331 return (void *)acts;
2332
2333 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2334 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07002335 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002336 kfree(*sfa);
2337 *sfa = acts;
2338
2339out:
2340 (*sfa)->actions_len += req_size;
2341 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2342}
2343
Jesse Grossf0b128c2014-10-03 15:35:31 -07002344static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002345 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002346{
2347 struct nlattr *a;
2348
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002349 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002350 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002351 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002352
2353 a->nla_type = attrtype;
2354 a->nla_len = nla_attr_size(len);
2355
2356 if (data)
2357 memcpy(nla_data(a), data, len);
2358 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2359
Jesse Grossf0b128c2014-10-03 15:35:31 -07002360 return a;
2361}
2362
Joe Stringer7f8a4362015-08-26 11:31:48 -07002363int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2364 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002365{
2366 struct nlattr *a;
2367
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002368 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002369
Fabian Frederickf35423c12014-11-14 19:32:58 +01002370 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002371}
2372
2373static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002374 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002375{
2376 int used = (*sfa)->actions_len;
2377 int err;
2378
Joe Stringer7f8a4362015-08-26 11:31:48 -07002379 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002380 if (err)
2381 return err;
2382
2383 return used;
2384}
2385
2386static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2387 int st_offset)
2388{
2389 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2390 st_offset);
2391
2392 a->nla_len = sfa->actions_len - st_offset;
2393}
2394
Joe Stringer7f8a4362015-08-26 11:31:48 -07002395static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002396 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002397 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002398 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002399
Joe Stringer7f8a4362015-08-26 11:31:48 -07002400static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002401 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002402 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002403 __be16 eth_type, __be16 vlan_tci,
2404 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002405{
2406 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2407 const struct nlattr *probability, *actions;
2408 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002409 int rem, start, err;
2410 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002411
2412 memset(attrs, 0, sizeof(attrs));
2413 nla_for_each_nested(a, attr, rem) {
2414 int type = nla_type(a);
2415 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2416 return -EINVAL;
2417 attrs[type] = a;
2418 }
2419 if (rem)
2420 return -EINVAL;
2421
2422 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2423 if (!probability || nla_len(probability) != sizeof(u32))
2424 return -EINVAL;
2425
2426 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2427 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2428 return -EINVAL;
2429
2430 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002431 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002432 if (start < 0)
2433 return start;
andy zhou798c1662017-03-20 16:32:29 -07002434
2435 /* When both skb and flow may be changed, put the sample
2436 * into a deferred fifo. On the other hand, if only skb
2437 * may be modified, the actions can be executed in place.
2438 *
2439 * Do this analysis at the flow installation time.
2440 * Set 'clone_action->exec' to true if the actions can be
2441 * executed without being deferred.
2442 *
2443 * If the sample is the last action, it can always be excuted
2444 * rather than deferred.
2445 */
2446 arg.exec = last || !actions_may_change_flow(actions);
2447 arg.probability = nla_get_u32(probability);
2448
2449 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2450 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002451 if (err)
2452 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002453
andy zhou798c1662017-03-20 16:32:29 -07002454 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002455 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002456
Pravin B Shelare6445712013-10-03 18:16:47 -07002457 if (err)
2458 return err;
2459
Pravin B Shelare6445712013-10-03 18:16:47 -07002460 add_nested_action_end(*sfa, start);
2461
2462 return 0;
2463}
2464
Pravin B Shelare6445712013-10-03 18:16:47 -07002465void ovs_match_init(struct sw_flow_match *match,
2466 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002467 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002468 struct sw_flow_mask *mask)
2469{
2470 memset(match, 0, sizeof(*match));
2471 match->key = key;
2472 match->mask = mask;
2473
pravin shelar22799942016-09-19 13:51:00 -07002474 if (reset_key)
2475 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002476
2477 if (mask) {
2478 memset(&mask->key, 0, sizeof(mask->key));
2479 mask->range.start = mask->range.end = 0;
2480 }
2481}
2482
Thomas Grafd91641d2015-01-15 03:53:57 +01002483static int validate_geneve_opts(struct sw_flow_key *key)
2484{
2485 struct geneve_opt *option;
2486 int opts_len = key->tun_opts_len;
2487 bool crit_opt = false;
2488
2489 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2490 while (opts_len > 0) {
2491 int len;
2492
2493 if (opts_len < sizeof(*option))
2494 return -EINVAL;
2495
2496 len = sizeof(*option) + option->length * 4;
2497 if (len > opts_len)
2498 return -EINVAL;
2499
2500 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2501
2502 option = (struct geneve_opt *)((u8 *)option + len);
2503 opts_len -= len;
2504 };
2505
2506 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2507
2508 return 0;
2509}
2510
Pravin B Shelare6445712013-10-03 18:16:47 -07002511static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002512 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002513{
2514 struct sw_flow_match match;
2515 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002516 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002517 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002518 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002519 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002520 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002521
pravin shelar22799942016-09-19 13:51:00 -07002522 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002523 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002524 if (opts_type < 0)
2525 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002526
Jesse Grossf5796682014-10-03 15:35:33 -07002527 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002528 switch (opts_type) {
2529 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2530 err = validate_geneve_opts(&key);
2531 if (err < 0)
2532 return err;
2533 break;
2534 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2535 break;
William Tuceaa0012017-10-04 17:03:12 -07002536 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
2537 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002538 }
Jesse Grossf5796682014-10-03 15:35:33 -07002539 };
2540
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002541 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002542 if (start < 0)
2543 return start;
2544
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002545 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2546 GFP_KERNEL);
2547
Thomas Graf34ae9322015-07-21 10:44:03 +02002548 if (!tun_dst)
2549 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002550
Paolo Abenid71785f2016-02-12 15:43:57 +01002551 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2552 if (err) {
2553 dst_release((struct dst_entry *)tun_dst);
2554 return err;
2555 }
2556
Thomas Graf34ae9322015-07-21 10:44:03 +02002557 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2558 sizeof(*ovs_tun), log);
2559 if (IS_ERR(a)) {
2560 dst_release((struct dst_entry *)tun_dst);
2561 return PTR_ERR(a);
2562 }
2563
2564 ovs_tun = nla_data(a);
2565 ovs_tun->tun_dst = tun_dst;
2566
2567 tun_info = &tun_dst->u.tun_info;
2568 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002569 if (key.tun_proto == AF_INET6)
2570 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002571 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002572
Pravin B Shelar4c222792015-08-30 18:09:38 -07002573 /* We need to store the options in the action itself since
2574 * everything else will go away after flow setup. We can append
2575 * it to tun_info and then point there.
2576 */
2577 ip_tunnel_info_opts_set(tun_info,
2578 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2579 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002580 add_nested_action_end(*sfa, start);
2581
2582 return err;
2583}
2584
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002585static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2586 bool is_push_nsh, bool log)
2587{
2588 struct sw_flow_match match;
2589 struct sw_flow_key key;
2590 int ret = 0;
2591
2592 ovs_match_init(&match, &key, true, NULL);
2593 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2594 is_push_nsh, log);
2595 return !ret;
2596}
2597
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002598/* Return false if there are any non-masked bits set.
2599 * Mask follows data immediately, before any netlink padding.
2600 */
2601static bool validate_masked(u8 *data, int len)
2602{
2603 u8 *mask = data + len;
2604
2605 while (len--)
2606 if (*data++ & ~*mask++)
2607 return false;
2608
2609 return true;
2610}
2611
Pravin B Shelare6445712013-10-03 18:16:47 -07002612static int validate_set(const struct nlattr *a,
2613 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002614 struct sw_flow_actions **sfa, bool *skip_copy,
2615 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002616{
2617 const struct nlattr *ovs_key = nla_data(a);
2618 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002619 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002620
2621 /* There can be only one key in a action */
2622 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2623 return -EINVAL;
2624
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002625 key_len = nla_len(ovs_key);
2626 if (masked)
2627 key_len /= 2;
2628
Pravin B Shelare6445712013-10-03 18:16:47 -07002629 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002630 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002631 return -EINVAL;
2632
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002633 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2634 return -EINVAL;
2635
Pravin B Shelare6445712013-10-03 18:16:47 -07002636 switch (key_type) {
2637 const struct ovs_key_ipv4 *ipv4_key;
2638 const struct ovs_key_ipv6 *ipv6_key;
2639 int err;
2640
2641 case OVS_KEY_ATTR_PRIORITY:
2642 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002643 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002644 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002645 break;
2646
Jiri Benc0a6410f2016-11-10 16:28:22 +01002647 case OVS_KEY_ATTR_ETHERNET:
2648 if (mac_proto != MAC_PROTO_ETHERNET)
2649 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002650 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002651
Pravin B Shelare6445712013-10-03 18:16:47 -07002652 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002653 if (masked)
2654 return -EINVAL; /* Masked tunnel set not supported. */
2655
2656 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002657 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002658 if (err)
2659 return err;
2660 break;
2661
2662 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002663 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002664 return -EINVAL;
2665
Pravin B Shelare6445712013-10-03 18:16:47 -07002666 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002667
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002668 if (masked) {
2669 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002670
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002671 /* Non-writeable fields. */
2672 if (mask->ipv4_proto || mask->ipv4_frag)
2673 return -EINVAL;
2674 } else {
2675 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2676 return -EINVAL;
2677
2678 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2679 return -EINVAL;
2680 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002681 break;
2682
2683 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002684 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002685 return -EINVAL;
2686
Pravin B Shelare6445712013-10-03 18:16:47 -07002687 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002688
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002689 if (masked) {
2690 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002691
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002692 /* Non-writeable fields. */
2693 if (mask->ipv6_proto || mask->ipv6_frag)
2694 return -EINVAL;
2695
2696 /* Invalid bits in the flow label mask? */
2697 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2698 return -EINVAL;
2699 } else {
2700 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2701 return -EINVAL;
2702
2703 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2704 return -EINVAL;
2705 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002706 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2707 return -EINVAL;
2708
2709 break;
2710
2711 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002712 if ((eth_type != htons(ETH_P_IP) &&
2713 eth_type != htons(ETH_P_IPV6)) ||
2714 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002715 return -EINVAL;
2716
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002717 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002718
2719 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002720 if ((eth_type != htons(ETH_P_IP) &&
2721 eth_type != htons(ETH_P_IPV6)) ||
2722 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002723 return -EINVAL;
2724
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002725 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002726
2727 case OVS_KEY_ATTR_MPLS:
2728 if (!eth_p_mpls(eth_type))
2729 return -EINVAL;
2730 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002731
2732 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002733 if ((eth_type != htons(ETH_P_IP) &&
2734 eth_type != htons(ETH_P_IPV6)) ||
2735 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002736 return -EINVAL;
2737
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002738 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002739
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002740 case OVS_KEY_ATTR_NSH:
2741 if (eth_type != htons(ETH_P_NSH))
2742 return -EINVAL;
2743 if (!validate_nsh(nla_data(a), masked, false, log))
2744 return -EINVAL;
2745 break;
2746
Pravin B Shelare6445712013-10-03 18:16:47 -07002747 default:
2748 return -EINVAL;
2749 }
2750
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002751 /* Convert non-masked non-tunnel set actions to masked set actions. */
2752 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2753 int start, len = key_len * 2;
2754 struct nlattr *at;
2755
2756 *skip_copy = true;
2757
2758 start = add_nested_action_start(sfa,
2759 OVS_ACTION_ATTR_SET_TO_MASKED,
2760 log);
2761 if (start < 0)
2762 return start;
2763
2764 at = __add_action(sfa, key_type, NULL, len, log);
2765 if (IS_ERR(at))
2766 return PTR_ERR(at);
2767
2768 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2769 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2770 /* Clear non-writeable bits from otherwise writeable fields. */
2771 if (key_type == OVS_KEY_ATTR_IPV6) {
2772 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2773
2774 mask->ipv6_label &= htonl(0x000FFFFF);
2775 }
2776 add_nested_action_end(*sfa, start);
2777 }
2778
Pravin B Shelare6445712013-10-03 18:16:47 -07002779 return 0;
2780}
2781
2782static int validate_userspace(const struct nlattr *attr)
2783{
2784 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2785 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2786 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002787 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002788 };
2789 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2790 int error;
2791
Johannes Bergfceb6432017-04-12 14:34:07 +02002792 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2793 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002794 if (error)
2795 return error;
2796
2797 if (!a[OVS_USERSPACE_ATTR_PID] ||
2798 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2799 return -EINVAL;
2800
2801 return 0;
2802}
2803
2804static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002805 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002806{
2807 int totlen = NLA_ALIGN(from->nla_len);
2808 struct nlattr *to;
2809
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002810 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002811 if (IS_ERR(to))
2812 return PTR_ERR(to);
2813
2814 memcpy(to, from, totlen);
2815 return 0;
2816}
2817
Joe Stringer7f8a4362015-08-26 11:31:48 -07002818static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002819 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002820 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002821 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002822{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002823 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002824 const struct nlattr *a;
2825 int rem, err;
2826
Pravin B Shelare6445712013-10-03 18:16:47 -07002827 nla_for_each_nested(a, attr, rem) {
2828 /* Expected argument lengths, (u32)-1 for variable length. */
2829 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2830 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002831 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002832 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002833 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2834 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002835 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2836 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2837 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002838 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002839 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002840 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2841 [OVS_ACTION_ATTR_CT] = (u32)-1,
Eric Garverb8226962017-10-10 16:54:44 -04002842 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
William Tuf2a4d082016-06-10 11:49:33 -07002843 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002844 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2845 [OVS_ACTION_ATTR_POP_ETH] = 0,
Yi Yangb2d0f5d2017-11-07 21:07:02 +08002846 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
2847 [OVS_ACTION_ATTR_POP_NSH] = 0,
Andy Zhoucd8a6c32017-11-10 12:09:43 -08002848 [OVS_ACTION_ATTR_METER] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002849 };
2850 const struct ovs_action_push_vlan *vlan;
2851 int type = nla_type(a);
2852 bool skip_copy;
2853
2854 if (type > OVS_ACTION_ATTR_MAX ||
2855 (action_lens[type] != nla_len(a) &&
2856 action_lens[type] != (u32)-1))
2857 return -EINVAL;
2858
2859 skip_copy = false;
2860 switch (type) {
2861 case OVS_ACTION_ATTR_UNSPEC:
2862 return -EINVAL;
2863
2864 case OVS_ACTION_ATTR_USERSPACE:
2865 err = validate_userspace(a);
2866 if (err)
2867 return err;
2868 break;
2869
2870 case OVS_ACTION_ATTR_OUTPUT:
2871 if (nla_get_u32(a) >= DP_MAX_PORTS)
2872 return -EINVAL;
2873 break;
2874
William Tuf2a4d082016-06-10 11:49:33 -07002875 case OVS_ACTION_ATTR_TRUNC: {
2876 const struct ovs_action_trunc *trunc = nla_data(a);
2877
2878 if (trunc->max_len < ETH_HLEN)
2879 return -EINVAL;
2880 break;
2881 }
2882
Andy Zhou971427f32014-09-15 19:37:25 -07002883 case OVS_ACTION_ATTR_HASH: {
2884 const struct ovs_action_hash *act_hash = nla_data(a);
2885
2886 switch (act_hash->hash_alg) {
2887 case OVS_HASH_ALG_L4:
2888 break;
2889 default:
2890 return -EINVAL;
2891 }
2892
2893 break;
2894 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002895
2896 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002897 if (mac_proto != MAC_PROTO_ETHERNET)
2898 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002899 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002900 break;
2901
2902 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002903 if (mac_proto != MAC_PROTO_ETHERNET)
2904 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002905 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002906 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002907 return -EINVAL;
2908 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2909 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002910 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002911 break;
2912
Andy Zhou971427f32014-09-15 19:37:25 -07002913 case OVS_ACTION_ATTR_RECIRC:
2914 break;
2915
Simon Horman25cd9ba2014-10-06 05:05:13 -07002916 case OVS_ACTION_ATTR_PUSH_MPLS: {
2917 const struct ovs_action_push_mpls *mpls = nla_data(a);
2918
Simon Horman25cd9ba2014-10-06 05:05:13 -07002919 if (!eth_p_mpls(mpls->mpls_ethertype))
2920 return -EINVAL;
2921 /* Prohibit push MPLS other than to a white list
2922 * for packets that have a known tag order.
2923 */
2924 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2925 (eth_type != htons(ETH_P_IP) &&
2926 eth_type != htons(ETH_P_IPV6) &&
2927 eth_type != htons(ETH_P_ARP) &&
2928 eth_type != htons(ETH_P_RARP) &&
2929 !eth_p_mpls(eth_type)))
2930 return -EINVAL;
2931 eth_type = mpls->mpls_ethertype;
2932 break;
2933 }
2934
2935 case OVS_ACTION_ATTR_POP_MPLS:
2936 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2937 !eth_p_mpls(eth_type))
2938 return -EINVAL;
2939
2940 /* Disallow subsequent L2.5+ set and mpls_pop actions
2941 * as there is no check here to ensure that the new
2942 * eth_type is valid and thus set actions could
2943 * write off the end of the packet or otherwise
2944 * corrupt it.
2945 *
2946 * Support for these actions is planned using packet
2947 * recirculation.
2948 */
2949 eth_type = htons(0);
2950 break;
2951
Pravin B Shelare6445712013-10-03 18:16:47 -07002952 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002953 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002954 &skip_copy, mac_proto, eth_type,
2955 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002956 if (err)
2957 return err;
2958 break;
2959
2960 case OVS_ACTION_ATTR_SET_MASKED:
2961 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002962 &skip_copy, mac_proto, eth_type,
2963 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002964 if (err)
2965 return err;
2966 break;
2967
andy zhou798c1662017-03-20 16:32:29 -07002968 case OVS_ACTION_ATTR_SAMPLE: {
2969 bool last = nla_is_last(a, rem);
2970
2971 err = validate_and_copy_sample(net, a, key, sfa,
2972 eth_type, vlan_tci,
2973 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07002974 if (err)
2975 return err;
2976 skip_copy = true;
2977 break;
andy zhou798c1662017-03-20 16:32:29 -07002978 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002979
Joe Stringer7f8a4362015-08-26 11:31:48 -07002980 case OVS_ACTION_ATTR_CT:
2981 err = ovs_ct_copy_action(net, a, key, sfa, log);
2982 if (err)
2983 return err;
2984 skip_copy = true;
2985 break;
2986
Eric Garverb8226962017-10-10 16:54:44 -04002987 case OVS_ACTION_ATTR_CT_CLEAR:
2988 break;
2989
Jiri Benc91820da2016-11-10 16:28:23 +01002990 case OVS_ACTION_ATTR_PUSH_ETH:
2991 /* Disallow pushing an Ethernet header if one
2992 * is already present */
2993 if (mac_proto != MAC_PROTO_NONE)
2994 return -EINVAL;
2995 mac_proto = MAC_PROTO_NONE;
2996 break;
2997
2998 case OVS_ACTION_ATTR_POP_ETH:
2999 if (mac_proto != MAC_PROTO_ETHERNET)
3000 return -EINVAL;
3001 if (vlan_tci & htons(VLAN_TAG_PRESENT))
3002 return -EINVAL;
3003 mac_proto = MAC_PROTO_ETHERNET;
3004 break;
3005
Yi Yangb2d0f5d2017-11-07 21:07:02 +08003006 case OVS_ACTION_ATTR_PUSH_NSH:
3007 if (mac_proto != MAC_PROTO_ETHERNET) {
3008 u8 next_proto;
3009
3010 next_proto = tun_p_from_eth_p(eth_type);
3011 if (!next_proto)
3012 return -EINVAL;
3013 }
3014 mac_proto = MAC_PROTO_NONE;
3015 if (!validate_nsh(nla_data(a), false, true, true))
3016 return -EINVAL;
3017 break;
3018
3019 case OVS_ACTION_ATTR_POP_NSH: {
3020 __be16 inner_proto;
3021
3022 if (eth_type != htons(ETH_P_NSH))
3023 return -EINVAL;
3024 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
3025 if (!inner_proto)
3026 return -EINVAL;
3027 if (key->nsh.base.np == TUN_P_ETHERNET)
3028 mac_proto = MAC_PROTO_ETHERNET;
3029 else
3030 mac_proto = MAC_PROTO_NONE;
3031 break;
3032 }
3033
Andy Zhoucd8a6c32017-11-10 12:09:43 -08003034 case OVS_ACTION_ATTR_METER:
3035 /* Non-existent meters are simply ignored. */
3036 break;
3037
Pravin B Shelare6445712013-10-03 18:16:47 -07003038 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003039 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07003040 return -EINVAL;
3041 }
3042 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003043 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07003044 if (err)
3045 return err;
3046 }
3047 }
3048
3049 if (rem > 0)
3050 return -EINVAL;
3051
3052 return 0;
3053}
3054
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003055/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07003056int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07003057 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08003058 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07003059{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003060 int err;
3061
zhangliping67c8d222017-11-25 22:02:12 +08003062 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003063 if (IS_ERR(*sfa))
3064 return PTR_ERR(*sfa);
3065
Joe Stringer8e2fed12015-08-26 11:31:44 -07003066 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07003067 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04003068 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003069 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02003070 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07003071
3072 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07003073}
3074
andy zhou798c1662017-03-20 16:32:29 -07003075static int sample_action_to_attr(const struct nlattr *attr,
3076 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07003077{
andy zhou798c1662017-03-20 16:32:29 -07003078 struct nlattr *start, *ac_start = NULL, *sample_arg;
3079 int err = 0, rem = nla_len(attr);
3080 const struct sample_arg *arg;
3081 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07003082
3083 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
3084 if (!start)
3085 return -EMSGSIZE;
3086
andy zhou798c1662017-03-20 16:32:29 -07003087 sample_arg = nla_data(attr);
3088 arg = nla_data(sample_arg);
3089 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07003090
andy zhou798c1662017-03-20 16:32:29 -07003091 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3092 err = -EMSGSIZE;
3093 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07003094 }
3095
andy zhou798c1662017-03-20 16:32:29 -07003096 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
3097 if (!ac_start) {
3098 err = -EMSGSIZE;
3099 goto out;
3100 }
3101
3102 err = ovs_nla_put_actions(actions, rem, skb);
3103
3104out:
3105 if (err) {
3106 nla_nest_cancel(skb, ac_start);
3107 nla_nest_cancel(skb, start);
3108 } else {
3109 nla_nest_end(skb, ac_start);
3110 nla_nest_end(skb, start);
3111 }
3112
Pravin B Shelare6445712013-10-03 18:16:47 -07003113 return err;
3114}
3115
3116static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3117{
3118 const struct nlattr *ovs_key = nla_data(a);
3119 int key_type = nla_type(ovs_key);
3120 struct nlattr *start;
3121 int err;
3122
3123 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07003124 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02003125 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3126 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003127
Pravin B Shelare6445712013-10-03 18:16:47 -07003128 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3129 if (!start)
3130 return -EMSGSIZE;
3131
Simon Hormane905eab2015-12-18 19:43:15 +09003132 err = ip_tun_to_nlattr(skb, &tun_info->key,
3133 ip_tunnel_info_opts(tun_info),
3134 tun_info->options_len,
3135 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07003136 if (err)
3137 return err;
3138 nla_nest_end(skb, start);
3139 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07003140 }
Pravin B Shelare6445712013-10-03 18:16:47 -07003141 default:
3142 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3143 return -EMSGSIZE;
3144 break;
3145 }
3146
3147 return 0;
3148}
3149
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003150static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3151 struct sk_buff *skb)
3152{
3153 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08003154 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003155 size_t key_len = nla_len(ovs_key) / 2;
3156
3157 /* Revert the conversion we did from a non-masked set action to
3158 * masked set action.
3159 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08003160 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3161 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003162 return -EMSGSIZE;
3163
Joe Stringerf4f8e732015-03-02 18:49:56 -08003164 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3165 return -EMSGSIZE;
3166
3167 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003168 return 0;
3169}
3170
Pravin B Shelare6445712013-10-03 18:16:47 -07003171int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3172{
3173 const struct nlattr *a;
3174 int rem, err;
3175
3176 nla_for_each_attr(a, attr, len, rem) {
3177 int type = nla_type(a);
3178
3179 switch (type) {
3180 case OVS_ACTION_ATTR_SET:
3181 err = set_action_to_attr(a, skb);
3182 if (err)
3183 return err;
3184 break;
3185
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08003186 case OVS_ACTION_ATTR_SET_TO_MASKED:
3187 err = masked_set_action_to_set_action_attr(a, skb);
3188 if (err)
3189 return err;
3190 break;
3191
Pravin B Shelare6445712013-10-03 18:16:47 -07003192 case OVS_ACTION_ATTR_SAMPLE:
3193 err = sample_action_to_attr(a, skb);
3194 if (err)
3195 return err;
3196 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07003197
3198 case OVS_ACTION_ATTR_CT:
3199 err = ovs_ct_action_to_attr(nla_data(a), skb);
3200 if (err)
3201 return err;
3202 break;
3203
Pravin B Shelare6445712013-10-03 18:16:47 -07003204 default:
3205 if (nla_put(skb, type, nla_len(a), nla_data(a)))
3206 return -EMSGSIZE;
3207 break;
3208 }
3209 }
3210
3211 return 0;
3212}