blob: dc0d79092e7429b3098a0b0665f88e5e835decb9 [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>
William Tuceaa0012017-10-04 17:03:12 -070051#include <net/erspan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070052
53#include "flow_netlink.h"
54
Thomas Graf81bfe3c32015-01-15 03:53:58 +010055struct ovs_len_tbl {
56 int len;
57 const struct ovs_len_tbl *next;
58};
59
60#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070061#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c32015-01-15 03:53:58 +010062
andy zhou798c1662017-03-20 16:32:29 -070063static bool actions_may_change_flow(const struct nlattr *actions)
64{
65 struct nlattr *nla;
66 int rem;
67
68 nla_for_each_nested(nla, actions, rem) {
69 u16 action = nla_type(nla);
70
71 switch (action) {
72 case OVS_ACTION_ATTR_OUTPUT:
73 case OVS_ACTION_ATTR_RECIRC:
74 case OVS_ACTION_ATTR_TRUNC:
75 case OVS_ACTION_ATTR_USERSPACE:
76 break;
77
78 case OVS_ACTION_ATTR_CT:
Eric Garverb8226962017-10-10 16:54:44 -040079 case OVS_ACTION_ATTR_CT_CLEAR:
andy zhou798c1662017-03-20 16:32:29 -070080 case OVS_ACTION_ATTR_HASH:
81 case OVS_ACTION_ATTR_POP_ETH:
82 case OVS_ACTION_ATTR_POP_MPLS:
83 case OVS_ACTION_ATTR_POP_VLAN:
84 case OVS_ACTION_ATTR_PUSH_ETH:
85 case OVS_ACTION_ATTR_PUSH_MPLS:
86 case OVS_ACTION_ATTR_PUSH_VLAN:
87 case OVS_ACTION_ATTR_SAMPLE:
88 case OVS_ACTION_ATTR_SET:
89 case OVS_ACTION_ATTR_SET_MASKED:
90 default:
91 return true;
92 }
93 }
94 return false;
95}
96
Pravin B Shelara85311b2014-10-19 12:03:40 -070097static void update_range(struct sw_flow_match *match,
98 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070099{
Pravin B Shelara85311b2014-10-19 12:03:40 -0700100 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -0700101 size_t start = rounddown(offset, sizeof(long));
102 size_t end = roundup(offset + size, sizeof(long));
103
104 if (!is_mask)
105 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -0700106 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700107 range = &match->mask->range;
108
Pravin B Shelare6445712013-10-03 18:16:47 -0700109 if (range->start == range->end) {
110 range->start = start;
111 range->end = end;
112 return;
113 }
114
115 if (range->start > start)
116 range->start = start;
117
118 if (range->end < end)
119 range->end = end;
120}
121
122#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
123 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700124 update_range(match, offsetof(struct sw_flow_key, field), \
125 sizeof((match)->key->field), is_mask); \
126 if (is_mask) \
127 (match)->mask->key.field = value; \
128 else \
Pravin B Shelare6445712013-10-03 18:16:47 -0700129 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700130 } while (0)
131
Jesse Grossf5796682014-10-03 15:35:33 -0700132#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
133 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700134 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700135 if (is_mask) \
136 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700137 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700138 else \
139 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700140 } while (0)
141
Jesse Grossf5796682014-10-03 15:35:33 -0700142#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
143 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
144 value_p, len, is_mask)
145
Pravin B Shelara85311b2014-10-19 12:03:40 -0700146#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
147 do { \
148 update_range(match, offsetof(struct sw_flow_key, field), \
149 sizeof((match)->key->field), is_mask); \
150 if (is_mask) \
151 memset((u8 *)&(match)->mask->key.field, value, \
152 sizeof((match)->mask->key.field)); \
153 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700154 memset((u8 *)&(match)->key->field, value, \
155 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700156 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700157
158static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800159 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700160{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100161 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700162 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
163
164 /* The following mask attributes allowed only if they
165 * pass the validation tests. */
166 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800167 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700168 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800169 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700170 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700171 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700172 | (1 << OVS_KEY_ATTR_UDP)
173 | (1 << OVS_KEY_ATTR_SCTP)
174 | (1 << OVS_KEY_ATTR_ICMP)
175 | (1 << OVS_KEY_ATTR_ICMPV6)
176 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700177 | (1 << OVS_KEY_ATTR_ND)
178 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700179
180 /* Always allowed mask fields. */
181 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
182 | (1 << OVS_KEY_ATTR_IN_PORT)
183 | (1 << OVS_KEY_ATTR_ETHERTYPE));
184
185 /* Check key attributes. */
186 if (match->key->eth.type == htons(ETH_P_ARP)
187 || match->key->eth.type == htons(ETH_P_RARP)) {
188 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800189 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700190 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
191 }
192
Simon Horman25cd9ba2014-10-06 05:05:13 -0700193 if (eth_p_mpls(match->key->eth.type)) {
194 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
195 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
196 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
197 }
198
Pravin B Shelare6445712013-10-03 18:16:47 -0700199 if (match->key->eth.type == htons(ETH_P_IP)) {
200 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800201 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700202 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800203 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
204 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700205
206 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
207 if (match->key->ip.proto == IPPROTO_UDP) {
208 key_expected |= 1 << OVS_KEY_ATTR_UDP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_SCTP) {
214 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
215 if (match->mask && (match->mask->key.ip.proto == 0xff))
216 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
217 }
218
219 if (match->key->ip.proto == IPPROTO_TCP) {
220 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700221 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
222 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700223 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700224 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
225 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700226 }
227
228 if (match->key->ip.proto == IPPROTO_ICMP) {
229 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
230 if (match->mask && (match->mask->key.ip.proto == 0xff))
231 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
232 }
233 }
234 }
235
236 if (match->key->eth.type == htons(ETH_P_IPV6)) {
237 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800238 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700239 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800240 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
241 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700242
243 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
244 if (match->key->ip.proto == IPPROTO_UDP) {
245 key_expected |= 1 << OVS_KEY_ATTR_UDP;
246 if (match->mask && (match->mask->key.ip.proto == 0xff))
247 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
248 }
249
250 if (match->key->ip.proto == IPPROTO_SCTP) {
251 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
252 if (match->mask && (match->mask->key.ip.proto == 0xff))
253 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
254 }
255
256 if (match->key->ip.proto == IPPROTO_TCP) {
257 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700258 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
259 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700260 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700261 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
262 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700263 }
264
265 if (match->key->ip.proto == IPPROTO_ICMPV6) {
266 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
267 if (match->mask && (match->mask->key.ip.proto == 0xff))
268 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
269
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700270 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700271 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700272 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700273 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800274 /* Original direction conntrack tuple
275 * uses the same space as the ND fields
276 * in the key, so both are not allowed
277 * at the same time.
278 */
279 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800280 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700281 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
282 }
283 }
284 }
285 }
286
287 if ((key_attrs & key_expected) != key_expected) {
288 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800289 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
290 (unsigned long long)key_attrs,
291 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700292 return false;
293 }
294
295 if ((mask_attrs & mask_allowed) != mask_attrs) {
296 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800297 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
298 (unsigned long long)mask_attrs,
299 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700300 return false;
301 }
302
303 return true;
304}
305
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800306size_t ovs_tun_key_attr_size(void)
307{
308 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
309 * updating this function.
310 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200311 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200312 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
313 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800314 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
315 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
316 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
317 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
318 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
319 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100320 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
321 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
322 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800323 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
William Tuceaa0012017-10-04 17:03:12 -0700324 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_DST */
325 + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800326}
327
Joe Stringer41af73e2014-10-18 16:14:14 -0700328size_t ovs_key_attr_size(void)
329{
330 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
331 * updating this function.
332 */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800333 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
Joe Stringer41af73e2014-10-18 16:14:14 -0700334
335 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
336 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800337 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700338 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
339 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
340 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
341 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700342 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700343 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700344 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700345 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800346 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Joe Stringer41af73e2014-10-18 16:14:14 -0700347 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
348 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
349 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
350 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
351 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
352 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
353 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
354 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
355}
356
Jesse Gross982b5272015-09-11 18:38:28 -0700357static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
358 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
359};
360
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100361static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
362 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
363 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
364 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
365 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
366 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
367 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
368 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
369 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
370 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
371 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700372 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
373 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
374 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200375 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
376 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
William Tuceaa0012017-10-04 17:03:12 -0700377 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = sizeof(u32) },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100378};
379
Pravin B Shelare6445712013-10-03 18:16:47 -0700380/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100381static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
382 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
383 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
384 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
385 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
386 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
387 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
388 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
389 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
390 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
391 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
392 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
393 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
394 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
395 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
396 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
397 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
398 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
399 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
400 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
401 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
402 .next = ovs_tunnel_key_lens, },
403 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700404 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700405 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700406 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700407 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800408 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
409 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
410 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
411 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700412};
413
Jesse Gross982b5272015-09-11 18:38:28 -0700414static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
415{
416 return expected_len == attr_len ||
417 expected_len == OVS_ATTR_NESTED ||
418 expected_len == OVS_ATTR_VARIABLE;
419}
420
Pravin B Shelare6445712013-10-03 18:16:47 -0700421static bool is_all_zero(const u8 *fp, size_t size)
422{
423 int i;
424
425 if (!fp)
426 return false;
427
428 for (i = 0; i < size; i++)
429 if (fp[i])
430 return false;
431
432 return true;
433}
434
435static int __parse_flow_nlattrs(const struct nlattr *attr,
436 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800437 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700438{
439 const struct nlattr *nla;
440 u64 attrs;
441 int rem;
442
443 attrs = *attrsp;
444 nla_for_each_nested(nla, attr, rem) {
445 u16 type = nla_type(nla);
446 int expected_len;
447
448 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800449 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700450 type, OVS_KEY_ATTR_MAX);
451 return -EINVAL;
452 }
453
454 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800455 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700456 return -EINVAL;
457 }
458
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100459 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700460 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800461 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
462 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700463 return -EINVAL;
464 }
465
466 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
467 attrs |= 1 << type;
468 a[type] = nla;
469 }
470 }
471 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800472 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700473 return -EINVAL;
474 }
475
476 *attrsp = attrs;
477 return 0;
478}
479
480static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800481 const struct nlattr *a[], u64 *attrsp,
482 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700483{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800484 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700485}
486
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800487int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
488 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700489{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800490 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
491}
492
493static int genev_tun_opt_from_nlattr(const struct nlattr *a,
494 struct sw_flow_match *match, bool is_mask,
495 bool log)
496{
497 unsigned long opt_key_offset;
498
499 if (nla_len(a) > sizeof(match->key->tun_opts)) {
500 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
501 nla_len(a), sizeof(match->key->tun_opts));
502 return -EINVAL;
503 }
504
505 if (nla_len(a) % 4 != 0) {
506 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
507 nla_len(a));
508 return -EINVAL;
509 }
510
511 /* We need to record the length of the options passed
512 * down, otherwise packets with the same format but
513 * additional options will be silently matched.
514 */
515 if (!is_mask) {
516 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
517 false);
518 } else {
519 /* This is somewhat unusual because it looks at
520 * both the key and mask while parsing the
521 * attributes (and by extension assumes the key
522 * is parsed first). Normally, we would verify
523 * that each is the correct length and that the
524 * attributes line up in the validate function.
525 * However, that is difficult because this is
526 * variable length and we won't have the
527 * information later.
528 */
529 if (match->key->tun_opts_len != nla_len(a)) {
530 OVS_NLERR(log, "Geneve option len %d != mask len %d",
531 match->key->tun_opts_len, nla_len(a));
532 return -EINVAL;
533 }
534
535 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
536 }
537
Thomas Grafd91641d2015-01-15 03:53:57 +0100538 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800539 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
540 nla_len(a), is_mask);
541 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700542}
543
Jesse Gross982b5272015-09-11 18:38:28 -0700544static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100545 struct sw_flow_match *match, bool is_mask,
546 bool log)
547{
Jesse Gross982b5272015-09-11 18:38:28 -0700548 struct nlattr *a;
549 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100550 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200551 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100552
553 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
554
Thomas Graf1dd144c2015-01-15 03:53:59 +0100555 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700556 nla_for_each_nested(a, attr, rem) {
557 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100558
Jesse Gross982b5272015-09-11 18:38:28 -0700559 if (type > OVS_VXLAN_EXT_MAX) {
560 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
561 type, OVS_VXLAN_EXT_MAX);
562 return -EINVAL;
563 }
564
565 if (!check_attr_len(nla_len(a),
566 ovs_vxlan_ext_key_lens[type].len)) {
567 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
568 type, nla_len(a),
569 ovs_vxlan_ext_key_lens[type].len);
570 return -EINVAL;
571 }
572
573 switch (type) {
574 case OVS_VXLAN_EXT_GBP:
575 opts.gbp = nla_get_u32(a);
576 break;
577 default:
578 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
579 type);
580 return -EINVAL;
581 }
582 }
583 if (rem) {
584 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
585 rem);
586 return -EINVAL;
587 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100588
589 if (!is_mask)
590 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
591 else
592 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
593
594 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
595 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
596 is_mask);
597 return 0;
598}
599
William Tuceaa0012017-10-04 17:03:12 -0700600static int erspan_tun_opt_from_nlattr(const struct nlattr *attr,
601 struct sw_flow_match *match, bool is_mask,
602 bool log)
603{
604 unsigned long opt_key_offset;
605 struct erspan_metadata opts;
606
607 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
608
609 memset(&opts, 0, sizeof(opts));
610 opts.index = nla_get_be32(attr);
611
612 /* Index has only 20-bit */
613 if (ntohl(opts.index) & ~INDEX_MASK) {
614 OVS_NLERR(log, "ERSPAN index number %x too large.",
615 ntohl(opts.index));
616 return -EINVAL;
617 }
618
619 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), is_mask);
620 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
621 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
622 is_mask);
623
624 return 0;
625}
626
Jiri Benc6b26ba32015-10-05 13:09:47 +0200627static int ip_tun_from_nlattr(const struct nlattr *attr,
628 struct sw_flow_match *match, bool is_mask,
629 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700630{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700631 bool ttl = false, ipv4 = false, ipv6 = false;
632 __be16 tun_flags = 0;
633 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700634 struct nlattr *a;
635 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700636
637 nla_for_each_nested(a, attr, rem) {
638 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800639 int err;
640
Pravin B Shelare6445712013-10-03 18:16:47 -0700641 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800642 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
643 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700644 return -EINVAL;
645 }
646
Jesse Gross982b5272015-09-11 18:38:28 -0700647 if (!check_attr_len(nla_len(a),
648 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800649 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100650 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700651 return -EINVAL;
652 }
653
654 switch (type) {
655 case OVS_TUNNEL_KEY_ATTR_ID:
656 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
657 nla_get_be64(a), is_mask);
658 tun_flags |= TUNNEL_KEY;
659 break;
660 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200661 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200662 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200663 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700664 break;
665 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200666 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200667 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200668 ipv4 = true;
669 break;
670 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200671 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200672 nla_get_in6_addr(a), is_mask);
673 ipv6 = true;
674 break;
675 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
676 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
677 nla_get_in6_addr(a), is_mask);
678 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700679 break;
680 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200681 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700682 nla_get_u8(a), is_mask);
683 break;
684 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200685 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700686 nla_get_u8(a), is_mask);
687 ttl = true;
688 break;
689 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
690 tun_flags |= TUNNEL_DONT_FRAGMENT;
691 break;
692 case OVS_TUNNEL_KEY_ATTR_CSUM:
693 tun_flags |= TUNNEL_CSUM;
694 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800695 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
696 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
697 nla_get_be16(a), is_mask);
698 break;
699 case OVS_TUNNEL_KEY_ATTR_TP_DST:
700 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
701 nla_get_be16(a), is_mask);
702 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700703 case OVS_TUNNEL_KEY_ATTR_OAM:
704 tun_flags |= TUNNEL_OAM;
705 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700706 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100707 if (opts_type) {
708 OVS_NLERR(log, "Multiple metadata blocks provided");
709 return -EINVAL;
710 }
711
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800712 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
713 if (err)
714 return err;
715
Thomas Graf1dd144c2015-01-15 03:53:59 +0100716 tun_flags |= TUNNEL_GENEVE_OPT;
717 opts_type = type;
718 break;
719 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
720 if (opts_type) {
721 OVS_NLERR(log, "Multiple metadata blocks provided");
722 return -EINVAL;
723 }
724
725 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
726 if (err)
727 return err;
728
729 tun_flags |= TUNNEL_VXLAN_OPT;
730 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700731 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500732 case OVS_TUNNEL_KEY_ATTR_PAD:
733 break;
William Tuceaa0012017-10-04 17:03:12 -0700734 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
735 if (opts_type) {
736 OVS_NLERR(log, "Multiple metadata blocks provided");
737 return -EINVAL;
738 }
739
740 err = erspan_tun_opt_from_nlattr(a, match, is_mask, log);
741 if (err)
742 return err;
743
744 tun_flags |= TUNNEL_ERSPAN_OPT;
745 opts_type = type;
746 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700747 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200748 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700749 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700750 return -EINVAL;
751 }
752 }
753
754 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200755 if (is_mask)
756 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
757 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200758 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
759 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700760
761 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200762 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800763 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700764 return -EINVAL;
765 }
766
Jiri Benc6b26ba32015-10-05 13:09:47 +0200767 if (ipv4 && ipv6) {
768 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
769 return -EINVAL;
770 }
771
Pravin B Shelare6445712013-10-03 18:16:47 -0700772 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200773 if (!ipv4 && !ipv6) {
774 OVS_NLERR(log, "IP tunnel dst address not specified");
775 return -EINVAL;
776 }
777 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800778 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700779 return -EINVAL;
780 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200781 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
782 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
783 return -EINVAL;
784 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700785
786 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200787 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700788 return -EINVAL;
789 }
790 }
791
Thomas Graf1dd144c2015-01-15 03:53:59 +0100792 return opts_type;
793}
794
795static int vxlan_opt_to_nlattr(struct sk_buff *skb,
796 const void *tun_opts, int swkey_tun_opts_len)
797{
Thomas Graf614732e2015-07-21 10:44:06 +0200798 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100799 struct nlattr *nla;
800
801 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
802 if (!nla)
803 return -EMSGSIZE;
804
805 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
806 return -EMSGSIZE;
807
808 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700809 return 0;
810}
811
Jiri Benc6b26ba32015-10-05 13:09:47 +0200812static int __ip_tun_to_nlattr(struct sk_buff *skb,
813 const struct ip_tunnel_key *output,
814 const void *tun_opts, int swkey_tun_opts_len,
815 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700816{
Pravin B Shelare6445712013-10-03 18:16:47 -0700817 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200818 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
819 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700820 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200821 switch (tun_proto) {
822 case AF_INET:
823 if (output->u.ipv4.src &&
824 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
825 output->u.ipv4.src))
826 return -EMSGSIZE;
827 if (output->u.ipv4.dst &&
828 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
829 output->u.ipv4.dst))
830 return -EMSGSIZE;
831 break;
832 case AF_INET6:
833 if (!ipv6_addr_any(&output->u.ipv6.src) &&
834 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
835 &output->u.ipv6.src))
836 return -EMSGSIZE;
837 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
838 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
839 &output->u.ipv6.dst))
840 return -EMSGSIZE;
841 break;
842 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200843 if (output->tos &&
844 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700845 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200846 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700847 return -EMSGSIZE;
848 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700849 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700850 return -EMSGSIZE;
851 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700852 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
853 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800854 if (output->tp_src &&
855 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
856 return -EMSGSIZE;
857 if (output->tp_dst &&
858 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
859 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700860 if ((output->tun_flags & TUNNEL_OAM) &&
861 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700862 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700863 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100864 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
865 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
866 swkey_tun_opts_len, tun_opts))
867 return -EMSGSIZE;
868 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
869 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
870 return -EMSGSIZE;
William Tuceaa0012017-10-04 17:03:12 -0700871 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
872 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
873 ((struct erspan_metadata *)tun_opts)->index))
874 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100875 }
Jesse Grossf5796682014-10-03 15:35:33 -0700876
877 return 0;
878}
879
Jiri Benc6b26ba32015-10-05 13:09:47 +0200880static int ip_tun_to_nlattr(struct sk_buff *skb,
881 const struct ip_tunnel_key *output,
882 const void *tun_opts, int swkey_tun_opts_len,
883 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700884{
885 struct nlattr *nla;
886 int err;
887
888 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
889 if (!nla)
890 return -EMSGSIZE;
891
Jiri Benc6b26ba32015-10-05 13:09:47 +0200892 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
893 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700894 if (err)
895 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700896
897 nla_nest_end(skb, nla);
898 return 0;
899}
900
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700901int ovs_nla_put_tunnel_info(struct sk_buff *skb,
902 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800903{
David S. Millerba3e2082015-10-24 06:54:12 -0700904 return __ip_tun_to_nlattr(skb, &tun_info->key,
905 ip_tunnel_info_opts(tun_info),
906 tun_info->options_len,
907 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800908}
909
Eric Garver018c1dd2016-09-07 12:56:59 -0400910static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
911 const struct nlattr *a[],
912 bool is_mask, bool inner)
913{
914 __be16 tci = 0;
915 __be16 tpid = 0;
916
917 if (a[OVS_KEY_ATTR_VLAN])
918 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
919
920 if (a[OVS_KEY_ATTR_ETHERTYPE])
921 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
922
923 if (likely(!inner)) {
924 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
925 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
926 } else {
927 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
928 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
929 }
930 return 0;
931}
932
933static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
934 u64 key_attrs, bool inner,
935 const struct nlattr **a, bool log)
936{
937 __be16 tci = 0;
938
939 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
940 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
941 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
942 /* Not a VLAN. */
943 return 0;
944 }
945
946 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
947 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
948 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
949 return -EINVAL;
950 }
951
952 if (a[OVS_KEY_ATTR_VLAN])
953 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
954
955 if (!(tci & htons(VLAN_TAG_PRESENT))) {
956 if (tci) {
957 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
958 (inner) ? "C-VLAN" : "VLAN");
959 return -EINVAL;
960 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
961 /* Corner case for truncated VLAN header. */
962 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
963 (inner) ? "C-VLAN" : "VLAN");
964 return -EINVAL;
965 }
966 }
967
968 return 1;
969}
970
971static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
972 u64 key_attrs, bool inner,
973 const struct nlattr **a, bool log)
974{
975 __be16 tci = 0;
976 __be16 tpid = 0;
977 bool encap_valid = !!(match->key->eth.vlan.tci &
978 htons(VLAN_TAG_PRESENT));
979 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
980 htons(VLAN_TAG_PRESENT));
981
982 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
983 /* Not a VLAN. */
984 return 0;
985 }
986
987 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
988 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
989 (inner) ? "C-VLAN" : "VLAN");
990 return -EINVAL;
991 }
992
993 if (a[OVS_KEY_ATTR_VLAN])
994 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
995
996 if (a[OVS_KEY_ATTR_ETHERTYPE])
997 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
998
999 if (tpid != htons(0xffff)) {
1000 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1001 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1002 return -EINVAL;
1003 }
1004 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1005 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
1006 (inner) ? "C-VLAN" : "VLAN");
1007 return -EINVAL;
1008 }
1009
1010 return 1;
1011}
1012
1013static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1014 u64 *key_attrs, bool inner,
1015 const struct nlattr **a, bool is_mask,
1016 bool log)
1017{
1018 int err;
1019 const struct nlattr *encap;
1020
1021 if (!is_mask)
1022 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1023 a, log);
1024 else
1025 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1026 a, log);
1027 if (err <= 0)
1028 return err;
1029
1030 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1031 if (err)
1032 return err;
1033
1034 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1035 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1036 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1037
1038 encap = a[OVS_KEY_ATTR_ENCAP];
1039
1040 if (!is_mask)
1041 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1042 else
1043 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1044
1045 return err;
1046}
1047
1048static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1049 u64 *key_attrs, const struct nlattr **a,
1050 bool is_mask, bool log)
1051{
1052 int err;
1053 bool encap_valid = false;
1054
1055 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1056 is_mask, log);
1057 if (err)
1058 return err;
1059
1060 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1061 if (encap_valid) {
1062 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1063 is_mask, log);
1064 if (err)
1065 return err;
1066 }
1067
1068 return 0;
1069}
1070
Jiri Benc0a6410f2016-11-10 16:28:22 +01001071static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1072 u64 *attrs, const struct nlattr **a,
1073 bool is_mask, bool log)
1074{
1075 __be16 eth_type;
1076
1077 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1078 if (is_mask) {
1079 /* Always exact match EtherType. */
1080 eth_type = htons(0xffff);
1081 } else if (!eth_proto_is_802_3(eth_type)) {
1082 OVS_NLERR(log, "EtherType %x is less than min %x",
1083 ntohs(eth_type), ETH_P_802_3_MIN);
1084 return -EINVAL;
1085 }
1086
1087 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1088 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1089 return 0;
1090}
1091
Joe Stringerc2ac6672015-08-26 11:31:52 -07001092static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1093 u64 *attrs, const struct nlattr **a,
1094 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001095{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001096 u8 mac_proto = MAC_PROTO_ETHERNET;
1097
Andy Zhou971427f32014-09-15 19:37:25 -07001098 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1099 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1100
1101 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1102 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1103 }
1104
1105 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1106 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1107
1108 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1109 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1110 }
1111
Pravin B Shelare6445712013-10-03 18:16:47 -07001112 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1113 SW_FLOW_KEY_PUT(match, phy.priority,
1114 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1115 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1116 }
1117
1118 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1119 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1120
Jesse Gross426cda52014-10-06 05:08:38 -07001121 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001122 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001123 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001124 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001125 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001126 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001127 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001128
1129 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1130 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1131 } else if (!is_mask) {
1132 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1133 }
1134
1135 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1136 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1137
1138 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1139 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1140 }
1141 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001142 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1143 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001144 return -EINVAL;
1145 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1146 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001147
1148 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001149 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001150 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001151
Joe Stringer9e384712015-10-19 19:18:57 -07001152 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001153 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001154 ct_state);
1155 return -EINVAL;
1156 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001157
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001158 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001159 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1160 }
1161 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001162 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001163 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1164
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001165 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001166 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1167 }
Joe Stringer182e3042015-08-26 11:31:49 -07001168 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001169 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001170 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1171
1172 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1173 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1174 }
Joe Stringer33db4122015-10-01 15:00:37 -07001175 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1176 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1177 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001178
Joe Stringer33db4122015-10-01 15:00:37 -07001179 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1180 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001181 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001182 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001183 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001184 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1185 const struct ovs_key_ct_tuple_ipv4 *ct;
1186
1187 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1188
1189 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1190 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1191 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1192 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001193 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001194 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1195 }
1196 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1197 const struct ovs_key_ct_tuple_ipv6 *ct;
1198
1199 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1200
1201 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1202 sizeof(match->key->ipv6.ct_orig.src),
1203 is_mask);
1204 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1205 sizeof(match->key->ipv6.ct_orig.dst),
1206 is_mask);
1207 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1208 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001209 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001210 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1211 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001212
Jiri Benc0a6410f2016-11-10 16:28:22 +01001213 /* For layer 3 packets the Ethernet type is provided
1214 * and treated as metadata but no MAC addresses are provided.
1215 */
1216 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1217 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1218 mac_proto = MAC_PROTO_NONE;
1219
Jiri Benc329f45b2016-11-10 16:28:18 +01001220 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001221 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1222
1223 if (mac_proto == MAC_PROTO_NONE)
1224 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1225 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001226
Pravin B Shelare6445712013-10-03 18:16:47 -07001227 return 0;
1228}
1229
Joe Stringerc2ac6672015-08-26 11:31:52 -07001230static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1231 u64 attrs, const struct nlattr **a,
1232 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001233{
1234 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001235
Joe Stringerc2ac6672015-08-26 11:31:52 -07001236 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001237 if (err)
1238 return err;
1239
1240 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1241 const struct ovs_key_ethernet *eth_key;
1242
1243 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1244 SW_FLOW_KEY_MEMCPY(match, eth.src,
1245 eth_key->eth_src, ETH_ALEN, is_mask);
1246 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1247 eth_key->eth_dst, ETH_ALEN, is_mask);
1248 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001249
Jiri Benc0a6410f2016-11-10 16:28:22 +01001250 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1251 /* VLAN attribute is always parsed before getting here since it
1252 * may occur multiple times.
1253 */
1254 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001255 return -EINVAL;
1256 }
1257
Jiri Benc0a6410f2016-11-10 16:28:22 +01001258 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1259 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1260 log);
1261 if (err)
1262 return err;
1263 } else if (!is_mask) {
1264 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1265 }
1266 } else if (!match->key->eth.type) {
1267 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1268 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001269 }
1270
1271 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1272 const struct ovs_key_ipv4 *ipv4_key;
1273
1274 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1275 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001276 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1277 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001278 return -EINVAL;
1279 }
1280 SW_FLOW_KEY_PUT(match, ip.proto,
1281 ipv4_key->ipv4_proto, is_mask);
1282 SW_FLOW_KEY_PUT(match, ip.tos,
1283 ipv4_key->ipv4_tos, is_mask);
1284 SW_FLOW_KEY_PUT(match, ip.ttl,
1285 ipv4_key->ipv4_ttl, is_mask);
1286 SW_FLOW_KEY_PUT(match, ip.frag,
1287 ipv4_key->ipv4_frag, is_mask);
1288 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1289 ipv4_key->ipv4_src, is_mask);
1290 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1291 ipv4_key->ipv4_dst, is_mask);
1292 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1293 }
1294
1295 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1296 const struct ovs_key_ipv6 *ipv6_key;
1297
1298 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1299 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001300 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1301 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001302 return -EINVAL;
1303 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001304
Joe Stringerd3052bb2014-11-19 13:54:49 -08001305 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001306 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001307 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1308 return -EINVAL;
1309 }
1310
Pravin B Shelare6445712013-10-03 18:16:47 -07001311 SW_FLOW_KEY_PUT(match, ipv6.label,
1312 ipv6_key->ipv6_label, is_mask);
1313 SW_FLOW_KEY_PUT(match, ip.proto,
1314 ipv6_key->ipv6_proto, is_mask);
1315 SW_FLOW_KEY_PUT(match, ip.tos,
1316 ipv6_key->ipv6_tclass, is_mask);
1317 SW_FLOW_KEY_PUT(match, ip.ttl,
1318 ipv6_key->ipv6_hlimit, is_mask);
1319 SW_FLOW_KEY_PUT(match, ip.frag,
1320 ipv6_key->ipv6_frag, is_mask);
1321 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1322 ipv6_key->ipv6_src,
1323 sizeof(match->key->ipv6.addr.src),
1324 is_mask);
1325 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1326 ipv6_key->ipv6_dst,
1327 sizeof(match->key->ipv6.addr.dst),
1328 is_mask);
1329
1330 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1331 }
1332
1333 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1334 const struct ovs_key_arp *arp_key;
1335
1336 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1337 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001338 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001339 arp_key->arp_op);
1340 return -EINVAL;
1341 }
1342
1343 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1344 arp_key->arp_sip, is_mask);
1345 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1346 arp_key->arp_tip, is_mask);
1347 SW_FLOW_KEY_PUT(match, ip.proto,
1348 ntohs(arp_key->arp_op), is_mask);
1349 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1350 arp_key->arp_sha, ETH_ALEN, is_mask);
1351 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1352 arp_key->arp_tha, ETH_ALEN, is_mask);
1353
1354 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1355 }
1356
Simon Horman25cd9ba2014-10-06 05:05:13 -07001357 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1358 const struct ovs_key_mpls *mpls_key;
1359
1360 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1361 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1362 mpls_key->mpls_lse, is_mask);
1363
1364 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1365 }
1366
Pravin B Shelare6445712013-10-03 18:16:47 -07001367 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1368 const struct ovs_key_tcp *tcp_key;
1369
1370 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001371 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1372 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001373 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1374 }
1375
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001376 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001377 SW_FLOW_KEY_PUT(match, tp.flags,
1378 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1379 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001380 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1381 }
1382
Pravin B Shelare6445712013-10-03 18:16:47 -07001383 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1384 const struct ovs_key_udp *udp_key;
1385
1386 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001387 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1388 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001389 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1390 }
1391
1392 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1393 const struct ovs_key_sctp *sctp_key;
1394
1395 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001396 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1397 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001398 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1399 }
1400
1401 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1402 const struct ovs_key_icmp *icmp_key;
1403
1404 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001405 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001406 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001407 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001408 htons(icmp_key->icmp_code), is_mask);
1409 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1410 }
1411
1412 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1413 const struct ovs_key_icmpv6 *icmpv6_key;
1414
1415 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001416 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001417 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001418 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001419 htons(icmpv6_key->icmpv6_code), is_mask);
1420 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1421 }
1422
1423 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1424 const struct ovs_key_nd *nd_key;
1425
1426 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1427 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1428 nd_key->nd_target,
1429 sizeof(match->key->ipv6.nd.target),
1430 is_mask);
1431 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1432 nd_key->nd_sll, ETH_ALEN, is_mask);
1433 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1434 nd_key->nd_tll, ETH_ALEN, is_mask);
1435 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1436 }
1437
Jesse Gross426cda52014-10-06 05:08:38 -07001438 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001439 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001440 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001441 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001442 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001443
1444 return 0;
1445}
1446
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001447static void nlattr_set(struct nlattr *attr, u8 val,
1448 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001449{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001450 struct nlattr *nla;
1451 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001452
Pravin B Shelarf47de062014-10-16 21:55:45 -07001453 /* The nlattr stream should already have been validated */
1454 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001455 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1456 if (tbl[nla_type(nla)].next)
1457 tbl = tbl[nla_type(nla)].next;
1458 nlattr_set(nla, val, tbl);
1459 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001460 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001461 }
Joe Stringer9e384712015-10-19 19:18:57 -07001462
1463 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1464 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001465 }
1466}
1467
1468static void mask_set_nlattr(struct nlattr *attr, u8 val)
1469{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001470 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001471}
1472
1473/**
1474 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1475 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1476 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1477 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001478 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001479 * @match: receives the extracted flow match information.
1480 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1481 * sequence. The fields should of the packet that triggered the creation
1482 * of this flow.
1483 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1484 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001485 * @log: Boolean to allow kernel error logging. Normally true, but when
1486 * probing for feature compatibility this should be passed in as false to
1487 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001488 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001489int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001490 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001491 const struct nlattr *nla_mask,
1492 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001493{
1494 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001495 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001496 u64 key_attrs = 0;
1497 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001498 int err;
1499
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001500 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001501 if (err)
1502 return err;
1503
Eric Garver018c1dd2016-09-07 12:56:59 -04001504 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1505 if (err)
1506 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001507
Joe Stringerc2ac6672015-08-26 11:31:52 -07001508 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001509 if (err)
1510 return err;
1511
Pravin B Shelara85311b2014-10-19 12:03:40 -07001512 if (match->mask) {
1513 if (!nla_mask) {
1514 /* Create an exact match mask. We need to set to 0xff
1515 * all the 'match->mask' fields that have been touched
1516 * in 'match->key'. We cannot simply memset
1517 * 'match->mask', because padding bytes and fields not
1518 * specified in 'match->key' should be left to 0.
1519 * Instead, we use a stream of netlink attributes,
1520 * copied from 'key' and set to 0xff.
1521 * ovs_key_from_nlattrs() will take care of filling
1522 * 'match->mask' appropriately.
1523 */
1524 newmask = kmemdup(nla_key,
1525 nla_total_size(nla_len(nla_key)),
1526 GFP_KERNEL);
1527 if (!newmask)
1528 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001529
Pravin B Shelara85311b2014-10-19 12:03:40 -07001530 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001531
Pravin B Shelara85311b2014-10-19 12:03:40 -07001532 /* The userspace does not send tunnel attributes that
1533 * are 0, but we should not wildcard them nonetheless.
1534 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001535 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001536 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1537 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001538
Pravin B Shelara85311b2014-10-19 12:03:40 -07001539 nla_mask = newmask;
1540 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001541
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001542 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001543 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001544 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001545
Pravin B Shelara85311b2014-10-19 12:03:40 -07001546 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001547 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1548 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001549
Eric Garver018c1dd2016-09-07 12:56:59 -04001550 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1551 if (err)
1552 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001553
Joe Stringerc2ac6672015-08-26 11:31:52 -07001554 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1555 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001556 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001557 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001558 }
1559
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001560 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001561 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001562
Pravin B Shelarf47de062014-10-16 21:55:45 -07001563free_newmask:
1564 kfree(newmask);
1565 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001566}
1567
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001568static size_t get_ufid_len(const struct nlattr *attr, bool log)
1569{
1570 size_t len;
1571
1572 if (!attr)
1573 return 0;
1574
1575 len = nla_len(attr);
1576 if (len < 1 || len > MAX_UFID_LENGTH) {
1577 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1578 nla_len(attr), MAX_UFID_LENGTH);
1579 return 0;
1580 }
1581
1582 return len;
1583}
1584
1585/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1586 * or false otherwise.
1587 */
1588bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1589 bool log)
1590{
1591 sfid->ufid_len = get_ufid_len(attr, log);
1592 if (sfid->ufid_len)
1593 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1594
1595 return sfid->ufid_len;
1596}
1597
1598int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1599 const struct sw_flow_key *key, bool log)
1600{
1601 struct sw_flow_key *new_key;
1602
1603 if (ovs_nla_get_ufid(sfid, ufid, log))
1604 return 0;
1605
1606 /* If UFID was not provided, use unmasked key. */
1607 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1608 if (!new_key)
1609 return -ENOMEM;
1610 memcpy(new_key, key, sizeof(*key));
1611 sfid->unmasked_key = new_key;
1612
1613 return 0;
1614}
1615
1616u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1617{
1618 return attr ? nla_get_u32(attr) : 0;
1619}
1620
Pravin B Shelare6445712013-10-03 18:16:47 -07001621/**
1622 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001623 * @net: Network namespace.
1624 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1625 * metadata.
1626 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1627 * attributes.
1628 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001629 * @log: Boolean to allow kernel error logging. Normally true, but when
1630 * probing for feature compatibility this should be passed in as false to
1631 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001632 *
1633 * This parses a series of Netlink attributes that form a flow key, which must
1634 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1635 * get the metadata, that is, the parts of the flow key that cannot be
1636 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001637 *
1638 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001639 */
1640
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001641int ovs_nla_get_flow_metadata(struct net *net,
1642 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1643 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001644{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001645 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001646
1647 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001648 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001649
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001650 key->ct_state = 0;
1651 key->ct_zone = 0;
1652 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001653 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001654 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1655 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1656
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001657 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001658
Joe Stringerc2ac6672015-08-26 11:31:52 -07001659 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001660}
1661
Eric Garver018c1dd2016-09-07 12:56:59 -04001662static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1663 bool is_mask)
1664{
1665 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1666
1667 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1668 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1669 return -EMSGSIZE;
1670 return 0;
1671}
1672
Joe Stringer5b4237b2015-01-21 16:42:48 -08001673static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1674 const struct sw_flow_key *output, bool is_mask,
1675 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001676{
1677 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001678 struct nlattr *nla;
1679 struct nlattr *encap = NULL;
1680 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001681
Andy Zhou971427f32014-09-15 19:37:25 -07001682 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1683 goto nla_put_failure;
1684
1685 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1686 goto nla_put_failure;
1687
Pravin B Shelare6445712013-10-03 18:16:47 -07001688 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1689 goto nla_put_failure;
1690
Jiri Benc00a93ba2015-10-05 13:09:46 +02001691 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001692 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001693
1694 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001695 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001696
Jiri Benc6b26ba32015-10-05 13:09:47 +02001697 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1698 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001699 goto nla_put_failure;
1700 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001701
1702 if (swkey->phy.in_port == DP_MAX_PORTS) {
1703 if (is_mask && (output->phy.in_port == 0xffff))
1704 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1705 goto nla_put_failure;
1706 } else {
1707 u16 upper_u16;
1708 upper_u16 = !is_mask ? 0 : 0xffff;
1709
1710 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1711 (upper_u16 << 16) | output->phy.in_port))
1712 goto nla_put_failure;
1713 }
1714
1715 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1716 goto nla_put_failure;
1717
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001718 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001719 goto nla_put_failure;
1720
Jiri Benc0a6410f2016-11-10 16:28:22 +01001721 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1722 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1723 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07001724 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04001725
Jiri Benc0a6410f2016-11-10 16:28:22 +01001726 eth_key = nla_data(nla);
1727 ether_addr_copy(eth_key->eth_src, output->eth.src);
1728 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1729
1730 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1731 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04001732 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01001733 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1734 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04001735 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001736
Jiri Benc0a6410f2016-11-10 16:28:22 +01001737 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1738 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1739 goto nla_put_failure;
1740 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1741 if (!swkey->eth.cvlan.tci)
1742 goto unencap;
1743 }
1744 }
1745
1746 if (swkey->eth.type == htons(ETH_P_802_2)) {
1747 /*
1748 * Ethertype 802.2 is represented in the netlink with omitted
1749 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1750 * 0xffff in the mask attribute. Ethertype can also
1751 * be wildcarded.
1752 */
1753 if (is_mask && output->eth.type)
1754 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1755 output->eth.type))
1756 goto nla_put_failure;
1757 goto unencap;
1758 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001759 }
1760
1761 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1762 goto nla_put_failure;
1763
Eric Garver018c1dd2016-09-07 12:56:59 -04001764 if (eth_type_vlan(swkey->eth.type)) {
1765 /* There are 3 VLAN tags, we don't know anything about the rest
1766 * of the packet, so truncate here.
1767 */
1768 WARN_ON_ONCE(!(encap && in_encap));
1769 goto unencap;
1770 }
1771
Pravin B Shelare6445712013-10-03 18:16:47 -07001772 if (swkey->eth.type == htons(ETH_P_IP)) {
1773 struct ovs_key_ipv4 *ipv4_key;
1774
1775 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1776 if (!nla)
1777 goto nla_put_failure;
1778 ipv4_key = nla_data(nla);
1779 ipv4_key->ipv4_src = output->ipv4.addr.src;
1780 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1781 ipv4_key->ipv4_proto = output->ip.proto;
1782 ipv4_key->ipv4_tos = output->ip.tos;
1783 ipv4_key->ipv4_ttl = output->ip.ttl;
1784 ipv4_key->ipv4_frag = output->ip.frag;
1785 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1786 struct ovs_key_ipv6 *ipv6_key;
1787
1788 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1789 if (!nla)
1790 goto nla_put_failure;
1791 ipv6_key = nla_data(nla);
1792 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1793 sizeof(ipv6_key->ipv6_src));
1794 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1795 sizeof(ipv6_key->ipv6_dst));
1796 ipv6_key->ipv6_label = output->ipv6.label;
1797 ipv6_key->ipv6_proto = output->ip.proto;
1798 ipv6_key->ipv6_tclass = output->ip.tos;
1799 ipv6_key->ipv6_hlimit = output->ip.ttl;
1800 ipv6_key->ipv6_frag = output->ip.frag;
1801 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1802 swkey->eth.type == htons(ETH_P_RARP)) {
1803 struct ovs_key_arp *arp_key;
1804
1805 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1806 if (!nla)
1807 goto nla_put_failure;
1808 arp_key = nla_data(nla);
1809 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1810 arp_key->arp_sip = output->ipv4.addr.src;
1811 arp_key->arp_tip = output->ipv4.addr.dst;
1812 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001813 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1814 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001815 } else if (eth_p_mpls(swkey->eth.type)) {
1816 struct ovs_key_mpls *mpls_key;
1817
1818 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1819 if (!nla)
1820 goto nla_put_failure;
1821 mpls_key = nla_data(nla);
1822 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001823 }
1824
1825 if ((swkey->eth.type == htons(ETH_P_IP) ||
1826 swkey->eth.type == htons(ETH_P_IPV6)) &&
1827 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1828
1829 if (swkey->ip.proto == IPPROTO_TCP) {
1830 struct ovs_key_tcp *tcp_key;
1831
1832 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1833 if (!nla)
1834 goto nla_put_failure;
1835 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001836 tcp_key->tcp_src = output->tp.src;
1837 tcp_key->tcp_dst = output->tp.dst;
1838 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1839 output->tp.flags))
1840 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001841 } else if (swkey->ip.proto == IPPROTO_UDP) {
1842 struct ovs_key_udp *udp_key;
1843
1844 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1845 if (!nla)
1846 goto nla_put_failure;
1847 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001848 udp_key->udp_src = output->tp.src;
1849 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001850 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1851 struct ovs_key_sctp *sctp_key;
1852
1853 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1854 if (!nla)
1855 goto nla_put_failure;
1856 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001857 sctp_key->sctp_src = output->tp.src;
1858 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001859 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1860 swkey->ip.proto == IPPROTO_ICMP) {
1861 struct ovs_key_icmp *icmp_key;
1862
1863 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1864 if (!nla)
1865 goto nla_put_failure;
1866 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001867 icmp_key->icmp_type = ntohs(output->tp.src);
1868 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001869 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1870 swkey->ip.proto == IPPROTO_ICMPV6) {
1871 struct ovs_key_icmpv6 *icmpv6_key;
1872
1873 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1874 sizeof(*icmpv6_key));
1875 if (!nla)
1876 goto nla_put_failure;
1877 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001878 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1879 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001880
1881 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1882 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1883 struct ovs_key_nd *nd_key;
1884
1885 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1886 if (!nla)
1887 goto nla_put_failure;
1888 nd_key = nla_data(nla);
1889 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1890 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001891 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1892 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001893 }
1894 }
1895 }
1896
1897unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04001898 if (in_encap)
1899 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07001900 if (encap)
1901 nla_nest_end(skb, encap);
1902
1903 return 0;
1904
1905nla_put_failure:
1906 return -EMSGSIZE;
1907}
1908
Joe Stringer5b4237b2015-01-21 16:42:48 -08001909int ovs_nla_put_key(const struct sw_flow_key *swkey,
1910 const struct sw_flow_key *output, int attr, bool is_mask,
1911 struct sk_buff *skb)
1912{
1913 int err;
1914 struct nlattr *nla;
1915
1916 nla = nla_nest_start(skb, attr);
1917 if (!nla)
1918 return -EMSGSIZE;
1919 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1920 if (err)
1921 return err;
1922 nla_nest_end(skb, nla);
1923
1924 return 0;
1925}
1926
1927/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001928int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001929{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001930 if (ovs_identifier_is_ufid(&flow->id))
1931 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1932 flow->id.ufid);
1933
1934 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1935 OVS_FLOW_ATTR_KEY, false, skb);
1936}
1937
1938/* Called with ovs_mutex or RCU read lock. */
1939int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1940{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001941 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001942 OVS_FLOW_ATTR_KEY, false, skb);
1943}
1944
1945/* Called with ovs_mutex or RCU read lock. */
1946int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1947{
1948 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1949 OVS_FLOW_ATTR_MASK, true, skb);
1950}
1951
Pravin B Shelare6445712013-10-03 18:16:47 -07001952#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1953
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001954static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001955{
1956 struct sw_flow_actions *sfa;
1957
Jesse Gross426cda52014-10-06 05:08:38 -07001958 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001959 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001960 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001961 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001962
1963 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1964 if (!sfa)
1965 return ERR_PTR(-ENOMEM);
1966
1967 sfa->actions_len = 0;
1968 return sfa;
1969}
1970
Thomas Graf34ae9322015-07-21 10:44:03 +02001971static void ovs_nla_free_set_action(const struct nlattr *a)
1972{
1973 const struct nlattr *ovs_key = nla_data(a);
1974 struct ovs_tunnel_info *ovs_tun;
1975
1976 switch (nla_type(ovs_key)) {
1977 case OVS_KEY_ATTR_TUNNEL_INFO:
1978 ovs_tun = nla_data(ovs_key);
1979 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1980 break;
1981 }
1982}
1983
Pravin B Shelare6445712013-10-03 18:16:47 -07001984void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1985{
Thomas Graf34ae9322015-07-21 10:44:03 +02001986 const struct nlattr *a;
1987 int rem;
1988
1989 if (!sf_acts)
1990 return;
1991
1992 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1993 switch (nla_type(a)) {
1994 case OVS_ACTION_ATTR_SET:
1995 ovs_nla_free_set_action(a);
1996 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001997 case OVS_ACTION_ATTR_CT:
1998 ovs_ct_free_action(a);
1999 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02002000 }
2001 }
2002
2003 kfree(sf_acts);
2004}
2005
2006static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2007{
2008 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2009}
2010
2011/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2012 * The caller must hold rcu_read_lock for this to be sensible. */
2013void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2014{
2015 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07002016}
2017
2018static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002019 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002020{
2021
2022 struct sw_flow_actions *acts;
2023 int new_acts_size;
2024 int req_size = NLA_ALIGN(attr_len);
2025 int next_offset = offsetof(struct sw_flow_actions, actions) +
2026 (*sfa)->actions_len;
2027
2028 if (req_size <= (ksize(*sfa) - next_offset))
2029 goto out;
2030
2031 new_acts_size = ksize(*sfa) * 2;
2032
2033 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
2034 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
2035 return ERR_PTR(-EMSGSIZE);
2036 new_acts_size = MAX_ACTIONS_BUFSIZE;
2037 }
2038
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002039 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002040 if (IS_ERR(acts))
2041 return (void *)acts;
2042
2043 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2044 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07002045 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002046 kfree(*sfa);
2047 *sfa = acts;
2048
2049out:
2050 (*sfa)->actions_len += req_size;
2051 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2052}
2053
Jesse Grossf0b128c2014-10-03 15:35:31 -07002054static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002055 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002056{
2057 struct nlattr *a;
2058
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002059 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002060 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002061 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002062
2063 a->nla_type = attrtype;
2064 a->nla_len = nla_attr_size(len);
2065
2066 if (data)
2067 memcpy(nla_data(a), data, len);
2068 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2069
Jesse Grossf0b128c2014-10-03 15:35:31 -07002070 return a;
2071}
2072
Joe Stringer7f8a4362015-08-26 11:31:48 -07002073int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2074 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002075{
2076 struct nlattr *a;
2077
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002078 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002079
Fabian Frederickf35423c12014-11-14 19:32:58 +01002080 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002081}
2082
2083static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002084 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002085{
2086 int used = (*sfa)->actions_len;
2087 int err;
2088
Joe Stringer7f8a4362015-08-26 11:31:48 -07002089 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002090 if (err)
2091 return err;
2092
2093 return used;
2094}
2095
2096static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2097 int st_offset)
2098{
2099 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2100 st_offset);
2101
2102 a->nla_len = sfa->actions_len - st_offset;
2103}
2104
Joe Stringer7f8a4362015-08-26 11:31:48 -07002105static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002106 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002107 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002108 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002109
Joe Stringer7f8a4362015-08-26 11:31:48 -07002110static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002111 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002112 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002113 __be16 eth_type, __be16 vlan_tci,
2114 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002115{
2116 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2117 const struct nlattr *probability, *actions;
2118 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002119 int rem, start, err;
2120 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002121
2122 memset(attrs, 0, sizeof(attrs));
2123 nla_for_each_nested(a, attr, rem) {
2124 int type = nla_type(a);
2125 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2126 return -EINVAL;
2127 attrs[type] = a;
2128 }
2129 if (rem)
2130 return -EINVAL;
2131
2132 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2133 if (!probability || nla_len(probability) != sizeof(u32))
2134 return -EINVAL;
2135
2136 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2137 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2138 return -EINVAL;
2139
2140 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002141 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002142 if (start < 0)
2143 return start;
andy zhou798c1662017-03-20 16:32:29 -07002144
2145 /* When both skb and flow may be changed, put the sample
2146 * into a deferred fifo. On the other hand, if only skb
2147 * may be modified, the actions can be executed in place.
2148 *
2149 * Do this analysis at the flow installation time.
2150 * Set 'clone_action->exec' to true if the actions can be
2151 * executed without being deferred.
2152 *
2153 * If the sample is the last action, it can always be excuted
2154 * rather than deferred.
2155 */
2156 arg.exec = last || !actions_may_change_flow(actions);
2157 arg.probability = nla_get_u32(probability);
2158
2159 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2160 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002161 if (err)
2162 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002163
andy zhou798c1662017-03-20 16:32:29 -07002164 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002165 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002166
Pravin B Shelare6445712013-10-03 18:16:47 -07002167 if (err)
2168 return err;
2169
Pravin B Shelare6445712013-10-03 18:16:47 -07002170 add_nested_action_end(*sfa, start);
2171
2172 return 0;
2173}
2174
Pravin B Shelare6445712013-10-03 18:16:47 -07002175void ovs_match_init(struct sw_flow_match *match,
2176 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002177 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002178 struct sw_flow_mask *mask)
2179{
2180 memset(match, 0, sizeof(*match));
2181 match->key = key;
2182 match->mask = mask;
2183
pravin shelar22799942016-09-19 13:51:00 -07002184 if (reset_key)
2185 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002186
2187 if (mask) {
2188 memset(&mask->key, 0, sizeof(mask->key));
2189 mask->range.start = mask->range.end = 0;
2190 }
2191}
2192
Thomas Grafd91641d2015-01-15 03:53:57 +01002193static int validate_geneve_opts(struct sw_flow_key *key)
2194{
2195 struct geneve_opt *option;
2196 int opts_len = key->tun_opts_len;
2197 bool crit_opt = false;
2198
2199 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2200 while (opts_len > 0) {
2201 int len;
2202
2203 if (opts_len < sizeof(*option))
2204 return -EINVAL;
2205
2206 len = sizeof(*option) + option->length * 4;
2207 if (len > opts_len)
2208 return -EINVAL;
2209
2210 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2211
2212 option = (struct geneve_opt *)((u8 *)option + len);
2213 opts_len -= len;
2214 };
2215
2216 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2217
2218 return 0;
2219}
2220
Pravin B Shelare6445712013-10-03 18:16:47 -07002221static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002222 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002223{
2224 struct sw_flow_match match;
2225 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002226 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002227 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002228 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002229 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002230 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002231
pravin shelar22799942016-09-19 13:51:00 -07002232 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002233 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002234 if (opts_type < 0)
2235 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002236
Jesse Grossf5796682014-10-03 15:35:33 -07002237 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002238 switch (opts_type) {
2239 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2240 err = validate_geneve_opts(&key);
2241 if (err < 0)
2242 return err;
2243 break;
2244 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2245 break;
William Tuceaa0012017-10-04 17:03:12 -07002246 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
2247 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002248 }
Jesse Grossf5796682014-10-03 15:35:33 -07002249 };
2250
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002251 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002252 if (start < 0)
2253 return start;
2254
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002255 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2256 GFP_KERNEL);
2257
Thomas Graf34ae9322015-07-21 10:44:03 +02002258 if (!tun_dst)
2259 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002260
Paolo Abenid71785f2016-02-12 15:43:57 +01002261 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2262 if (err) {
2263 dst_release((struct dst_entry *)tun_dst);
2264 return err;
2265 }
2266
Thomas Graf34ae9322015-07-21 10:44:03 +02002267 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2268 sizeof(*ovs_tun), log);
2269 if (IS_ERR(a)) {
2270 dst_release((struct dst_entry *)tun_dst);
2271 return PTR_ERR(a);
2272 }
2273
2274 ovs_tun = nla_data(a);
2275 ovs_tun->tun_dst = tun_dst;
2276
2277 tun_info = &tun_dst->u.tun_info;
2278 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002279 if (key.tun_proto == AF_INET6)
2280 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002281 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002282
Pravin B Shelar4c222792015-08-30 18:09:38 -07002283 /* We need to store the options in the action itself since
2284 * everything else will go away after flow setup. We can append
2285 * it to tun_info and then point there.
2286 */
2287 ip_tunnel_info_opts_set(tun_info,
2288 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2289 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002290 add_nested_action_end(*sfa, start);
2291
2292 return err;
2293}
2294
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002295/* Return false if there are any non-masked bits set.
2296 * Mask follows data immediately, before any netlink padding.
2297 */
2298static bool validate_masked(u8 *data, int len)
2299{
2300 u8 *mask = data + len;
2301
2302 while (len--)
2303 if (*data++ & ~*mask++)
2304 return false;
2305
2306 return true;
2307}
2308
Pravin B Shelare6445712013-10-03 18:16:47 -07002309static int validate_set(const struct nlattr *a,
2310 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002311 struct sw_flow_actions **sfa, bool *skip_copy,
2312 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002313{
2314 const struct nlattr *ovs_key = nla_data(a);
2315 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002316 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002317
2318 /* There can be only one key in a action */
2319 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2320 return -EINVAL;
2321
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002322 key_len = nla_len(ovs_key);
2323 if (masked)
2324 key_len /= 2;
2325
Pravin B Shelare6445712013-10-03 18:16:47 -07002326 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002327 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002328 return -EINVAL;
2329
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002330 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2331 return -EINVAL;
2332
Pravin B Shelare6445712013-10-03 18:16:47 -07002333 switch (key_type) {
2334 const struct ovs_key_ipv4 *ipv4_key;
2335 const struct ovs_key_ipv6 *ipv6_key;
2336 int err;
2337
2338 case OVS_KEY_ATTR_PRIORITY:
2339 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002340 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002341 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002342 break;
2343
Jiri Benc0a6410f2016-11-10 16:28:22 +01002344 case OVS_KEY_ATTR_ETHERNET:
2345 if (mac_proto != MAC_PROTO_ETHERNET)
2346 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002347 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002348
Pravin B Shelare6445712013-10-03 18:16:47 -07002349 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002350 if (masked)
2351 return -EINVAL; /* Masked tunnel set not supported. */
2352
2353 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002354 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002355 if (err)
2356 return err;
2357 break;
2358
2359 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002360 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002361 return -EINVAL;
2362
Pravin B Shelare6445712013-10-03 18:16:47 -07002363 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002364
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002365 if (masked) {
2366 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002367
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002368 /* Non-writeable fields. */
2369 if (mask->ipv4_proto || mask->ipv4_frag)
2370 return -EINVAL;
2371 } else {
2372 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2373 return -EINVAL;
2374
2375 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2376 return -EINVAL;
2377 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002378 break;
2379
2380 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002381 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002382 return -EINVAL;
2383
Pravin B Shelare6445712013-10-03 18:16:47 -07002384 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002385
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002386 if (masked) {
2387 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002388
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002389 /* Non-writeable fields. */
2390 if (mask->ipv6_proto || mask->ipv6_frag)
2391 return -EINVAL;
2392
2393 /* Invalid bits in the flow label mask? */
2394 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2395 return -EINVAL;
2396 } else {
2397 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2398 return -EINVAL;
2399
2400 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2401 return -EINVAL;
2402 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002403 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2404 return -EINVAL;
2405
2406 break;
2407
2408 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002409 if ((eth_type != htons(ETH_P_IP) &&
2410 eth_type != htons(ETH_P_IPV6)) ||
2411 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002412 return -EINVAL;
2413
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002414 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002415
2416 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002417 if ((eth_type != htons(ETH_P_IP) &&
2418 eth_type != htons(ETH_P_IPV6)) ||
2419 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002420 return -EINVAL;
2421
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002422 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002423
2424 case OVS_KEY_ATTR_MPLS:
2425 if (!eth_p_mpls(eth_type))
2426 return -EINVAL;
2427 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002428
2429 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002430 if ((eth_type != htons(ETH_P_IP) &&
2431 eth_type != htons(ETH_P_IPV6)) ||
2432 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002433 return -EINVAL;
2434
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002435 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002436
2437 default:
2438 return -EINVAL;
2439 }
2440
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002441 /* Convert non-masked non-tunnel set actions to masked set actions. */
2442 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2443 int start, len = key_len * 2;
2444 struct nlattr *at;
2445
2446 *skip_copy = true;
2447
2448 start = add_nested_action_start(sfa,
2449 OVS_ACTION_ATTR_SET_TO_MASKED,
2450 log);
2451 if (start < 0)
2452 return start;
2453
2454 at = __add_action(sfa, key_type, NULL, len, log);
2455 if (IS_ERR(at))
2456 return PTR_ERR(at);
2457
2458 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2459 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2460 /* Clear non-writeable bits from otherwise writeable fields. */
2461 if (key_type == OVS_KEY_ATTR_IPV6) {
2462 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2463
2464 mask->ipv6_label &= htonl(0x000FFFFF);
2465 }
2466 add_nested_action_end(*sfa, start);
2467 }
2468
Pravin B Shelare6445712013-10-03 18:16:47 -07002469 return 0;
2470}
2471
2472static int validate_userspace(const struct nlattr *attr)
2473{
2474 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2475 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2476 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002477 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002478 };
2479 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2480 int error;
2481
Johannes Bergfceb6432017-04-12 14:34:07 +02002482 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2483 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002484 if (error)
2485 return error;
2486
2487 if (!a[OVS_USERSPACE_ATTR_PID] ||
2488 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2489 return -EINVAL;
2490
2491 return 0;
2492}
2493
2494static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002495 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002496{
2497 int totlen = NLA_ALIGN(from->nla_len);
2498 struct nlattr *to;
2499
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002500 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002501 if (IS_ERR(to))
2502 return PTR_ERR(to);
2503
2504 memcpy(to, from, totlen);
2505 return 0;
2506}
2507
Joe Stringer7f8a4362015-08-26 11:31:48 -07002508static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002509 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002510 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002511 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002512{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002513 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002514 const struct nlattr *a;
2515 int rem, err;
2516
Pravin B Shelare6445712013-10-03 18:16:47 -07002517 nla_for_each_nested(a, attr, rem) {
2518 /* Expected argument lengths, (u32)-1 for variable length. */
2519 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2520 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002521 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002522 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002523 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2524 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002525 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2526 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2527 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002528 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002529 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002530 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2531 [OVS_ACTION_ATTR_CT] = (u32)-1,
Eric Garverb8226962017-10-10 16:54:44 -04002532 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
William Tuf2a4d082016-06-10 11:49:33 -07002533 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002534 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2535 [OVS_ACTION_ATTR_POP_ETH] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -07002536 };
2537 const struct ovs_action_push_vlan *vlan;
2538 int type = nla_type(a);
2539 bool skip_copy;
2540
2541 if (type > OVS_ACTION_ATTR_MAX ||
2542 (action_lens[type] != nla_len(a) &&
2543 action_lens[type] != (u32)-1))
2544 return -EINVAL;
2545
2546 skip_copy = false;
2547 switch (type) {
2548 case OVS_ACTION_ATTR_UNSPEC:
2549 return -EINVAL;
2550
2551 case OVS_ACTION_ATTR_USERSPACE:
2552 err = validate_userspace(a);
2553 if (err)
2554 return err;
2555 break;
2556
2557 case OVS_ACTION_ATTR_OUTPUT:
2558 if (nla_get_u32(a) >= DP_MAX_PORTS)
2559 return -EINVAL;
2560 break;
2561
William Tuf2a4d082016-06-10 11:49:33 -07002562 case OVS_ACTION_ATTR_TRUNC: {
2563 const struct ovs_action_trunc *trunc = nla_data(a);
2564
2565 if (trunc->max_len < ETH_HLEN)
2566 return -EINVAL;
2567 break;
2568 }
2569
Andy Zhou971427f32014-09-15 19:37:25 -07002570 case OVS_ACTION_ATTR_HASH: {
2571 const struct ovs_action_hash *act_hash = nla_data(a);
2572
2573 switch (act_hash->hash_alg) {
2574 case OVS_HASH_ALG_L4:
2575 break;
2576 default:
2577 return -EINVAL;
2578 }
2579
2580 break;
2581 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002582
2583 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002584 if (mac_proto != MAC_PROTO_ETHERNET)
2585 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002586 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002587 break;
2588
2589 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002590 if (mac_proto != MAC_PROTO_ETHERNET)
2591 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002592 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002593 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002594 return -EINVAL;
2595 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2596 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002597 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002598 break;
2599
Andy Zhou971427f32014-09-15 19:37:25 -07002600 case OVS_ACTION_ATTR_RECIRC:
2601 break;
2602
Simon Horman25cd9ba2014-10-06 05:05:13 -07002603 case OVS_ACTION_ATTR_PUSH_MPLS: {
2604 const struct ovs_action_push_mpls *mpls = nla_data(a);
2605
Simon Horman25cd9ba2014-10-06 05:05:13 -07002606 if (!eth_p_mpls(mpls->mpls_ethertype))
2607 return -EINVAL;
2608 /* Prohibit push MPLS other than to a white list
2609 * for packets that have a known tag order.
2610 */
2611 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2612 (eth_type != htons(ETH_P_IP) &&
2613 eth_type != htons(ETH_P_IPV6) &&
2614 eth_type != htons(ETH_P_ARP) &&
2615 eth_type != htons(ETH_P_RARP) &&
2616 !eth_p_mpls(eth_type)))
2617 return -EINVAL;
2618 eth_type = mpls->mpls_ethertype;
2619 break;
2620 }
2621
2622 case OVS_ACTION_ATTR_POP_MPLS:
2623 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2624 !eth_p_mpls(eth_type))
2625 return -EINVAL;
2626
2627 /* Disallow subsequent L2.5+ set and mpls_pop actions
2628 * as there is no check here to ensure that the new
2629 * eth_type is valid and thus set actions could
2630 * write off the end of the packet or otherwise
2631 * corrupt it.
2632 *
2633 * Support for these actions is planned using packet
2634 * recirculation.
2635 */
2636 eth_type = htons(0);
2637 break;
2638
Pravin B Shelare6445712013-10-03 18:16:47 -07002639 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002640 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002641 &skip_copy, mac_proto, eth_type,
2642 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002643 if (err)
2644 return err;
2645 break;
2646
2647 case OVS_ACTION_ATTR_SET_MASKED:
2648 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002649 &skip_copy, mac_proto, eth_type,
2650 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002651 if (err)
2652 return err;
2653 break;
2654
andy zhou798c1662017-03-20 16:32:29 -07002655 case OVS_ACTION_ATTR_SAMPLE: {
2656 bool last = nla_is_last(a, rem);
2657
2658 err = validate_and_copy_sample(net, a, key, sfa,
2659 eth_type, vlan_tci,
2660 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07002661 if (err)
2662 return err;
2663 skip_copy = true;
2664 break;
andy zhou798c1662017-03-20 16:32:29 -07002665 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002666
Joe Stringer7f8a4362015-08-26 11:31:48 -07002667 case OVS_ACTION_ATTR_CT:
2668 err = ovs_ct_copy_action(net, a, key, sfa, log);
2669 if (err)
2670 return err;
2671 skip_copy = true;
2672 break;
2673
Eric Garverb8226962017-10-10 16:54:44 -04002674 case OVS_ACTION_ATTR_CT_CLEAR:
2675 break;
2676
Jiri Benc91820da2016-11-10 16:28:23 +01002677 case OVS_ACTION_ATTR_PUSH_ETH:
2678 /* Disallow pushing an Ethernet header if one
2679 * is already present */
2680 if (mac_proto != MAC_PROTO_NONE)
2681 return -EINVAL;
2682 mac_proto = MAC_PROTO_NONE;
2683 break;
2684
2685 case OVS_ACTION_ATTR_POP_ETH:
2686 if (mac_proto != MAC_PROTO_ETHERNET)
2687 return -EINVAL;
2688 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2689 return -EINVAL;
2690 mac_proto = MAC_PROTO_ETHERNET;
2691 break;
2692
Pravin B Shelare6445712013-10-03 18:16:47 -07002693 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002694 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002695 return -EINVAL;
2696 }
2697 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002698 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002699 if (err)
2700 return err;
2701 }
2702 }
2703
2704 if (rem > 0)
2705 return -EINVAL;
2706
2707 return 0;
2708}
2709
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002710/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002711int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002712 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002713 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002714{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002715 int err;
2716
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002717 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002718 if (IS_ERR(*sfa))
2719 return PTR_ERR(*sfa);
2720
Joe Stringer8e2fed12015-08-26 11:31:44 -07002721 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07002722 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04002723 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002724 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002725 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002726
2727 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002728}
2729
andy zhou798c1662017-03-20 16:32:29 -07002730static int sample_action_to_attr(const struct nlattr *attr,
2731 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07002732{
andy zhou798c1662017-03-20 16:32:29 -07002733 struct nlattr *start, *ac_start = NULL, *sample_arg;
2734 int err = 0, rem = nla_len(attr);
2735 const struct sample_arg *arg;
2736 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07002737
2738 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2739 if (!start)
2740 return -EMSGSIZE;
2741
andy zhou798c1662017-03-20 16:32:29 -07002742 sample_arg = nla_data(attr);
2743 arg = nla_data(sample_arg);
2744 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07002745
andy zhou798c1662017-03-20 16:32:29 -07002746 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
2747 err = -EMSGSIZE;
2748 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07002749 }
2750
andy zhou798c1662017-03-20 16:32:29 -07002751 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2752 if (!ac_start) {
2753 err = -EMSGSIZE;
2754 goto out;
2755 }
2756
2757 err = ovs_nla_put_actions(actions, rem, skb);
2758
2759out:
2760 if (err) {
2761 nla_nest_cancel(skb, ac_start);
2762 nla_nest_cancel(skb, start);
2763 } else {
2764 nla_nest_end(skb, ac_start);
2765 nla_nest_end(skb, start);
2766 }
2767
Pravin B Shelare6445712013-10-03 18:16:47 -07002768 return err;
2769}
2770
2771static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2772{
2773 const struct nlattr *ovs_key = nla_data(a);
2774 int key_type = nla_type(ovs_key);
2775 struct nlattr *start;
2776 int err;
2777
2778 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002779 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002780 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2781 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002782
Pravin B Shelare6445712013-10-03 18:16:47 -07002783 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2784 if (!start)
2785 return -EMSGSIZE;
2786
Simon Hormane905eab2015-12-18 19:43:15 +09002787 err = ip_tun_to_nlattr(skb, &tun_info->key,
2788 ip_tunnel_info_opts(tun_info),
2789 tun_info->options_len,
2790 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07002791 if (err)
2792 return err;
2793 nla_nest_end(skb, start);
2794 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002795 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002796 default:
2797 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2798 return -EMSGSIZE;
2799 break;
2800 }
2801
2802 return 0;
2803}
2804
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002805static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2806 struct sk_buff *skb)
2807{
2808 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002809 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002810 size_t key_len = nla_len(ovs_key) / 2;
2811
2812 /* Revert the conversion we did from a non-masked set action to
2813 * masked set action.
2814 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002815 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2816 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002817 return -EMSGSIZE;
2818
Joe Stringerf4f8e732015-03-02 18:49:56 -08002819 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2820 return -EMSGSIZE;
2821
2822 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002823 return 0;
2824}
2825
Pravin B Shelare6445712013-10-03 18:16:47 -07002826int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2827{
2828 const struct nlattr *a;
2829 int rem, err;
2830
2831 nla_for_each_attr(a, attr, len, rem) {
2832 int type = nla_type(a);
2833
2834 switch (type) {
2835 case OVS_ACTION_ATTR_SET:
2836 err = set_action_to_attr(a, skb);
2837 if (err)
2838 return err;
2839 break;
2840
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002841 case OVS_ACTION_ATTR_SET_TO_MASKED:
2842 err = masked_set_action_to_set_action_attr(a, skb);
2843 if (err)
2844 return err;
2845 break;
2846
Pravin B Shelare6445712013-10-03 18:16:47 -07002847 case OVS_ACTION_ATTR_SAMPLE:
2848 err = sample_action_to_attr(a, skb);
2849 if (err)
2850 return err;
2851 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002852
2853 case OVS_ACTION_ATTR_CT:
2854 err = ovs_ct_action_to_attr(nla_data(a), skb);
2855 if (err)
2856 return err;
2857 break;
2858
Pravin B Shelare6445712013-10-03 18:16:47 -07002859 default:
2860 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2861 return -EMSGSIZE;
2862 break;
2863 }
2864 }
2865
2866 return 0;
2867}