ubifs: make ubifs_[get|set]xattr atomic
[deliverable/linux.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 */
7f8a436e
JS
294 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
295 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
182e3042 296 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
c2ac6673 297 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABEL */
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) },
7f8a436e
JS
352 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
353 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
182e3042 354 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
c2ac6673 355 [OVS_KEY_ATTR_CT_LABEL] = { .len = sizeof(struct ovs_key_ct_label) },
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)) {
7f8a436e
JS
817 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
818
819 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
820 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
821 }
822 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
c2ac6673 823 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
7f8a436e
JS
824 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
825
826 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
827 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
828 }
182e3042 829 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
c2ac6673 830 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
182e3042
JS
831 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
832
833 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
834 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
835 }
c2ac6673
JS
836 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABEL) &&
837 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABEL)) {
838 const struct ovs_key_ct_label *cl;
839
840 cl = nla_data(a[OVS_KEY_ATTR_CT_LABEL]);
841 SW_FLOW_KEY_MEMCPY(match, ct.label, cl->ct_label,
842 sizeof(*cl), is_mask);
843 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABEL);
844 }
e6445719
PS
845 return 0;
846}
847
c2ac6673
JS
848static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
849 u64 attrs, const struct nlattr **a,
850 bool is_mask, bool log)
e6445719
PS
851{
852 int err;
e6445719 853
c2ac6673 854 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
e6445719
PS
855 if (err)
856 return err;
857
858 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
859 const struct ovs_key_ethernet *eth_key;
860
861 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
862 SW_FLOW_KEY_MEMCPY(match, eth.src,
863 eth_key->eth_src, ETH_ALEN, is_mask);
864 SW_FLOW_KEY_MEMCPY(match, eth.dst,
865 eth_key->eth_dst, ETH_ALEN, is_mask);
866 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
867 }
868
869 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
870 __be16 tci;
871
872 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
873 if (!(tci & htons(VLAN_TAG_PRESENT))) {
874 if (is_mask)
05da5898 875 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
e6445719 876 else
05da5898 877 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
e6445719
PS
878
879 return -EINVAL;
880 }
881
882 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
883 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
a85311bf 884 }
e6445719
PS
885
886 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
887 __be16 eth_type;
888
889 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
890 if (is_mask) {
891 /* Always exact match EtherType. */
892 eth_type = htons(0xffff);
6713fc9b 893 } else if (!eth_proto_is_802_3(eth_type)) {
05da5898
JR
894 OVS_NLERR(log, "EtherType %x is less than min %x",
895 ntohs(eth_type), ETH_P_802_3_MIN);
e6445719
PS
896 return -EINVAL;
897 }
898
899 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
900 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
901 } else if (!is_mask) {
902 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
903 }
904
905 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
906 const struct ovs_key_ipv4 *ipv4_key;
907
908 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
909 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
910 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
911 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
912 return -EINVAL;
913 }
914 SW_FLOW_KEY_PUT(match, ip.proto,
915 ipv4_key->ipv4_proto, is_mask);
916 SW_FLOW_KEY_PUT(match, ip.tos,
917 ipv4_key->ipv4_tos, is_mask);
918 SW_FLOW_KEY_PUT(match, ip.ttl,
919 ipv4_key->ipv4_ttl, is_mask);
920 SW_FLOW_KEY_PUT(match, ip.frag,
921 ipv4_key->ipv4_frag, is_mask);
922 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
923 ipv4_key->ipv4_src, is_mask);
924 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
925 ipv4_key->ipv4_dst, is_mask);
926 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
927 }
928
929 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
930 const struct ovs_key_ipv6 *ipv6_key;
931
932 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
933 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
934 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
935 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
936 return -EINVAL;
937 }
fecaef85 938
d3052bb5 939 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
14591433 940 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
fecaef85
JR
941 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
942 return -EINVAL;
943 }
944
e6445719
PS
945 SW_FLOW_KEY_PUT(match, ipv6.label,
946 ipv6_key->ipv6_label, is_mask);
947 SW_FLOW_KEY_PUT(match, ip.proto,
948 ipv6_key->ipv6_proto, is_mask);
949 SW_FLOW_KEY_PUT(match, ip.tos,
950 ipv6_key->ipv6_tclass, is_mask);
951 SW_FLOW_KEY_PUT(match, ip.ttl,
952 ipv6_key->ipv6_hlimit, is_mask);
953 SW_FLOW_KEY_PUT(match, ip.frag,
954 ipv6_key->ipv6_frag, is_mask);
955 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
956 ipv6_key->ipv6_src,
957 sizeof(match->key->ipv6.addr.src),
958 is_mask);
959 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
960 ipv6_key->ipv6_dst,
961 sizeof(match->key->ipv6.addr.dst),
962 is_mask);
963
964 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
965 }
966
967 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
968 const struct ovs_key_arp *arp_key;
969
970 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
971 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
05da5898 972 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
e6445719
PS
973 arp_key->arp_op);
974 return -EINVAL;
975 }
976
977 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
978 arp_key->arp_sip, is_mask);
979 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
980 arp_key->arp_tip, is_mask);
981 SW_FLOW_KEY_PUT(match, ip.proto,
982 ntohs(arp_key->arp_op), is_mask);
983 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
984 arp_key->arp_sha, ETH_ALEN, is_mask);
985 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
986 arp_key->arp_tha, ETH_ALEN, is_mask);
987
988 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
989 }
990
25cd9ba0
SH
991 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
992 const struct ovs_key_mpls *mpls_key;
993
994 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
995 SW_FLOW_KEY_PUT(match, mpls.top_lse,
996 mpls_key->mpls_lse, is_mask);
997
998 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
999 }
1000
e6445719
PS
1001 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1002 const struct ovs_key_tcp *tcp_key;
1003
1004 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
1005 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1006 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
1007 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1008 }
1009
5eb26b15 1010 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1b760fb9
JS
1011 SW_FLOW_KEY_PUT(match, tp.flags,
1012 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1013 is_mask);
5eb26b15
JR
1014 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1015 }
1016
e6445719
PS
1017 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1018 const struct ovs_key_udp *udp_key;
1019
1020 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
1021 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1022 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
1023 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1024 }
1025
1026 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1027 const struct ovs_key_sctp *sctp_key;
1028
1029 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
1030 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1031 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
1032 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1033 }
1034
1035 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1036 const struct ovs_key_icmp *icmp_key;
1037
1038 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 1039 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1040 htons(icmp_key->icmp_type), is_mask);
1139e241 1041 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1042 htons(icmp_key->icmp_code), is_mask);
1043 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1044 }
1045
1046 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1047 const struct ovs_key_icmpv6 *icmpv6_key;
1048
1049 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 1050 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1051 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 1052 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1053 htons(icmpv6_key->icmpv6_code), is_mask);
1054 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1055 }
1056
1057 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1058 const struct ovs_key_nd *nd_key;
1059
1060 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1061 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1062 nd_key->nd_target,
1063 sizeof(match->key->ipv6.nd.target),
1064 is_mask);
1065 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1066 nd_key->nd_sll, ETH_ALEN, is_mask);
1067 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1068 nd_key->nd_tll, ETH_ALEN, is_mask);
1069 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1070 }
1071
426cda5c 1072 if (attrs != 0) {
05da5898 1073 OVS_NLERR(log, "Unknown key attributes %llx",
426cda5c 1074 (unsigned long long)attrs);
e6445719 1075 return -EINVAL;
426cda5c 1076 }
e6445719
PS
1077
1078 return 0;
1079}
1080
81bfe3c3
TG
1081static void nlattr_set(struct nlattr *attr, u8 val,
1082 const struct ovs_len_tbl *tbl)
e6445719 1083{
f47de068
PS
1084 struct nlattr *nla;
1085 int rem;
e6445719 1086
f47de068
PS
1087 /* The nlattr stream should already have been validated */
1088 nla_for_each_nested(nla, attr, rem) {
982b5270
JG
1089 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1090 if (tbl[nla_type(nla)].next)
1091 tbl = tbl[nla_type(nla)].next;
1092 nlattr_set(nla, val, tbl);
1093 } else {
f47de068 1094 memset(nla_data(nla), val, nla_len(nla));
982b5270 1095 }
f47de068
PS
1096 }
1097}
1098
1099static void mask_set_nlattr(struct nlattr *attr, u8 val)
1100{
81bfe3c3 1101 nlattr_set(attr, val, ovs_key_lens);
e6445719
PS
1102}
1103
1104/**
1105 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1106 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1107 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1108 * does not include any don't care bit.
c2ac6673 1109 * @net: Used to determine per-namespace field support.
e6445719
PS
1110 * @match: receives the extracted flow match information.
1111 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1112 * sequence. The fields should of the packet that triggered the creation
1113 * of this flow.
1114 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1115 * attribute specifies the mask field of the wildcarded flow.
05da5898
JR
1116 * @log: Boolean to allow kernel error logging. Normally true, but when
1117 * probing for feature compatibility this should be passed in as false to
1118 * suppress unnecessary error logging.
e6445719 1119 */
c2ac6673 1120int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
a85311bf 1121 const struct nlattr *nla_key,
05da5898
JR
1122 const struct nlattr *nla_mask,
1123 bool log)
e6445719
PS
1124{
1125 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1126 const struct nlattr *encap;
f47de068 1127 struct nlattr *newmask = NULL;
e6445719
PS
1128 u64 key_attrs = 0;
1129 u64 mask_attrs = 0;
1130 bool encap_valid = false;
1131 int err;
1132
05da5898 1133 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
e6445719
PS
1134 if (err)
1135 return err;
1136
1137 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1138 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1139 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1140 __be16 tci;
1141
1142 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1143 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
05da5898 1144 OVS_NLERR(log, "Invalid Vlan frame.");
e6445719
PS
1145 return -EINVAL;
1146 }
1147
1148 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1149 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1150 encap = a[OVS_KEY_ATTR_ENCAP];
1151 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1152 encap_valid = true;
1153
1154 if (tci & htons(VLAN_TAG_PRESENT)) {
05da5898 1155 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
e6445719
PS
1156 if (err)
1157 return err;
1158 } else if (!tci) {
1159 /* Corner case for truncated 802.1Q header. */
1160 if (nla_len(encap)) {
05da5898 1161 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
e6445719
PS
1162 return -EINVAL;
1163 }
1164 } else {
05da5898 1165 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
e6445719
PS
1166 return -EINVAL;
1167 }
1168 }
1169
c2ac6673 1170 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
e6445719
PS
1171 if (err)
1172 return err;
1173
a85311bf
PS
1174 if (match->mask) {
1175 if (!nla_mask) {
1176 /* Create an exact match mask. We need to set to 0xff
1177 * all the 'match->mask' fields that have been touched
1178 * in 'match->key'. We cannot simply memset
1179 * 'match->mask', because padding bytes and fields not
1180 * specified in 'match->key' should be left to 0.
1181 * Instead, we use a stream of netlink attributes,
1182 * copied from 'key' and set to 0xff.
1183 * ovs_key_from_nlattrs() will take care of filling
1184 * 'match->mask' appropriately.
1185 */
1186 newmask = kmemdup(nla_key,
1187 nla_total_size(nla_len(nla_key)),
1188 GFP_KERNEL);
1189 if (!newmask)
1190 return -ENOMEM;
f47de068 1191
a85311bf 1192 mask_set_nlattr(newmask, 0xff);
f47de068 1193
a85311bf
PS
1194 /* The userspace does not send tunnel attributes that
1195 * are 0, but we should not wildcard them nonetheless.
1196 */
c1ea5d67 1197 if (match->key->tun_key.u.ipv4.dst)
a85311bf
PS
1198 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1199 0xff, true);
f47de068 1200
a85311bf
PS
1201 nla_mask = newmask;
1202 }
f47de068 1203
05da5898 1204 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
e6445719 1205 if (err)
f47de068 1206 goto free_newmask;
e6445719 1207
a85311bf
PS
1208 /* Always match on tci. */
1209 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1210
f47de068 1211 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
e6445719
PS
1212 __be16 eth_type = 0;
1213 __be16 tci = 0;
1214
1215 if (!encap_valid) {
05da5898 1216 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
f47de068
PS
1217 err = -EINVAL;
1218 goto free_newmask;
e6445719
PS
1219 }
1220
1221 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1222 if (a[OVS_KEY_ATTR_ETHERTYPE])
1223 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1224
1225 if (eth_type == htons(0xffff)) {
1226 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1227 encap = a[OVS_KEY_ATTR_ENCAP];
05da5898
JR
1228 err = parse_flow_mask_nlattrs(encap, a,
1229 &mask_attrs, log);
f47de068
PS
1230 if (err)
1231 goto free_newmask;
e6445719 1232 } else {
05da5898
JR
1233 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1234 ntohs(eth_type));
f47de068
PS
1235 err = -EINVAL;
1236 goto free_newmask;
e6445719
PS
1237 }
1238
1239 if (a[OVS_KEY_ATTR_VLAN])
1240 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1241
1242 if (!(tci & htons(VLAN_TAG_PRESENT))) {
05da5898
JR
1243 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1244 ntohs(tci));
f47de068
PS
1245 err = -EINVAL;
1246 goto free_newmask;
e6445719
PS
1247 }
1248 }
1249
c2ac6673
JS
1250 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1251 log);
e6445719 1252 if (err)
f47de068 1253 goto free_newmask;
e6445719
PS
1254 }
1255
05da5898 1256 if (!match_validate(match, key_attrs, mask_attrs, log))
f47de068 1257 err = -EINVAL;
e6445719 1258
f47de068
PS
1259free_newmask:
1260 kfree(newmask);
1261 return err;
e6445719
PS
1262}
1263
74ed7ab9
JS
1264static size_t get_ufid_len(const struct nlattr *attr, bool log)
1265{
1266 size_t len;
1267
1268 if (!attr)
1269 return 0;
1270
1271 len = nla_len(attr);
1272 if (len < 1 || len > MAX_UFID_LENGTH) {
1273 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1274 nla_len(attr), MAX_UFID_LENGTH);
1275 return 0;
1276 }
1277
1278 return len;
1279}
1280
1281/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1282 * or false otherwise.
1283 */
1284bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1285 bool log)
1286{
1287 sfid->ufid_len = get_ufid_len(attr, log);
1288 if (sfid->ufid_len)
1289 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1290
1291 return sfid->ufid_len;
1292}
1293
1294int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1295 const struct sw_flow_key *key, bool log)
1296{
1297 struct sw_flow_key *new_key;
1298
1299 if (ovs_nla_get_ufid(sfid, ufid, log))
1300 return 0;
1301
1302 /* If UFID was not provided, use unmasked key. */
1303 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1304 if (!new_key)
1305 return -ENOMEM;
1306 memcpy(new_key, key, sizeof(*key));
1307 sfid->unmasked_key = new_key;
1308
1309 return 0;
1310}
1311
1312u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1313{
1314 return attr ? nla_get_u32(attr) : 0;
1315}
1316
e6445719
PS
1317/**
1318 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
83c8df26 1319 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
e6445719
PS
1320 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1321 * sequence.
05da5898
JR
1322 * @log: Boolean to allow kernel error logging. Normally true, but when
1323 * probing for feature compatibility this should be passed in as false to
1324 * suppress unnecessary error logging.
e6445719
PS
1325 *
1326 * This parses a series of Netlink attributes that form a flow key, which must
1327 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1328 * get the metadata, that is, the parts of the flow key that cannot be
1329 * extracted from the packet itself.
1330 */
1331
c2ac6673 1332int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
05da5898
JR
1333 struct sw_flow_key *key,
1334 bool log)
e6445719 1335{
e6445719 1336 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
83c8df26 1337 struct sw_flow_match match;
e6445719
PS
1338 u64 attrs = 0;
1339 int err;
e6445719 1340
05da5898 1341 err = parse_flow_nlattrs(attr, a, &attrs, log);
e6445719
PS
1342 if (err)
1343 return -EINVAL;
1344
1345 memset(&match, 0, sizeof(match));
83c8df26 1346 match.key = key;
e6445719 1347
7f8a436e 1348 memset(&key->ct, 0, sizeof(key->ct));
83c8df26 1349 key->phy.in_port = DP_MAX_PORTS;
e6445719 1350
c2ac6673 1351 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
e6445719
PS
1352}
1353
5b4237bb
JS
1354static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1355 const struct sw_flow_key *output, bool is_mask,
1356 struct sk_buff *skb)
e6445719
PS
1357{
1358 struct ovs_key_ethernet *eth_key;
1359 struct nlattr *nla, *encap;
e6445719 1360
971427f3
AZ
1361 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1362 goto nla_put_failure;
1363
1364 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1365 goto nla_put_failure;
1366
e6445719
PS
1367 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1368 goto nla_put_failure;
1369
c1ea5d67 1370 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
d91641d9 1371 const void *opts = NULL;
f5796684
JG
1372
1373 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
d91641d9 1374 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
f5796684
JG
1375
1376 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1377 swkey->tun_opts_len))
1378 goto nla_put_failure;
1379 }
e6445719
PS
1380
1381 if (swkey->phy.in_port == DP_MAX_PORTS) {
1382 if (is_mask && (output->phy.in_port == 0xffff))
1383 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1384 goto nla_put_failure;
1385 } else {
1386 u16 upper_u16;
1387 upper_u16 = !is_mask ? 0 : 0xffff;
1388
1389 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1390 (upper_u16 << 16) | output->phy.in_port))
1391 goto nla_put_failure;
1392 }
1393
1394 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1395 goto nla_put_failure;
1396
7f8a436e
JS
1397 if (ovs_ct_put_key(output, skb))
1398 goto nla_put_failure;
1399
e6445719
PS
1400 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1401 if (!nla)
1402 goto nla_put_failure;
1403
1404 eth_key = nla_data(nla);
8c63ff09
JP
1405 ether_addr_copy(eth_key->eth_src, output->eth.src);
1406 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
e6445719
PS
1407
1408 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1409 __be16 eth_type;
1410 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1411 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1412 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1413 goto nla_put_failure;
1414 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1415 if (!swkey->eth.tci)
1416 goto unencap;
1417 } else
1418 encap = NULL;
1419
1420 if (swkey->eth.type == htons(ETH_P_802_2)) {
1421 /*
1422 * Ethertype 802.2 is represented in the netlink with omitted
1423 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1424 * 0xffff in the mask attribute. Ethertype can also
1425 * be wildcarded.
1426 */
1427 if (is_mask && output->eth.type)
1428 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1429 output->eth.type))
1430 goto nla_put_failure;
1431 goto unencap;
1432 }
1433
1434 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1435 goto nla_put_failure;
1436
1437 if (swkey->eth.type == htons(ETH_P_IP)) {
1438 struct ovs_key_ipv4 *ipv4_key;
1439
1440 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1441 if (!nla)
1442 goto nla_put_failure;
1443 ipv4_key = nla_data(nla);
1444 ipv4_key->ipv4_src = output->ipv4.addr.src;
1445 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1446 ipv4_key->ipv4_proto = output->ip.proto;
1447 ipv4_key->ipv4_tos = output->ip.tos;
1448 ipv4_key->ipv4_ttl = output->ip.ttl;
1449 ipv4_key->ipv4_frag = output->ip.frag;
1450 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1451 struct ovs_key_ipv6 *ipv6_key;
1452
1453 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1454 if (!nla)
1455 goto nla_put_failure;
1456 ipv6_key = nla_data(nla);
1457 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1458 sizeof(ipv6_key->ipv6_src));
1459 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1460 sizeof(ipv6_key->ipv6_dst));
1461 ipv6_key->ipv6_label = output->ipv6.label;
1462 ipv6_key->ipv6_proto = output->ip.proto;
1463 ipv6_key->ipv6_tclass = output->ip.tos;
1464 ipv6_key->ipv6_hlimit = output->ip.ttl;
1465 ipv6_key->ipv6_frag = output->ip.frag;
1466 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1467 swkey->eth.type == htons(ETH_P_RARP)) {
1468 struct ovs_key_arp *arp_key;
1469
1470 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1471 if (!nla)
1472 goto nla_put_failure;
1473 arp_key = nla_data(nla);
1474 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1475 arp_key->arp_sip = output->ipv4.addr.src;
1476 arp_key->arp_tip = output->ipv4.addr.dst;
1477 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
1478 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1479 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
25cd9ba0
SH
1480 } else if (eth_p_mpls(swkey->eth.type)) {
1481 struct ovs_key_mpls *mpls_key;
1482
1483 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1484 if (!nla)
1485 goto nla_put_failure;
1486 mpls_key = nla_data(nla);
1487 mpls_key->mpls_lse = output->mpls.top_lse;
e6445719
PS
1488 }
1489
1490 if ((swkey->eth.type == htons(ETH_P_IP) ||
1491 swkey->eth.type == htons(ETH_P_IPV6)) &&
1492 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1493
1494 if (swkey->ip.proto == IPPROTO_TCP) {
1495 struct ovs_key_tcp *tcp_key;
1496
1497 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1498 if (!nla)
1499 goto nla_put_failure;
1500 tcp_key = nla_data(nla);
1139e241
JR
1501 tcp_key->tcp_src = output->tp.src;
1502 tcp_key->tcp_dst = output->tp.dst;
1503 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1504 output->tp.flags))
1505 goto nla_put_failure;
e6445719
PS
1506 } else if (swkey->ip.proto == IPPROTO_UDP) {
1507 struct ovs_key_udp *udp_key;
1508
1509 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1510 if (!nla)
1511 goto nla_put_failure;
1512 udp_key = nla_data(nla);
1139e241
JR
1513 udp_key->udp_src = output->tp.src;
1514 udp_key->udp_dst = output->tp.dst;
e6445719
PS
1515 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1516 struct ovs_key_sctp *sctp_key;
1517
1518 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1519 if (!nla)
1520 goto nla_put_failure;
1521 sctp_key = nla_data(nla);
1139e241
JR
1522 sctp_key->sctp_src = output->tp.src;
1523 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
1524 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1525 swkey->ip.proto == IPPROTO_ICMP) {
1526 struct ovs_key_icmp *icmp_key;
1527
1528 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1529 if (!nla)
1530 goto nla_put_failure;
1531 icmp_key = nla_data(nla);
1139e241
JR
1532 icmp_key->icmp_type = ntohs(output->tp.src);
1533 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
1534 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1535 swkey->ip.proto == IPPROTO_ICMPV6) {
1536 struct ovs_key_icmpv6 *icmpv6_key;
1537
1538 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1539 sizeof(*icmpv6_key));
1540 if (!nla)
1541 goto nla_put_failure;
1542 icmpv6_key = nla_data(nla);
1139e241
JR
1543 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1544 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
1545
1546 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1547 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1548 struct ovs_key_nd *nd_key;
1549
1550 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1551 if (!nla)
1552 goto nla_put_failure;
1553 nd_key = nla_data(nla);
1554 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1555 sizeof(nd_key->nd_target));
8c63ff09
JP
1556 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1557 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
1558 }
1559 }
1560 }
1561
1562unencap:
1563 if (encap)
1564 nla_nest_end(skb, encap);
1565
1566 return 0;
1567
1568nla_put_failure:
1569 return -EMSGSIZE;
1570}
1571
5b4237bb
JS
1572int ovs_nla_put_key(const struct sw_flow_key *swkey,
1573 const struct sw_flow_key *output, int attr, bool is_mask,
1574 struct sk_buff *skb)
1575{
1576 int err;
1577 struct nlattr *nla;
1578
1579 nla = nla_nest_start(skb, attr);
1580 if (!nla)
1581 return -EMSGSIZE;
1582 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1583 if (err)
1584 return err;
1585 nla_nest_end(skb, nla);
1586
1587 return 0;
1588}
1589
1590/* Called with ovs_mutex or RCU read lock. */
74ed7ab9
JS
1591int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1592{
1593 if (ovs_identifier_is_ufid(&flow->id))
1594 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1595 flow->id.ufid);
1596
1597 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1598 OVS_FLOW_ATTR_KEY, false, skb);
1599}
1600
1601/* Called with ovs_mutex or RCU read lock. */
1602int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
5b4237bb 1603{
26ad0b83 1604 return ovs_nla_put_key(&flow->key, &flow->key,
5b4237bb
JS
1605 OVS_FLOW_ATTR_KEY, false, skb);
1606}
1607
1608/* Called with ovs_mutex or RCU read lock. */
1609int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1610{
1611 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1612 OVS_FLOW_ATTR_MASK, true, skb);
1613}
1614
e6445719
PS
1615#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1616
05da5898 1617static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
e6445719
PS
1618{
1619 struct sw_flow_actions *sfa;
1620
426cda5c 1621 if (size > MAX_ACTIONS_BUFSIZE) {
05da5898 1622 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
e6445719 1623 return ERR_PTR(-EINVAL);
426cda5c 1624 }
e6445719
PS
1625
1626 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1627 if (!sfa)
1628 return ERR_PTR(-ENOMEM);
1629
1630 sfa->actions_len = 0;
1631 return sfa;
1632}
1633
34ae932a
TG
1634static void ovs_nla_free_set_action(const struct nlattr *a)
1635{
1636 const struct nlattr *ovs_key = nla_data(a);
1637 struct ovs_tunnel_info *ovs_tun;
1638
1639 switch (nla_type(ovs_key)) {
1640 case OVS_KEY_ATTR_TUNNEL_INFO:
1641 ovs_tun = nla_data(ovs_key);
1642 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1643 break;
1644 }
1645}
1646
1647void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1648{
1649 const struct nlattr *a;
1650 int rem;
1651
1652 if (!sf_acts)
1653 return;
1654
1655 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1656 switch (nla_type(a)) {
1657 case OVS_ACTION_ATTR_SET:
1658 ovs_nla_free_set_action(a);
1659 break;
7f8a436e
JS
1660 case OVS_ACTION_ATTR_CT:
1661 ovs_ct_free_action(a);
1662 break;
34ae932a
TG
1663 }
1664 }
1665
1666 kfree(sf_acts);
1667}
1668
1669static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1670{
1671 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1672}
1673
e6445719
PS
1674/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1675 * The caller must hold rcu_read_lock for this to be sensible. */
34ae932a 1676void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
e6445719 1677{
34ae932a 1678 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
e6445719
PS
1679}
1680
1681static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
05da5898 1682 int attr_len, bool log)
e6445719
PS
1683{
1684
1685 struct sw_flow_actions *acts;
1686 int new_acts_size;
1687 int req_size = NLA_ALIGN(attr_len);
1688 int next_offset = offsetof(struct sw_flow_actions, actions) +
1689 (*sfa)->actions_len;
1690
1691 if (req_size <= (ksize(*sfa) - next_offset))
1692 goto out;
1693
1694 new_acts_size = ksize(*sfa) * 2;
1695
1696 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1697 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1698 return ERR_PTR(-EMSGSIZE);
1699 new_acts_size = MAX_ACTIONS_BUFSIZE;
1700 }
1701
05da5898 1702 acts = nla_alloc_flow_actions(new_acts_size, log);
e6445719
PS
1703 if (IS_ERR(acts))
1704 return (void *)acts;
1705
1706 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1707 acts->actions_len = (*sfa)->actions_len;
8e2fed1c 1708 acts->orig_len = (*sfa)->orig_len;
e6445719
PS
1709 kfree(*sfa);
1710 *sfa = acts;
1711
1712out:
1713 (*sfa)->actions_len += req_size;
1714 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1715}
1716
f0b128c1 1717static struct nlattr *__add_action(struct sw_flow_actions **sfa,
05da5898 1718 int attrtype, void *data, int len, bool log)
e6445719
PS
1719{
1720 struct nlattr *a;
1721
05da5898 1722 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
e6445719 1723 if (IS_ERR(a))
f0b128c1 1724 return a;
e6445719
PS
1725
1726 a->nla_type = attrtype;
1727 a->nla_len = nla_attr_size(len);
1728
1729 if (data)
1730 memcpy(nla_data(a), data, len);
1731 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1732
f0b128c1
JG
1733 return a;
1734}
1735
7f8a436e
JS
1736int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1737 int len, bool log)
f0b128c1
JG
1738{
1739 struct nlattr *a;
1740
05da5898 1741 a = __add_action(sfa, attrtype, data, len, log);
f0b128c1 1742
f35423c1 1743 return PTR_ERR_OR_ZERO(a);
e6445719
PS
1744}
1745
1746static inline int add_nested_action_start(struct sw_flow_actions **sfa,
05da5898 1747 int attrtype, bool log)
e6445719
PS
1748{
1749 int used = (*sfa)->actions_len;
1750 int err;
1751
7f8a436e 1752 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
e6445719
PS
1753 if (err)
1754 return err;
1755
1756 return used;
1757}
1758
1759static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1760 int st_offset)
1761{
1762 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1763 st_offset);
1764
1765 a->nla_len = sfa->actions_len - st_offset;
1766}
1767
7f8a436e 1768static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0
SH
1769 const struct sw_flow_key *key,
1770 int depth, struct sw_flow_actions **sfa,
05da5898 1771 __be16 eth_type, __be16 vlan_tci, bool log);
25cd9ba0 1772
7f8a436e 1773static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
e6445719 1774 const struct sw_flow_key *key, int depth,
25cd9ba0 1775 struct sw_flow_actions **sfa,
05da5898 1776 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719
PS
1777{
1778 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1779 const struct nlattr *probability, *actions;
1780 const struct nlattr *a;
1781 int rem, start, err, st_acts;
1782
1783 memset(attrs, 0, sizeof(attrs));
1784 nla_for_each_nested(a, attr, rem) {
1785 int type = nla_type(a);
1786 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1787 return -EINVAL;
1788 attrs[type] = a;
1789 }
1790 if (rem)
1791 return -EINVAL;
1792
1793 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1794 if (!probability || nla_len(probability) != sizeof(u32))
1795 return -EINVAL;
1796
1797 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1798 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1799 return -EINVAL;
1800
1801 /* validation done, copy sample action. */
05da5898 1802 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
e6445719
PS
1803 if (start < 0)
1804 return start;
7f8a436e
JS
1805 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1806 nla_data(probability), sizeof(u32), log);
e6445719
PS
1807 if (err)
1808 return err;
05da5898 1809 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
e6445719
PS
1810 if (st_acts < 0)
1811 return st_acts;
1812
7f8a436e 1813 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
05da5898 1814 eth_type, vlan_tci, log);
e6445719
PS
1815 if (err)
1816 return err;
1817
1818 add_nested_action_end(*sfa, st_acts);
1819 add_nested_action_end(*sfa, start);
1820
1821 return 0;
1822}
1823
e6445719
PS
1824void ovs_match_init(struct sw_flow_match *match,
1825 struct sw_flow_key *key,
1826 struct sw_flow_mask *mask)
1827{
1828 memset(match, 0, sizeof(*match));
1829 match->key = key;
1830 match->mask = mask;
1831
1832 memset(key, 0, sizeof(*key));
1833
1834 if (mask) {
1835 memset(&mask->key, 0, sizeof(mask->key));
1836 mask->range.start = mask->range.end = 0;
1837 }
1838}
1839
d91641d9
TG
1840static int validate_geneve_opts(struct sw_flow_key *key)
1841{
1842 struct geneve_opt *option;
1843 int opts_len = key->tun_opts_len;
1844 bool crit_opt = false;
1845
1846 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1847 while (opts_len > 0) {
1848 int len;
1849
1850 if (opts_len < sizeof(*option))
1851 return -EINVAL;
1852
1853 len = sizeof(*option) + option->length * 4;
1854 if (len > opts_len)
1855 return -EINVAL;
1856
1857 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1858
1859 option = (struct geneve_opt *)((u8 *)option + len);
1860 opts_len -= len;
1861 };
1862
1863 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1864
1865 return 0;
1866}
1867
e6445719 1868static int validate_and_copy_set_tun(const struct nlattr *attr,
05da5898 1869 struct sw_flow_actions **sfa, bool log)
e6445719
PS
1870{
1871 struct sw_flow_match match;
1872 struct sw_flow_key key;
34ae932a 1873 struct metadata_dst *tun_dst;
1d8fff90 1874 struct ip_tunnel_info *tun_info;
34ae932a 1875 struct ovs_tunnel_info *ovs_tun;
f0b128c1 1876 struct nlattr *a;
13101602 1877 int err = 0, start, opts_type;
e6445719
PS
1878
1879 ovs_match_init(&match, &key, NULL);
1dd144cf
TG
1880 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1881 if (opts_type < 0)
1882 return opts_type;
e6445719 1883
f5796684 1884 if (key.tun_opts_len) {
1dd144cf
TG
1885 switch (opts_type) {
1886 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1887 err = validate_geneve_opts(&key);
1888 if (err < 0)
1889 return err;
1890 break;
1891 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1892 break;
1893 }
f5796684
JG
1894 };
1895
05da5898 1896 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
e6445719
PS
1897 if (start < 0)
1898 return start;
1899
34ae932a
TG
1900 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1901 if (!tun_dst)
1902 return -ENOMEM;
1903
f0b128c1 1904 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
34ae932a
TG
1905 sizeof(*ovs_tun), log);
1906 if (IS_ERR(a)) {
1907 dst_release((struct dst_entry *)tun_dst);
f0b128c1 1908 return PTR_ERR(a);
34ae932a
TG
1909 }
1910
1911 ovs_tun = nla_data(a);
1912 ovs_tun->tun_dst = tun_dst;
f0b128c1 1913
34ae932a
TG
1914 tun_info = &tun_dst->u.tun_info;
1915 tun_info->mode = IP_TUNNEL_INFO_TX;
1d8fff90 1916 tun_info->key = key.tun_key;
f0b128c1 1917
4c222798
PS
1918 /* We need to store the options in the action itself since
1919 * everything else will go away after flow setup. We can append
1920 * it to tun_info and then point there.
1921 */
1922 ip_tunnel_info_opts_set(tun_info,
1923 TUN_METADATA_OPTS(&key, key.tun_opts_len),
1924 key.tun_opts_len);
e6445719
PS
1925 add_nested_action_end(*sfa, start);
1926
1927 return err;
1928}
1929
83d2b9ba
JR
1930/* Return false if there are any non-masked bits set.
1931 * Mask follows data immediately, before any netlink padding.
1932 */
1933static bool validate_masked(u8 *data, int len)
1934{
1935 u8 *mask = data + len;
1936
1937 while (len--)
1938 if (*data++ & ~*mask++)
1939 return false;
1940
1941 return true;
1942}
1943
e6445719
PS
1944static int validate_set(const struct nlattr *a,
1945 const struct sw_flow_key *flow_key,
1946 struct sw_flow_actions **sfa,
83d2b9ba 1947 bool *skip_copy, __be16 eth_type, bool masked, bool log)
e6445719
PS
1948{
1949 const struct nlattr *ovs_key = nla_data(a);
1950 int key_type = nla_type(ovs_key);
83d2b9ba 1951 size_t key_len;
e6445719
PS
1952
1953 /* There can be only one key in a action */
1954 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1955 return -EINVAL;
1956
83d2b9ba
JR
1957 key_len = nla_len(ovs_key);
1958 if (masked)
1959 key_len /= 2;
1960
e6445719 1961 if (key_type > OVS_KEY_ATTR_MAX ||
982b5270 1962 !check_attr_len(key_len, ovs_key_lens[key_type].len))
e6445719
PS
1963 return -EINVAL;
1964
83d2b9ba
JR
1965 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1966 return -EINVAL;
1967
e6445719
PS
1968 switch (key_type) {
1969 const struct ovs_key_ipv4 *ipv4_key;
1970 const struct ovs_key_ipv6 *ipv6_key;
1971 int err;
1972
1973 case OVS_KEY_ATTR_PRIORITY:
1974 case OVS_KEY_ATTR_SKB_MARK:
182e3042 1975 case OVS_KEY_ATTR_CT_MARK:
c2ac6673 1976 case OVS_KEY_ATTR_CT_LABEL:
e6445719
PS
1977 case OVS_KEY_ATTR_ETHERNET:
1978 break;
1979
1980 case OVS_KEY_ATTR_TUNNEL:
25cd9ba0
SH
1981 if (eth_p_mpls(eth_type))
1982 return -EINVAL;
1983
83d2b9ba
JR
1984 if (masked)
1985 return -EINVAL; /* Masked tunnel set not supported. */
1986
1987 *skip_copy = true;
05da5898 1988 err = validate_and_copy_set_tun(a, sfa, log);
e6445719
PS
1989 if (err)
1990 return err;
1991 break;
1992
1993 case OVS_KEY_ATTR_IPV4:
25cd9ba0 1994 if (eth_type != htons(ETH_P_IP))
e6445719
PS
1995 return -EINVAL;
1996
e6445719 1997 ipv4_key = nla_data(ovs_key);
e6445719 1998
83d2b9ba
JR
1999 if (masked) {
2000 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
e6445719 2001
83d2b9ba
JR
2002 /* Non-writeable fields. */
2003 if (mask->ipv4_proto || mask->ipv4_frag)
2004 return -EINVAL;
2005 } else {
2006 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2007 return -EINVAL;
2008
2009 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2010 return -EINVAL;
2011 }
e6445719
PS
2012 break;
2013
2014 case OVS_KEY_ATTR_IPV6:
25cd9ba0 2015 if (eth_type != htons(ETH_P_IPV6))
e6445719
PS
2016 return -EINVAL;
2017
e6445719 2018 ipv6_key = nla_data(ovs_key);
e6445719 2019
83d2b9ba
JR
2020 if (masked) {
2021 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2022
2023 /* Non-writeable fields. */
2024 if (mask->ipv6_proto || mask->ipv6_frag)
2025 return -EINVAL;
2026
2027 /* Invalid bits in the flow label mask? */
2028 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2029 return -EINVAL;
2030 } else {
2031 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2032 return -EINVAL;
e6445719 2033
83d2b9ba
JR
2034 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2035 return -EINVAL;
2036 }
e6445719
PS
2037 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2038 return -EINVAL;
2039
2040 break;
2041
2042 case OVS_KEY_ATTR_TCP:
83d2b9ba
JR
2043 if ((eth_type != htons(ETH_P_IP) &&
2044 eth_type != htons(ETH_P_IPV6)) ||
2045 flow_key->ip.proto != IPPROTO_TCP)
e6445719
PS
2046 return -EINVAL;
2047
83d2b9ba 2048 break;
e6445719
PS
2049
2050 case OVS_KEY_ATTR_UDP:
83d2b9ba
JR
2051 if ((eth_type != htons(ETH_P_IP) &&
2052 eth_type != htons(ETH_P_IPV6)) ||
2053 flow_key->ip.proto != IPPROTO_UDP)
e6445719
PS
2054 return -EINVAL;
2055
83d2b9ba 2056 break;
25cd9ba0
SH
2057
2058 case OVS_KEY_ATTR_MPLS:
2059 if (!eth_p_mpls(eth_type))
2060 return -EINVAL;
2061 break;
e6445719
PS
2062
2063 case OVS_KEY_ATTR_SCTP:
83d2b9ba
JR
2064 if ((eth_type != htons(ETH_P_IP) &&
2065 eth_type != htons(ETH_P_IPV6)) ||
2066 flow_key->ip.proto != IPPROTO_SCTP)
e6445719
PS
2067 return -EINVAL;
2068
83d2b9ba 2069 break;
e6445719
PS
2070
2071 default:
2072 return -EINVAL;
2073 }
2074
83d2b9ba
JR
2075 /* Convert non-masked non-tunnel set actions to masked set actions. */
2076 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2077 int start, len = key_len * 2;
2078 struct nlattr *at;
2079
2080 *skip_copy = true;
2081
2082 start = add_nested_action_start(sfa,
2083 OVS_ACTION_ATTR_SET_TO_MASKED,
2084 log);
2085 if (start < 0)
2086 return start;
2087
2088 at = __add_action(sfa, key_type, NULL, len, log);
2089 if (IS_ERR(at))
2090 return PTR_ERR(at);
2091
2092 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2093 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2094 /* Clear non-writeable bits from otherwise writeable fields. */
2095 if (key_type == OVS_KEY_ATTR_IPV6) {
2096 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2097
2098 mask->ipv6_label &= htonl(0x000FFFFF);
2099 }
2100 add_nested_action_end(*sfa, start);
2101 }
2102
e6445719
PS
2103 return 0;
2104}
2105
2106static int validate_userspace(const struct nlattr *attr)
2107{
2108 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2109 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2110 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
8f0aad6f 2111 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
e6445719
PS
2112 };
2113 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2114 int error;
2115
2116 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2117 attr, userspace_policy);
2118 if (error)
2119 return error;
2120
2121 if (!a[OVS_USERSPACE_ATTR_PID] ||
2122 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2123 return -EINVAL;
2124
2125 return 0;
2126}
2127
2128static int copy_action(const struct nlattr *from,
05da5898 2129 struct sw_flow_actions **sfa, bool log)
e6445719
PS
2130{
2131 int totlen = NLA_ALIGN(from->nla_len);
2132 struct nlattr *to;
2133
05da5898 2134 to = reserve_sfa_size(sfa, from->nla_len, log);
e6445719
PS
2135 if (IS_ERR(to))
2136 return PTR_ERR(to);
2137
2138 memcpy(to, from, totlen);
2139 return 0;
2140}
2141
7f8a436e 2142static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0
SH
2143 const struct sw_flow_key *key,
2144 int depth, struct sw_flow_actions **sfa,
05da5898 2145 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719
PS
2146{
2147 const struct nlattr *a;
2148 int rem, err;
2149
2150 if (depth >= SAMPLE_ACTION_DEPTH)
2151 return -EOVERFLOW;
2152
2153 nla_for_each_nested(a, attr, rem) {
2154 /* Expected argument lengths, (u32)-1 for variable length. */
2155 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2156 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 2157 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719 2158 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
25cd9ba0
SH
2159 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2160 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
e6445719
PS
2161 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2162 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2163 [OVS_ACTION_ATTR_SET] = (u32)-1,
83d2b9ba 2164 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
971427f3 2165 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
7f8a436e
JS
2166 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2167 [OVS_ACTION_ATTR_CT] = (u32)-1,
e6445719
PS
2168 };
2169 const struct ovs_action_push_vlan *vlan;
2170 int type = nla_type(a);
2171 bool skip_copy;
2172
2173 if (type > OVS_ACTION_ATTR_MAX ||
2174 (action_lens[type] != nla_len(a) &&
2175 action_lens[type] != (u32)-1))
2176 return -EINVAL;
2177
2178 skip_copy = false;
2179 switch (type) {
2180 case OVS_ACTION_ATTR_UNSPEC:
2181 return -EINVAL;
2182
2183 case OVS_ACTION_ATTR_USERSPACE:
2184 err = validate_userspace(a);
2185 if (err)
2186 return err;
2187 break;
2188
2189 case OVS_ACTION_ATTR_OUTPUT:
2190 if (nla_get_u32(a) >= DP_MAX_PORTS)
2191 return -EINVAL;
2192 break;
2193
971427f3
AZ
2194 case OVS_ACTION_ATTR_HASH: {
2195 const struct ovs_action_hash *act_hash = nla_data(a);
2196
2197 switch (act_hash->hash_alg) {
2198 case OVS_HASH_ALG_L4:
2199 break;
2200 default:
2201 return -EINVAL;
2202 }
2203
2204 break;
2205 }
e6445719
PS
2206
2207 case OVS_ACTION_ATTR_POP_VLAN:
25cd9ba0 2208 vlan_tci = htons(0);
e6445719
PS
2209 break;
2210
2211 case OVS_ACTION_ATTR_PUSH_VLAN:
2212 vlan = nla_data(a);
2213 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2214 return -EINVAL;
2215 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2216 return -EINVAL;
25cd9ba0 2217 vlan_tci = vlan->vlan_tci;
e6445719
PS
2218 break;
2219
971427f3
AZ
2220 case OVS_ACTION_ATTR_RECIRC:
2221 break;
2222
25cd9ba0
SH
2223 case OVS_ACTION_ATTR_PUSH_MPLS: {
2224 const struct ovs_action_push_mpls *mpls = nla_data(a);
2225
25cd9ba0
SH
2226 if (!eth_p_mpls(mpls->mpls_ethertype))
2227 return -EINVAL;
2228 /* Prohibit push MPLS other than to a white list
2229 * for packets that have a known tag order.
2230 */
2231 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2232 (eth_type != htons(ETH_P_IP) &&
2233 eth_type != htons(ETH_P_IPV6) &&
2234 eth_type != htons(ETH_P_ARP) &&
2235 eth_type != htons(ETH_P_RARP) &&
2236 !eth_p_mpls(eth_type)))
2237 return -EINVAL;
2238 eth_type = mpls->mpls_ethertype;
2239 break;
2240 }
2241
2242 case OVS_ACTION_ATTR_POP_MPLS:
2243 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2244 !eth_p_mpls(eth_type))
2245 return -EINVAL;
2246
2247 /* Disallow subsequent L2.5+ set and mpls_pop actions
2248 * as there is no check here to ensure that the new
2249 * eth_type is valid and thus set actions could
2250 * write off the end of the packet or otherwise
2251 * corrupt it.
2252 *
2253 * Support for these actions is planned using packet
2254 * recirculation.
2255 */
2256 eth_type = htons(0);
2257 break;
2258
e6445719 2259 case OVS_ACTION_ATTR_SET:
25cd9ba0 2260 err = validate_set(a, key, sfa,
83d2b9ba
JR
2261 &skip_copy, eth_type, false, log);
2262 if (err)
2263 return err;
2264 break;
2265
2266 case OVS_ACTION_ATTR_SET_MASKED:
2267 err = validate_set(a, key, sfa,
2268 &skip_copy, eth_type, true, log);
e6445719
PS
2269 if (err)
2270 return err;
2271 break;
2272
2273 case OVS_ACTION_ATTR_SAMPLE:
7f8a436e 2274 err = validate_and_copy_sample(net, a, key, depth, sfa,
05da5898 2275 eth_type, vlan_tci, log);
e6445719
PS
2276 if (err)
2277 return err;
2278 skip_copy = true;
2279 break;
2280
7f8a436e
JS
2281 case OVS_ACTION_ATTR_CT:
2282 err = ovs_ct_copy_action(net, a, key, sfa, log);
2283 if (err)
2284 return err;
2285 skip_copy = true;
2286 break;
2287
e6445719 2288 default:
05da5898 2289 OVS_NLERR(log, "Unknown Action type %d", type);
e6445719
PS
2290 return -EINVAL;
2291 }
2292 if (!skip_copy) {
05da5898 2293 err = copy_action(a, sfa, log);
e6445719
PS
2294 if (err)
2295 return err;
2296 }
2297 }
2298
2299 if (rem > 0)
2300 return -EINVAL;
2301
2302 return 0;
2303}
2304
83d2b9ba 2305/* 'key' must be the masked key. */
7f8a436e 2306int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0 2307 const struct sw_flow_key *key,
05da5898 2308 struct sw_flow_actions **sfa, bool log)
25cd9ba0 2309{
2fdb957d
PS
2310 int err;
2311
05da5898 2312 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2fdb957d
PS
2313 if (IS_ERR(*sfa))
2314 return PTR_ERR(*sfa);
2315
8e2fed1c 2316 (*sfa)->orig_len = nla_len(attr);
7f8a436e 2317 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
05da5898 2318 key->eth.tci, log);
2fdb957d 2319 if (err)
34ae932a 2320 ovs_nla_free_flow_actions(*sfa);
2fdb957d
PS
2321
2322 return err;
25cd9ba0
SH
2323}
2324
e6445719
PS
2325static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2326{
2327 const struct nlattr *a;
2328 struct nlattr *start;
2329 int err = 0, rem;
2330
2331 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2332 if (!start)
2333 return -EMSGSIZE;
2334
2335 nla_for_each_nested(a, attr, rem) {
2336 int type = nla_type(a);
2337 struct nlattr *st_sample;
2338
2339 switch (type) {
2340 case OVS_SAMPLE_ATTR_PROBABILITY:
2341 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2342 sizeof(u32), nla_data(a)))
2343 return -EMSGSIZE;
2344 break;
2345 case OVS_SAMPLE_ATTR_ACTIONS:
2346 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2347 if (!st_sample)
2348 return -EMSGSIZE;
2349 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2350 if (err)
2351 return err;
2352 nla_nest_end(skb, st_sample);
2353 break;
2354 }
2355 }
2356
2357 nla_nest_end(skb, start);
2358 return err;
2359}
2360
2361static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2362{
2363 const struct nlattr *ovs_key = nla_data(a);
2364 int key_type = nla_type(ovs_key);
2365 struct nlattr *start;
2366 int err;
2367
2368 switch (key_type) {
f0b128c1 2369 case OVS_KEY_ATTR_TUNNEL_INFO: {
34ae932a
TG
2370 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2371 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
f0b128c1 2372
e6445719
PS
2373 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2374 if (!start)
2375 return -EMSGSIZE;
2376
1d8fff90 2377 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
f5796684 2378 tun_info->options_len ?
4c222798 2379 ip_tunnel_info_opts(tun_info) : NULL,
f5796684 2380 tun_info->options_len);
e6445719
PS
2381 if (err)
2382 return err;
2383 nla_nest_end(skb, start);
2384 break;
f0b128c1 2385 }
e6445719
PS
2386 default:
2387 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2388 return -EMSGSIZE;
2389 break;
2390 }
2391
2392 return 0;
2393}
2394
83d2b9ba
JR
2395static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2396 struct sk_buff *skb)
2397{
2398 const struct nlattr *ovs_key = nla_data(a);
f4f8e738 2399 struct nlattr *nla;
83d2b9ba
JR
2400 size_t key_len = nla_len(ovs_key) / 2;
2401
2402 /* Revert the conversion we did from a non-masked set action to
2403 * masked set action.
2404 */
f4f8e738
JS
2405 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2406 if (!nla)
83d2b9ba
JR
2407 return -EMSGSIZE;
2408
f4f8e738
JS
2409 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2410 return -EMSGSIZE;
2411
2412 nla_nest_end(skb, nla);
83d2b9ba
JR
2413 return 0;
2414}
2415
e6445719
PS
2416int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2417{
2418 const struct nlattr *a;
2419 int rem, err;
2420
2421 nla_for_each_attr(a, attr, len, rem) {
2422 int type = nla_type(a);
2423
2424 switch (type) {
2425 case OVS_ACTION_ATTR_SET:
2426 err = set_action_to_attr(a, skb);
2427 if (err)
2428 return err;
2429 break;
2430
83d2b9ba
JR
2431 case OVS_ACTION_ATTR_SET_TO_MASKED:
2432 err = masked_set_action_to_set_action_attr(a, skb);
2433 if (err)
2434 return err;
2435 break;
2436
e6445719
PS
2437 case OVS_ACTION_ATTR_SAMPLE:
2438 err = sample_action_to_attr(a, skb);
2439 if (err)
2440 return err;
2441 break;
7f8a436e
JS
2442
2443 case OVS_ACTION_ATTR_CT:
2444 err = ovs_ct_action_to_attr(nla_data(a), skb);
2445 if (err)
2446 return err;
2447 break;
2448
e6445719
PS
2449 default:
2450 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2451 return -EMSGSIZE;
2452 break;
2453 }
2454 }
2455
2456 return 0;
2457}
This page took 0.319869 seconds and 5 git commands to generate.