]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/openvswitch/flow_netlink.c
openvswitch: Add support for matching on OAM packets.
[mirror_ubuntu-artful-kernel.git] / net / openvswitch / flow_netlink.c
CommitLineData
e6445719 1/*
971427f3 2 * Copyright (c) 2007-2014 Nicira, Inc.
e6445719
PS
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
2235ad1c
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
e6445719
PS
21#include "flow.h"
22#include "datapath.h"
23#include <linux/uaccess.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <net/llc_pdu.h>
29#include <linux/kernel.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/llc.h>
33#include <linux/module.h>
34#include <linux/in.h>
35#include <linux/rcupdate.h>
36#include <linux/if_arp.h>
37#include <linux/ip.h>
38#include <linux/ipv6.h>
39#include <linux/sctp.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
43#include <linux/icmpv6.h>
44#include <linux/rculist.h>
45#include <net/ip.h>
46#include <net/ipv6.h>
47#include <net/ndisc.h>
48
49#include "flow_netlink.h"
50
51static void update_range__(struct sw_flow_match *match,
52 size_t offset, size_t size, bool is_mask)
53{
54 struct sw_flow_key_range *range = NULL;
55 size_t start = rounddown(offset, sizeof(long));
56 size_t end = roundup(offset + size, sizeof(long));
57
58 if (!is_mask)
59 range = &match->range;
60 else if (match->mask)
61 range = &match->mask->range;
62
63 if (!range)
64 return;
65
66 if (range->start == range->end) {
67 range->start = start;
68 range->end = end;
69 return;
70 }
71
72 if (range->start > start)
73 range->start = start;
74
75 if (range->end < end)
76 range->end = end;
77}
78
79#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
80 do { \
81 update_range__(match, offsetof(struct sw_flow_key, field), \
82 sizeof((match)->key->field), is_mask); \
83 if (is_mask) { \
84 if ((match)->mask) \
85 (match)->mask->key.field = value; \
86 } else { \
87 (match)->key->field = value; \
88 } \
89 } while (0)
90
91#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
92 do { \
93 update_range__(match, offsetof(struct sw_flow_key, field), \
94 len, is_mask); \
95 if (is_mask) { \
96 if ((match)->mask) \
97 memcpy(&(match)->mask->key.field, value_p, len);\
98 } else { \
99 memcpy(&(match)->key->field, value_p, len); \
100 } \
101 } while (0)
102
103static u16 range_n_bytes(const struct sw_flow_key_range *range)
104{
105 return range->end - range->start;
106}
107
108static bool match_validate(const struct sw_flow_match *match,
109 u64 key_attrs, u64 mask_attrs)
110{
111 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
112 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
113
114 /* The following mask attributes allowed only if they
115 * pass the validation tests. */
116 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
117 | (1 << OVS_KEY_ATTR_IPV6)
118 | (1 << OVS_KEY_ATTR_TCP)
5eb26b15 119 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
e6445719
PS
120 | (1 << OVS_KEY_ATTR_UDP)
121 | (1 << OVS_KEY_ATTR_SCTP)
122 | (1 << OVS_KEY_ATTR_ICMP)
123 | (1 << OVS_KEY_ATTR_ICMPV6)
124 | (1 << OVS_KEY_ATTR_ARP)
125 | (1 << OVS_KEY_ATTR_ND));
126
127 /* Always allowed mask fields. */
128 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
129 | (1 << OVS_KEY_ATTR_IN_PORT)
130 | (1 << OVS_KEY_ATTR_ETHERTYPE));
131
132 /* Check key attributes. */
133 if (match->key->eth.type == htons(ETH_P_ARP)
134 || match->key->eth.type == htons(ETH_P_RARP)) {
135 key_expected |= 1 << OVS_KEY_ATTR_ARP;
136 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
137 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
138 }
139
140 if (match->key->eth.type == htons(ETH_P_IP)) {
141 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
142 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
143 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
144
145 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
146 if (match->key->ip.proto == IPPROTO_UDP) {
147 key_expected |= 1 << OVS_KEY_ATTR_UDP;
148 if (match->mask && (match->mask->key.ip.proto == 0xff))
149 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
150 }
151
152 if (match->key->ip.proto == IPPROTO_SCTP) {
153 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
154 if (match->mask && (match->mask->key.ip.proto == 0xff))
155 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
156 }
157
158 if (match->key->ip.proto == IPPROTO_TCP) {
159 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
160 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
161 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 162 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
163 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
164 }
e6445719
PS
165 }
166
167 if (match->key->ip.proto == IPPROTO_ICMP) {
168 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
171 }
172 }
173 }
174
175 if (match->key->eth.type == htons(ETH_P_IPV6)) {
176 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
177 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
178 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
179
180 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
181 if (match->key->ip.proto == IPPROTO_UDP) {
182 key_expected |= 1 << OVS_KEY_ATTR_UDP;
183 if (match->mask && (match->mask->key.ip.proto == 0xff))
184 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
185 }
186
187 if (match->key->ip.proto == IPPROTO_SCTP) {
188 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
191 }
192
193 if (match->key->ip.proto == IPPROTO_TCP) {
194 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
195 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
196 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 197 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
198 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
199 }
e6445719
PS
200 }
201
202 if (match->key->ip.proto == IPPROTO_ICMPV6) {
203 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
206
1139e241 207 if (match->key->tp.src ==
e6445719 208 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1139e241 209 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e6445719 210 key_expected |= 1 << OVS_KEY_ATTR_ND;
1139e241 211 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
e6445719
PS
212 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
213 }
214 }
215 }
216 }
217
218 if ((key_attrs & key_expected) != key_expected) {
219 /* Key attributes check failed. */
220 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
cc23ebf3 221 (unsigned long long)key_attrs, (unsigned long long)key_expected);
e6445719
PS
222 return false;
223 }
224
225 if ((mask_attrs & mask_allowed) != mask_attrs) {
226 /* Mask attributes check failed. */
227 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
cc23ebf3 228 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
e6445719
PS
229 return false;
230 }
231
232 return true;
233}
234
235/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
236static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
237 [OVS_KEY_ATTR_ENCAP] = -1,
238 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
239 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
240 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
241 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
242 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
243 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
244 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
245 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
246 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
5eb26b15 247 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
e6445719
PS
248 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
249 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
250 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
251 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
252 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
253 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
971427f3
AZ
254 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
255 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
e6445719
PS
256 [OVS_KEY_ATTR_TUNNEL] = -1,
257};
258
259static bool is_all_zero(const u8 *fp, size_t size)
260{
261 int i;
262
263 if (!fp)
264 return false;
265
266 for (i = 0; i < size; i++)
267 if (fp[i])
268 return false;
269
270 return true;
271}
272
273static int __parse_flow_nlattrs(const struct nlattr *attr,
274 const struct nlattr *a[],
275 u64 *attrsp, bool nz)
276{
277 const struct nlattr *nla;
278 u64 attrs;
279 int rem;
280
281 attrs = *attrsp;
282 nla_for_each_nested(nla, attr, rem) {
283 u16 type = nla_type(nla);
284 int expected_len;
285
286 if (type > OVS_KEY_ATTR_MAX) {
287 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
288 type, OVS_KEY_ATTR_MAX);
289 return -EINVAL;
290 }
291
292 if (attrs & (1 << type)) {
293 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
294 return -EINVAL;
295 }
296
297 expected_len = ovs_key_lens[type];
298 if (nla_len(nla) != expected_len && expected_len != -1) {
299 OVS_NLERR("Key attribute has unexpected length (type=%d"
300 ", length=%d, expected=%d).\n", type,
301 nla_len(nla), expected_len);
302 return -EINVAL;
303 }
304
305 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
306 attrs |= 1 << type;
307 a[type] = nla;
308 }
309 }
310 if (rem) {
311 OVS_NLERR("Message has %d unknown bytes.\n", rem);
312 return -EINVAL;
313 }
314
315 *attrsp = attrs;
316 return 0;
317}
318
319static int parse_flow_mask_nlattrs(const struct nlattr *attr,
320 const struct nlattr *a[], u64 *attrsp)
321{
322 return __parse_flow_nlattrs(attr, a, attrsp, true);
323}
324
325static int parse_flow_nlattrs(const struct nlattr *attr,
326 const struct nlattr *a[], u64 *attrsp)
327{
328 return __parse_flow_nlattrs(attr, a, attrsp, false);
329}
330
331static int ipv4_tun_from_nlattr(const struct nlattr *attr,
332 struct sw_flow_match *match, bool is_mask)
333{
334 struct nlattr *a;
335 int rem;
336 bool ttl = false;
337 __be16 tun_flags = 0;
338
339 nla_for_each_nested(a, attr, rem) {
340 int type = nla_type(a);
341 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
342 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
343 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
344 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
345 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
346 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
347 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
348 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
67fa0341 349 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
e6445719
PS
350 };
351
352 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
353 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
354 type, OVS_TUNNEL_KEY_ATTR_MAX);
355 return -EINVAL;
356 }
357
358 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
359 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
360 " length (type=%d, length=%d, expected=%d).\n",
361 type, nla_len(a), ovs_tunnel_key_lens[type]);
362 return -EINVAL;
363 }
364
365 switch (type) {
366 case OVS_TUNNEL_KEY_ATTR_ID:
367 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
368 nla_get_be64(a), is_mask);
369 tun_flags |= TUNNEL_KEY;
370 break;
371 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
372 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
373 nla_get_be32(a), is_mask);
374 break;
375 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
376 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
377 nla_get_be32(a), is_mask);
378 break;
379 case OVS_TUNNEL_KEY_ATTR_TOS:
380 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
381 nla_get_u8(a), is_mask);
382 break;
383 case OVS_TUNNEL_KEY_ATTR_TTL:
384 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
385 nla_get_u8(a), is_mask);
386 ttl = true;
387 break;
388 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
389 tun_flags |= TUNNEL_DONT_FRAGMENT;
390 break;
391 case OVS_TUNNEL_KEY_ATTR_CSUM:
392 tun_flags |= TUNNEL_CSUM;
393 break;
67fa0341
JG
394 case OVS_TUNNEL_KEY_ATTR_OAM:
395 tun_flags |= TUNNEL_OAM;
396 break;
e6445719
PS
397 default:
398 return -EINVAL;
399 }
400 }
401
402 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
403
404 if (rem > 0) {
405 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
406 return -EINVAL;
407 }
408
409 if (!is_mask) {
410 if (!match->key->tun_key.ipv4_dst) {
411 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
412 return -EINVAL;
413 }
414
415 if (!ttl) {
416 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
417 return -EINVAL;
418 }
419 }
420
421 return 0;
422}
423
424static int ipv4_tun_to_nlattr(struct sk_buff *skb,
425 const struct ovs_key_ipv4_tunnel *tun_key,
426 const struct ovs_key_ipv4_tunnel *output)
427{
428 struct nlattr *nla;
429
430 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
431 if (!nla)
432 return -EMSGSIZE;
433
434 if (output->tun_flags & TUNNEL_KEY &&
435 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
436 return -EMSGSIZE;
437 if (output->ipv4_src &&
67fa0341 438 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
e6445719
PS
439 return -EMSGSIZE;
440 if (output->ipv4_dst &&
67fa0341 441 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
e6445719
PS
442 return -EMSGSIZE;
443 if (output->ipv4_tos &&
67fa0341 444 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
e6445719
PS
445 return -EMSGSIZE;
446 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
447 return -EMSGSIZE;
448 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
67fa0341 449 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
e6445719
PS
450 return -EMSGSIZE;
451 if ((output->tun_flags & TUNNEL_CSUM) &&
67fa0341
JG
452 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
453 return -EMSGSIZE;
454 if ((output->tun_flags & TUNNEL_OAM) &&
455 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
e6445719
PS
456 return -EMSGSIZE;
457
458 nla_nest_end(skb, nla);
459 return 0;
460}
461
462
463static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
464 const struct nlattr **a, bool is_mask)
465{
971427f3
AZ
466 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
467 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
468
469 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
470 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
471 }
472
473 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
474 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
475
476 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
477 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
478 }
479
e6445719
PS
480 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
481 SW_FLOW_KEY_PUT(match, phy.priority,
482 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
483 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
484 }
485
486 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
487 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
488
489 if (is_mask)
490 in_port = 0xffffffff; /* Always exact match in_port. */
491 else if (in_port >= DP_MAX_PORTS)
492 return -EINVAL;
493
494 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
495 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
496 } else if (!is_mask) {
497 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
498 }
499
500 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
501 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
502
503 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
504 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
505 }
506 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
507 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
508 is_mask))
509 return -EINVAL;
510 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
511 }
512 return 0;
513}
514
23dabf88
JR
515static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
516 const struct nlattr **a, bool is_mask)
e6445719
PS
517{
518 int err;
519 u64 orig_attrs = attrs;
520
521 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
522 if (err)
523 return err;
524
525 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
526 const struct ovs_key_ethernet *eth_key;
527
528 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
529 SW_FLOW_KEY_MEMCPY(match, eth.src,
530 eth_key->eth_src, ETH_ALEN, is_mask);
531 SW_FLOW_KEY_MEMCPY(match, eth.dst,
532 eth_key->eth_dst, ETH_ALEN, is_mask);
533 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
534 }
535
536 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
537 __be16 tci;
538
539 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
540 if (!(tci & htons(VLAN_TAG_PRESENT))) {
541 if (is_mask)
542 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
543 else
544 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
545
546 return -EINVAL;
547 }
548
549 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
550 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
551 } else if (!is_mask)
552 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
553
554 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
555 __be16 eth_type;
556
557 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
558 if (is_mask) {
559 /* Always exact match EtherType. */
560 eth_type = htons(0xffff);
561 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
562 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
563 ntohs(eth_type), ETH_P_802_3_MIN);
564 return -EINVAL;
565 }
566
567 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
568 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
569 } else if (!is_mask) {
570 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
571 }
572
573 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
574 const struct ovs_key_ipv4 *ipv4_key;
575
576 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
577 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
578 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
579 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
580 return -EINVAL;
581 }
582 SW_FLOW_KEY_PUT(match, ip.proto,
583 ipv4_key->ipv4_proto, is_mask);
584 SW_FLOW_KEY_PUT(match, ip.tos,
585 ipv4_key->ipv4_tos, is_mask);
586 SW_FLOW_KEY_PUT(match, ip.ttl,
587 ipv4_key->ipv4_ttl, is_mask);
588 SW_FLOW_KEY_PUT(match, ip.frag,
589 ipv4_key->ipv4_frag, is_mask);
590 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
591 ipv4_key->ipv4_src, is_mask);
592 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
593 ipv4_key->ipv4_dst, is_mask);
594 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
595 }
596
597 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
598 const struct ovs_key_ipv6 *ipv6_key;
599
600 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
601 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
602 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
603 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
604 return -EINVAL;
605 }
606 SW_FLOW_KEY_PUT(match, ipv6.label,
607 ipv6_key->ipv6_label, is_mask);
608 SW_FLOW_KEY_PUT(match, ip.proto,
609 ipv6_key->ipv6_proto, is_mask);
610 SW_FLOW_KEY_PUT(match, ip.tos,
611 ipv6_key->ipv6_tclass, is_mask);
612 SW_FLOW_KEY_PUT(match, ip.ttl,
613 ipv6_key->ipv6_hlimit, is_mask);
614 SW_FLOW_KEY_PUT(match, ip.frag,
615 ipv6_key->ipv6_frag, is_mask);
616 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
617 ipv6_key->ipv6_src,
618 sizeof(match->key->ipv6.addr.src),
619 is_mask);
620 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
621 ipv6_key->ipv6_dst,
622 sizeof(match->key->ipv6.addr.dst),
623 is_mask);
624
625 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
626 }
627
628 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
629 const struct ovs_key_arp *arp_key;
630
631 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
632 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
633 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
634 arp_key->arp_op);
635 return -EINVAL;
636 }
637
638 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
639 arp_key->arp_sip, is_mask);
640 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
641 arp_key->arp_tip, is_mask);
642 SW_FLOW_KEY_PUT(match, ip.proto,
643 ntohs(arp_key->arp_op), is_mask);
644 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
645 arp_key->arp_sha, ETH_ALEN, is_mask);
646 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
647 arp_key->arp_tha, ETH_ALEN, is_mask);
648
649 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
650 }
651
652 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
653 const struct ovs_key_tcp *tcp_key;
654
655 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
656 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
657 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
658 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
659 }
660
5eb26b15
JR
661 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
662 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1139e241 663 SW_FLOW_KEY_PUT(match, tp.flags,
5eb26b15
JR
664 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
665 is_mask);
666 } else {
1139e241 667 SW_FLOW_KEY_PUT(match, tp.flags,
5eb26b15
JR
668 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
669 is_mask);
670 }
671 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
672 }
673
e6445719
PS
674 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
675 const struct ovs_key_udp *udp_key;
676
677 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
678 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
679 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
680 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
681 }
682
683 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
684 const struct ovs_key_sctp *sctp_key;
685
686 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
687 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
688 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
689 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
690 }
691
692 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
693 const struct ovs_key_icmp *icmp_key;
694
695 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 696 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 697 htons(icmp_key->icmp_type), is_mask);
1139e241 698 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
699 htons(icmp_key->icmp_code), is_mask);
700 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
701 }
702
703 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
704 const struct ovs_key_icmpv6 *icmpv6_key;
705
706 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 707 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 708 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 709 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
710 htons(icmpv6_key->icmpv6_code), is_mask);
711 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
712 }
713
714 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
715 const struct ovs_key_nd *nd_key;
716
717 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
718 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
719 nd_key->nd_target,
720 sizeof(match->key->ipv6.nd.target),
721 is_mask);
722 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
723 nd_key->nd_sll, ETH_ALEN, is_mask);
724 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
725 nd_key->nd_tll, ETH_ALEN, is_mask);
726 attrs &= ~(1 << OVS_KEY_ATTR_ND);
727 }
728
729 if (attrs != 0)
730 return -EINVAL;
731
732 return 0;
733}
734
735static void sw_flow_mask_set(struct sw_flow_mask *mask,
736 struct sw_flow_key_range *range, u8 val)
737{
738 u8 *m = (u8 *)&mask->key + range->start;
739
740 mask->range = *range;
741 memset(m, val, range_n_bytes(range));
742}
743
744/**
745 * ovs_nla_get_match - parses Netlink attributes into a flow key and
746 * mask. In case the 'mask' is NULL, the flow is treated as exact match
747 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
748 * does not include any don't care bit.
749 * @match: receives the extracted flow match information.
750 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
751 * sequence. The fields should of the packet that triggered the creation
752 * of this flow.
753 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
754 * attribute specifies the mask field of the wildcarded flow.
755 */
756int ovs_nla_get_match(struct sw_flow_match *match,
757 const struct nlattr *key,
758 const struct nlattr *mask)
759{
760 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
761 const struct nlattr *encap;
762 u64 key_attrs = 0;
763 u64 mask_attrs = 0;
764 bool encap_valid = false;
765 int err;
766
767 err = parse_flow_nlattrs(key, a, &key_attrs);
768 if (err)
769 return err;
770
771 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
772 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
773 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
774 __be16 tci;
775
776 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
777 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
778 OVS_NLERR("Invalid Vlan frame.\n");
779 return -EINVAL;
780 }
781
782 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
783 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
784 encap = a[OVS_KEY_ATTR_ENCAP];
785 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
786 encap_valid = true;
787
788 if (tci & htons(VLAN_TAG_PRESENT)) {
789 err = parse_flow_nlattrs(encap, a, &key_attrs);
790 if (err)
791 return err;
792 } else if (!tci) {
793 /* Corner case for truncated 802.1Q header. */
794 if (nla_len(encap)) {
795 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
796 return -EINVAL;
797 }
798 } else {
799 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
800 return -EINVAL;
801 }
802 }
803
23dabf88 804 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
e6445719
PS
805 if (err)
806 return err;
807
808 if (mask) {
809 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
810 if (err)
811 return err;
812
813 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
814 __be16 eth_type = 0;
815 __be16 tci = 0;
816
817 if (!encap_valid) {
818 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
819 return -EINVAL;
820 }
821
822 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
823 if (a[OVS_KEY_ATTR_ETHERTYPE])
824 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
825
826 if (eth_type == htons(0xffff)) {
827 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
828 encap = a[OVS_KEY_ATTR_ENCAP];
829 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
830 } else {
831 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
832 ntohs(eth_type));
833 return -EINVAL;
834 }
835
836 if (a[OVS_KEY_ATTR_VLAN])
837 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
838
839 if (!(tci & htons(VLAN_TAG_PRESENT))) {
840 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
841 return -EINVAL;
842 }
843 }
844
23dabf88 845 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
e6445719
PS
846 if (err)
847 return err;
848 } else {
849 /* Populate exact match flow's key mask. */
850 if (match->mask)
851 sw_flow_mask_set(match->mask, &match->range, 0xff);
852 }
853
854 if (!match_validate(match, key_attrs, mask_attrs))
855 return -EINVAL;
856
857 return 0;
858}
859
860/**
861 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
83c8df26 862 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
e6445719
PS
863 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
864 * sequence.
865 *
866 * This parses a series of Netlink attributes that form a flow key, which must
867 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
868 * get the metadata, that is, the parts of the flow key that cannot be
869 * extracted from the packet itself.
870 */
871
83c8df26
PS
872int ovs_nla_get_flow_metadata(const struct nlattr *attr,
873 struct sw_flow_key *key)
e6445719 874{
e6445719 875 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
83c8df26 876 struct sw_flow_match match;
e6445719
PS
877 u64 attrs = 0;
878 int err;
e6445719
PS
879
880 err = parse_flow_nlattrs(attr, a, &attrs);
881 if (err)
882 return -EINVAL;
883
884 memset(&match, 0, sizeof(match));
83c8df26 885 match.key = key;
e6445719 886
83c8df26 887 key->phy.in_port = DP_MAX_PORTS;
e6445719 888
83c8df26 889 return metadata_from_nlattrs(&match, &attrs, a, false);
e6445719
PS
890}
891
892int ovs_nla_put_flow(const struct sw_flow_key *swkey,
893 const struct sw_flow_key *output, struct sk_buff *skb)
894{
895 struct ovs_key_ethernet *eth_key;
896 struct nlattr *nla, *encap;
897 bool is_mask = (swkey != output);
898
971427f3
AZ
899 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
900 goto nla_put_failure;
901
902 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
903 goto nla_put_failure;
904
e6445719
PS
905 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
906 goto nla_put_failure;
907
908 if ((swkey->tun_key.ipv4_dst || is_mask) &&
909 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
910 goto nla_put_failure;
911
912 if (swkey->phy.in_port == DP_MAX_PORTS) {
913 if (is_mask && (output->phy.in_port == 0xffff))
914 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
915 goto nla_put_failure;
916 } else {
917 u16 upper_u16;
918 upper_u16 = !is_mask ? 0 : 0xffff;
919
920 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
921 (upper_u16 << 16) | output->phy.in_port))
922 goto nla_put_failure;
923 }
924
925 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
926 goto nla_put_failure;
927
928 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
929 if (!nla)
930 goto nla_put_failure;
931
932 eth_key = nla_data(nla);
8c63ff09
JP
933 ether_addr_copy(eth_key->eth_src, output->eth.src);
934 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
e6445719
PS
935
936 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
937 __be16 eth_type;
938 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
939 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
940 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
941 goto nla_put_failure;
942 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
943 if (!swkey->eth.tci)
944 goto unencap;
945 } else
946 encap = NULL;
947
948 if (swkey->eth.type == htons(ETH_P_802_2)) {
949 /*
950 * Ethertype 802.2 is represented in the netlink with omitted
951 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
952 * 0xffff in the mask attribute. Ethertype can also
953 * be wildcarded.
954 */
955 if (is_mask && output->eth.type)
956 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
957 output->eth.type))
958 goto nla_put_failure;
959 goto unencap;
960 }
961
962 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
963 goto nla_put_failure;
964
965 if (swkey->eth.type == htons(ETH_P_IP)) {
966 struct ovs_key_ipv4 *ipv4_key;
967
968 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
969 if (!nla)
970 goto nla_put_failure;
971 ipv4_key = nla_data(nla);
972 ipv4_key->ipv4_src = output->ipv4.addr.src;
973 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
974 ipv4_key->ipv4_proto = output->ip.proto;
975 ipv4_key->ipv4_tos = output->ip.tos;
976 ipv4_key->ipv4_ttl = output->ip.ttl;
977 ipv4_key->ipv4_frag = output->ip.frag;
978 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
979 struct ovs_key_ipv6 *ipv6_key;
980
981 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
982 if (!nla)
983 goto nla_put_failure;
984 ipv6_key = nla_data(nla);
985 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
986 sizeof(ipv6_key->ipv6_src));
987 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
988 sizeof(ipv6_key->ipv6_dst));
989 ipv6_key->ipv6_label = output->ipv6.label;
990 ipv6_key->ipv6_proto = output->ip.proto;
991 ipv6_key->ipv6_tclass = output->ip.tos;
992 ipv6_key->ipv6_hlimit = output->ip.ttl;
993 ipv6_key->ipv6_frag = output->ip.frag;
994 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
995 swkey->eth.type == htons(ETH_P_RARP)) {
996 struct ovs_key_arp *arp_key;
997
998 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
999 if (!nla)
1000 goto nla_put_failure;
1001 arp_key = nla_data(nla);
1002 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1003 arp_key->arp_sip = output->ipv4.addr.src;
1004 arp_key->arp_tip = output->ipv4.addr.dst;
1005 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
1006 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1007 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
e6445719
PS
1008 }
1009
1010 if ((swkey->eth.type == htons(ETH_P_IP) ||
1011 swkey->eth.type == htons(ETH_P_IPV6)) &&
1012 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1013
1014 if (swkey->ip.proto == IPPROTO_TCP) {
1015 struct ovs_key_tcp *tcp_key;
1016
1017 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1018 if (!nla)
1019 goto nla_put_failure;
1020 tcp_key = nla_data(nla);
1139e241
JR
1021 tcp_key->tcp_src = output->tp.src;
1022 tcp_key->tcp_dst = output->tp.dst;
1023 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1024 output->tp.flags))
1025 goto nla_put_failure;
e6445719
PS
1026 } else if (swkey->ip.proto == IPPROTO_UDP) {
1027 struct ovs_key_udp *udp_key;
1028
1029 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1030 if (!nla)
1031 goto nla_put_failure;
1032 udp_key = nla_data(nla);
1139e241
JR
1033 udp_key->udp_src = output->tp.src;
1034 udp_key->udp_dst = output->tp.dst;
e6445719
PS
1035 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1036 struct ovs_key_sctp *sctp_key;
1037
1038 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1039 if (!nla)
1040 goto nla_put_failure;
1041 sctp_key = nla_data(nla);
1139e241
JR
1042 sctp_key->sctp_src = output->tp.src;
1043 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
1044 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1045 swkey->ip.proto == IPPROTO_ICMP) {
1046 struct ovs_key_icmp *icmp_key;
1047
1048 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1049 if (!nla)
1050 goto nla_put_failure;
1051 icmp_key = nla_data(nla);
1139e241
JR
1052 icmp_key->icmp_type = ntohs(output->tp.src);
1053 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
1054 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1055 swkey->ip.proto == IPPROTO_ICMPV6) {
1056 struct ovs_key_icmpv6 *icmpv6_key;
1057
1058 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1059 sizeof(*icmpv6_key));
1060 if (!nla)
1061 goto nla_put_failure;
1062 icmpv6_key = nla_data(nla);
1139e241
JR
1063 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1064 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
1065
1066 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1067 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1068 struct ovs_key_nd *nd_key;
1069
1070 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1071 if (!nla)
1072 goto nla_put_failure;
1073 nd_key = nla_data(nla);
1074 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1075 sizeof(nd_key->nd_target));
8c63ff09
JP
1076 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1077 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
1078 }
1079 }
1080 }
1081
1082unencap:
1083 if (encap)
1084 nla_nest_end(skb, encap);
1085
1086 return 0;
1087
1088nla_put_failure:
1089 return -EMSGSIZE;
1090}
1091
1092#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1093
1094struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1095{
1096 struct sw_flow_actions *sfa;
1097
1098 if (size > MAX_ACTIONS_BUFSIZE)
1099 return ERR_PTR(-EINVAL);
1100
1101 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1102 if (!sfa)
1103 return ERR_PTR(-ENOMEM);
1104
1105 sfa->actions_len = 0;
1106 return sfa;
1107}
1108
e6445719
PS
1109/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1110 * The caller must hold rcu_read_lock for this to be sensible. */
1111void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1112{
11d6c461 1113 kfree_rcu(sf_acts, rcu);
e6445719
PS
1114}
1115
1116static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1117 int attr_len)
1118{
1119
1120 struct sw_flow_actions *acts;
1121 int new_acts_size;
1122 int req_size = NLA_ALIGN(attr_len);
1123 int next_offset = offsetof(struct sw_flow_actions, actions) +
1124 (*sfa)->actions_len;
1125
1126 if (req_size <= (ksize(*sfa) - next_offset))
1127 goto out;
1128
1129 new_acts_size = ksize(*sfa) * 2;
1130
1131 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1132 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1133 return ERR_PTR(-EMSGSIZE);
1134 new_acts_size = MAX_ACTIONS_BUFSIZE;
1135 }
1136
1137 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1138 if (IS_ERR(acts))
1139 return (void *)acts;
1140
1141 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1142 acts->actions_len = (*sfa)->actions_len;
1143 kfree(*sfa);
1144 *sfa = acts;
1145
1146out:
1147 (*sfa)->actions_len += req_size;
1148 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1149}
1150
1151static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1152{
1153 struct nlattr *a;
1154
1155 a = reserve_sfa_size(sfa, nla_attr_size(len));
1156 if (IS_ERR(a))
1157 return PTR_ERR(a);
1158
1159 a->nla_type = attrtype;
1160 a->nla_len = nla_attr_size(len);
1161
1162 if (data)
1163 memcpy(nla_data(a), data, len);
1164 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1165
1166 return 0;
1167}
1168
1169static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1170 int attrtype)
1171{
1172 int used = (*sfa)->actions_len;
1173 int err;
1174
1175 err = add_action(sfa, attrtype, NULL, 0);
1176 if (err)
1177 return err;
1178
1179 return used;
1180}
1181
1182static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1183 int st_offset)
1184{
1185 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1186 st_offset);
1187
1188 a->nla_len = sfa->actions_len - st_offset;
1189}
1190
1191static int validate_and_copy_sample(const struct nlattr *attr,
1192 const struct sw_flow_key *key, int depth,
1193 struct sw_flow_actions **sfa)
1194{
1195 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1196 const struct nlattr *probability, *actions;
1197 const struct nlattr *a;
1198 int rem, start, err, st_acts;
1199
1200 memset(attrs, 0, sizeof(attrs));
1201 nla_for_each_nested(a, attr, rem) {
1202 int type = nla_type(a);
1203 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1204 return -EINVAL;
1205 attrs[type] = a;
1206 }
1207 if (rem)
1208 return -EINVAL;
1209
1210 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1211 if (!probability || nla_len(probability) != sizeof(u32))
1212 return -EINVAL;
1213
1214 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1215 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1216 return -EINVAL;
1217
1218 /* validation done, copy sample action. */
1219 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1220 if (start < 0)
1221 return start;
1222 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1223 nla_data(probability), sizeof(u32));
1224 if (err)
1225 return err;
1226 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1227 if (st_acts < 0)
1228 return st_acts;
1229
1230 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1231 if (err)
1232 return err;
1233
1234 add_nested_action_end(*sfa, st_acts);
1235 add_nested_action_end(*sfa, start);
1236
1237 return 0;
1238}
1239
1240static int validate_tp_port(const struct sw_flow_key *flow_key)
1241{
1139e241
JR
1242 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1243 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1244 (flow_key->tp.src || flow_key->tp.dst))
1245 return 0;
e6445719
PS
1246
1247 return -EINVAL;
1248}
1249
1250void ovs_match_init(struct sw_flow_match *match,
1251 struct sw_flow_key *key,
1252 struct sw_flow_mask *mask)
1253{
1254 memset(match, 0, sizeof(*match));
1255 match->key = key;
1256 match->mask = mask;
1257
1258 memset(key, 0, sizeof(*key));
1259
1260 if (mask) {
1261 memset(&mask->key, 0, sizeof(mask->key));
1262 mask->range.start = mask->range.end = 0;
1263 }
1264}
1265
1266static int validate_and_copy_set_tun(const struct nlattr *attr,
1267 struct sw_flow_actions **sfa)
1268{
1269 struct sw_flow_match match;
1270 struct sw_flow_key key;
1271 int err, start;
1272
1273 ovs_match_init(&match, &key, NULL);
1274 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1275 if (err)
1276 return err;
1277
1278 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1279 if (start < 0)
1280 return start;
1281
1282 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1283 sizeof(match.key->tun_key));
1284 add_nested_action_end(*sfa, start);
1285
1286 return err;
1287}
1288
1289static int validate_set(const struct nlattr *a,
1290 const struct sw_flow_key *flow_key,
1291 struct sw_flow_actions **sfa,
1292 bool *set_tun)
1293{
1294 const struct nlattr *ovs_key = nla_data(a);
1295 int key_type = nla_type(ovs_key);
1296
1297 /* There can be only one key in a action */
1298 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1299 return -EINVAL;
1300
1301 if (key_type > OVS_KEY_ATTR_MAX ||
1302 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1303 ovs_key_lens[key_type] != -1))
1304 return -EINVAL;
1305
1306 switch (key_type) {
1307 const struct ovs_key_ipv4 *ipv4_key;
1308 const struct ovs_key_ipv6 *ipv6_key;
1309 int err;
1310
1311 case OVS_KEY_ATTR_PRIORITY:
1312 case OVS_KEY_ATTR_SKB_MARK:
1313 case OVS_KEY_ATTR_ETHERNET:
1314 break;
1315
1316 case OVS_KEY_ATTR_TUNNEL:
1317 *set_tun = true;
1318 err = validate_and_copy_set_tun(a, sfa);
1319 if (err)
1320 return err;
1321 break;
1322
1323 case OVS_KEY_ATTR_IPV4:
1324 if (flow_key->eth.type != htons(ETH_P_IP))
1325 return -EINVAL;
1326
1327 if (!flow_key->ip.proto)
1328 return -EINVAL;
1329
1330 ipv4_key = nla_data(ovs_key);
1331 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1332 return -EINVAL;
1333
1334 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1335 return -EINVAL;
1336
1337 break;
1338
1339 case OVS_KEY_ATTR_IPV6:
1340 if (flow_key->eth.type != htons(ETH_P_IPV6))
1341 return -EINVAL;
1342
1343 if (!flow_key->ip.proto)
1344 return -EINVAL;
1345
1346 ipv6_key = nla_data(ovs_key);
1347 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1348 return -EINVAL;
1349
1350 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1351 return -EINVAL;
1352
1353 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1354 return -EINVAL;
1355
1356 break;
1357
1358 case OVS_KEY_ATTR_TCP:
1359 if (flow_key->ip.proto != IPPROTO_TCP)
1360 return -EINVAL;
1361
1362 return validate_tp_port(flow_key);
1363
1364 case OVS_KEY_ATTR_UDP:
1365 if (flow_key->ip.proto != IPPROTO_UDP)
1366 return -EINVAL;
1367
1368 return validate_tp_port(flow_key);
1369
1370 case OVS_KEY_ATTR_SCTP:
1371 if (flow_key->ip.proto != IPPROTO_SCTP)
1372 return -EINVAL;
1373
1374 return validate_tp_port(flow_key);
1375
1376 default:
1377 return -EINVAL;
1378 }
1379
1380 return 0;
1381}
1382
1383static int validate_userspace(const struct nlattr *attr)
1384{
1385 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1386 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1387 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1388 };
1389 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1390 int error;
1391
1392 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1393 attr, userspace_policy);
1394 if (error)
1395 return error;
1396
1397 if (!a[OVS_USERSPACE_ATTR_PID] ||
1398 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1399 return -EINVAL;
1400
1401 return 0;
1402}
1403
1404static int copy_action(const struct nlattr *from,
1405 struct sw_flow_actions **sfa)
1406{
1407 int totlen = NLA_ALIGN(from->nla_len);
1408 struct nlattr *to;
1409
1410 to = reserve_sfa_size(sfa, from->nla_len);
1411 if (IS_ERR(to))
1412 return PTR_ERR(to);
1413
1414 memcpy(to, from, totlen);
1415 return 0;
1416}
1417
1418int ovs_nla_copy_actions(const struct nlattr *attr,
1419 const struct sw_flow_key *key,
1420 int depth,
1421 struct sw_flow_actions **sfa)
1422{
1423 const struct nlattr *a;
1424 int rem, err;
1425
1426 if (depth >= SAMPLE_ACTION_DEPTH)
1427 return -EOVERFLOW;
1428
1429 nla_for_each_nested(a, attr, rem) {
1430 /* Expected argument lengths, (u32)-1 for variable length. */
1431 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1432 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 1433 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719
PS
1434 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1435 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1436 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1437 [OVS_ACTION_ATTR_SET] = (u32)-1,
971427f3
AZ
1438 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1439 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
e6445719
PS
1440 };
1441 const struct ovs_action_push_vlan *vlan;
1442 int type = nla_type(a);
1443 bool skip_copy;
1444
1445 if (type > OVS_ACTION_ATTR_MAX ||
1446 (action_lens[type] != nla_len(a) &&
1447 action_lens[type] != (u32)-1))
1448 return -EINVAL;
1449
1450 skip_copy = false;
1451 switch (type) {
1452 case OVS_ACTION_ATTR_UNSPEC:
1453 return -EINVAL;
1454
1455 case OVS_ACTION_ATTR_USERSPACE:
1456 err = validate_userspace(a);
1457 if (err)
1458 return err;
1459 break;
1460
1461 case OVS_ACTION_ATTR_OUTPUT:
1462 if (nla_get_u32(a) >= DP_MAX_PORTS)
1463 return -EINVAL;
1464 break;
1465
971427f3
AZ
1466 case OVS_ACTION_ATTR_HASH: {
1467 const struct ovs_action_hash *act_hash = nla_data(a);
1468
1469 switch (act_hash->hash_alg) {
1470 case OVS_HASH_ALG_L4:
1471 break;
1472 default:
1473 return -EINVAL;
1474 }
1475
1476 break;
1477 }
e6445719
PS
1478
1479 case OVS_ACTION_ATTR_POP_VLAN:
1480 break;
1481
1482 case OVS_ACTION_ATTR_PUSH_VLAN:
1483 vlan = nla_data(a);
1484 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1485 return -EINVAL;
1486 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1487 return -EINVAL;
1488 break;
1489
971427f3
AZ
1490 case OVS_ACTION_ATTR_RECIRC:
1491 break;
1492
e6445719
PS
1493 case OVS_ACTION_ATTR_SET:
1494 err = validate_set(a, key, sfa, &skip_copy);
1495 if (err)
1496 return err;
1497 break;
1498
1499 case OVS_ACTION_ATTR_SAMPLE:
1500 err = validate_and_copy_sample(a, key, depth, sfa);
1501 if (err)
1502 return err;
1503 skip_copy = true;
1504 break;
1505
1506 default:
1507 return -EINVAL;
1508 }
1509 if (!skip_copy) {
1510 err = copy_action(a, sfa);
1511 if (err)
1512 return err;
1513 }
1514 }
1515
1516 if (rem > 0)
1517 return -EINVAL;
1518
1519 return 0;
1520}
1521
1522static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1523{
1524 const struct nlattr *a;
1525 struct nlattr *start;
1526 int err = 0, rem;
1527
1528 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1529 if (!start)
1530 return -EMSGSIZE;
1531
1532 nla_for_each_nested(a, attr, rem) {
1533 int type = nla_type(a);
1534 struct nlattr *st_sample;
1535
1536 switch (type) {
1537 case OVS_SAMPLE_ATTR_PROBABILITY:
1538 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1539 sizeof(u32), nla_data(a)))
1540 return -EMSGSIZE;
1541 break;
1542 case OVS_SAMPLE_ATTR_ACTIONS:
1543 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1544 if (!st_sample)
1545 return -EMSGSIZE;
1546 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1547 if (err)
1548 return err;
1549 nla_nest_end(skb, st_sample);
1550 break;
1551 }
1552 }
1553
1554 nla_nest_end(skb, start);
1555 return err;
1556}
1557
1558static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1559{
1560 const struct nlattr *ovs_key = nla_data(a);
1561 int key_type = nla_type(ovs_key);
1562 struct nlattr *start;
1563 int err;
1564
1565 switch (key_type) {
1566 case OVS_KEY_ATTR_IPV4_TUNNEL:
1567 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1568 if (!start)
1569 return -EMSGSIZE;
1570
1571 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1572 nla_data(ovs_key));
1573 if (err)
1574 return err;
1575 nla_nest_end(skb, start);
1576 break;
1577 default:
1578 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1579 return -EMSGSIZE;
1580 break;
1581 }
1582
1583 return 0;
1584}
1585
1586int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1587{
1588 const struct nlattr *a;
1589 int rem, err;
1590
1591 nla_for_each_attr(a, attr, len, rem) {
1592 int type = nla_type(a);
1593
1594 switch (type) {
1595 case OVS_ACTION_ATTR_SET:
1596 err = set_action_to_attr(a, skb);
1597 if (err)
1598 return err;
1599 break;
1600
1601 case OVS_ACTION_ATTR_SAMPLE:
1602 err = sample_action_to_attr(a, skb);
1603 if (err)
1604 return err;
1605 break;
1606 default:
1607 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1608 return -EMSGSIZE;
1609 break;
1610 }
1611 }
1612
1613 return 0;
1614}