blob: 5d6194d9dadc79a979aab988a1839450ba5783d4 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
Andy Zhou971427f32014-09-15 19:37:25 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
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
Joe Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#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)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700119 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700120 | (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;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700160 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
161 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700162 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700163 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
164 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700165 }
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;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700195 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
196 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700197 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700198 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
199 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700200 }
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
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700207 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700208 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700209 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700210 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700211 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700212 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",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800221 (unsigned long long)key_attrs, (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700222 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",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800228 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700229 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),
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700247 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -0700248 [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),
Andy Zhou971427f32014-09-15 19:37:25 -0700254 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
255 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -0700256 [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,
Jesse Gross67fa0342014-10-03 15:35:30 -0700349 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -0700350 };
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;
Jesse Gross67fa0342014-10-03 15:35:30 -0700394 case OVS_TUNNEL_KEY_ATTR_OAM:
395 tun_flags |= TUNNEL_OAM;
396 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700397 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 &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700438 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700439 return -EMSGSIZE;
440 if (output->ipv4_dst &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700441 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700442 return -EMSGSIZE;
443 if (output->ipv4_tos &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700444 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700445 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) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700449 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700450 return -EMSGSIZE;
451 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700452 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))
Pravin B Shelare6445712013-10-03 18:16:47 -0700456 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{
Andy Zhou971427f32014-09-15 19:37:25 -0700466 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
Pravin B Shelare6445712013-10-03 18:16:47 -0700480 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
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700515static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
516 const struct nlattr **a, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700517{
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]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700656 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);
Pravin B Shelare6445712013-10-03 18:16:47 -0700658 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
659 }
660
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700661 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
662 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700663 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700664 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
665 is_mask);
666 } else {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700667 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700668 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
669 is_mask);
670 }
671 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
672 }
673
Pravin B Shelare6445712013-10-03 18:16:47 -0700674 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]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700678 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);
Pravin B Shelare6445712013-10-03 18:16:47 -0700680 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]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700687 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);
Pravin B Shelare6445712013-10-03 18:16:47 -0700689 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]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700696 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700697 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700698 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 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]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700707 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700708 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700709 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700710 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
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700804 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700805 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
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700845 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700846 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.
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700862 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -0700863 * @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
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700872int ovs_nla_get_flow_metadata(const struct nlattr *attr,
873 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700874{
Pravin B Shelare6445712013-10-03 18:16:47 -0700875 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700876 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -0700877 u64 attrs = 0;
878 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700879
880 err = parse_flow_nlattrs(attr, a, &attrs);
881 if (err)
882 return -EINVAL;
883
884 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700885 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -0700886
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700887 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -0700888
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700889 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700890}
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
Andy Zhou971427f32014-09-15 19:37:25 -0700899 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
Pravin B Shelare6445712013-10-03 18:16:47 -0700905 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);
Joe Perches8c63ff02014-02-18 11:15:45 -0800933 ether_addr_copy(eth_key->eth_src, output->eth.src);
934 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -0700935
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);
Joe Perches8c63ff02014-02-18 11:15:45 -08001006 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1007 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Pravin B Shelare6445712013-10-03 18:16:47 -07001008 }
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);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001021 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;
Pravin B Shelare6445712013-10-03 18:16:47 -07001026 } 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);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001033 udp_key->udp_src = output->tp.src;
1034 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001035 } 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);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001042 sctp_key->sctp_src = output->tp.src;
1043 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001044 } 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);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001052 icmp_key->icmp_type = ntohs(output->tp.src);
1053 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001054 } 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);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001063 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1064 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001065
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));
Joe Perches8c63ff02014-02-18 11:15:45 -08001076 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1077 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001078 }
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
Pravin B Shelare6445712013-10-03 18:16:47 -07001109/* 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{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001113 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001114}
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
Jesse Grossf0b128c2014-10-03 15:35:31 -07001151static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1152 int attrtype, void *data, int len)
Pravin B Shelare6445712013-10-03 18:16:47 -07001153{
1154 struct nlattr *a;
1155
1156 a = reserve_sfa_size(sfa, nla_attr_size(len));
1157 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001158 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001159
1160 a->nla_type = attrtype;
1161 a->nla_len = nla_attr_size(len);
1162
1163 if (data)
1164 memcpy(nla_data(a), data, len);
1165 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1166
Jesse Grossf0b128c2014-10-03 15:35:31 -07001167 return a;
1168}
1169
1170static int add_action(struct sw_flow_actions **sfa, int attrtype,
1171 void *data, int len)
1172{
1173 struct nlattr *a;
1174
1175 a = __add_action(sfa, attrtype, data, len);
1176 if (IS_ERR(a))
1177 return PTR_ERR(a);
1178
Pravin B Shelare6445712013-10-03 18:16:47 -07001179 return 0;
1180}
1181
1182static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1183 int attrtype)
1184{
1185 int used = (*sfa)->actions_len;
1186 int err;
1187
1188 err = add_action(sfa, attrtype, NULL, 0);
1189 if (err)
1190 return err;
1191
1192 return used;
1193}
1194
1195static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1196 int st_offset)
1197{
1198 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1199 st_offset);
1200
1201 a->nla_len = sfa->actions_len - st_offset;
1202}
1203
1204static int validate_and_copy_sample(const struct nlattr *attr,
1205 const struct sw_flow_key *key, int depth,
1206 struct sw_flow_actions **sfa)
1207{
1208 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1209 const struct nlattr *probability, *actions;
1210 const struct nlattr *a;
1211 int rem, start, err, st_acts;
1212
1213 memset(attrs, 0, sizeof(attrs));
1214 nla_for_each_nested(a, attr, rem) {
1215 int type = nla_type(a);
1216 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1217 return -EINVAL;
1218 attrs[type] = a;
1219 }
1220 if (rem)
1221 return -EINVAL;
1222
1223 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1224 if (!probability || nla_len(probability) != sizeof(u32))
1225 return -EINVAL;
1226
1227 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1228 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1229 return -EINVAL;
1230
1231 /* validation done, copy sample action. */
1232 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1233 if (start < 0)
1234 return start;
1235 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1236 nla_data(probability), sizeof(u32));
1237 if (err)
1238 return err;
1239 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1240 if (st_acts < 0)
1241 return st_acts;
1242
1243 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1244 if (err)
1245 return err;
1246
1247 add_nested_action_end(*sfa, st_acts);
1248 add_nested_action_end(*sfa, start);
1249
1250 return 0;
1251}
1252
1253static int validate_tp_port(const struct sw_flow_key *flow_key)
1254{
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001255 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1256 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1257 (flow_key->tp.src || flow_key->tp.dst))
1258 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001259
1260 return -EINVAL;
1261}
1262
1263void ovs_match_init(struct sw_flow_match *match,
1264 struct sw_flow_key *key,
1265 struct sw_flow_mask *mask)
1266{
1267 memset(match, 0, sizeof(*match));
1268 match->key = key;
1269 match->mask = mask;
1270
1271 memset(key, 0, sizeof(*key));
1272
1273 if (mask) {
1274 memset(&mask->key, 0, sizeof(mask->key));
1275 mask->range.start = mask->range.end = 0;
1276 }
1277}
1278
1279static int validate_and_copy_set_tun(const struct nlattr *attr,
1280 struct sw_flow_actions **sfa)
1281{
1282 struct sw_flow_match match;
1283 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001284 struct ovs_tunnel_info *tun_info;
1285 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001286 int err, start;
1287
1288 ovs_match_init(&match, &key, NULL);
1289 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1290 if (err)
1291 return err;
1292
1293 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1294 if (start < 0)
1295 return start;
1296
Jesse Grossf0b128c2014-10-03 15:35:31 -07001297 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1298 sizeof(*tun_info));
1299 if (IS_ERR(a))
1300 return PTR_ERR(a);
1301
1302 tun_info = nla_data(a);
1303 tun_info->tunnel = key.tun_key;
1304
Pravin B Shelare6445712013-10-03 18:16:47 -07001305 add_nested_action_end(*sfa, start);
1306
1307 return err;
1308}
1309
1310static int validate_set(const struct nlattr *a,
1311 const struct sw_flow_key *flow_key,
1312 struct sw_flow_actions **sfa,
1313 bool *set_tun)
1314{
1315 const struct nlattr *ovs_key = nla_data(a);
1316 int key_type = nla_type(ovs_key);
1317
1318 /* There can be only one key in a action */
1319 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1320 return -EINVAL;
1321
1322 if (key_type > OVS_KEY_ATTR_MAX ||
1323 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1324 ovs_key_lens[key_type] != -1))
1325 return -EINVAL;
1326
1327 switch (key_type) {
1328 const struct ovs_key_ipv4 *ipv4_key;
1329 const struct ovs_key_ipv6 *ipv6_key;
1330 int err;
1331
1332 case OVS_KEY_ATTR_PRIORITY:
1333 case OVS_KEY_ATTR_SKB_MARK:
1334 case OVS_KEY_ATTR_ETHERNET:
1335 break;
1336
1337 case OVS_KEY_ATTR_TUNNEL:
1338 *set_tun = true;
1339 err = validate_and_copy_set_tun(a, sfa);
1340 if (err)
1341 return err;
1342 break;
1343
1344 case OVS_KEY_ATTR_IPV4:
1345 if (flow_key->eth.type != htons(ETH_P_IP))
1346 return -EINVAL;
1347
1348 if (!flow_key->ip.proto)
1349 return -EINVAL;
1350
1351 ipv4_key = nla_data(ovs_key);
1352 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1353 return -EINVAL;
1354
1355 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1356 return -EINVAL;
1357
1358 break;
1359
1360 case OVS_KEY_ATTR_IPV6:
1361 if (flow_key->eth.type != htons(ETH_P_IPV6))
1362 return -EINVAL;
1363
1364 if (!flow_key->ip.proto)
1365 return -EINVAL;
1366
1367 ipv6_key = nla_data(ovs_key);
1368 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1369 return -EINVAL;
1370
1371 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1372 return -EINVAL;
1373
1374 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1375 return -EINVAL;
1376
1377 break;
1378
1379 case OVS_KEY_ATTR_TCP:
1380 if (flow_key->ip.proto != IPPROTO_TCP)
1381 return -EINVAL;
1382
1383 return validate_tp_port(flow_key);
1384
1385 case OVS_KEY_ATTR_UDP:
1386 if (flow_key->ip.proto != IPPROTO_UDP)
1387 return -EINVAL;
1388
1389 return validate_tp_port(flow_key);
1390
1391 case OVS_KEY_ATTR_SCTP:
1392 if (flow_key->ip.proto != IPPROTO_SCTP)
1393 return -EINVAL;
1394
1395 return validate_tp_port(flow_key);
1396
1397 default:
1398 return -EINVAL;
1399 }
1400
1401 return 0;
1402}
1403
1404static int validate_userspace(const struct nlattr *attr)
1405{
1406 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1407 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1408 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1409 };
1410 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1411 int error;
1412
1413 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1414 attr, userspace_policy);
1415 if (error)
1416 return error;
1417
1418 if (!a[OVS_USERSPACE_ATTR_PID] ||
1419 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1420 return -EINVAL;
1421
1422 return 0;
1423}
1424
1425static int copy_action(const struct nlattr *from,
1426 struct sw_flow_actions **sfa)
1427{
1428 int totlen = NLA_ALIGN(from->nla_len);
1429 struct nlattr *to;
1430
1431 to = reserve_sfa_size(sfa, from->nla_len);
1432 if (IS_ERR(to))
1433 return PTR_ERR(to);
1434
1435 memcpy(to, from, totlen);
1436 return 0;
1437}
1438
1439int ovs_nla_copy_actions(const struct nlattr *attr,
1440 const struct sw_flow_key *key,
1441 int depth,
1442 struct sw_flow_actions **sfa)
1443{
1444 const struct nlattr *a;
1445 int rem, err;
1446
1447 if (depth >= SAMPLE_ACTION_DEPTH)
1448 return -EOVERFLOW;
1449
1450 nla_for_each_nested(a, attr, rem) {
1451 /* Expected argument lengths, (u32)-1 for variable length. */
1452 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1453 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001454 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001455 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1456 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1457 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1458 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001459 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1460 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001461 };
1462 const struct ovs_action_push_vlan *vlan;
1463 int type = nla_type(a);
1464 bool skip_copy;
1465
1466 if (type > OVS_ACTION_ATTR_MAX ||
1467 (action_lens[type] != nla_len(a) &&
1468 action_lens[type] != (u32)-1))
1469 return -EINVAL;
1470
1471 skip_copy = false;
1472 switch (type) {
1473 case OVS_ACTION_ATTR_UNSPEC:
1474 return -EINVAL;
1475
1476 case OVS_ACTION_ATTR_USERSPACE:
1477 err = validate_userspace(a);
1478 if (err)
1479 return err;
1480 break;
1481
1482 case OVS_ACTION_ATTR_OUTPUT:
1483 if (nla_get_u32(a) >= DP_MAX_PORTS)
1484 return -EINVAL;
1485 break;
1486
Andy Zhou971427f32014-09-15 19:37:25 -07001487 case OVS_ACTION_ATTR_HASH: {
1488 const struct ovs_action_hash *act_hash = nla_data(a);
1489
1490 switch (act_hash->hash_alg) {
1491 case OVS_HASH_ALG_L4:
1492 break;
1493 default:
1494 return -EINVAL;
1495 }
1496
1497 break;
1498 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001499
1500 case OVS_ACTION_ATTR_POP_VLAN:
1501 break;
1502
1503 case OVS_ACTION_ATTR_PUSH_VLAN:
1504 vlan = nla_data(a);
1505 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1506 return -EINVAL;
1507 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1508 return -EINVAL;
1509 break;
1510
Andy Zhou971427f32014-09-15 19:37:25 -07001511 case OVS_ACTION_ATTR_RECIRC:
1512 break;
1513
Pravin B Shelare6445712013-10-03 18:16:47 -07001514 case OVS_ACTION_ATTR_SET:
1515 err = validate_set(a, key, sfa, &skip_copy);
1516 if (err)
1517 return err;
1518 break;
1519
1520 case OVS_ACTION_ATTR_SAMPLE:
1521 err = validate_and_copy_sample(a, key, depth, sfa);
1522 if (err)
1523 return err;
1524 skip_copy = true;
1525 break;
1526
1527 default:
1528 return -EINVAL;
1529 }
1530 if (!skip_copy) {
1531 err = copy_action(a, sfa);
1532 if (err)
1533 return err;
1534 }
1535 }
1536
1537 if (rem > 0)
1538 return -EINVAL;
1539
1540 return 0;
1541}
1542
1543static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1544{
1545 const struct nlattr *a;
1546 struct nlattr *start;
1547 int err = 0, rem;
1548
1549 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1550 if (!start)
1551 return -EMSGSIZE;
1552
1553 nla_for_each_nested(a, attr, rem) {
1554 int type = nla_type(a);
1555 struct nlattr *st_sample;
1556
1557 switch (type) {
1558 case OVS_SAMPLE_ATTR_PROBABILITY:
1559 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1560 sizeof(u32), nla_data(a)))
1561 return -EMSGSIZE;
1562 break;
1563 case OVS_SAMPLE_ATTR_ACTIONS:
1564 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1565 if (!st_sample)
1566 return -EMSGSIZE;
1567 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1568 if (err)
1569 return err;
1570 nla_nest_end(skb, st_sample);
1571 break;
1572 }
1573 }
1574
1575 nla_nest_end(skb, start);
1576 return err;
1577}
1578
1579static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1580{
1581 const struct nlattr *ovs_key = nla_data(a);
1582 int key_type = nla_type(ovs_key);
1583 struct nlattr *start;
1584 int err;
1585
1586 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001587 case OVS_KEY_ATTR_TUNNEL_INFO: {
1588 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1589
Pravin B Shelare6445712013-10-03 18:16:47 -07001590 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1591 if (!start)
1592 return -EMSGSIZE;
1593
Jesse Grossf0b128c2014-10-03 15:35:31 -07001594 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1595 nla_data(ovs_key));
Pravin B Shelare6445712013-10-03 18:16:47 -07001596 if (err)
1597 return err;
1598 nla_nest_end(skb, start);
1599 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001600 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001601 default:
1602 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1603 return -EMSGSIZE;
1604 break;
1605 }
1606
1607 return 0;
1608}
1609
1610int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1611{
1612 const struct nlattr *a;
1613 int rem, err;
1614
1615 nla_for_each_attr(a, attr, len, rem) {
1616 int type = nla_type(a);
1617
1618 switch (type) {
1619 case OVS_ACTION_ATTR_SET:
1620 err = set_action_to_attr(a, skb);
1621 if (err)
1622 return err;
1623 break;
1624
1625 case OVS_ACTION_ATTR_SAMPLE:
1626 err = sample_action_to_attr(a, skb);
1627 if (err)
1628 return err;
1629 break;
1630 default:
1631 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1632 return -EMSGSIZE;
1633 break;
1634 }
1635 }
1636
1637 return 0;
1638}