openvswitch: Refactor action alloc and copy api.
[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>
e6445719
PS
50
51#include "flow_netlink.h"
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{
56 struct sw_flow_key_range *range = NULL;
57 size_t start = rounddown(offset, sizeof(long));
58 size_t end = roundup(offset + size, sizeof(long));
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76
77 if (range->end < end)
78 range->end = end;
79}
80
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask) { \
86 if ((match)->mask) \
87 (match)->mask->key.field = value; \
88 } else { \
89 (match)->key->field = value; \
90 } \
91 } while (0)
92
f5796684
JG
93#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
94 do { \
95 update_range__(match, offset, len, is_mask); \
96 if (is_mask) \
97 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
98 len); \
99 else \
100 memcpy((u8 *)(match)->key + offset, value_p, len); \
e6445719
PS
101 } while (0)
102
f5796684
JG
103#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
104 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
105 value_p, len, is_mask)
106
f47de068
PS
107#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
108 do { \
109 update_range__(match, offsetof(struct sw_flow_key, field), \
110 sizeof((match)->key->field), is_mask); \
111 if (is_mask) { \
112 if ((match)->mask) \
113 memset((u8 *)&(match)->mask->key.field, value,\
114 sizeof((match)->mask->key.field)); \
115 } else { \
116 memset((u8 *)&(match)->key->field, value, \
117 sizeof((match)->key->field)); \
118 } \
119 } while (0)
e6445719
PS
120
121static bool match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs)
123{
124 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
126
127 /* The following mask attributes allowed only if they
128 * pass the validation tests. */
129 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
130 | (1 << OVS_KEY_ATTR_IPV6)
131 | (1 << OVS_KEY_ATTR_TCP)
5eb26b15 132 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
e6445719
PS
133 | (1 << OVS_KEY_ATTR_UDP)
134 | (1 << OVS_KEY_ATTR_SCTP)
135 | (1 << OVS_KEY_ATTR_ICMP)
136 | (1 << OVS_KEY_ATTR_ICMPV6)
137 | (1 << OVS_KEY_ATTR_ARP)
25cd9ba0
SH
138 | (1 << OVS_KEY_ATTR_ND)
139 | (1 << OVS_KEY_ATTR_MPLS));
e6445719
PS
140
141 /* Always allowed mask fields. */
142 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
143 | (1 << OVS_KEY_ATTR_IN_PORT)
144 | (1 << OVS_KEY_ATTR_ETHERTYPE));
145
146 /* Check key attributes. */
147 if (match->key->eth.type == htons(ETH_P_ARP)
148 || match->key->eth.type == htons(ETH_P_RARP)) {
149 key_expected |= 1 << OVS_KEY_ATTR_ARP;
150 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
152 }
153
25cd9ba0
SH
154 if (eth_p_mpls(match->key->eth.type)) {
155 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
156 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
157 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
158 }
159
e6445719
PS
160 if (match->key->eth.type == htons(ETH_P_IP)) {
161 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
162 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
163 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
164
165 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
166 if (match->key->ip.proto == IPPROTO_UDP) {
167 key_expected |= 1 << OVS_KEY_ATTR_UDP;
168 if (match->mask && (match->mask->key.ip.proto == 0xff))
169 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
170 }
171
172 if (match->key->ip.proto == IPPROTO_SCTP) {
173 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
174 if (match->mask && (match->mask->key.ip.proto == 0xff))
175 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
176 }
177
178 if (match->key->ip.proto == IPPROTO_TCP) {
179 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
180 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
181 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 182 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
183 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
184 }
e6445719
PS
185 }
186
187 if (match->key->ip.proto == IPPROTO_ICMP) {
188 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
191 }
192 }
193 }
194
195 if (match->key->eth.type == htons(ETH_P_IPV6)) {
196 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
197 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
198 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
199
200 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
201 if (match->key->ip.proto == IPPROTO_UDP) {
202 key_expected |= 1 << OVS_KEY_ATTR_UDP;
203 if (match->mask && (match->mask->key.ip.proto == 0xff))
204 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
205 }
206
207 if (match->key->ip.proto == IPPROTO_SCTP) {
208 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_TCP) {
214 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
215 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
216 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 217 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
218 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
219 }
e6445719
PS
220 }
221
222 if (match->key->ip.proto == IPPROTO_ICMPV6) {
223 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
224 if (match->mask && (match->mask->key.ip.proto == 0xff))
225 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
226
1139e241 227 if (match->key->tp.src ==
e6445719 228 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1139e241 229 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e6445719 230 key_expected |= 1 << OVS_KEY_ATTR_ND;
1139e241 231 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
e6445719
PS
232 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
233 }
234 }
235 }
236 }
237
238 if ((key_attrs & key_expected) != key_expected) {
239 /* Key attributes check failed. */
240 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
cc23ebf3 241 (unsigned long long)key_attrs, (unsigned long long)key_expected);
e6445719
PS
242 return false;
243 }
244
245 if ((mask_attrs & mask_allowed) != mask_attrs) {
246 /* Mask attributes check failed. */
247 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
cc23ebf3 248 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
e6445719
PS
249 return false;
250 }
251
252 return true;
253}
254
41af73e9
JS
255size_t ovs_key_attr_size(void)
256{
257 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
258 * updating this function.
259 */
260 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
261
262 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
263 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
264 + 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 */
273 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
274 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
275 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
276 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
277 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
278 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
279 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
280 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
281 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
282 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
283 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
284 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
285}
286
e6445719
PS
287/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
288static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
289 [OVS_KEY_ATTR_ENCAP] = -1,
290 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
291 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
292 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
293 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
294 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
295 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
296 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
297 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
298 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
5eb26b15 299 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
e6445719
PS
300 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
301 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
302 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
303 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
304 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
305 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
971427f3
AZ
306 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
307 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
e6445719 308 [OVS_KEY_ATTR_TUNNEL] = -1,
25cd9ba0 309 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
e6445719
PS
310};
311
312static bool is_all_zero(const u8 *fp, size_t size)
313{
314 int i;
315
316 if (!fp)
317 return false;
318
319 for (i = 0; i < size; i++)
320 if (fp[i])
321 return false;
322
323 return true;
324}
325
326static int __parse_flow_nlattrs(const struct nlattr *attr,
327 const struct nlattr *a[],
328 u64 *attrsp, bool nz)
329{
330 const struct nlattr *nla;
331 u64 attrs;
332 int rem;
333
334 attrs = *attrsp;
335 nla_for_each_nested(nla, attr, rem) {
336 u16 type = nla_type(nla);
337 int expected_len;
338
339 if (type > OVS_KEY_ATTR_MAX) {
340 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
341 type, OVS_KEY_ATTR_MAX);
342 return -EINVAL;
343 }
344
345 if (attrs & (1 << type)) {
346 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
347 return -EINVAL;
348 }
349
350 expected_len = ovs_key_lens[type];
351 if (nla_len(nla) != expected_len && expected_len != -1) {
352 OVS_NLERR("Key attribute has unexpected length (type=%d"
353 ", length=%d, expected=%d).\n", type,
354 nla_len(nla), expected_len);
355 return -EINVAL;
356 }
357
358 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
359 attrs |= 1 << type;
360 a[type] = nla;
361 }
362 }
363 if (rem) {
364 OVS_NLERR("Message has %d unknown bytes.\n", rem);
365 return -EINVAL;
366 }
367
368 *attrsp = attrs;
369 return 0;
370}
371
372static int parse_flow_mask_nlattrs(const struct nlattr *attr,
373 const struct nlattr *a[], u64 *attrsp)
374{
375 return __parse_flow_nlattrs(attr, a, attrsp, true);
376}
377
378static int parse_flow_nlattrs(const struct nlattr *attr,
379 const struct nlattr *a[], u64 *attrsp)
380{
381 return __parse_flow_nlattrs(attr, a, attrsp, false);
382}
383
384static int ipv4_tun_from_nlattr(const struct nlattr *attr,
385 struct sw_flow_match *match, bool is_mask)
386{
387 struct nlattr *a;
388 int rem;
389 bool ttl = false;
390 __be16 tun_flags = 0;
f5796684 391 unsigned long opt_key_offset;
e6445719
PS
392
393 nla_for_each_nested(a, attr, rem) {
394 int type = nla_type(a);
395 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
396 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
397 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
398 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
399 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
400 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
401 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
402 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
67fa0341 403 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
f5796684 404 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
e6445719
PS
405 };
406
407 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
408 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
409 type, OVS_TUNNEL_KEY_ATTR_MAX);
410 return -EINVAL;
411 }
412
f5796684
JG
413 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
414 ovs_tunnel_key_lens[type] != -1) {
e6445719
PS
415 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
416 " length (type=%d, length=%d, expected=%d).\n",
417 type, nla_len(a), ovs_tunnel_key_lens[type]);
418 return -EINVAL;
419 }
420
421 switch (type) {
422 case OVS_TUNNEL_KEY_ATTR_ID:
423 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
424 nla_get_be64(a), is_mask);
425 tun_flags |= TUNNEL_KEY;
426 break;
427 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
428 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
429 nla_get_be32(a), is_mask);
430 break;
431 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
432 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
433 nla_get_be32(a), is_mask);
434 break;
435 case OVS_TUNNEL_KEY_ATTR_TOS:
436 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
437 nla_get_u8(a), is_mask);
438 break;
439 case OVS_TUNNEL_KEY_ATTR_TTL:
440 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
441 nla_get_u8(a), is_mask);
442 ttl = true;
443 break;
444 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
445 tun_flags |= TUNNEL_DONT_FRAGMENT;
446 break;
447 case OVS_TUNNEL_KEY_ATTR_CSUM:
448 tun_flags |= TUNNEL_CSUM;
449 break;
67fa0341
JG
450 case OVS_TUNNEL_KEY_ATTR_OAM:
451 tun_flags |= TUNNEL_OAM;
452 break;
f5796684
JG
453 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
454 tun_flags |= TUNNEL_OPTIONS_PRESENT;
455 if (nla_len(a) > sizeof(match->key->tun_opts)) {
456 OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
457 nla_len(a),
458 sizeof(match->key->tun_opts));
459 return -EINVAL;
460 }
461
462 if (nla_len(a) % 4 != 0) {
463 OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
464 nla_len(a));
465 return -EINVAL;
466 }
467
468 /* We need to record the length of the options passed
469 * down, otherwise packets with the same format but
470 * additional options will be silently matched.
471 */
472 if (!is_mask) {
473 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
474 false);
475 } else {
476 /* This is somewhat unusual because it looks at
477 * both the key and mask while parsing the
478 * attributes (and by extension assumes the key
479 * is parsed first). Normally, we would verify
480 * that each is the correct length and that the
481 * attributes line up in the validate function.
482 * However, that is difficult because this is
483 * variable length and we won't have the
484 * information later.
485 */
486 if (match->key->tun_opts_len != nla_len(a)) {
487 OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
488 match->key->tun_opts_len,
489 nla_len(a));
490 return -EINVAL;
491 }
492
493 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
494 true);
495 }
496
497 opt_key_offset = (unsigned long)GENEVE_OPTS(
498 (struct sw_flow_key *)0,
499 nla_len(a));
500 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset,
501 nla_data(a), nla_len(a),
502 is_mask);
503 break;
e6445719 504 default:
f5796684
JG
505 OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
506 type);
e6445719
PS
507 return -EINVAL;
508 }
509 }
510
511 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
512
513 if (rem > 0) {
514 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
515 return -EINVAL;
516 }
517
518 if (!is_mask) {
519 if (!match->key->tun_key.ipv4_dst) {
520 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
521 return -EINVAL;
522 }
523
524 if (!ttl) {
525 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
526 return -EINVAL;
527 }
528 }
529
530 return 0;
531}
532
f5796684
JG
533static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
534 const struct ovs_key_ipv4_tunnel *output,
535 const struct geneve_opt *tun_opts,
536 int swkey_tun_opts_len)
e6445719 537{
e6445719
PS
538 if (output->tun_flags & TUNNEL_KEY &&
539 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
540 return -EMSGSIZE;
541 if (output->ipv4_src &&
67fa0341 542 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
e6445719
PS
543 return -EMSGSIZE;
544 if (output->ipv4_dst &&
67fa0341 545 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
e6445719
PS
546 return -EMSGSIZE;
547 if (output->ipv4_tos &&
67fa0341 548 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
e6445719
PS
549 return -EMSGSIZE;
550 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
551 return -EMSGSIZE;
552 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
67fa0341 553 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
e6445719
PS
554 return -EMSGSIZE;
555 if ((output->tun_flags & TUNNEL_CSUM) &&
67fa0341
JG
556 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
557 return -EMSGSIZE;
558 if ((output->tun_flags & TUNNEL_OAM) &&
559 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
e6445719 560 return -EMSGSIZE;
f5796684
JG
561 if (tun_opts &&
562 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
563 swkey_tun_opts_len, tun_opts))
564 return -EMSGSIZE;
e6445719 565
e6445719
PS
566 return 0;
567}
568
569
f5796684
JG
570static int ipv4_tun_to_nlattr(struct sk_buff *skb,
571 const struct ovs_key_ipv4_tunnel *output,
572 const struct geneve_opt *tun_opts,
573 int swkey_tun_opts_len)
574{
575 struct nlattr *nla;
576 int err;
577
578 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
579 if (!nla)
580 return -EMSGSIZE;
581
582 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
583 if (err)
584 return err;
585
586 nla_nest_end(skb, nla);
587 return 0;
588}
589
e6445719
PS
590static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
591 const struct nlattr **a, bool is_mask)
592{
971427f3
AZ
593 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
594 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
595
596 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
597 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
598 }
599
600 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
601 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
602
603 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
604 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
605 }
606
e6445719
PS
607 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
608 SW_FLOW_KEY_PUT(match, phy.priority,
609 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
610 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
611 }
612
613 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
614 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
615
426cda5c 616 if (is_mask) {
e6445719 617 in_port = 0xffffffff; /* Always exact match in_port. */
426cda5c
JG
618 } else if (in_port >= DP_MAX_PORTS) {
619 OVS_NLERR("Port (%d) exceeds maximum allowable (%d).\n",
620 in_port, DP_MAX_PORTS);
e6445719 621 return -EINVAL;
426cda5c 622 }
e6445719
PS
623
624 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
625 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
626 } else if (!is_mask) {
627 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
628 }
629
630 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
631 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
632
633 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
634 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
635 }
636 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
637 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
638 is_mask))
639 return -EINVAL;
640 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
641 }
642 return 0;
643}
644
23dabf88
JR
645static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
646 const struct nlattr **a, bool is_mask)
e6445719
PS
647{
648 int err;
e6445719
PS
649
650 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
651 if (err)
652 return err;
653
654 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
655 const struct ovs_key_ethernet *eth_key;
656
657 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
658 SW_FLOW_KEY_MEMCPY(match, eth.src,
659 eth_key->eth_src, ETH_ALEN, is_mask);
660 SW_FLOW_KEY_MEMCPY(match, eth.dst,
661 eth_key->eth_dst, ETH_ALEN, is_mask);
662 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
663 }
664
665 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
666 __be16 tci;
667
668 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
669 if (!(tci & htons(VLAN_TAG_PRESENT))) {
670 if (is_mask)
671 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
672 else
673 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
674
675 return -EINVAL;
676 }
677
678 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
679 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
680 } else if (!is_mask)
681 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
682
683 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
684 __be16 eth_type;
685
686 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
687 if (is_mask) {
688 /* Always exact match EtherType. */
689 eth_type = htons(0xffff);
690 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
691 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
692 ntohs(eth_type), ETH_P_802_3_MIN);
693 return -EINVAL;
694 }
695
696 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
697 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
698 } else if (!is_mask) {
699 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
700 }
701
702 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
703 const struct ovs_key_ipv4 *ipv4_key;
704
705 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
706 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
707 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
708 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
709 return -EINVAL;
710 }
711 SW_FLOW_KEY_PUT(match, ip.proto,
712 ipv4_key->ipv4_proto, is_mask);
713 SW_FLOW_KEY_PUT(match, ip.tos,
714 ipv4_key->ipv4_tos, is_mask);
715 SW_FLOW_KEY_PUT(match, ip.ttl,
716 ipv4_key->ipv4_ttl, is_mask);
717 SW_FLOW_KEY_PUT(match, ip.frag,
718 ipv4_key->ipv4_frag, is_mask);
719 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
720 ipv4_key->ipv4_src, is_mask);
721 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
722 ipv4_key->ipv4_dst, is_mask);
723 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
724 }
725
726 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
727 const struct ovs_key_ipv6 *ipv6_key;
728
729 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
730 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
731 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
732 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
733 return -EINVAL;
734 }
735 SW_FLOW_KEY_PUT(match, ipv6.label,
736 ipv6_key->ipv6_label, is_mask);
737 SW_FLOW_KEY_PUT(match, ip.proto,
738 ipv6_key->ipv6_proto, is_mask);
739 SW_FLOW_KEY_PUT(match, ip.tos,
740 ipv6_key->ipv6_tclass, is_mask);
741 SW_FLOW_KEY_PUT(match, ip.ttl,
742 ipv6_key->ipv6_hlimit, is_mask);
743 SW_FLOW_KEY_PUT(match, ip.frag,
744 ipv6_key->ipv6_frag, is_mask);
745 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
746 ipv6_key->ipv6_src,
747 sizeof(match->key->ipv6.addr.src),
748 is_mask);
749 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
750 ipv6_key->ipv6_dst,
751 sizeof(match->key->ipv6.addr.dst),
752 is_mask);
753
754 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
755 }
756
757 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
758 const struct ovs_key_arp *arp_key;
759
760 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
761 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
762 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
763 arp_key->arp_op);
764 return -EINVAL;
765 }
766
767 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
768 arp_key->arp_sip, is_mask);
769 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
770 arp_key->arp_tip, is_mask);
771 SW_FLOW_KEY_PUT(match, ip.proto,
772 ntohs(arp_key->arp_op), is_mask);
773 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
774 arp_key->arp_sha, ETH_ALEN, is_mask);
775 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
776 arp_key->arp_tha, ETH_ALEN, is_mask);
777
778 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
779 }
780
25cd9ba0
SH
781 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
782 const struct ovs_key_mpls *mpls_key;
783
784 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
785 SW_FLOW_KEY_PUT(match, mpls.top_lse,
786 mpls_key->mpls_lse, is_mask);
787
788 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
789 }
790
e6445719
PS
791 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
792 const struct ovs_key_tcp *tcp_key;
793
794 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
795 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
796 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
797 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
798 }
799
5eb26b15 800 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1b760fb9
JS
801 SW_FLOW_KEY_PUT(match, tp.flags,
802 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
803 is_mask);
5eb26b15
JR
804 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
805 }
806
e6445719
PS
807 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
808 const struct ovs_key_udp *udp_key;
809
810 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
811 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
812 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
813 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
814 }
815
816 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
817 const struct ovs_key_sctp *sctp_key;
818
819 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
820 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
821 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
822 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
823 }
824
825 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
826 const struct ovs_key_icmp *icmp_key;
827
828 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 829 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 830 htons(icmp_key->icmp_type), is_mask);
1139e241 831 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
832 htons(icmp_key->icmp_code), is_mask);
833 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
834 }
835
836 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
837 const struct ovs_key_icmpv6 *icmpv6_key;
838
839 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 840 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 841 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 842 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
843 htons(icmpv6_key->icmpv6_code), is_mask);
844 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
845 }
846
847 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
848 const struct ovs_key_nd *nd_key;
849
850 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
851 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
852 nd_key->nd_target,
853 sizeof(match->key->ipv6.nd.target),
854 is_mask);
855 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
856 nd_key->nd_sll, ETH_ALEN, is_mask);
857 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
858 nd_key->nd_tll, ETH_ALEN, is_mask);
859 attrs &= ~(1 << OVS_KEY_ATTR_ND);
860 }
861
426cda5c
JG
862 if (attrs != 0) {
863 OVS_NLERR("Unknown key attributes (%llx).\n",
864 (unsigned long long)attrs);
e6445719 865 return -EINVAL;
426cda5c 866 }
e6445719
PS
867
868 return 0;
869}
870
f47de068 871static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
e6445719 872{
f47de068
PS
873 struct nlattr *nla;
874 int rem;
e6445719 875
f47de068
PS
876 /* The nlattr stream should already have been validated */
877 nla_for_each_nested(nla, attr, rem) {
878 /* We assume that ovs_key_lens[type] == -1 means that type is a
879 * nested attribute
880 */
881 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
882 nlattr_set(nla, val, false);
883 else
884 memset(nla_data(nla), val, nla_len(nla));
885 }
886}
887
888static void mask_set_nlattr(struct nlattr *attr, u8 val)
889{
890 nlattr_set(attr, val, true);
e6445719
PS
891}
892
893/**
894 * ovs_nla_get_match - parses Netlink attributes into a flow key and
895 * mask. In case the 'mask' is NULL, the flow is treated as exact match
896 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
897 * does not include any don't care bit.
898 * @match: receives the extracted flow match information.
899 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
900 * sequence. The fields should of the packet that triggered the creation
901 * of this flow.
902 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
903 * attribute specifies the mask field of the wildcarded flow.
904 */
905int ovs_nla_get_match(struct sw_flow_match *match,
906 const struct nlattr *key,
907 const struct nlattr *mask)
908{
909 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
910 const struct nlattr *encap;
f47de068 911 struct nlattr *newmask = NULL;
e6445719
PS
912 u64 key_attrs = 0;
913 u64 mask_attrs = 0;
914 bool encap_valid = false;
915 int err;
916
917 err = parse_flow_nlattrs(key, a, &key_attrs);
918 if (err)
919 return err;
920
921 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
922 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
923 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
924 __be16 tci;
925
926 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
927 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
928 OVS_NLERR("Invalid Vlan frame.\n");
929 return -EINVAL;
930 }
931
932 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
933 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
934 encap = a[OVS_KEY_ATTR_ENCAP];
935 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
936 encap_valid = true;
937
938 if (tci & htons(VLAN_TAG_PRESENT)) {
939 err = parse_flow_nlattrs(encap, a, &key_attrs);
940 if (err)
941 return err;
942 } else if (!tci) {
943 /* Corner case for truncated 802.1Q header. */
944 if (nla_len(encap)) {
945 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
946 return -EINVAL;
947 }
948 } else {
949 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
950 return -EINVAL;
951 }
952 }
953
23dabf88 954 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
e6445719
PS
955 if (err)
956 return err;
957
f47de068
PS
958 if (match->mask && !mask) {
959 /* Create an exact match mask. We need to set to 0xff all the
960 * 'match->mask' fields that have been touched in 'match->key'.
961 * We cannot simply memset 'match->mask', because padding bytes
962 * and fields not specified in 'match->key' should be left to 0.
963 * Instead, we use a stream of netlink attributes, copied from
964 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
965 * of filling 'match->mask' appropriately.
966 */
967 newmask = kmemdup(key, nla_total_size(nla_len(key)),
968 GFP_KERNEL);
969 if (!newmask)
970 return -ENOMEM;
971
972 mask_set_nlattr(newmask, 0xff);
973
974 /* The userspace does not send tunnel attributes that are 0,
975 * but we should not wildcard them nonetheless.
976 */
977 if (match->key->tun_key.ipv4_dst)
978 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
979
980 mask = newmask;
981 }
982
e6445719
PS
983 if (mask) {
984 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
985 if (err)
f47de068 986 goto free_newmask;
e6445719 987
f47de068 988 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
e6445719
PS
989 __be16 eth_type = 0;
990 __be16 tci = 0;
991
992 if (!encap_valid) {
993 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
f47de068
PS
994 err = -EINVAL;
995 goto free_newmask;
e6445719
PS
996 }
997
998 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
999 if (a[OVS_KEY_ATTR_ETHERTYPE])
1000 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1001
1002 if (eth_type == htons(0xffff)) {
1003 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1004 encap = a[OVS_KEY_ATTR_ENCAP];
1005 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
f47de068
PS
1006 if (err)
1007 goto free_newmask;
e6445719
PS
1008 } else {
1009 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1010 ntohs(eth_type));
f47de068
PS
1011 err = -EINVAL;
1012 goto free_newmask;
e6445719
PS
1013 }
1014
1015 if (a[OVS_KEY_ATTR_VLAN])
1016 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1017
1018 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1019 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
f47de068
PS
1020 err = -EINVAL;
1021 goto free_newmask;
e6445719
PS
1022 }
1023 }
1024
23dabf88 1025 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
e6445719 1026 if (err)
f47de068 1027 goto free_newmask;
e6445719
PS
1028 }
1029
1030 if (!match_validate(match, key_attrs, mask_attrs))
f47de068 1031 err = -EINVAL;
e6445719 1032
f47de068
PS
1033free_newmask:
1034 kfree(newmask);
1035 return err;
e6445719
PS
1036}
1037
1038/**
1039 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
83c8df26 1040 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
e6445719
PS
1041 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1042 * sequence.
1043 *
1044 * This parses a series of Netlink attributes that form a flow key, which must
1045 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1046 * get the metadata, that is, the parts of the flow key that cannot be
1047 * extracted from the packet itself.
1048 */
1049
83c8df26
PS
1050int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1051 struct sw_flow_key *key)
e6445719 1052{
e6445719 1053 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
83c8df26 1054 struct sw_flow_match match;
e6445719
PS
1055 u64 attrs = 0;
1056 int err;
e6445719
PS
1057
1058 err = parse_flow_nlattrs(attr, a, &attrs);
1059 if (err)
1060 return -EINVAL;
1061
1062 memset(&match, 0, sizeof(match));
83c8df26 1063 match.key = key;
e6445719 1064
83c8df26 1065 key->phy.in_port = DP_MAX_PORTS;
e6445719 1066
83c8df26 1067 return metadata_from_nlattrs(&match, &attrs, a, false);
e6445719
PS
1068}
1069
1070int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1071 const struct sw_flow_key *output, struct sk_buff *skb)
1072{
1073 struct ovs_key_ethernet *eth_key;
1074 struct nlattr *nla, *encap;
1075 bool is_mask = (swkey != output);
1076
971427f3
AZ
1077 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1078 goto nla_put_failure;
1079
1080 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1081 goto nla_put_failure;
1082
e6445719
PS
1083 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1084 goto nla_put_failure;
1085
f5796684
JG
1086 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1087 const struct geneve_opt *opts = NULL;
1088
1089 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1090 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1091
1092 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1093 swkey->tun_opts_len))
1094 goto nla_put_failure;
1095 }
e6445719
PS
1096
1097 if (swkey->phy.in_port == DP_MAX_PORTS) {
1098 if (is_mask && (output->phy.in_port == 0xffff))
1099 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1100 goto nla_put_failure;
1101 } else {
1102 u16 upper_u16;
1103 upper_u16 = !is_mask ? 0 : 0xffff;
1104
1105 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1106 (upper_u16 << 16) | output->phy.in_port))
1107 goto nla_put_failure;
1108 }
1109
1110 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1111 goto nla_put_failure;
1112
1113 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1114 if (!nla)
1115 goto nla_put_failure;
1116
1117 eth_key = nla_data(nla);
8c63ff09
JP
1118 ether_addr_copy(eth_key->eth_src, output->eth.src);
1119 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
e6445719
PS
1120
1121 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1122 __be16 eth_type;
1123 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1124 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1125 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1126 goto nla_put_failure;
1127 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1128 if (!swkey->eth.tci)
1129 goto unencap;
1130 } else
1131 encap = NULL;
1132
1133 if (swkey->eth.type == htons(ETH_P_802_2)) {
1134 /*
1135 * Ethertype 802.2 is represented in the netlink with omitted
1136 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1137 * 0xffff in the mask attribute. Ethertype can also
1138 * be wildcarded.
1139 */
1140 if (is_mask && output->eth.type)
1141 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1142 output->eth.type))
1143 goto nla_put_failure;
1144 goto unencap;
1145 }
1146
1147 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1148 goto nla_put_failure;
1149
1150 if (swkey->eth.type == htons(ETH_P_IP)) {
1151 struct ovs_key_ipv4 *ipv4_key;
1152
1153 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1154 if (!nla)
1155 goto nla_put_failure;
1156 ipv4_key = nla_data(nla);
1157 ipv4_key->ipv4_src = output->ipv4.addr.src;
1158 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1159 ipv4_key->ipv4_proto = output->ip.proto;
1160 ipv4_key->ipv4_tos = output->ip.tos;
1161 ipv4_key->ipv4_ttl = output->ip.ttl;
1162 ipv4_key->ipv4_frag = output->ip.frag;
1163 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1164 struct ovs_key_ipv6 *ipv6_key;
1165
1166 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1167 if (!nla)
1168 goto nla_put_failure;
1169 ipv6_key = nla_data(nla);
1170 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1171 sizeof(ipv6_key->ipv6_src));
1172 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1173 sizeof(ipv6_key->ipv6_dst));
1174 ipv6_key->ipv6_label = output->ipv6.label;
1175 ipv6_key->ipv6_proto = output->ip.proto;
1176 ipv6_key->ipv6_tclass = output->ip.tos;
1177 ipv6_key->ipv6_hlimit = output->ip.ttl;
1178 ipv6_key->ipv6_frag = output->ip.frag;
1179 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1180 swkey->eth.type == htons(ETH_P_RARP)) {
1181 struct ovs_key_arp *arp_key;
1182
1183 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1184 if (!nla)
1185 goto nla_put_failure;
1186 arp_key = nla_data(nla);
1187 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1188 arp_key->arp_sip = output->ipv4.addr.src;
1189 arp_key->arp_tip = output->ipv4.addr.dst;
1190 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
1191 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1192 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
25cd9ba0
SH
1193 } else if (eth_p_mpls(swkey->eth.type)) {
1194 struct ovs_key_mpls *mpls_key;
1195
1196 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1197 if (!nla)
1198 goto nla_put_failure;
1199 mpls_key = nla_data(nla);
1200 mpls_key->mpls_lse = output->mpls.top_lse;
e6445719
PS
1201 }
1202
1203 if ((swkey->eth.type == htons(ETH_P_IP) ||
1204 swkey->eth.type == htons(ETH_P_IPV6)) &&
1205 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1206
1207 if (swkey->ip.proto == IPPROTO_TCP) {
1208 struct ovs_key_tcp *tcp_key;
1209
1210 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1211 if (!nla)
1212 goto nla_put_failure;
1213 tcp_key = nla_data(nla);
1139e241
JR
1214 tcp_key->tcp_src = output->tp.src;
1215 tcp_key->tcp_dst = output->tp.dst;
1216 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1217 output->tp.flags))
1218 goto nla_put_failure;
e6445719
PS
1219 } else if (swkey->ip.proto == IPPROTO_UDP) {
1220 struct ovs_key_udp *udp_key;
1221
1222 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1223 if (!nla)
1224 goto nla_put_failure;
1225 udp_key = nla_data(nla);
1139e241
JR
1226 udp_key->udp_src = output->tp.src;
1227 udp_key->udp_dst = output->tp.dst;
e6445719
PS
1228 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1229 struct ovs_key_sctp *sctp_key;
1230
1231 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1232 if (!nla)
1233 goto nla_put_failure;
1234 sctp_key = nla_data(nla);
1139e241
JR
1235 sctp_key->sctp_src = output->tp.src;
1236 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
1237 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1238 swkey->ip.proto == IPPROTO_ICMP) {
1239 struct ovs_key_icmp *icmp_key;
1240
1241 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1242 if (!nla)
1243 goto nla_put_failure;
1244 icmp_key = nla_data(nla);
1139e241
JR
1245 icmp_key->icmp_type = ntohs(output->tp.src);
1246 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
1247 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1248 swkey->ip.proto == IPPROTO_ICMPV6) {
1249 struct ovs_key_icmpv6 *icmpv6_key;
1250
1251 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1252 sizeof(*icmpv6_key));
1253 if (!nla)
1254 goto nla_put_failure;
1255 icmpv6_key = nla_data(nla);
1139e241
JR
1256 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1257 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
1258
1259 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1260 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1261 struct ovs_key_nd *nd_key;
1262
1263 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1264 if (!nla)
1265 goto nla_put_failure;
1266 nd_key = nla_data(nla);
1267 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1268 sizeof(nd_key->nd_target));
8c63ff09
JP
1269 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1270 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
1271 }
1272 }
1273 }
1274
1275unencap:
1276 if (encap)
1277 nla_nest_end(skb, encap);
1278
1279 return 0;
1280
1281nla_put_failure:
1282 return -EMSGSIZE;
1283}
1284
1285#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1286
2fdb957d 1287static struct sw_flow_actions *nla_alloc_flow_actions(int size)
e6445719
PS
1288{
1289 struct sw_flow_actions *sfa;
1290
426cda5c
JG
1291 if (size > MAX_ACTIONS_BUFSIZE) {
1292 OVS_NLERR("Flow action size (%u bytes) exceeds maximum", size);
e6445719 1293 return ERR_PTR(-EINVAL);
426cda5c 1294 }
e6445719
PS
1295
1296 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1297 if (!sfa)
1298 return ERR_PTR(-ENOMEM);
1299
1300 sfa->actions_len = 0;
1301 return sfa;
1302}
1303
e6445719
PS
1304/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1305 * The caller must hold rcu_read_lock for this to be sensible. */
1306void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1307{
11d6c461 1308 kfree_rcu(sf_acts, rcu);
e6445719
PS
1309}
1310
1311static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1312 int attr_len)
1313{
1314
1315 struct sw_flow_actions *acts;
1316 int new_acts_size;
1317 int req_size = NLA_ALIGN(attr_len);
1318 int next_offset = offsetof(struct sw_flow_actions, actions) +
1319 (*sfa)->actions_len;
1320
1321 if (req_size <= (ksize(*sfa) - next_offset))
1322 goto out;
1323
1324 new_acts_size = ksize(*sfa) * 2;
1325
1326 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1327 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1328 return ERR_PTR(-EMSGSIZE);
1329 new_acts_size = MAX_ACTIONS_BUFSIZE;
1330 }
1331
2fdb957d 1332 acts = nla_alloc_flow_actions(new_acts_size);
e6445719
PS
1333 if (IS_ERR(acts))
1334 return (void *)acts;
1335
1336 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1337 acts->actions_len = (*sfa)->actions_len;
1338 kfree(*sfa);
1339 *sfa = acts;
1340
1341out:
1342 (*sfa)->actions_len += req_size;
1343 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1344}
1345
f0b128c1
JG
1346static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1347 int attrtype, void *data, int len)
e6445719
PS
1348{
1349 struct nlattr *a;
1350
1351 a = reserve_sfa_size(sfa, nla_attr_size(len));
1352 if (IS_ERR(a))
f0b128c1 1353 return a;
e6445719
PS
1354
1355 a->nla_type = attrtype;
1356 a->nla_len = nla_attr_size(len);
1357
1358 if (data)
1359 memcpy(nla_data(a), data, len);
1360 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1361
f0b128c1
JG
1362 return a;
1363}
1364
1365static int add_action(struct sw_flow_actions **sfa, int attrtype,
1366 void *data, int len)
1367{
1368 struct nlattr *a;
1369
1370 a = __add_action(sfa, attrtype, data, len);
1371 if (IS_ERR(a))
1372 return PTR_ERR(a);
1373
e6445719
PS
1374 return 0;
1375}
1376
1377static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1378 int attrtype)
1379{
1380 int used = (*sfa)->actions_len;
1381 int err;
1382
1383 err = add_action(sfa, attrtype, NULL, 0);
1384 if (err)
1385 return err;
1386
1387 return used;
1388}
1389
1390static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1391 int st_offset)
1392{
1393 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1394 st_offset);
1395
1396 a->nla_len = sfa->actions_len - st_offset;
1397}
1398
2fdb957d 1399static int __ovs_nla_copy_actions(const struct nlattr *attr,
25cd9ba0
SH
1400 const struct sw_flow_key *key,
1401 int depth, struct sw_flow_actions **sfa,
1402 __be16 eth_type, __be16 vlan_tci);
1403
e6445719
PS
1404static int validate_and_copy_sample(const struct nlattr *attr,
1405 const struct sw_flow_key *key, int depth,
25cd9ba0
SH
1406 struct sw_flow_actions **sfa,
1407 __be16 eth_type, __be16 vlan_tci)
e6445719
PS
1408{
1409 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1410 const struct nlattr *probability, *actions;
1411 const struct nlattr *a;
1412 int rem, start, err, st_acts;
1413
1414 memset(attrs, 0, sizeof(attrs));
1415 nla_for_each_nested(a, attr, rem) {
1416 int type = nla_type(a);
1417 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1418 return -EINVAL;
1419 attrs[type] = a;
1420 }
1421 if (rem)
1422 return -EINVAL;
1423
1424 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1425 if (!probability || nla_len(probability) != sizeof(u32))
1426 return -EINVAL;
1427
1428 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1429 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1430 return -EINVAL;
1431
1432 /* validation done, copy sample action. */
1433 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1434 if (start < 0)
1435 return start;
1436 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1437 nla_data(probability), sizeof(u32));
1438 if (err)
1439 return err;
1440 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1441 if (st_acts < 0)
1442 return st_acts;
1443
2fdb957d 1444 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
25cd9ba0 1445 eth_type, vlan_tci);
e6445719
PS
1446 if (err)
1447 return err;
1448
1449 add_nested_action_end(*sfa, st_acts);
1450 add_nested_action_end(*sfa, start);
1451
1452 return 0;
1453}
1454
25cd9ba0
SH
1455static int validate_tp_port(const struct sw_flow_key *flow_key,
1456 __be16 eth_type)
e6445719 1457{
25cd9ba0 1458 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1139e241
JR
1459 (flow_key->tp.src || flow_key->tp.dst))
1460 return 0;
e6445719
PS
1461
1462 return -EINVAL;
1463}
1464
1465void ovs_match_init(struct sw_flow_match *match,
1466 struct sw_flow_key *key,
1467 struct sw_flow_mask *mask)
1468{
1469 memset(match, 0, sizeof(*match));
1470 match->key = key;
1471 match->mask = mask;
1472
1473 memset(key, 0, sizeof(*key));
1474
1475 if (mask) {
1476 memset(&mask->key, 0, sizeof(mask->key));
1477 mask->range.start = mask->range.end = 0;
1478 }
1479}
1480
1481static int validate_and_copy_set_tun(const struct nlattr *attr,
1482 struct sw_flow_actions **sfa)
1483{
1484 struct sw_flow_match match;
1485 struct sw_flow_key key;
f0b128c1
JG
1486 struct ovs_tunnel_info *tun_info;
1487 struct nlattr *a;
e6445719
PS
1488 int err, start;
1489
1490 ovs_match_init(&match, &key, NULL);
1491 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1492 if (err)
1493 return err;
1494
f5796684
JG
1495 if (key.tun_opts_len) {
1496 struct geneve_opt *option = GENEVE_OPTS(&key,
1497 key.tun_opts_len);
1498 int opts_len = key.tun_opts_len;
1499 bool crit_opt = false;
1500
1501 while (opts_len > 0) {
1502 int len;
1503
1504 if (opts_len < sizeof(*option))
1505 return -EINVAL;
1506
1507 len = sizeof(*option) + option->length * 4;
1508 if (len > opts_len)
1509 return -EINVAL;
1510
1511 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1512
1513 option = (struct geneve_opt *)((u8 *)option + len);
1514 opts_len -= len;
1515 };
1516
1517 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1518 };
1519
e6445719
PS
1520 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1521 if (start < 0)
1522 return start;
1523
f0b128c1 1524 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
f5796684 1525 sizeof(*tun_info) + key.tun_opts_len);
f0b128c1
JG
1526 if (IS_ERR(a))
1527 return PTR_ERR(a);
1528
1529 tun_info = nla_data(a);
1530 tun_info->tunnel = key.tun_key;
f5796684
JG
1531 tun_info->options_len = key.tun_opts_len;
1532
1533 if (tun_info->options_len) {
1534 /* We need to store the options in the action itself since
1535 * everything else will go away after flow setup. We can append
1536 * it to tun_info and then point there.
1537 */
1538 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1539 key.tun_opts_len);
1540 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1541 } else {
1542 tun_info->options = NULL;
1543 }
f0b128c1 1544
e6445719
PS
1545 add_nested_action_end(*sfa, start);
1546
1547 return err;
1548}
1549
1550static int validate_set(const struct nlattr *a,
1551 const struct sw_flow_key *flow_key,
1552 struct sw_flow_actions **sfa,
25cd9ba0 1553 bool *set_tun, __be16 eth_type)
e6445719
PS
1554{
1555 const struct nlattr *ovs_key = nla_data(a);
1556 int key_type = nla_type(ovs_key);
1557
1558 /* There can be only one key in a action */
1559 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1560 return -EINVAL;
1561
1562 if (key_type > OVS_KEY_ATTR_MAX ||
1563 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1564 ovs_key_lens[key_type] != -1))
1565 return -EINVAL;
1566
1567 switch (key_type) {
1568 const struct ovs_key_ipv4 *ipv4_key;
1569 const struct ovs_key_ipv6 *ipv6_key;
1570 int err;
1571
1572 case OVS_KEY_ATTR_PRIORITY:
1573 case OVS_KEY_ATTR_SKB_MARK:
1574 case OVS_KEY_ATTR_ETHERNET:
1575 break;
1576
1577 case OVS_KEY_ATTR_TUNNEL:
25cd9ba0
SH
1578 if (eth_p_mpls(eth_type))
1579 return -EINVAL;
1580
e6445719
PS
1581 *set_tun = true;
1582 err = validate_and_copy_set_tun(a, sfa);
1583 if (err)
1584 return err;
1585 break;
1586
1587 case OVS_KEY_ATTR_IPV4:
25cd9ba0 1588 if (eth_type != htons(ETH_P_IP))
e6445719
PS
1589 return -EINVAL;
1590
1591 if (!flow_key->ip.proto)
1592 return -EINVAL;
1593
1594 ipv4_key = nla_data(ovs_key);
1595 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1596 return -EINVAL;
1597
1598 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1599 return -EINVAL;
1600
1601 break;
1602
1603 case OVS_KEY_ATTR_IPV6:
25cd9ba0 1604 if (eth_type != htons(ETH_P_IPV6))
e6445719
PS
1605 return -EINVAL;
1606
1607 if (!flow_key->ip.proto)
1608 return -EINVAL;
1609
1610 ipv6_key = nla_data(ovs_key);
1611 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1612 return -EINVAL;
1613
1614 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1615 return -EINVAL;
1616
1617 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1618 return -EINVAL;
1619
1620 break;
1621
1622 case OVS_KEY_ATTR_TCP:
1623 if (flow_key->ip.proto != IPPROTO_TCP)
1624 return -EINVAL;
1625
25cd9ba0 1626 return validate_tp_port(flow_key, eth_type);
e6445719
PS
1627
1628 case OVS_KEY_ATTR_UDP:
1629 if (flow_key->ip.proto != IPPROTO_UDP)
1630 return -EINVAL;
1631
25cd9ba0
SH
1632 return validate_tp_port(flow_key, eth_type);
1633
1634 case OVS_KEY_ATTR_MPLS:
1635 if (!eth_p_mpls(eth_type))
1636 return -EINVAL;
1637 break;
e6445719
PS
1638
1639 case OVS_KEY_ATTR_SCTP:
1640 if (flow_key->ip.proto != IPPROTO_SCTP)
1641 return -EINVAL;
1642
25cd9ba0 1643 return validate_tp_port(flow_key, eth_type);
e6445719
PS
1644
1645 default:
1646 return -EINVAL;
1647 }
1648
1649 return 0;
1650}
1651
1652static int validate_userspace(const struct nlattr *attr)
1653{
1654 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1655 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1656 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1657 };
1658 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1659 int error;
1660
1661 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1662 attr, userspace_policy);
1663 if (error)
1664 return error;
1665
1666 if (!a[OVS_USERSPACE_ATTR_PID] ||
1667 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1668 return -EINVAL;
1669
1670 return 0;
1671}
1672
1673static int copy_action(const struct nlattr *from,
1674 struct sw_flow_actions **sfa)
1675{
1676 int totlen = NLA_ALIGN(from->nla_len);
1677 struct nlattr *to;
1678
1679 to = reserve_sfa_size(sfa, from->nla_len);
1680 if (IS_ERR(to))
1681 return PTR_ERR(to);
1682
1683 memcpy(to, from, totlen);
1684 return 0;
1685}
1686
2fdb957d 1687static int __ovs_nla_copy_actions(const struct nlattr *attr,
25cd9ba0
SH
1688 const struct sw_flow_key *key,
1689 int depth, struct sw_flow_actions **sfa,
1690 __be16 eth_type, __be16 vlan_tci)
e6445719
PS
1691{
1692 const struct nlattr *a;
25cd9ba0 1693 bool out_tnl_port = false;
e6445719
PS
1694 int rem, err;
1695
1696 if (depth >= SAMPLE_ACTION_DEPTH)
1697 return -EOVERFLOW;
1698
1699 nla_for_each_nested(a, attr, rem) {
1700 /* Expected argument lengths, (u32)-1 for variable length. */
1701 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1702 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 1703 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719 1704 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
25cd9ba0
SH
1705 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1706 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
e6445719
PS
1707 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1708 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1709 [OVS_ACTION_ATTR_SET] = (u32)-1,
971427f3
AZ
1710 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1711 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
e6445719
PS
1712 };
1713 const struct ovs_action_push_vlan *vlan;
1714 int type = nla_type(a);
1715 bool skip_copy;
1716
1717 if (type > OVS_ACTION_ATTR_MAX ||
1718 (action_lens[type] != nla_len(a) &&
1719 action_lens[type] != (u32)-1))
1720 return -EINVAL;
1721
1722 skip_copy = false;
1723 switch (type) {
1724 case OVS_ACTION_ATTR_UNSPEC:
1725 return -EINVAL;
1726
1727 case OVS_ACTION_ATTR_USERSPACE:
1728 err = validate_userspace(a);
1729 if (err)
1730 return err;
1731 break;
1732
1733 case OVS_ACTION_ATTR_OUTPUT:
1734 if (nla_get_u32(a) >= DP_MAX_PORTS)
1735 return -EINVAL;
25cd9ba0
SH
1736 out_tnl_port = false;
1737
e6445719
PS
1738 break;
1739
971427f3
AZ
1740 case OVS_ACTION_ATTR_HASH: {
1741 const struct ovs_action_hash *act_hash = nla_data(a);
1742
1743 switch (act_hash->hash_alg) {
1744 case OVS_HASH_ALG_L4:
1745 break;
1746 default:
1747 return -EINVAL;
1748 }
1749
1750 break;
1751 }
e6445719
PS
1752
1753 case OVS_ACTION_ATTR_POP_VLAN:
25cd9ba0 1754 vlan_tci = htons(0);
e6445719
PS
1755 break;
1756
1757 case OVS_ACTION_ATTR_PUSH_VLAN:
1758 vlan = nla_data(a);
1759 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1760 return -EINVAL;
1761 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1762 return -EINVAL;
25cd9ba0 1763 vlan_tci = vlan->vlan_tci;
e6445719
PS
1764 break;
1765
971427f3
AZ
1766 case OVS_ACTION_ATTR_RECIRC:
1767 break;
1768
25cd9ba0
SH
1769 case OVS_ACTION_ATTR_PUSH_MPLS: {
1770 const struct ovs_action_push_mpls *mpls = nla_data(a);
1771
1772 /* Networking stack do not allow simultaneous Tunnel
1773 * and MPLS GSO.
1774 */
1775 if (out_tnl_port)
1776 return -EINVAL;
1777
1778 if (!eth_p_mpls(mpls->mpls_ethertype))
1779 return -EINVAL;
1780 /* Prohibit push MPLS other than to a white list
1781 * for packets that have a known tag order.
1782 */
1783 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1784 (eth_type != htons(ETH_P_IP) &&
1785 eth_type != htons(ETH_P_IPV6) &&
1786 eth_type != htons(ETH_P_ARP) &&
1787 eth_type != htons(ETH_P_RARP) &&
1788 !eth_p_mpls(eth_type)))
1789 return -EINVAL;
1790 eth_type = mpls->mpls_ethertype;
1791 break;
1792 }
1793
1794 case OVS_ACTION_ATTR_POP_MPLS:
1795 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1796 !eth_p_mpls(eth_type))
1797 return -EINVAL;
1798
1799 /* Disallow subsequent L2.5+ set and mpls_pop actions
1800 * as there is no check here to ensure that the new
1801 * eth_type is valid and thus set actions could
1802 * write off the end of the packet or otherwise
1803 * corrupt it.
1804 *
1805 * Support for these actions is planned using packet
1806 * recirculation.
1807 */
1808 eth_type = htons(0);
1809 break;
1810
e6445719 1811 case OVS_ACTION_ATTR_SET:
25cd9ba0
SH
1812 err = validate_set(a, key, sfa,
1813 &out_tnl_port, eth_type);
e6445719
PS
1814 if (err)
1815 return err;
25cd9ba0
SH
1816
1817 skip_copy = out_tnl_port;
e6445719
PS
1818 break;
1819
1820 case OVS_ACTION_ATTR_SAMPLE:
25cd9ba0
SH
1821 err = validate_and_copy_sample(a, key, depth, sfa,
1822 eth_type, vlan_tci);
e6445719
PS
1823 if (err)
1824 return err;
1825 skip_copy = true;
1826 break;
1827
1828 default:
426cda5c 1829 OVS_NLERR("Unknown tunnel attribute (%d).\n", type);
e6445719
PS
1830 return -EINVAL;
1831 }
1832 if (!skip_copy) {
1833 err = copy_action(a, sfa);
1834 if (err)
1835 return err;
1836 }
1837 }
1838
1839 if (rem > 0)
1840 return -EINVAL;
1841
1842 return 0;
1843}
1844
25cd9ba0
SH
1845int ovs_nla_copy_actions(const struct nlattr *attr,
1846 const struct sw_flow_key *key,
1847 struct sw_flow_actions **sfa)
1848{
2fdb957d
PS
1849 int err;
1850
1851 *sfa = nla_alloc_flow_actions(nla_len(attr));
1852 if (IS_ERR(*sfa))
1853 return PTR_ERR(*sfa);
1854
1855 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
1856 key->eth.tci);
1857 if (err)
1858 kfree(*sfa);
1859
1860 return err;
25cd9ba0
SH
1861}
1862
e6445719
PS
1863static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1864{
1865 const struct nlattr *a;
1866 struct nlattr *start;
1867 int err = 0, rem;
1868
1869 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1870 if (!start)
1871 return -EMSGSIZE;
1872
1873 nla_for_each_nested(a, attr, rem) {
1874 int type = nla_type(a);
1875 struct nlattr *st_sample;
1876
1877 switch (type) {
1878 case OVS_SAMPLE_ATTR_PROBABILITY:
1879 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1880 sizeof(u32), nla_data(a)))
1881 return -EMSGSIZE;
1882 break;
1883 case OVS_SAMPLE_ATTR_ACTIONS:
1884 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1885 if (!st_sample)
1886 return -EMSGSIZE;
1887 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1888 if (err)
1889 return err;
1890 nla_nest_end(skb, st_sample);
1891 break;
1892 }
1893 }
1894
1895 nla_nest_end(skb, start);
1896 return err;
1897}
1898
1899static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1900{
1901 const struct nlattr *ovs_key = nla_data(a);
1902 int key_type = nla_type(ovs_key);
1903 struct nlattr *start;
1904 int err;
1905
1906 switch (key_type) {
f0b128c1
JG
1907 case OVS_KEY_ATTR_TUNNEL_INFO: {
1908 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1909
e6445719
PS
1910 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1911 if (!start)
1912 return -EMSGSIZE;
1913
f0b128c1 1914 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
f5796684
JG
1915 tun_info->options_len ?
1916 tun_info->options : NULL,
1917 tun_info->options_len);
e6445719
PS
1918 if (err)
1919 return err;
1920 nla_nest_end(skb, start);
1921 break;
f0b128c1 1922 }
e6445719
PS
1923 default:
1924 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1925 return -EMSGSIZE;
1926 break;
1927 }
1928
1929 return 0;
1930}
1931
1932int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1933{
1934 const struct nlattr *a;
1935 int rem, err;
1936
1937 nla_for_each_attr(a, attr, len, rem) {
1938 int type = nla_type(a);
1939
1940 switch (type) {
1941 case OVS_ACTION_ATTR_SET:
1942 err = set_action_to_attr(a, skb);
1943 if (err)
1944 return err;
1945 break;
1946
1947 case OVS_ACTION_ATTR_SAMPLE:
1948 err = sample_action_to_attr(a, skb);
1949 if (err)
1950 return err;
1951 break;
1952 default:
1953 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1954 return -EMSGSIZE;
1955 break;
1956 }
1957 }
1958
1959 return 0;
1960}
This page took 1.902775 seconds and 5 git commands to generate.