]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/openvswitch/flow_netlink.c
openvswitch: Add original direction conntrack tuple to sw_flow_key.
[mirror_ubuntu-artful-kernel.git] / net / openvswitch / flow_netlink.c
CommitLineData
e6445719 1/*
971427f3 2 * Copyright (c) 2007-2014 Nicira, Inc.
e6445719
PS
3 *
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
2235ad1c
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
e6445719
PS
21#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>
f5796684 45#include <net/geneve.h>
e6445719
PS
46#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
25cd9ba0 49#include <net/mpls.h>
614732ea 50#include <net/vxlan.h>
e6445719
PS
51
52#include "flow_netlink.h"
53
81bfe3c3
TG
54struct ovs_len_tbl {
55 int len;
56 const struct ovs_len_tbl *next;
57};
58
59#define OVS_ATTR_NESTED -1
982b5270 60#define OVS_ATTR_VARIABLE -2
81bfe3c3 61
a85311bf
PS
62static void update_range(struct sw_flow_match *match,
63 size_t offset, size_t size, bool is_mask)
e6445719 64{
a85311bf 65 struct sw_flow_key_range *range;
e6445719
PS
66 size_t start = rounddown(offset, sizeof(long));
67 size_t end = roundup(offset + size, sizeof(long));
68
69 if (!is_mask)
70 range = &match->range;
a85311bf 71 else
e6445719
PS
72 range = &match->mask->range;
73
e6445719
PS
74 if (range->start == range->end) {
75 range->start = start;
76 range->end = end;
77 return;
78 }
79
80 if (range->start > start)
81 range->start = start;
82
83 if (range->end < end)
84 range->end = end;
85}
86
87#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
88 do { \
a85311bf
PS
89 update_range(match, offsetof(struct sw_flow_key, field), \
90 sizeof((match)->key->field), is_mask); \
91 if (is_mask) \
92 (match)->mask->key.field = value; \
93 else \
e6445719 94 (match)->key->field = value; \
e6445719
PS
95 } while (0)
96
f5796684
JG
97#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
98 do { \
a85311bf 99 update_range(match, offset, len, is_mask); \
f5796684
JG
100 if (is_mask) \
101 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
a85311bf 102 len); \
f5796684
JG
103 else \
104 memcpy((u8 *)(match)->key + offset, value_p, len); \
e6445719
PS
105 } while (0)
106
f5796684
JG
107#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
108 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
109 value_p, len, is_mask)
110
a85311bf
PS
111#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
112 do { \
113 update_range(match, offsetof(struct sw_flow_key, field), \
114 sizeof((match)->key->field), is_mask); \
115 if (is_mask) \
116 memset((u8 *)&(match)->mask->key.field, value, \
117 sizeof((match)->mask->key.field)); \
118 else \
f47de068
PS
119 memset((u8 *)&(match)->key->field, value, \
120 sizeof((match)->key->field)); \
f47de068 121 } while (0)
e6445719
PS
122
123static bool match_validate(const struct sw_flow_match *match,
05da5898 124 u64 key_attrs, u64 mask_attrs, bool log)
e6445719 125{
0a6410fb 126 u64 key_expected = 0;
e6445719
PS
127 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
128
129 /* The following mask attributes allowed only if they
130 * pass the validation tests. */
131 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
9dd7f890 132 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
e6445719 133 | (1 << OVS_KEY_ATTR_IPV6)
9dd7f890 134 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
e6445719 135 | (1 << OVS_KEY_ATTR_TCP)
5eb26b15 136 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
e6445719
PS
137 | (1 << OVS_KEY_ATTR_UDP)
138 | (1 << OVS_KEY_ATTR_SCTP)
139 | (1 << OVS_KEY_ATTR_ICMP)
140 | (1 << OVS_KEY_ATTR_ICMPV6)
141 | (1 << OVS_KEY_ATTR_ARP)
25cd9ba0
SH
142 | (1 << OVS_KEY_ATTR_ND)
143 | (1 << OVS_KEY_ATTR_MPLS));
e6445719
PS
144
145 /* Always allowed mask fields. */
146 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
147 | (1 << OVS_KEY_ATTR_IN_PORT)
148 | (1 << OVS_KEY_ATTR_ETHERTYPE));
149
150 /* Check key attributes. */
151 if (match->key->eth.type == htons(ETH_P_ARP)
152 || match->key->eth.type == htons(ETH_P_RARP)) {
153 key_expected |= 1 << OVS_KEY_ATTR_ARP;
f2a01517 154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
e6445719
PS
155 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
156 }
157
25cd9ba0
SH
158 if (eth_p_mpls(match->key->eth.type)) {
159 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
160 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
161 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
162 }
163
e6445719
PS
164 if (match->key->eth.type == htons(ETH_P_IP)) {
165 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
9dd7f890 166 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
e6445719 167 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
9dd7f890
JR
168 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
169 }
e6445719
PS
170
171 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
172 if (match->key->ip.proto == IPPROTO_UDP) {
173 key_expected |= 1 << OVS_KEY_ATTR_UDP;
174 if (match->mask && (match->mask->key.ip.proto == 0xff))
175 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
176 }
177
178 if (match->key->ip.proto == IPPROTO_SCTP) {
179 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
180 if (match->mask && (match->mask->key.ip.proto == 0xff))
181 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
182 }
183
184 if (match->key->ip.proto == IPPROTO_TCP) {
185 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
186 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
187 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 188 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
189 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
190 }
e6445719
PS
191 }
192
193 if (match->key->ip.proto == IPPROTO_ICMP) {
194 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
195 if (match->mask && (match->mask->key.ip.proto == 0xff))
196 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
197 }
198 }
199 }
200
201 if (match->key->eth.type == htons(ETH_P_IPV6)) {
202 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
9dd7f890 203 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
e6445719 204 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
9dd7f890
JR
205 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
206 }
e6445719
PS
207
208 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
209 if (match->key->ip.proto == IPPROTO_UDP) {
210 key_expected |= 1 << OVS_KEY_ATTR_UDP;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
213 }
214
215 if (match->key->ip.proto == IPPROTO_SCTP) {
216 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
217 if (match->mask && (match->mask->key.ip.proto == 0xff))
218 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
219 }
220
221 if (match->key->ip.proto == IPPROTO_TCP) {
222 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
223 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
224 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 225 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
226 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
227 }
e6445719
PS
228 }
229
230 if (match->key->ip.proto == IPPROTO_ICMPV6) {
231 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
232 if (match->mask && (match->mask->key.ip.proto == 0xff))
233 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
234
1139e241 235 if (match->key->tp.src ==
e6445719 236 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1139e241 237 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e6445719 238 key_expected |= 1 << OVS_KEY_ATTR_ND;
9dd7f890
JR
239 /* Original direction conntrack tuple
240 * uses the same space as the ND fields
241 * in the key, so both are not allowed
242 * at the same time.
243 */
244 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
f2a01517 245 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
e6445719
PS
246 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
247 }
248 }
249 }
250 }
251
252 if ((key_attrs & key_expected) != key_expected) {
253 /* Key attributes check failed. */
05da5898
JR
254 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
255 (unsigned long long)key_attrs,
256 (unsigned long long)key_expected);
e6445719
PS
257 return false;
258 }
259
260 if ((mask_attrs & mask_allowed) != mask_attrs) {
261 /* Mask attributes check failed. */
05da5898
JR
262 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
263 (unsigned long long)mask_attrs,
264 (unsigned long long)mask_allowed);
e6445719
PS
265 return false;
266 }
267
268 return true;
269}
270
8f0aad6f
WZ
271size_t ovs_tun_key_attr_size(void)
272{
273 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
274 * updating this function.
275 */
b46f6ded 276 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
6b26ba3a
JB
277 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
278 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
8f0aad6f
WZ
279 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
280 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
281 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
282 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
283 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
284 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
1dd144cf
TG
285 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
286 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
287 */
8f0aad6f
WZ
288 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
289 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
290}
291
41af73e9
JS
292size_t ovs_key_attr_size(void)
293{
294 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
295 * updating this function.
296 */
9dd7f890 297 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
41af73e9
JS
298
299 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
300 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
8f0aad6f 301 + ovs_tun_key_attr_size()
41af73e9
JS
302 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
303 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
304 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
305 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
fbccce59 306 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
7f8a436e 307 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
182e3042 308 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
33db4125 309 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
9dd7f890 310 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
41af73e9
JS
311 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
312 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
313 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
314 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
315 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
316 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
317 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
318 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
319}
320
982b5270
JG
321static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
322 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
323};
324
81bfe3c3
TG
325static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
326 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
327 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
328 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
329 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
330 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
331 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
332 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
333 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
334 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
335 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
982b5270
JG
336 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
337 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
338 .next = ovs_vxlan_ext_key_lens },
6b26ba3a
JB
339 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
340 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
81bfe3c3
TG
341};
342
e6445719 343/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
81bfe3c3
TG
344static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
345 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
346 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
347 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
348 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
349 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
350 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
351 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
352 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
353 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
354 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
355 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
356 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
357 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
358 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
359 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
360 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
361 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
362 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
363 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
364 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
365 .next = ovs_tunnel_key_lens, },
366 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
fbccce59 367 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
7f8a436e 368 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
182e3042 369 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
33db4125 370 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
9dd7f890
JR
371 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
372 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
373 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
374 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
e6445719
PS
375};
376
982b5270
JG
377static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
378{
379 return expected_len == attr_len ||
380 expected_len == OVS_ATTR_NESTED ||
381 expected_len == OVS_ATTR_VARIABLE;
382}
383
e6445719
PS
384static bool is_all_zero(const u8 *fp, size_t size)
385{
386 int i;
387
388 if (!fp)
389 return false;
390
391 for (i = 0; i < size; i++)
392 if (fp[i])
393 return false;
394
395 return true;
396}
397
398static int __parse_flow_nlattrs(const struct nlattr *attr,
399 const struct nlattr *a[],
05da5898 400 u64 *attrsp, bool log, bool nz)
e6445719
PS
401{
402 const struct nlattr *nla;
403 u64 attrs;
404 int rem;
405
406 attrs = *attrsp;
407 nla_for_each_nested(nla, attr, rem) {
408 u16 type = nla_type(nla);
409 int expected_len;
410
411 if (type > OVS_KEY_ATTR_MAX) {
05da5898 412 OVS_NLERR(log, "Key type %d is out of range max %d",
e6445719
PS
413 type, OVS_KEY_ATTR_MAX);
414 return -EINVAL;
415 }
416
417 if (attrs & (1 << type)) {
05da5898 418 OVS_NLERR(log, "Duplicate key (type %d).", type);
e6445719
PS
419 return -EINVAL;
420 }
421
81bfe3c3 422 expected_len = ovs_key_lens[type].len;
982b5270 423 if (!check_attr_len(nla_len(nla), expected_len)) {
05da5898
JR
424 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
425 type, nla_len(nla), expected_len);
e6445719
PS
426 return -EINVAL;
427 }
428
429 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
430 attrs |= 1 << type;
431 a[type] = nla;
432 }
433 }
434 if (rem) {
05da5898 435 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
e6445719
PS
436 return -EINVAL;
437 }
438
439 *attrsp = attrs;
440 return 0;
441}
442
443static int parse_flow_mask_nlattrs(const struct nlattr *attr,
05da5898
JR
444 const struct nlattr *a[], u64 *attrsp,
445 bool log)
e6445719 446{
05da5898 447 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
e6445719
PS
448}
449
9dd7f890
JR
450int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
451 u64 *attrsp, bool log)
e6445719 452{
05da5898
JR
453 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
454}
455
456static int genev_tun_opt_from_nlattr(const struct nlattr *a,
457 struct sw_flow_match *match, bool is_mask,
458 bool log)
459{
460 unsigned long opt_key_offset;
461
462 if (nla_len(a) > sizeof(match->key->tun_opts)) {
463 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
464 nla_len(a), sizeof(match->key->tun_opts));
465 return -EINVAL;
466 }
467
468 if (nla_len(a) % 4 != 0) {
469 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
470 nla_len(a));
471 return -EINVAL;
472 }
473
474 /* We need to record the length of the options passed
475 * down, otherwise packets with the same format but
476 * additional options will be silently matched.
477 */
478 if (!is_mask) {
479 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
480 false);
481 } else {
482 /* This is somewhat unusual because it looks at
483 * both the key and mask while parsing the
484 * attributes (and by extension assumes the key
485 * is parsed first). Normally, we would verify
486 * that each is the correct length and that the
487 * attributes line up in the validate function.
488 * However, that is difficult because this is
489 * variable length and we won't have the
490 * information later.
491 */
492 if (match->key->tun_opts_len != nla_len(a)) {
493 OVS_NLERR(log, "Geneve option len %d != mask len %d",
494 match->key->tun_opts_len, nla_len(a));
495 return -EINVAL;
496 }
497
498 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
499 }
500
d91641d9 501 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
05da5898
JR
502 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
503 nla_len(a), is_mask);
504 return 0;
e6445719
PS
505}
506
982b5270 507static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
1dd144cf
TG
508 struct sw_flow_match *match, bool is_mask,
509 bool log)
510{
982b5270
JG
511 struct nlattr *a;
512 int rem;
1dd144cf 513 unsigned long opt_key_offset;
614732ea 514 struct vxlan_metadata opts;
1dd144cf
TG
515
516 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
517
1dd144cf 518 memset(&opts, 0, sizeof(opts));
982b5270
JG
519 nla_for_each_nested(a, attr, rem) {
520 int type = nla_type(a);
1dd144cf 521
982b5270
JG
522 if (type > OVS_VXLAN_EXT_MAX) {
523 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
524 type, OVS_VXLAN_EXT_MAX);
525 return -EINVAL;
526 }
527
528 if (!check_attr_len(nla_len(a),
529 ovs_vxlan_ext_key_lens[type].len)) {
530 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
531 type, nla_len(a),
532 ovs_vxlan_ext_key_lens[type].len);
533 return -EINVAL;
534 }
535
536 switch (type) {
537 case OVS_VXLAN_EXT_GBP:
538 opts.gbp = nla_get_u32(a);
539 break;
540 default:
541 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
542 type);
543 return -EINVAL;
544 }
545 }
546 if (rem) {
547 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
548 rem);
549 return -EINVAL;
550 }
1dd144cf
TG
551
552 if (!is_mask)
553 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
554 else
555 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
556
557 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
558 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
559 is_mask);
560 return 0;
561}
562
6b26ba3a
JB
563static int ip_tun_from_nlattr(const struct nlattr *attr,
564 struct sw_flow_match *match, bool is_mask,
565 bool log)
e6445719 566{
99e28f18
PS
567 bool ttl = false, ipv4 = false, ipv6 = false;
568 __be16 tun_flags = 0;
569 int opts_type = 0;
e6445719
PS
570 struct nlattr *a;
571 int rem;
e6445719
PS
572
573 nla_for_each_nested(a, attr, rem) {
574 int type = nla_type(a);
05da5898
JR
575 int err;
576
e6445719 577 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
05da5898
JR
578 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
579 type, OVS_TUNNEL_KEY_ATTR_MAX);
e6445719
PS
580 return -EINVAL;
581 }
582
982b5270
JG
583 if (!check_attr_len(nla_len(a),
584 ovs_tunnel_key_lens[type].len)) {
05da5898 585 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
81bfe3c3 586 type, nla_len(a), ovs_tunnel_key_lens[type].len);
e6445719
PS
587 return -EINVAL;
588 }
589
590 switch (type) {
591 case OVS_TUNNEL_KEY_ATTR_ID:
592 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
593 nla_get_be64(a), is_mask);
594 tun_flags |= TUNNEL_KEY;
595 break;
596 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
c1ea5d67 597 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
67b61f6c 598 nla_get_in_addr(a), is_mask);
6b26ba3a 599 ipv4 = true;
e6445719
PS
600 break;
601 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
c1ea5d67 602 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
67b61f6c 603 nla_get_in_addr(a), is_mask);
6b26ba3a
JB
604 ipv4 = true;
605 break;
606 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
607 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
608 nla_get_in6_addr(a), is_mask);
609 ipv6 = true;
610 break;
611 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
612 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
613 nla_get_in6_addr(a), is_mask);
614 ipv6 = true;
e6445719
PS
615 break;
616 case OVS_TUNNEL_KEY_ATTR_TOS:
7c383fb2 617 SW_FLOW_KEY_PUT(match, tun_key.tos,
e6445719
PS
618 nla_get_u8(a), is_mask);
619 break;
620 case OVS_TUNNEL_KEY_ATTR_TTL:
7c383fb2 621 SW_FLOW_KEY_PUT(match, tun_key.ttl,
e6445719
PS
622 nla_get_u8(a), is_mask);
623 ttl = true;
624 break;
625 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
626 tun_flags |= TUNNEL_DONT_FRAGMENT;
627 break;
628 case OVS_TUNNEL_KEY_ATTR_CSUM:
629 tun_flags |= TUNNEL_CSUM;
630 break;
8f0aad6f
WZ
631 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
632 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
633 nla_get_be16(a), is_mask);
634 break;
635 case OVS_TUNNEL_KEY_ATTR_TP_DST:
636 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
637 nla_get_be16(a), is_mask);
638 break;
67fa0341
JG
639 case OVS_TUNNEL_KEY_ATTR_OAM:
640 tun_flags |= TUNNEL_OAM;
641 break;
f5796684 642 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1dd144cf
TG
643 if (opts_type) {
644 OVS_NLERR(log, "Multiple metadata blocks provided");
645 return -EINVAL;
646 }
647
05da5898
JR
648 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
649 if (err)
650 return err;
f5796684 651
1dd144cf
TG
652 tun_flags |= TUNNEL_GENEVE_OPT;
653 opts_type = type;
654 break;
655 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
656 if (opts_type) {
657 OVS_NLERR(log, "Multiple metadata blocks provided");
658 return -EINVAL;
659 }
660
661 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
662 if (err)
663 return err;
664
665 tun_flags |= TUNNEL_VXLAN_OPT;
666 opts_type = type;
f5796684 667 break;
e6445719 668 default:
6b26ba3a 669 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
f5796684 670 type);
e6445719
PS
671 return -EINVAL;
672 }
673 }
674
675 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
00a93bab
JB
676 if (is_mask)
677 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
678 else
6b26ba3a
JB
679 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
680 false);
e6445719
PS
681
682 if (rem > 0) {
6b26ba3a 683 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
05da5898 684 rem);
e6445719
PS
685 return -EINVAL;
686 }
687
6b26ba3a
JB
688 if (ipv4 && ipv6) {
689 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
690 return -EINVAL;
691 }
692
e6445719 693 if (!is_mask) {
6b26ba3a
JB
694 if (!ipv4 && !ipv6) {
695 OVS_NLERR(log, "IP tunnel dst address not specified");
696 return -EINVAL;
697 }
698 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
05da5898 699 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
e6445719
PS
700 return -EINVAL;
701 }
6b26ba3a
JB
702 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
703 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
704 return -EINVAL;
705 }
e6445719
PS
706
707 if (!ttl) {
6b26ba3a 708 OVS_NLERR(log, "IP tunnel TTL not specified.");
e6445719
PS
709 return -EINVAL;
710 }
711 }
712
1dd144cf
TG
713 return opts_type;
714}
715
716static int vxlan_opt_to_nlattr(struct sk_buff *skb,
717 const void *tun_opts, int swkey_tun_opts_len)
718{
614732ea 719 const struct vxlan_metadata *opts = tun_opts;
1dd144cf
TG
720 struct nlattr *nla;
721
722 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
723 if (!nla)
724 return -EMSGSIZE;
725
726 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
727 return -EMSGSIZE;
728
729 nla_nest_end(skb, nla);
e6445719
PS
730 return 0;
731}
732
6b26ba3a
JB
733static int __ip_tun_to_nlattr(struct sk_buff *skb,
734 const struct ip_tunnel_key *output,
735 const void *tun_opts, int swkey_tun_opts_len,
736 unsigned short tun_proto)
e6445719 737{
e6445719 738 if (output->tun_flags & TUNNEL_KEY &&
b46f6ded
ND
739 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
740 OVS_TUNNEL_KEY_ATTR_PAD))
e6445719 741 return -EMSGSIZE;
6b26ba3a
JB
742 switch (tun_proto) {
743 case AF_INET:
744 if (output->u.ipv4.src &&
745 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
746 output->u.ipv4.src))
747 return -EMSGSIZE;
748 if (output->u.ipv4.dst &&
749 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
750 output->u.ipv4.dst))
751 return -EMSGSIZE;
752 break;
753 case AF_INET6:
754 if (!ipv6_addr_any(&output->u.ipv6.src) &&
755 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
756 &output->u.ipv6.src))
757 return -EMSGSIZE;
758 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
759 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
760 &output->u.ipv6.dst))
761 return -EMSGSIZE;
762 break;
763 }
7c383fb2
JB
764 if (output->tos &&
765 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
e6445719 766 return -EMSGSIZE;
7c383fb2 767 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
e6445719
PS
768 return -EMSGSIZE;
769 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
67fa0341 770 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
e6445719
PS
771 return -EMSGSIZE;
772 if ((output->tun_flags & TUNNEL_CSUM) &&
67fa0341
JG
773 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
774 return -EMSGSIZE;
8f0aad6f
WZ
775 if (output->tp_src &&
776 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
777 return -EMSGSIZE;
778 if (output->tp_dst &&
779 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
780 return -EMSGSIZE;
67fa0341
JG
781 if ((output->tun_flags & TUNNEL_OAM) &&
782 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
e6445719 783 return -EMSGSIZE;
fc4099f1 784 if (swkey_tun_opts_len) {
1dd144cf
TG
785 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
786 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
787 swkey_tun_opts_len, tun_opts))
788 return -EMSGSIZE;
789 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
790 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
791 return -EMSGSIZE;
792 }
e6445719 793
e6445719
PS
794 return 0;
795}
796
6b26ba3a
JB
797static int ip_tun_to_nlattr(struct sk_buff *skb,
798 const struct ip_tunnel_key *output,
799 const void *tun_opts, int swkey_tun_opts_len,
800 unsigned short tun_proto)
f5796684
JG
801{
802 struct nlattr *nla;
803 int err;
804
805 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
806 if (!nla)
807 return -EMSGSIZE;
808
6b26ba3a
JB
809 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
810 tun_proto);
f5796684
JG
811 if (err)
812 return err;
813
814 nla_nest_end(skb, nla);
815 return 0;
816}
817
fc4099f1
PS
818int ovs_nla_put_tunnel_info(struct sk_buff *skb,
819 struct ip_tunnel_info *tun_info)
8f0aad6f 820{
ba3e2084
DM
821 return __ip_tun_to_nlattr(skb, &tun_info->key,
822 ip_tunnel_info_opts(tun_info),
823 tun_info->options_len,
824 ip_tunnel_info_af(tun_info));
8f0aad6f
WZ
825}
826
018c1dda
EG
827static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
828 const struct nlattr *a[],
829 bool is_mask, bool inner)
830{
831 __be16 tci = 0;
832 __be16 tpid = 0;
833
834 if (a[OVS_KEY_ATTR_VLAN])
835 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
836
837 if (a[OVS_KEY_ATTR_ETHERTYPE])
838 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
839
840 if (likely(!inner)) {
841 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
842 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
843 } else {
844 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
845 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
846 }
847 return 0;
848}
849
850static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
851 u64 key_attrs, bool inner,
852 const struct nlattr **a, bool log)
853{
854 __be16 tci = 0;
855
856 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
857 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
858 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
859 /* Not a VLAN. */
860 return 0;
861 }
862
863 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
864 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
865 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
866 return -EINVAL;
867 }
868
869 if (a[OVS_KEY_ATTR_VLAN])
870 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
871
872 if (!(tci & htons(VLAN_TAG_PRESENT))) {
873 if (tci) {
874 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
875 (inner) ? "C-VLAN" : "VLAN");
876 return -EINVAL;
877 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
878 /* Corner case for truncated VLAN header. */
879 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
880 (inner) ? "C-VLAN" : "VLAN");
881 return -EINVAL;
882 }
883 }
884
885 return 1;
886}
887
888static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
889 u64 key_attrs, bool inner,
890 const struct nlattr **a, bool log)
891{
892 __be16 tci = 0;
893 __be16 tpid = 0;
894 bool encap_valid = !!(match->key->eth.vlan.tci &
895 htons(VLAN_TAG_PRESENT));
896 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
897 htons(VLAN_TAG_PRESENT));
898
899 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
900 /* Not a VLAN. */
901 return 0;
902 }
903
904 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
905 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
906 (inner) ? "C-VLAN" : "VLAN");
907 return -EINVAL;
908 }
909
910 if (a[OVS_KEY_ATTR_VLAN])
911 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
912
913 if (a[OVS_KEY_ATTR_ETHERTYPE])
914 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
915
916 if (tpid != htons(0xffff)) {
917 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
918 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
919 return -EINVAL;
920 }
921 if (!(tci & htons(VLAN_TAG_PRESENT))) {
922 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
923 (inner) ? "C-VLAN" : "VLAN");
924 return -EINVAL;
925 }
926
927 return 1;
928}
929
930static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
931 u64 *key_attrs, bool inner,
932 const struct nlattr **a, bool is_mask,
933 bool log)
934{
935 int err;
936 const struct nlattr *encap;
937
938 if (!is_mask)
939 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
940 a, log);
941 else
942 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
943 a, log);
944 if (err <= 0)
945 return err;
946
947 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
948 if (err)
949 return err;
950
951 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
952 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
953 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
954
955 encap = a[OVS_KEY_ATTR_ENCAP];
956
957 if (!is_mask)
958 err = parse_flow_nlattrs(encap, a, key_attrs, log);
959 else
960 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
961
962 return err;
963}
964
965static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
966 u64 *key_attrs, const struct nlattr **a,
967 bool is_mask, bool log)
968{
969 int err;
970 bool encap_valid = false;
971
972 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
973 is_mask, log);
974 if (err)
975 return err;
976
977 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
978 if (encap_valid) {
979 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
980 is_mask, log);
981 if (err)
982 return err;
983 }
984
985 return 0;
986}
987
0a6410fb
JB
988static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
989 u64 *attrs, const struct nlattr **a,
990 bool is_mask, bool log)
991{
992 __be16 eth_type;
993
994 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
995 if (is_mask) {
996 /* Always exact match EtherType. */
997 eth_type = htons(0xffff);
998 } else if (!eth_proto_is_802_3(eth_type)) {
999 OVS_NLERR(log, "EtherType %x is less than min %x",
1000 ntohs(eth_type), ETH_P_802_3_MIN);
1001 return -EINVAL;
1002 }
1003
1004 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1005 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1006 return 0;
1007}
1008
c2ac6673
JS
1009static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1010 u64 *attrs, const struct nlattr **a,
1011 bool is_mask, bool log)
e6445719 1012{
0a6410fb
JB
1013 u8 mac_proto = MAC_PROTO_ETHERNET;
1014
971427f3
AZ
1015 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1016 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1017
1018 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1019 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1020 }
1021
1022 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1023 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1024
1025 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1026 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1027 }
1028
e6445719
PS
1029 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1030 SW_FLOW_KEY_PUT(match, phy.priority,
1031 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1032 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1033 }
1034
1035 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1036 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1037
426cda5c 1038 if (is_mask) {
e6445719 1039 in_port = 0xffffffff; /* Always exact match in_port. */
426cda5c 1040 } else if (in_port >= DP_MAX_PORTS) {
05da5898 1041 OVS_NLERR(log, "Port %d exceeds max allowable %d",
426cda5c 1042 in_port, DP_MAX_PORTS);
e6445719 1043 return -EINVAL;
426cda5c 1044 }
e6445719
PS
1045
1046 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1047 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1048 } else if (!is_mask) {
1049 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1050 }
1051
1052 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1053 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1054
1055 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1056 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1057 }
1058 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
6b26ba3a
JB
1059 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1060 is_mask, log) < 0)
e6445719
PS
1061 return -EINVAL;
1062 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1063 }
7f8a436e
JS
1064
1065 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
c2ac6673 1066 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
fbccce59 1067 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
7f8a436e 1068
9e384715 1069 if (ct_state & ~CT_SUPPORTED_MASK) {
fbccce59 1070 OVS_NLERR(log, "ct_state flags %08x unsupported",
6f225952
JS
1071 ct_state);
1072 return -EINVAL;
1073 }
7f8a436e
JS
1074
1075 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
1076 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1077 }
1078 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
c2ac6673 1079 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
7f8a436e
JS
1080 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1081
1082 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
1083 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1084 }
182e3042 1085 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
c2ac6673 1086 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
182e3042
JS
1087 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1088
1089 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1090 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1091 }
33db4125
JS
1092 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1093 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1094 const struct ovs_key_ct_labels *cl;
c2ac6673 1095
33db4125
JS
1096 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1097 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
c2ac6673 1098 sizeof(*cl), is_mask);
33db4125 1099 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
c2ac6673 1100 }
9dd7f890
JR
1101 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1102 const struct ovs_key_ct_tuple_ipv4 *ct;
1103
1104 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1105
1106 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1107 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1108 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1109 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1110 SW_FLOW_KEY_PUT(match, ct.orig_proto, ct->ipv4_proto, is_mask);
1111 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1112 }
1113 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1114 const struct ovs_key_ct_tuple_ipv6 *ct;
1115
1116 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1117
1118 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1119 sizeof(match->key->ipv6.ct_orig.src),
1120 is_mask);
1121 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1122 sizeof(match->key->ipv6.ct_orig.dst),
1123 is_mask);
1124 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1125 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1126 SW_FLOW_KEY_PUT(match, ct.orig_proto, ct->ipv6_proto, is_mask);
1127 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1128 }
329f45bc 1129
0a6410fb
JB
1130 /* For layer 3 packets the Ethernet type is provided
1131 * and treated as metadata but no MAC addresses are provided.
1132 */
1133 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1134 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1135 mac_proto = MAC_PROTO_NONE;
1136
329f45bc 1137 /* Always exact match mac_proto */
0a6410fb
JB
1138 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1139
1140 if (mac_proto == MAC_PROTO_NONE)
1141 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1142 log);
329f45bc 1143
e6445719
PS
1144 return 0;
1145}
1146
c2ac6673
JS
1147static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1148 u64 attrs, const struct nlattr **a,
1149 bool is_mask, bool log)
e6445719
PS
1150{
1151 int err;
e6445719 1152
c2ac6673 1153 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
e6445719
PS
1154 if (err)
1155 return err;
1156
1157 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1158 const struct ovs_key_ethernet *eth_key;
1159
1160 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1161 SW_FLOW_KEY_MEMCPY(match, eth.src,
1162 eth_key->eth_src, ETH_ALEN, is_mask);
1163 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1164 eth_key->eth_dst, ETH_ALEN, is_mask);
1165 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
e6445719 1166
0a6410fb
JB
1167 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1168 /* VLAN attribute is always parsed before getting here since it
1169 * may occur multiple times.
1170 */
1171 OVS_NLERR(log, "VLAN attribute unexpected.");
e6445719
PS
1172 return -EINVAL;
1173 }
1174
0a6410fb
JB
1175 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1176 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1177 log);
1178 if (err)
1179 return err;
1180 } else if (!is_mask) {
1181 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1182 }
1183 } else if (!match->key->eth.type) {
1184 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1185 return -EINVAL;
e6445719
PS
1186 }
1187
1188 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1189 const struct ovs_key_ipv4 *ipv4_key;
1190
1191 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1192 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
1193 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1194 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
1195 return -EINVAL;
1196 }
1197 SW_FLOW_KEY_PUT(match, ip.proto,
1198 ipv4_key->ipv4_proto, is_mask);
1199 SW_FLOW_KEY_PUT(match, ip.tos,
1200 ipv4_key->ipv4_tos, is_mask);
1201 SW_FLOW_KEY_PUT(match, ip.ttl,
1202 ipv4_key->ipv4_ttl, is_mask);
1203 SW_FLOW_KEY_PUT(match, ip.frag,
1204 ipv4_key->ipv4_frag, is_mask);
1205 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1206 ipv4_key->ipv4_src, is_mask);
1207 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1208 ipv4_key->ipv4_dst, is_mask);
1209 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1210 }
1211
1212 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1213 const struct ovs_key_ipv6 *ipv6_key;
1214
1215 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1216 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
1217 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1218 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
1219 return -EINVAL;
1220 }
fecaef85 1221
d3052bb5 1222 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
14591433 1223 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
fecaef85
JR
1224 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1225 return -EINVAL;
1226 }
1227
e6445719
PS
1228 SW_FLOW_KEY_PUT(match, ipv6.label,
1229 ipv6_key->ipv6_label, is_mask);
1230 SW_FLOW_KEY_PUT(match, ip.proto,
1231 ipv6_key->ipv6_proto, is_mask);
1232 SW_FLOW_KEY_PUT(match, ip.tos,
1233 ipv6_key->ipv6_tclass, is_mask);
1234 SW_FLOW_KEY_PUT(match, ip.ttl,
1235 ipv6_key->ipv6_hlimit, is_mask);
1236 SW_FLOW_KEY_PUT(match, ip.frag,
1237 ipv6_key->ipv6_frag, is_mask);
1238 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1239 ipv6_key->ipv6_src,
1240 sizeof(match->key->ipv6.addr.src),
1241 is_mask);
1242 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1243 ipv6_key->ipv6_dst,
1244 sizeof(match->key->ipv6.addr.dst),
1245 is_mask);
1246
1247 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1248 }
1249
1250 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1251 const struct ovs_key_arp *arp_key;
1252
1253 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1254 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
05da5898 1255 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
e6445719
PS
1256 arp_key->arp_op);
1257 return -EINVAL;
1258 }
1259
1260 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1261 arp_key->arp_sip, is_mask);
1262 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1263 arp_key->arp_tip, is_mask);
1264 SW_FLOW_KEY_PUT(match, ip.proto,
1265 ntohs(arp_key->arp_op), is_mask);
1266 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1267 arp_key->arp_sha, ETH_ALEN, is_mask);
1268 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1269 arp_key->arp_tha, ETH_ALEN, is_mask);
1270
1271 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1272 }
1273
25cd9ba0
SH
1274 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1275 const struct ovs_key_mpls *mpls_key;
1276
1277 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1278 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1279 mpls_key->mpls_lse, is_mask);
1280
1281 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1282 }
1283
e6445719
PS
1284 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1285 const struct ovs_key_tcp *tcp_key;
1286
1287 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
1288 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1289 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
1290 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1291 }
1292
5eb26b15 1293 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1b760fb9
JS
1294 SW_FLOW_KEY_PUT(match, tp.flags,
1295 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1296 is_mask);
5eb26b15
JR
1297 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1298 }
1299
e6445719
PS
1300 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1301 const struct ovs_key_udp *udp_key;
1302
1303 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
1304 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1305 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
1306 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1307 }
1308
1309 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1310 const struct ovs_key_sctp *sctp_key;
1311
1312 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
1313 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1314 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
1315 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1316 }
1317
1318 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1319 const struct ovs_key_icmp *icmp_key;
1320
1321 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 1322 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1323 htons(icmp_key->icmp_type), is_mask);
1139e241 1324 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1325 htons(icmp_key->icmp_code), is_mask);
1326 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1327 }
1328
1329 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1330 const struct ovs_key_icmpv6 *icmpv6_key;
1331
1332 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 1333 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1334 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 1335 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1336 htons(icmpv6_key->icmpv6_code), is_mask);
1337 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1338 }
1339
1340 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1341 const struct ovs_key_nd *nd_key;
1342
1343 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1344 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1345 nd_key->nd_target,
1346 sizeof(match->key->ipv6.nd.target),
1347 is_mask);
1348 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1349 nd_key->nd_sll, ETH_ALEN, is_mask);
1350 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1351 nd_key->nd_tll, ETH_ALEN, is_mask);
1352 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1353 }
1354
426cda5c 1355 if (attrs != 0) {
05da5898 1356 OVS_NLERR(log, "Unknown key attributes %llx",
426cda5c 1357 (unsigned long long)attrs);
e6445719 1358 return -EINVAL;
426cda5c 1359 }
e6445719
PS
1360
1361 return 0;
1362}
1363
81bfe3c3
TG
1364static void nlattr_set(struct nlattr *attr, u8 val,
1365 const struct ovs_len_tbl *tbl)
e6445719 1366{
f47de068
PS
1367 struct nlattr *nla;
1368 int rem;
e6445719 1369
f47de068
PS
1370 /* The nlattr stream should already have been validated */
1371 nla_for_each_nested(nla, attr, rem) {
982b5270
JG
1372 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1373 if (tbl[nla_type(nla)].next)
1374 tbl = tbl[nla_type(nla)].next;
1375 nlattr_set(nla, val, tbl);
1376 } else {
f47de068 1377 memset(nla_data(nla), val, nla_len(nla));
982b5270 1378 }
9e384715
JS
1379
1380 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1381 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
f47de068
PS
1382 }
1383}
1384
1385static void mask_set_nlattr(struct nlattr *attr, u8 val)
1386{
81bfe3c3 1387 nlattr_set(attr, val, ovs_key_lens);
e6445719
PS
1388}
1389
1390/**
1391 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1392 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1393 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1394 * does not include any don't care bit.
c2ac6673 1395 * @net: Used to determine per-namespace field support.
e6445719
PS
1396 * @match: receives the extracted flow match information.
1397 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1398 * sequence. The fields should of the packet that triggered the creation
1399 * of this flow.
1400 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1401 * attribute specifies the mask field of the wildcarded flow.
05da5898
JR
1402 * @log: Boolean to allow kernel error logging. Normally true, but when
1403 * probing for feature compatibility this should be passed in as false to
1404 * suppress unnecessary error logging.
e6445719 1405 */
c2ac6673 1406int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
a85311bf 1407 const struct nlattr *nla_key,
05da5898
JR
1408 const struct nlattr *nla_mask,
1409 bool log)
e6445719
PS
1410{
1411 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
f47de068 1412 struct nlattr *newmask = NULL;
e6445719
PS
1413 u64 key_attrs = 0;
1414 u64 mask_attrs = 0;
e6445719
PS
1415 int err;
1416
05da5898 1417 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
e6445719
PS
1418 if (err)
1419 return err;
1420
018c1dda
EG
1421 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1422 if (err)
1423 return err;
e6445719 1424
c2ac6673 1425 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
e6445719
PS
1426 if (err)
1427 return err;
1428
a85311bf
PS
1429 if (match->mask) {
1430 if (!nla_mask) {
1431 /* Create an exact match mask. We need to set to 0xff
1432 * all the 'match->mask' fields that have been touched
1433 * in 'match->key'. We cannot simply memset
1434 * 'match->mask', because padding bytes and fields not
1435 * specified in 'match->key' should be left to 0.
1436 * Instead, we use a stream of netlink attributes,
1437 * copied from 'key' and set to 0xff.
1438 * ovs_key_from_nlattrs() will take care of filling
1439 * 'match->mask' appropriately.
1440 */
1441 newmask = kmemdup(nla_key,
1442 nla_total_size(nla_len(nla_key)),
1443 GFP_KERNEL);
1444 if (!newmask)
1445 return -ENOMEM;
f47de068 1446
a85311bf 1447 mask_set_nlattr(newmask, 0xff);
f47de068 1448
a85311bf
PS
1449 /* The userspace does not send tunnel attributes that
1450 * are 0, but we should not wildcard them nonetheless.
1451 */
00a93bab 1452 if (match->key->tun_proto)
a85311bf
PS
1453 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1454 0xff, true);
f47de068 1455
a85311bf
PS
1456 nla_mask = newmask;
1457 }
f47de068 1458
05da5898 1459 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
e6445719 1460 if (err)
f47de068 1461 goto free_newmask;
e6445719 1462
a85311bf 1463 /* Always match on tci. */
018c1dda
EG
1464 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1465 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
e6445719 1466
018c1dda
EG
1467 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1468 if (err)
1469 goto free_newmask;
e6445719 1470
c2ac6673
JS
1471 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1472 log);
e6445719 1473 if (err)
f47de068 1474 goto free_newmask;
e6445719
PS
1475 }
1476
05da5898 1477 if (!match_validate(match, key_attrs, mask_attrs, log))
f47de068 1478 err = -EINVAL;
e6445719 1479
f47de068
PS
1480free_newmask:
1481 kfree(newmask);
1482 return err;
e6445719
PS
1483}
1484
74ed7ab9
JS
1485static size_t get_ufid_len(const struct nlattr *attr, bool log)
1486{
1487 size_t len;
1488
1489 if (!attr)
1490 return 0;
1491
1492 len = nla_len(attr);
1493 if (len < 1 || len > MAX_UFID_LENGTH) {
1494 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1495 nla_len(attr), MAX_UFID_LENGTH);
1496 return 0;
1497 }
1498
1499 return len;
1500}
1501
1502/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1503 * or false otherwise.
1504 */
1505bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1506 bool log)
1507{
1508 sfid->ufid_len = get_ufid_len(attr, log);
1509 if (sfid->ufid_len)
1510 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1511
1512 return sfid->ufid_len;
1513}
1514
1515int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1516 const struct sw_flow_key *key, bool log)
1517{
1518 struct sw_flow_key *new_key;
1519
1520 if (ovs_nla_get_ufid(sfid, ufid, log))
1521 return 0;
1522
1523 /* If UFID was not provided, use unmasked key. */
1524 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1525 if (!new_key)
1526 return -ENOMEM;
1527 memcpy(new_key, key, sizeof(*key));
1528 sfid->unmasked_key = new_key;
1529
1530 return 0;
1531}
1532
1533u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1534{
1535 return attr ? nla_get_u32(attr) : 0;
1536}
1537
e6445719
PS
1538/**
1539 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
9dd7f890
JR
1540 * @net: Network namespace.
1541 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1542 * metadata.
1543 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1544 * attributes.
1545 * @attrs: Bit mask for the netlink attributes included in @a.
05da5898
JR
1546 * @log: Boolean to allow kernel error logging. Normally true, but when
1547 * probing for feature compatibility this should be passed in as false to
1548 * suppress unnecessary error logging.
e6445719
PS
1549 *
1550 * This parses a series of Netlink attributes that form a flow key, which must
1551 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1552 * get the metadata, that is, the parts of the flow key that cannot be
1553 * extracted from the packet itself.
9dd7f890
JR
1554 *
1555 * This must be called before the packet key fields are filled in 'key'.
e6445719
PS
1556 */
1557
9dd7f890
JR
1558int ovs_nla_get_flow_metadata(struct net *net,
1559 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1560 u64 attrs, struct sw_flow_key *key, bool log)
e6445719 1561{
83c8df26 1562 struct sw_flow_match match;
e6445719
PS
1563
1564 memset(&match, 0, sizeof(match));
83c8df26 1565 match.key = key;
e6445719 1566
7f8a436e 1567 memset(&key->ct, 0, sizeof(key->ct));
9dd7f890
JR
1568 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1569 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1570
83c8df26 1571 key->phy.in_port = DP_MAX_PORTS;
e6445719 1572
c2ac6673 1573 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
e6445719
PS
1574}
1575
018c1dda
EG
1576static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1577 bool is_mask)
1578{
1579 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1580
1581 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1582 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1583 return -EMSGSIZE;
1584 return 0;
1585}
1586
5b4237bb
JS
1587static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1588 const struct sw_flow_key *output, bool is_mask,
1589 struct sk_buff *skb)
e6445719
PS
1590{
1591 struct ovs_key_ethernet *eth_key;
018c1dda
EG
1592 struct nlattr *nla;
1593 struct nlattr *encap = NULL;
1594 struct nlattr *in_encap = NULL;
e6445719 1595
971427f3
AZ
1596 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1597 goto nla_put_failure;
1598
1599 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1600 goto nla_put_failure;
1601
e6445719
PS
1602 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1603 goto nla_put_failure;
1604
00a93bab 1605 if ((swkey->tun_proto || is_mask)) {
d91641d9 1606 const void *opts = NULL;
f5796684
JG
1607
1608 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
d91641d9 1609 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
f5796684 1610
6b26ba3a
JB
1611 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1612 swkey->tun_opts_len, swkey->tun_proto))
f5796684
JG
1613 goto nla_put_failure;
1614 }
e6445719
PS
1615
1616 if (swkey->phy.in_port == DP_MAX_PORTS) {
1617 if (is_mask && (output->phy.in_port == 0xffff))
1618 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1619 goto nla_put_failure;
1620 } else {
1621 u16 upper_u16;
1622 upper_u16 = !is_mask ? 0 : 0xffff;
1623
1624 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1625 (upper_u16 << 16) | output->phy.in_port))
1626 goto nla_put_failure;
1627 }
1628
1629 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1630 goto nla_put_failure;
1631
9dd7f890 1632 if (ovs_ct_put_key(swkey, output, skb))
7f8a436e
JS
1633 goto nla_put_failure;
1634
0a6410fb
JB
1635 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1636 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1637 if (!nla)
e6445719 1638 goto nla_put_failure;
018c1dda 1639
0a6410fb
JB
1640 eth_key = nla_data(nla);
1641 ether_addr_copy(eth_key->eth_src, output->eth.src);
1642 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1643
1644 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1645 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
018c1dda 1646 goto nla_put_failure;
0a6410fb
JB
1647 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1648 if (!swkey->eth.vlan.tci)
018c1dda 1649 goto unencap;
0a6410fb
JB
1650
1651 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1652 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1653 goto nla_put_failure;
1654 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1655 if (!swkey->eth.cvlan.tci)
1656 goto unencap;
1657 }
018c1dda 1658 }
e6445719 1659
0a6410fb
JB
1660 if (swkey->eth.type == htons(ETH_P_802_2)) {
1661 /*
1662 * Ethertype 802.2 is represented in the netlink with omitted
1663 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1664 * 0xffff in the mask attribute. Ethertype can also
1665 * be wildcarded.
1666 */
1667 if (is_mask && output->eth.type)
1668 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1669 output->eth.type))
1670 goto nla_put_failure;
1671 goto unencap;
1672 }
e6445719
PS
1673 }
1674
1675 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1676 goto nla_put_failure;
1677
018c1dda
EG
1678 if (eth_type_vlan(swkey->eth.type)) {
1679 /* There are 3 VLAN tags, we don't know anything about the rest
1680 * of the packet, so truncate here.
1681 */
1682 WARN_ON_ONCE(!(encap && in_encap));
1683 goto unencap;
1684 }
1685
e6445719
PS
1686 if (swkey->eth.type == htons(ETH_P_IP)) {
1687 struct ovs_key_ipv4 *ipv4_key;
1688
1689 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1690 if (!nla)
1691 goto nla_put_failure;
1692 ipv4_key = nla_data(nla);
1693 ipv4_key->ipv4_src = output->ipv4.addr.src;
1694 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1695 ipv4_key->ipv4_proto = output->ip.proto;
1696 ipv4_key->ipv4_tos = output->ip.tos;
1697 ipv4_key->ipv4_ttl = output->ip.ttl;
1698 ipv4_key->ipv4_frag = output->ip.frag;
1699 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1700 struct ovs_key_ipv6 *ipv6_key;
1701
1702 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1703 if (!nla)
1704 goto nla_put_failure;
1705 ipv6_key = nla_data(nla);
1706 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1707 sizeof(ipv6_key->ipv6_src));
1708 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1709 sizeof(ipv6_key->ipv6_dst));
1710 ipv6_key->ipv6_label = output->ipv6.label;
1711 ipv6_key->ipv6_proto = output->ip.proto;
1712 ipv6_key->ipv6_tclass = output->ip.tos;
1713 ipv6_key->ipv6_hlimit = output->ip.ttl;
1714 ipv6_key->ipv6_frag = output->ip.frag;
1715 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1716 swkey->eth.type == htons(ETH_P_RARP)) {
1717 struct ovs_key_arp *arp_key;
1718
1719 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1720 if (!nla)
1721 goto nla_put_failure;
1722 arp_key = nla_data(nla);
1723 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1724 arp_key->arp_sip = output->ipv4.addr.src;
1725 arp_key->arp_tip = output->ipv4.addr.dst;
1726 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
1727 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1728 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
25cd9ba0
SH
1729 } else if (eth_p_mpls(swkey->eth.type)) {
1730 struct ovs_key_mpls *mpls_key;
1731
1732 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1733 if (!nla)
1734 goto nla_put_failure;
1735 mpls_key = nla_data(nla);
1736 mpls_key->mpls_lse = output->mpls.top_lse;
e6445719
PS
1737 }
1738
1739 if ((swkey->eth.type == htons(ETH_P_IP) ||
1740 swkey->eth.type == htons(ETH_P_IPV6)) &&
1741 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1742
1743 if (swkey->ip.proto == IPPROTO_TCP) {
1744 struct ovs_key_tcp *tcp_key;
1745
1746 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1747 if (!nla)
1748 goto nla_put_failure;
1749 tcp_key = nla_data(nla);
1139e241
JR
1750 tcp_key->tcp_src = output->tp.src;
1751 tcp_key->tcp_dst = output->tp.dst;
1752 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1753 output->tp.flags))
1754 goto nla_put_failure;
e6445719
PS
1755 } else if (swkey->ip.proto == IPPROTO_UDP) {
1756 struct ovs_key_udp *udp_key;
1757
1758 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1759 if (!nla)
1760 goto nla_put_failure;
1761 udp_key = nla_data(nla);
1139e241
JR
1762 udp_key->udp_src = output->tp.src;
1763 udp_key->udp_dst = output->tp.dst;
e6445719
PS
1764 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1765 struct ovs_key_sctp *sctp_key;
1766
1767 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1768 if (!nla)
1769 goto nla_put_failure;
1770 sctp_key = nla_data(nla);
1139e241
JR
1771 sctp_key->sctp_src = output->tp.src;
1772 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
1773 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1774 swkey->ip.proto == IPPROTO_ICMP) {
1775 struct ovs_key_icmp *icmp_key;
1776
1777 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1778 if (!nla)
1779 goto nla_put_failure;
1780 icmp_key = nla_data(nla);
1139e241
JR
1781 icmp_key->icmp_type = ntohs(output->tp.src);
1782 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
1783 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1784 swkey->ip.proto == IPPROTO_ICMPV6) {
1785 struct ovs_key_icmpv6 *icmpv6_key;
1786
1787 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1788 sizeof(*icmpv6_key));
1789 if (!nla)
1790 goto nla_put_failure;
1791 icmpv6_key = nla_data(nla);
1139e241
JR
1792 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1793 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
1794
1795 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1796 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1797 struct ovs_key_nd *nd_key;
1798
1799 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1800 if (!nla)
1801 goto nla_put_failure;
1802 nd_key = nla_data(nla);
1803 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1804 sizeof(nd_key->nd_target));
8c63ff09
JP
1805 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1806 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
1807 }
1808 }
1809 }
1810
1811unencap:
018c1dda
EG
1812 if (in_encap)
1813 nla_nest_end(skb, in_encap);
e6445719
PS
1814 if (encap)
1815 nla_nest_end(skb, encap);
1816
1817 return 0;
1818
1819nla_put_failure:
1820 return -EMSGSIZE;
1821}
1822
5b4237bb
JS
1823int ovs_nla_put_key(const struct sw_flow_key *swkey,
1824 const struct sw_flow_key *output, int attr, bool is_mask,
1825 struct sk_buff *skb)
1826{
1827 int err;
1828 struct nlattr *nla;
1829
1830 nla = nla_nest_start(skb, attr);
1831 if (!nla)
1832 return -EMSGSIZE;
1833 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1834 if (err)
1835 return err;
1836 nla_nest_end(skb, nla);
1837
1838 return 0;
1839}
1840
1841/* Called with ovs_mutex or RCU read lock. */
74ed7ab9
JS
1842int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1843{
1844 if (ovs_identifier_is_ufid(&flow->id))
1845 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1846 flow->id.ufid);
1847
1848 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1849 OVS_FLOW_ATTR_KEY, false, skb);
1850}
1851
1852/* Called with ovs_mutex or RCU read lock. */
1853int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
5b4237bb 1854{
26ad0b83 1855 return ovs_nla_put_key(&flow->key, &flow->key,
5b4237bb
JS
1856 OVS_FLOW_ATTR_KEY, false, skb);
1857}
1858
1859/* Called with ovs_mutex or RCU read lock. */
1860int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1861{
1862 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1863 OVS_FLOW_ATTR_MASK, true, skb);
1864}
1865
e6445719
PS
1866#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1867
05da5898 1868static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
e6445719
PS
1869{
1870 struct sw_flow_actions *sfa;
1871
426cda5c 1872 if (size > MAX_ACTIONS_BUFSIZE) {
05da5898 1873 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
e6445719 1874 return ERR_PTR(-EINVAL);
426cda5c 1875 }
e6445719
PS
1876
1877 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1878 if (!sfa)
1879 return ERR_PTR(-ENOMEM);
1880
1881 sfa->actions_len = 0;
1882 return sfa;
1883}
1884
34ae932a
TG
1885static void ovs_nla_free_set_action(const struct nlattr *a)
1886{
1887 const struct nlattr *ovs_key = nla_data(a);
1888 struct ovs_tunnel_info *ovs_tun;
1889
1890 switch (nla_type(ovs_key)) {
1891 case OVS_KEY_ATTR_TUNNEL_INFO:
1892 ovs_tun = nla_data(ovs_key);
1893 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1894 break;
1895 }
1896}
1897
1898void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1899{
1900 const struct nlattr *a;
1901 int rem;
1902
1903 if (!sf_acts)
1904 return;
1905
1906 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1907 switch (nla_type(a)) {
1908 case OVS_ACTION_ATTR_SET:
1909 ovs_nla_free_set_action(a);
1910 break;
7f8a436e
JS
1911 case OVS_ACTION_ATTR_CT:
1912 ovs_ct_free_action(a);
1913 break;
34ae932a
TG
1914 }
1915 }
1916
1917 kfree(sf_acts);
1918}
1919
1920static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1921{
1922 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1923}
1924
e6445719
PS
1925/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1926 * The caller must hold rcu_read_lock for this to be sensible. */
34ae932a 1927void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
e6445719 1928{
34ae932a 1929 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
e6445719
PS
1930}
1931
1932static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
05da5898 1933 int attr_len, bool log)
e6445719
PS
1934{
1935
1936 struct sw_flow_actions *acts;
1937 int new_acts_size;
1938 int req_size = NLA_ALIGN(attr_len);
1939 int next_offset = offsetof(struct sw_flow_actions, actions) +
1940 (*sfa)->actions_len;
1941
1942 if (req_size <= (ksize(*sfa) - next_offset))
1943 goto out;
1944
1945 new_acts_size = ksize(*sfa) * 2;
1946
1947 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1948 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1949 return ERR_PTR(-EMSGSIZE);
1950 new_acts_size = MAX_ACTIONS_BUFSIZE;
1951 }
1952
05da5898 1953 acts = nla_alloc_flow_actions(new_acts_size, log);
e6445719
PS
1954 if (IS_ERR(acts))
1955 return (void *)acts;
1956
1957 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1958 acts->actions_len = (*sfa)->actions_len;
8e2fed1c 1959 acts->orig_len = (*sfa)->orig_len;
e6445719
PS
1960 kfree(*sfa);
1961 *sfa = acts;
1962
1963out:
1964 (*sfa)->actions_len += req_size;
1965 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1966}
1967
f0b128c1 1968static struct nlattr *__add_action(struct sw_flow_actions **sfa,
05da5898 1969 int attrtype, void *data, int len, bool log)
e6445719
PS
1970{
1971 struct nlattr *a;
1972
05da5898 1973 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
e6445719 1974 if (IS_ERR(a))
f0b128c1 1975 return a;
e6445719
PS
1976
1977 a->nla_type = attrtype;
1978 a->nla_len = nla_attr_size(len);
1979
1980 if (data)
1981 memcpy(nla_data(a), data, len);
1982 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1983
f0b128c1
JG
1984 return a;
1985}
1986
7f8a436e
JS
1987int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1988 int len, bool log)
f0b128c1
JG
1989{
1990 struct nlattr *a;
1991
05da5898 1992 a = __add_action(sfa, attrtype, data, len, log);
f0b128c1 1993
f35423c1 1994 return PTR_ERR_OR_ZERO(a);
e6445719
PS
1995}
1996
1997static inline int add_nested_action_start(struct sw_flow_actions **sfa,
05da5898 1998 int attrtype, bool log)
e6445719
PS
1999{
2000 int used = (*sfa)->actions_len;
2001 int err;
2002
7f8a436e 2003 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
e6445719
PS
2004 if (err)
2005 return err;
2006
2007 return used;
2008}
2009
2010static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2011 int st_offset)
2012{
2013 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2014 st_offset);
2015
2016 a->nla_len = sfa->actions_len - st_offset;
2017}
2018
7f8a436e 2019static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0
SH
2020 const struct sw_flow_key *key,
2021 int depth, struct sw_flow_actions **sfa,
05da5898 2022 __be16 eth_type, __be16 vlan_tci, bool log);
25cd9ba0 2023
7f8a436e 2024static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
e6445719 2025 const struct sw_flow_key *key, int depth,
25cd9ba0 2026 struct sw_flow_actions **sfa,
05da5898 2027 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719
PS
2028{
2029 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2030 const struct nlattr *probability, *actions;
2031 const struct nlattr *a;
2032 int rem, start, err, st_acts;
2033
2034 memset(attrs, 0, sizeof(attrs));
2035 nla_for_each_nested(a, attr, rem) {
2036 int type = nla_type(a);
2037 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2038 return -EINVAL;
2039 attrs[type] = a;
2040 }
2041 if (rem)
2042 return -EINVAL;
2043
2044 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2045 if (!probability || nla_len(probability) != sizeof(u32))
2046 return -EINVAL;
2047
2048 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2049 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2050 return -EINVAL;
2051
2052 /* validation done, copy sample action. */
05da5898 2053 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
e6445719
PS
2054 if (start < 0)
2055 return start;
7f8a436e
JS
2056 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
2057 nla_data(probability), sizeof(u32), log);
e6445719
PS
2058 if (err)
2059 return err;
05da5898 2060 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
e6445719
PS
2061 if (st_acts < 0)
2062 return st_acts;
2063
7f8a436e 2064 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
05da5898 2065 eth_type, vlan_tci, log);
e6445719
PS
2066 if (err)
2067 return err;
2068
2069 add_nested_action_end(*sfa, st_acts);
2070 add_nested_action_end(*sfa, start);
2071
2072 return 0;
2073}
2074
e6445719
PS
2075void ovs_match_init(struct sw_flow_match *match,
2076 struct sw_flow_key *key,
2279994d 2077 bool reset_key,
e6445719
PS
2078 struct sw_flow_mask *mask)
2079{
2080 memset(match, 0, sizeof(*match));
2081 match->key = key;
2082 match->mask = mask;
2083
2279994d 2084 if (reset_key)
2085 memset(key, 0, sizeof(*key));
e6445719
PS
2086
2087 if (mask) {
2088 memset(&mask->key, 0, sizeof(mask->key));
2089 mask->range.start = mask->range.end = 0;
2090 }
2091}
2092
d91641d9
TG
2093static int validate_geneve_opts(struct sw_flow_key *key)
2094{
2095 struct geneve_opt *option;
2096 int opts_len = key->tun_opts_len;
2097 bool crit_opt = false;
2098
2099 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2100 while (opts_len > 0) {
2101 int len;
2102
2103 if (opts_len < sizeof(*option))
2104 return -EINVAL;
2105
2106 len = sizeof(*option) + option->length * 4;
2107 if (len > opts_len)
2108 return -EINVAL;
2109
2110 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2111
2112 option = (struct geneve_opt *)((u8 *)option + len);
2113 opts_len -= len;
2114 };
2115
2116 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2117
2118 return 0;
2119}
2120
e6445719 2121static int validate_and_copy_set_tun(const struct nlattr *attr,
05da5898 2122 struct sw_flow_actions **sfa, bool log)
e6445719
PS
2123{
2124 struct sw_flow_match match;
2125 struct sw_flow_key key;
34ae932a 2126 struct metadata_dst *tun_dst;
1d8fff90 2127 struct ip_tunnel_info *tun_info;
34ae932a 2128 struct ovs_tunnel_info *ovs_tun;
f0b128c1 2129 struct nlattr *a;
13101602 2130 int err = 0, start, opts_type;
e6445719 2131
2279994d 2132 ovs_match_init(&match, &key, true, NULL);
6b26ba3a 2133 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
1dd144cf
TG
2134 if (opts_type < 0)
2135 return opts_type;
e6445719 2136
f5796684 2137 if (key.tun_opts_len) {
1dd144cf
TG
2138 switch (opts_type) {
2139 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2140 err = validate_geneve_opts(&key);
2141 if (err < 0)
2142 return err;
2143 break;
2144 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2145 break;
2146 }
f5796684
JG
2147 };
2148
05da5898 2149 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
e6445719
PS
2150 if (start < 0)
2151 return start;
2152
34ae932a
TG
2153 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
2154 if (!tun_dst)
2155 return -ENOMEM;
2156
d71785ff
PA
2157 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2158 if (err) {
2159 dst_release((struct dst_entry *)tun_dst);
2160 return err;
2161 }
2162
f0b128c1 2163 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
34ae932a
TG
2164 sizeof(*ovs_tun), log);
2165 if (IS_ERR(a)) {
2166 dst_release((struct dst_entry *)tun_dst);
f0b128c1 2167 return PTR_ERR(a);
34ae932a
TG
2168 }
2169
2170 ovs_tun = nla_data(a);
2171 ovs_tun->tun_dst = tun_dst;
f0b128c1 2172
34ae932a
TG
2173 tun_info = &tun_dst->u.tun_info;
2174 tun_info->mode = IP_TUNNEL_INFO_TX;
00a93bab
JB
2175 if (key.tun_proto == AF_INET6)
2176 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
1d8fff90 2177 tun_info->key = key.tun_key;
f0b128c1 2178
4c222798
PS
2179 /* We need to store the options in the action itself since
2180 * everything else will go away after flow setup. We can append
2181 * it to tun_info and then point there.
2182 */
2183 ip_tunnel_info_opts_set(tun_info,
2184 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2185 key.tun_opts_len);
e6445719
PS
2186 add_nested_action_end(*sfa, start);
2187
2188 return err;
2189}
2190
83d2b9ba
JR
2191/* Return false if there are any non-masked bits set.
2192 * Mask follows data immediately, before any netlink padding.
2193 */
2194static bool validate_masked(u8 *data, int len)
2195{
2196 u8 *mask = data + len;
2197
2198 while (len--)
2199 if (*data++ & ~*mask++)
2200 return false;
2201
2202 return true;
2203}
2204
e6445719
PS
2205static int validate_set(const struct nlattr *a,
2206 const struct sw_flow_key *flow_key,
0a6410fb
JB
2207 struct sw_flow_actions **sfa, bool *skip_copy,
2208 u8 mac_proto, __be16 eth_type, bool masked, bool log)
e6445719
PS
2209{
2210 const struct nlattr *ovs_key = nla_data(a);
2211 int key_type = nla_type(ovs_key);
83d2b9ba 2212 size_t key_len;
e6445719
PS
2213
2214 /* There can be only one key in a action */
2215 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2216 return -EINVAL;
2217
83d2b9ba
JR
2218 key_len = nla_len(ovs_key);
2219 if (masked)
2220 key_len /= 2;
2221
e6445719 2222 if (key_type > OVS_KEY_ATTR_MAX ||
982b5270 2223 !check_attr_len(key_len, ovs_key_lens[key_type].len))
e6445719
PS
2224 return -EINVAL;
2225
83d2b9ba
JR
2226 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2227 return -EINVAL;
2228
e6445719
PS
2229 switch (key_type) {
2230 const struct ovs_key_ipv4 *ipv4_key;
2231 const struct ovs_key_ipv6 *ipv6_key;
2232 int err;
2233
2234 case OVS_KEY_ATTR_PRIORITY:
2235 case OVS_KEY_ATTR_SKB_MARK:
182e3042 2236 case OVS_KEY_ATTR_CT_MARK:
33db4125 2237 case OVS_KEY_ATTR_CT_LABELS:
e6445719
PS
2238 break;
2239
0a6410fb
JB
2240 case OVS_KEY_ATTR_ETHERNET:
2241 if (mac_proto != MAC_PROTO_ETHERNET)
2242 return -EINVAL;
87e159c5 2243 break;
0a6410fb 2244
e6445719 2245 case OVS_KEY_ATTR_TUNNEL:
83d2b9ba
JR
2246 if (masked)
2247 return -EINVAL; /* Masked tunnel set not supported. */
2248
2249 *skip_copy = true;
05da5898 2250 err = validate_and_copy_set_tun(a, sfa, log);
e6445719
PS
2251 if (err)
2252 return err;
2253 break;
2254
2255 case OVS_KEY_ATTR_IPV4:
25cd9ba0 2256 if (eth_type != htons(ETH_P_IP))
e6445719
PS
2257 return -EINVAL;
2258
e6445719 2259 ipv4_key = nla_data(ovs_key);
e6445719 2260
83d2b9ba
JR
2261 if (masked) {
2262 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
e6445719 2263
83d2b9ba
JR
2264 /* Non-writeable fields. */
2265 if (mask->ipv4_proto || mask->ipv4_frag)
2266 return -EINVAL;
2267 } else {
2268 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2269 return -EINVAL;
2270
2271 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2272 return -EINVAL;
2273 }
e6445719
PS
2274 break;
2275
2276 case OVS_KEY_ATTR_IPV6:
25cd9ba0 2277 if (eth_type != htons(ETH_P_IPV6))
e6445719
PS
2278 return -EINVAL;
2279
e6445719 2280 ipv6_key = nla_data(ovs_key);
e6445719 2281
83d2b9ba
JR
2282 if (masked) {
2283 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2284
2285 /* Non-writeable fields. */
2286 if (mask->ipv6_proto || mask->ipv6_frag)
2287 return -EINVAL;
2288
2289 /* Invalid bits in the flow label mask? */
2290 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2291 return -EINVAL;
2292 } else {
2293 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2294 return -EINVAL;
e6445719 2295
83d2b9ba
JR
2296 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2297 return -EINVAL;
2298 }
e6445719
PS
2299 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2300 return -EINVAL;
2301
2302 break;
2303
2304 case OVS_KEY_ATTR_TCP:
83d2b9ba
JR
2305 if ((eth_type != htons(ETH_P_IP) &&
2306 eth_type != htons(ETH_P_IPV6)) ||
2307 flow_key->ip.proto != IPPROTO_TCP)
e6445719
PS
2308 return -EINVAL;
2309
83d2b9ba 2310 break;
e6445719
PS
2311
2312 case OVS_KEY_ATTR_UDP:
83d2b9ba
JR
2313 if ((eth_type != htons(ETH_P_IP) &&
2314 eth_type != htons(ETH_P_IPV6)) ||
2315 flow_key->ip.proto != IPPROTO_UDP)
e6445719
PS
2316 return -EINVAL;
2317
83d2b9ba 2318 break;
25cd9ba0
SH
2319
2320 case OVS_KEY_ATTR_MPLS:
2321 if (!eth_p_mpls(eth_type))
2322 return -EINVAL;
2323 break;
e6445719
PS
2324
2325 case OVS_KEY_ATTR_SCTP:
83d2b9ba
JR
2326 if ((eth_type != htons(ETH_P_IP) &&
2327 eth_type != htons(ETH_P_IPV6)) ||
2328 flow_key->ip.proto != IPPROTO_SCTP)
e6445719
PS
2329 return -EINVAL;
2330
83d2b9ba 2331 break;
e6445719
PS
2332
2333 default:
2334 return -EINVAL;
2335 }
2336
83d2b9ba
JR
2337 /* Convert non-masked non-tunnel set actions to masked set actions. */
2338 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2339 int start, len = key_len * 2;
2340 struct nlattr *at;
2341
2342 *skip_copy = true;
2343
2344 start = add_nested_action_start(sfa,
2345 OVS_ACTION_ATTR_SET_TO_MASKED,
2346 log);
2347 if (start < 0)
2348 return start;
2349
2350 at = __add_action(sfa, key_type, NULL, len, log);
2351 if (IS_ERR(at))
2352 return PTR_ERR(at);
2353
2354 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2355 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2356 /* Clear non-writeable bits from otherwise writeable fields. */
2357 if (key_type == OVS_KEY_ATTR_IPV6) {
2358 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2359
2360 mask->ipv6_label &= htonl(0x000FFFFF);
2361 }
2362 add_nested_action_end(*sfa, start);
2363 }
2364
e6445719
PS
2365 return 0;
2366}
2367
2368static int validate_userspace(const struct nlattr *attr)
2369{
2370 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2371 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2372 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
8f0aad6f 2373 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
e6445719
PS
2374 };
2375 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2376 int error;
2377
2378 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2379 attr, userspace_policy);
2380 if (error)
2381 return error;
2382
2383 if (!a[OVS_USERSPACE_ATTR_PID] ||
2384 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2385 return -EINVAL;
2386
2387 return 0;
2388}
2389
2390static int copy_action(const struct nlattr *from,
05da5898 2391 struct sw_flow_actions **sfa, bool log)
e6445719
PS
2392{
2393 int totlen = NLA_ALIGN(from->nla_len);
2394 struct nlattr *to;
2395
05da5898 2396 to = reserve_sfa_size(sfa, from->nla_len, log);
e6445719
PS
2397 if (IS_ERR(to))
2398 return PTR_ERR(to);
2399
2400 memcpy(to, from, totlen);
2401 return 0;
2402}
2403
7f8a436e 2404static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0
SH
2405 const struct sw_flow_key *key,
2406 int depth, struct sw_flow_actions **sfa,
05da5898 2407 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719 2408{
0a6410fb 2409 u8 mac_proto = ovs_key_mac_proto(key);
e6445719
PS
2410 const struct nlattr *a;
2411 int rem, err;
2412
2413 if (depth >= SAMPLE_ACTION_DEPTH)
2414 return -EOVERFLOW;
2415
2416 nla_for_each_nested(a, attr, rem) {
2417 /* Expected argument lengths, (u32)-1 for variable length. */
2418 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2419 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 2420 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719 2421 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
25cd9ba0
SH
2422 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2423 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
e6445719
PS
2424 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2425 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2426 [OVS_ACTION_ATTR_SET] = (u32)-1,
83d2b9ba 2427 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
971427f3 2428 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
7f8a436e
JS
2429 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2430 [OVS_ACTION_ATTR_CT] = (u32)-1,
f2a4d086 2431 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
91820da6
JB
2432 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2433 [OVS_ACTION_ATTR_POP_ETH] = 0,
e6445719
PS
2434 };
2435 const struct ovs_action_push_vlan *vlan;
2436 int type = nla_type(a);
2437 bool skip_copy;
2438
2439 if (type > OVS_ACTION_ATTR_MAX ||
2440 (action_lens[type] != nla_len(a) &&
2441 action_lens[type] != (u32)-1))
2442 return -EINVAL;
2443
2444 skip_copy = false;
2445 switch (type) {
2446 case OVS_ACTION_ATTR_UNSPEC:
2447 return -EINVAL;
2448
2449 case OVS_ACTION_ATTR_USERSPACE:
2450 err = validate_userspace(a);
2451 if (err)
2452 return err;
2453 break;
2454
2455 case OVS_ACTION_ATTR_OUTPUT:
2456 if (nla_get_u32(a) >= DP_MAX_PORTS)
2457 return -EINVAL;
2458 break;
2459
f2a4d086
WT
2460 case OVS_ACTION_ATTR_TRUNC: {
2461 const struct ovs_action_trunc *trunc = nla_data(a);
2462
2463 if (trunc->max_len < ETH_HLEN)
2464 return -EINVAL;
2465 break;
2466 }
2467
971427f3
AZ
2468 case OVS_ACTION_ATTR_HASH: {
2469 const struct ovs_action_hash *act_hash = nla_data(a);
2470
2471 switch (act_hash->hash_alg) {
2472 case OVS_HASH_ALG_L4:
2473 break;
2474 default:
2475 return -EINVAL;
2476 }
2477
2478 break;
2479 }
e6445719
PS
2480
2481 case OVS_ACTION_ATTR_POP_VLAN:
0a6410fb
JB
2482 if (mac_proto != MAC_PROTO_ETHERNET)
2483 return -EINVAL;
25cd9ba0 2484 vlan_tci = htons(0);
e6445719
PS
2485 break;
2486
2487 case OVS_ACTION_ATTR_PUSH_VLAN:
0a6410fb
JB
2488 if (mac_proto != MAC_PROTO_ETHERNET)
2489 return -EINVAL;
e6445719 2490 vlan = nla_data(a);
018c1dda 2491 if (!eth_type_vlan(vlan->vlan_tpid))
e6445719
PS
2492 return -EINVAL;
2493 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2494 return -EINVAL;
25cd9ba0 2495 vlan_tci = vlan->vlan_tci;
e6445719
PS
2496 break;
2497
971427f3
AZ
2498 case OVS_ACTION_ATTR_RECIRC:
2499 break;
2500
25cd9ba0
SH
2501 case OVS_ACTION_ATTR_PUSH_MPLS: {
2502 const struct ovs_action_push_mpls *mpls = nla_data(a);
2503
25cd9ba0
SH
2504 if (!eth_p_mpls(mpls->mpls_ethertype))
2505 return -EINVAL;
2506 /* Prohibit push MPLS other than to a white list
2507 * for packets that have a known tag order.
2508 */
2509 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2510 (eth_type != htons(ETH_P_IP) &&
2511 eth_type != htons(ETH_P_IPV6) &&
2512 eth_type != htons(ETH_P_ARP) &&
2513 eth_type != htons(ETH_P_RARP) &&
2514 !eth_p_mpls(eth_type)))
2515 return -EINVAL;
2516 eth_type = mpls->mpls_ethertype;
2517 break;
2518 }
2519
2520 case OVS_ACTION_ATTR_POP_MPLS:
2521 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2522 !eth_p_mpls(eth_type))
2523 return -EINVAL;
2524
2525 /* Disallow subsequent L2.5+ set and mpls_pop actions
2526 * as there is no check here to ensure that the new
2527 * eth_type is valid and thus set actions could
2528 * write off the end of the packet or otherwise
2529 * corrupt it.
2530 *
2531 * Support for these actions is planned using packet
2532 * recirculation.
2533 */
2534 eth_type = htons(0);
2535 break;
2536
e6445719 2537 case OVS_ACTION_ATTR_SET:
25cd9ba0 2538 err = validate_set(a, key, sfa,
0a6410fb
JB
2539 &skip_copy, mac_proto, eth_type,
2540 false, log);
83d2b9ba
JR
2541 if (err)
2542 return err;
2543 break;
2544
2545 case OVS_ACTION_ATTR_SET_MASKED:
2546 err = validate_set(a, key, sfa,
0a6410fb
JB
2547 &skip_copy, mac_proto, eth_type,
2548 true, log);
e6445719
PS
2549 if (err)
2550 return err;
2551 break;
2552
2553 case OVS_ACTION_ATTR_SAMPLE:
7f8a436e 2554 err = validate_and_copy_sample(net, a, key, depth, sfa,
05da5898 2555 eth_type, vlan_tci, log);
e6445719
PS
2556 if (err)
2557 return err;
2558 skip_copy = true;
2559 break;
2560
7f8a436e
JS
2561 case OVS_ACTION_ATTR_CT:
2562 err = ovs_ct_copy_action(net, a, key, sfa, log);
2563 if (err)
2564 return err;
2565 skip_copy = true;
2566 break;
2567
91820da6
JB
2568 case OVS_ACTION_ATTR_PUSH_ETH:
2569 /* Disallow pushing an Ethernet header if one
2570 * is already present */
2571 if (mac_proto != MAC_PROTO_NONE)
2572 return -EINVAL;
2573 mac_proto = MAC_PROTO_NONE;
2574 break;
2575
2576 case OVS_ACTION_ATTR_POP_ETH:
2577 if (mac_proto != MAC_PROTO_ETHERNET)
2578 return -EINVAL;
2579 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2580 return -EINVAL;
2581 mac_proto = MAC_PROTO_ETHERNET;
2582 break;
2583
e6445719 2584 default:
05da5898 2585 OVS_NLERR(log, "Unknown Action type %d", type);
e6445719
PS
2586 return -EINVAL;
2587 }
2588 if (!skip_copy) {
05da5898 2589 err = copy_action(a, sfa, log);
e6445719
PS
2590 if (err)
2591 return err;
2592 }
2593 }
2594
2595 if (rem > 0)
2596 return -EINVAL;
2597
2598 return 0;
2599}
2600
83d2b9ba 2601/* 'key' must be the masked key. */
7f8a436e 2602int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0 2603 const struct sw_flow_key *key,
05da5898 2604 struct sw_flow_actions **sfa, bool log)
25cd9ba0 2605{
2fdb957d
PS
2606 int err;
2607
05da5898 2608 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2fdb957d
PS
2609 if (IS_ERR(*sfa))
2610 return PTR_ERR(*sfa);
2611
8e2fed1c 2612 (*sfa)->orig_len = nla_len(attr);
7f8a436e 2613 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
018c1dda 2614 key->eth.vlan.tci, log);
2fdb957d 2615 if (err)
34ae932a 2616 ovs_nla_free_flow_actions(*sfa);
2fdb957d
PS
2617
2618 return err;
25cd9ba0
SH
2619}
2620
e6445719
PS
2621static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2622{
2623 const struct nlattr *a;
2624 struct nlattr *start;
2625 int err = 0, rem;
2626
2627 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2628 if (!start)
2629 return -EMSGSIZE;
2630
2631 nla_for_each_nested(a, attr, rem) {
2632 int type = nla_type(a);
2633 struct nlattr *st_sample;
2634
2635 switch (type) {
2636 case OVS_SAMPLE_ATTR_PROBABILITY:
2637 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2638 sizeof(u32), nla_data(a)))
2639 return -EMSGSIZE;
2640 break;
2641 case OVS_SAMPLE_ATTR_ACTIONS:
2642 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2643 if (!st_sample)
2644 return -EMSGSIZE;
2645 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2646 if (err)
2647 return err;
2648 nla_nest_end(skb, st_sample);
2649 break;
2650 }
2651 }
2652
2653 nla_nest_end(skb, start);
2654 return err;
2655}
2656
2657static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2658{
2659 const struct nlattr *ovs_key = nla_data(a);
2660 int key_type = nla_type(ovs_key);
2661 struct nlattr *start;
2662 int err;
2663
2664 switch (key_type) {
f0b128c1 2665 case OVS_KEY_ATTR_TUNNEL_INFO: {
34ae932a
TG
2666 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2667 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
f0b128c1 2668
e6445719
PS
2669 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2670 if (!start)
2671 return -EMSGSIZE;
2672
e905eabc
SH
2673 err = ip_tun_to_nlattr(skb, &tun_info->key,
2674 ip_tunnel_info_opts(tun_info),
2675 tun_info->options_len,
2676 ip_tunnel_info_af(tun_info));
e6445719
PS
2677 if (err)
2678 return err;
2679 nla_nest_end(skb, start);
2680 break;
f0b128c1 2681 }
e6445719
PS
2682 default:
2683 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2684 return -EMSGSIZE;
2685 break;
2686 }
2687
2688 return 0;
2689}
2690
83d2b9ba
JR
2691static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2692 struct sk_buff *skb)
2693{
2694 const struct nlattr *ovs_key = nla_data(a);
f4f8e738 2695 struct nlattr *nla;
83d2b9ba
JR
2696 size_t key_len = nla_len(ovs_key) / 2;
2697
2698 /* Revert the conversion we did from a non-masked set action to
2699 * masked set action.
2700 */
f4f8e738
JS
2701 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2702 if (!nla)
83d2b9ba
JR
2703 return -EMSGSIZE;
2704
f4f8e738
JS
2705 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2706 return -EMSGSIZE;
2707
2708 nla_nest_end(skb, nla);
83d2b9ba
JR
2709 return 0;
2710}
2711
e6445719
PS
2712int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2713{
2714 const struct nlattr *a;
2715 int rem, err;
2716
2717 nla_for_each_attr(a, attr, len, rem) {
2718 int type = nla_type(a);
2719
2720 switch (type) {
2721 case OVS_ACTION_ATTR_SET:
2722 err = set_action_to_attr(a, skb);
2723 if (err)
2724 return err;
2725 break;
2726
83d2b9ba
JR
2727 case OVS_ACTION_ATTR_SET_TO_MASKED:
2728 err = masked_set_action_to_set_action_attr(a, skb);
2729 if (err)
2730 return err;
2731 break;
2732
e6445719
PS
2733 case OVS_ACTION_ATTR_SAMPLE:
2734 err = sample_action_to_attr(a, skb);
2735 if (err)
2736 return err;
2737 break;
7f8a436e
JS
2738
2739 case OVS_ACTION_ATTR_CT:
2740 err = ovs_ct_action_to_attr(nla_data(a), skb);
2741 if (err)
2742 return err;
2743 break;
2744
e6445719
PS
2745 default:
2746 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2747 return -EMSGSIZE;
2748 break;
2749 }
2750 }
2751
2752 return 0;
2753}