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