blob: 630b320fbf3e9c8d69511c9f4c07d803c6a7fbb0 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
2 * Copyright (c) 2007-2013 Nicira, Inc.
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
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),
254 [OVS_KEY_ATTR_TUNNEL] = -1,
255};
256
257static bool is_all_zero(const u8 *fp, size_t size)
258{
259 int i;
260
261 if (!fp)
262 return false;
263
264 for (i = 0; i < size; i++)
265 if (fp[i])
266 return false;
267
268 return true;
269}
270
271static int __parse_flow_nlattrs(const struct nlattr *attr,
272 const struct nlattr *a[],
273 u64 *attrsp, bool nz)
274{
275 const struct nlattr *nla;
276 u64 attrs;
277 int rem;
278
279 attrs = *attrsp;
280 nla_for_each_nested(nla, attr, rem) {
281 u16 type = nla_type(nla);
282 int expected_len;
283
284 if (type > OVS_KEY_ATTR_MAX) {
285 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
286 type, OVS_KEY_ATTR_MAX);
287 return -EINVAL;
288 }
289
290 if (attrs & (1 << type)) {
291 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
292 return -EINVAL;
293 }
294
295 expected_len = ovs_key_lens[type];
296 if (nla_len(nla) != expected_len && expected_len != -1) {
297 OVS_NLERR("Key attribute has unexpected length (type=%d"
298 ", length=%d, expected=%d).\n", type,
299 nla_len(nla), expected_len);
300 return -EINVAL;
301 }
302
303 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
304 attrs |= 1 << type;
305 a[type] = nla;
306 }
307 }
308 if (rem) {
309 OVS_NLERR("Message has %d unknown bytes.\n", rem);
310 return -EINVAL;
311 }
312
313 *attrsp = attrs;
314 return 0;
315}
316
317static int parse_flow_mask_nlattrs(const struct nlattr *attr,
318 const struct nlattr *a[], u64 *attrsp)
319{
320 return __parse_flow_nlattrs(attr, a, attrsp, true);
321}
322
323static int parse_flow_nlattrs(const struct nlattr *attr,
324 const struct nlattr *a[], u64 *attrsp)
325{
326 return __parse_flow_nlattrs(attr, a, attrsp, false);
327}
328
329static int ipv4_tun_from_nlattr(const struct nlattr *attr,
330 struct sw_flow_match *match, bool is_mask)
331{
332 struct nlattr *a;
333 int rem;
334 bool ttl = false;
335 __be16 tun_flags = 0;
336
337 nla_for_each_nested(a, attr, rem) {
338 int type = nla_type(a);
339 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
340 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
341 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
342 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
343 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
344 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
345 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
346 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
347 };
348
349 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
350 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
351 type, OVS_TUNNEL_KEY_ATTR_MAX);
352 return -EINVAL;
353 }
354
355 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
356 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
357 " length (type=%d, length=%d, expected=%d).\n",
358 type, nla_len(a), ovs_tunnel_key_lens[type]);
359 return -EINVAL;
360 }
361
362 switch (type) {
363 case OVS_TUNNEL_KEY_ATTR_ID:
364 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
365 nla_get_be64(a), is_mask);
366 tun_flags |= TUNNEL_KEY;
367 break;
368 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
369 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
370 nla_get_be32(a), is_mask);
371 break;
372 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
373 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
374 nla_get_be32(a), is_mask);
375 break;
376 case OVS_TUNNEL_KEY_ATTR_TOS:
377 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
378 nla_get_u8(a), is_mask);
379 break;
380 case OVS_TUNNEL_KEY_ATTR_TTL:
381 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
382 nla_get_u8(a), is_mask);
383 ttl = true;
384 break;
385 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
386 tun_flags |= TUNNEL_DONT_FRAGMENT;
387 break;
388 case OVS_TUNNEL_KEY_ATTR_CSUM:
389 tun_flags |= TUNNEL_CSUM;
390 break;
391 default:
392 return -EINVAL;
393 }
394 }
395
396 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
397
398 if (rem > 0) {
399 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
400 return -EINVAL;
401 }
402
403 if (!is_mask) {
404 if (!match->key->tun_key.ipv4_dst) {
405 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
406 return -EINVAL;
407 }
408
409 if (!ttl) {
410 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
411 return -EINVAL;
412 }
413 }
414
415 return 0;
416}
417
418static int ipv4_tun_to_nlattr(struct sk_buff *skb,
419 const struct ovs_key_ipv4_tunnel *tun_key,
420 const struct ovs_key_ipv4_tunnel *output)
421{
422 struct nlattr *nla;
423
424 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
425 if (!nla)
426 return -EMSGSIZE;
427
428 if (output->tun_flags & TUNNEL_KEY &&
429 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
430 return -EMSGSIZE;
431 if (output->ipv4_src &&
432 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
433 return -EMSGSIZE;
434 if (output->ipv4_dst &&
435 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
436 return -EMSGSIZE;
437 if (output->ipv4_tos &&
438 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
439 return -EMSGSIZE;
440 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
441 return -EMSGSIZE;
442 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
443 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
444 return -EMSGSIZE;
445 if ((output->tun_flags & TUNNEL_CSUM) &&
446 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
447 return -EMSGSIZE;
448
449 nla_nest_end(skb, nla);
450 return 0;
451}
452
453
454static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
455 const struct nlattr **a, bool is_mask)
456{
457 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
458 SW_FLOW_KEY_PUT(match, phy.priority,
459 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
460 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
461 }
462
463 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
464 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
465
466 if (is_mask)
467 in_port = 0xffffffff; /* Always exact match in_port. */
468 else if (in_port >= DP_MAX_PORTS)
469 return -EINVAL;
470
471 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
472 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
473 } else if (!is_mask) {
474 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
475 }
476
477 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
478 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
479
480 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
481 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
482 }
483 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
484 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
485 is_mask))
486 return -EINVAL;
487 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
488 }
489 return 0;
490}
491
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700492static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
493 const struct nlattr **a, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700494{
495 int err;
496 u64 orig_attrs = attrs;
497
498 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
499 if (err)
500 return err;
501
502 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
503 const struct ovs_key_ethernet *eth_key;
504
505 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
506 SW_FLOW_KEY_MEMCPY(match, eth.src,
507 eth_key->eth_src, ETH_ALEN, is_mask);
508 SW_FLOW_KEY_MEMCPY(match, eth.dst,
509 eth_key->eth_dst, ETH_ALEN, is_mask);
510 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
511 }
512
513 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
514 __be16 tci;
515
516 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
517 if (!(tci & htons(VLAN_TAG_PRESENT))) {
518 if (is_mask)
519 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
520 else
521 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
522
523 return -EINVAL;
524 }
525
526 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
527 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
528 } else if (!is_mask)
529 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
530
531 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
532 __be16 eth_type;
533
534 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
535 if (is_mask) {
536 /* Always exact match EtherType. */
537 eth_type = htons(0xffff);
538 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
539 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
540 ntohs(eth_type), ETH_P_802_3_MIN);
541 return -EINVAL;
542 }
543
544 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
545 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
546 } else if (!is_mask) {
547 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
548 }
549
550 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
551 const struct ovs_key_ipv4 *ipv4_key;
552
553 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
554 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
555 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
556 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
557 return -EINVAL;
558 }
559 SW_FLOW_KEY_PUT(match, ip.proto,
560 ipv4_key->ipv4_proto, is_mask);
561 SW_FLOW_KEY_PUT(match, ip.tos,
562 ipv4_key->ipv4_tos, is_mask);
563 SW_FLOW_KEY_PUT(match, ip.ttl,
564 ipv4_key->ipv4_ttl, is_mask);
565 SW_FLOW_KEY_PUT(match, ip.frag,
566 ipv4_key->ipv4_frag, is_mask);
567 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
568 ipv4_key->ipv4_src, is_mask);
569 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
570 ipv4_key->ipv4_dst, is_mask);
571 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
572 }
573
574 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
575 const struct ovs_key_ipv6 *ipv6_key;
576
577 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
578 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
579 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
580 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
581 return -EINVAL;
582 }
583 SW_FLOW_KEY_PUT(match, ipv6.label,
584 ipv6_key->ipv6_label, is_mask);
585 SW_FLOW_KEY_PUT(match, ip.proto,
586 ipv6_key->ipv6_proto, is_mask);
587 SW_FLOW_KEY_PUT(match, ip.tos,
588 ipv6_key->ipv6_tclass, is_mask);
589 SW_FLOW_KEY_PUT(match, ip.ttl,
590 ipv6_key->ipv6_hlimit, is_mask);
591 SW_FLOW_KEY_PUT(match, ip.frag,
592 ipv6_key->ipv6_frag, is_mask);
593 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
594 ipv6_key->ipv6_src,
595 sizeof(match->key->ipv6.addr.src),
596 is_mask);
597 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
598 ipv6_key->ipv6_dst,
599 sizeof(match->key->ipv6.addr.dst),
600 is_mask);
601
602 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
603 }
604
605 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
606 const struct ovs_key_arp *arp_key;
607
608 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
609 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
610 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
611 arp_key->arp_op);
612 return -EINVAL;
613 }
614
615 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
616 arp_key->arp_sip, is_mask);
617 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
618 arp_key->arp_tip, is_mask);
619 SW_FLOW_KEY_PUT(match, ip.proto,
620 ntohs(arp_key->arp_op), is_mask);
621 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
622 arp_key->arp_sha, ETH_ALEN, is_mask);
623 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
624 arp_key->arp_tha, ETH_ALEN, is_mask);
625
626 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
627 }
628
629 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
630 const struct ovs_key_tcp *tcp_key;
631
632 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700633 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
634 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700635 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
636 }
637
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700638 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
639 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700640 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700641 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
642 is_mask);
643 } else {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700644 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700645 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
646 is_mask);
647 }
648 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
649 }
650
Pravin B Shelare6445712013-10-03 18:16:47 -0700651 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
652 const struct ovs_key_udp *udp_key;
653
654 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700655 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
656 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700657 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
658 }
659
660 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
661 const struct ovs_key_sctp *sctp_key;
662
663 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700664 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
665 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700666 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
667 }
668
669 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
670 const struct ovs_key_icmp *icmp_key;
671
672 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700673 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700674 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700675 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700676 htons(icmp_key->icmp_code), is_mask);
677 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
678 }
679
680 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
681 const struct ovs_key_icmpv6 *icmpv6_key;
682
683 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700684 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700685 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700686 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700687 htons(icmpv6_key->icmpv6_code), is_mask);
688 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
689 }
690
691 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
692 const struct ovs_key_nd *nd_key;
693
694 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
695 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
696 nd_key->nd_target,
697 sizeof(match->key->ipv6.nd.target),
698 is_mask);
699 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
700 nd_key->nd_sll, ETH_ALEN, is_mask);
701 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
702 nd_key->nd_tll, ETH_ALEN, is_mask);
703 attrs &= ~(1 << OVS_KEY_ATTR_ND);
704 }
705
706 if (attrs != 0)
707 return -EINVAL;
708
709 return 0;
710}
711
712static void sw_flow_mask_set(struct sw_flow_mask *mask,
713 struct sw_flow_key_range *range, u8 val)
714{
715 u8 *m = (u8 *)&mask->key + range->start;
716
717 mask->range = *range;
718 memset(m, val, range_n_bytes(range));
719}
720
721/**
722 * ovs_nla_get_match - parses Netlink attributes into a flow key and
723 * mask. In case the 'mask' is NULL, the flow is treated as exact match
724 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
725 * does not include any don't care bit.
726 * @match: receives the extracted flow match information.
727 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
728 * sequence. The fields should of the packet that triggered the creation
729 * of this flow.
730 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
731 * attribute specifies the mask field of the wildcarded flow.
732 */
733int ovs_nla_get_match(struct sw_flow_match *match,
734 const struct nlattr *key,
735 const struct nlattr *mask)
736{
737 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
738 const struct nlattr *encap;
739 u64 key_attrs = 0;
740 u64 mask_attrs = 0;
741 bool encap_valid = false;
742 int err;
743
744 err = parse_flow_nlattrs(key, a, &key_attrs);
745 if (err)
746 return err;
747
748 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
749 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
750 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
751 __be16 tci;
752
753 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
754 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
755 OVS_NLERR("Invalid Vlan frame.\n");
756 return -EINVAL;
757 }
758
759 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
760 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
761 encap = a[OVS_KEY_ATTR_ENCAP];
762 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
763 encap_valid = true;
764
765 if (tci & htons(VLAN_TAG_PRESENT)) {
766 err = parse_flow_nlattrs(encap, a, &key_attrs);
767 if (err)
768 return err;
769 } else if (!tci) {
770 /* Corner case for truncated 802.1Q header. */
771 if (nla_len(encap)) {
772 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
773 return -EINVAL;
774 }
775 } else {
776 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
777 return -EINVAL;
778 }
779 }
780
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700781 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700782 if (err)
783 return err;
784
785 if (mask) {
786 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
787 if (err)
788 return err;
789
790 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
791 __be16 eth_type = 0;
792 __be16 tci = 0;
793
794 if (!encap_valid) {
795 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
796 return -EINVAL;
797 }
798
799 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
800 if (a[OVS_KEY_ATTR_ETHERTYPE])
801 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
802
803 if (eth_type == htons(0xffff)) {
804 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
805 encap = a[OVS_KEY_ATTR_ENCAP];
806 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
807 } else {
808 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
809 ntohs(eth_type));
810 return -EINVAL;
811 }
812
813 if (a[OVS_KEY_ATTR_VLAN])
814 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
815
816 if (!(tci & htons(VLAN_TAG_PRESENT))) {
817 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
818 return -EINVAL;
819 }
820 }
821
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700822 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700823 if (err)
824 return err;
825 } else {
826 /* Populate exact match flow's key mask. */
827 if (match->mask)
828 sw_flow_mask_set(match->mask, &match->range, 0xff);
829 }
830
831 if (!match_validate(match, key_attrs, mask_attrs))
832 return -EINVAL;
833
834 return 0;
835}
836
837/**
838 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700839 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -0700840 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
841 * sequence.
842 *
843 * This parses a series of Netlink attributes that form a flow key, which must
844 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
845 * get the metadata, that is, the parts of the flow key that cannot be
846 * extracted from the packet itself.
847 */
848
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700849int ovs_nla_get_flow_metadata(const struct nlattr *attr,
850 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700851{
Pravin B Shelare6445712013-10-03 18:16:47 -0700852 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700853 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -0700854 u64 attrs = 0;
855 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700856
857 err = parse_flow_nlattrs(attr, a, &attrs);
858 if (err)
859 return -EINVAL;
860
861 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700862 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -0700863
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700864 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -0700865
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700866 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700867}
868
869int ovs_nla_put_flow(const struct sw_flow_key *swkey,
870 const struct sw_flow_key *output, struct sk_buff *skb)
871{
872 struct ovs_key_ethernet *eth_key;
873 struct nlattr *nla, *encap;
874 bool is_mask = (swkey != output);
875
876 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
877 goto nla_put_failure;
878
879 if ((swkey->tun_key.ipv4_dst || is_mask) &&
880 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
881 goto nla_put_failure;
882
883 if (swkey->phy.in_port == DP_MAX_PORTS) {
884 if (is_mask && (output->phy.in_port == 0xffff))
885 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
886 goto nla_put_failure;
887 } else {
888 u16 upper_u16;
889 upper_u16 = !is_mask ? 0 : 0xffff;
890
891 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
892 (upper_u16 << 16) | output->phy.in_port))
893 goto nla_put_failure;
894 }
895
896 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
897 goto nla_put_failure;
898
899 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
900 if (!nla)
901 goto nla_put_failure;
902
903 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -0800904 ether_addr_copy(eth_key->eth_src, output->eth.src);
905 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -0700906
907 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
908 __be16 eth_type;
909 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
910 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
911 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
912 goto nla_put_failure;
913 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
914 if (!swkey->eth.tci)
915 goto unencap;
916 } else
917 encap = NULL;
918
919 if (swkey->eth.type == htons(ETH_P_802_2)) {
920 /*
921 * Ethertype 802.2 is represented in the netlink with omitted
922 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
923 * 0xffff in the mask attribute. Ethertype can also
924 * be wildcarded.
925 */
926 if (is_mask && output->eth.type)
927 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
928 output->eth.type))
929 goto nla_put_failure;
930 goto unencap;
931 }
932
933 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
934 goto nla_put_failure;
935
936 if (swkey->eth.type == htons(ETH_P_IP)) {
937 struct ovs_key_ipv4 *ipv4_key;
938
939 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
940 if (!nla)
941 goto nla_put_failure;
942 ipv4_key = nla_data(nla);
943 ipv4_key->ipv4_src = output->ipv4.addr.src;
944 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
945 ipv4_key->ipv4_proto = output->ip.proto;
946 ipv4_key->ipv4_tos = output->ip.tos;
947 ipv4_key->ipv4_ttl = output->ip.ttl;
948 ipv4_key->ipv4_frag = output->ip.frag;
949 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
950 struct ovs_key_ipv6 *ipv6_key;
951
952 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
953 if (!nla)
954 goto nla_put_failure;
955 ipv6_key = nla_data(nla);
956 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
957 sizeof(ipv6_key->ipv6_src));
958 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
959 sizeof(ipv6_key->ipv6_dst));
960 ipv6_key->ipv6_label = output->ipv6.label;
961 ipv6_key->ipv6_proto = output->ip.proto;
962 ipv6_key->ipv6_tclass = output->ip.tos;
963 ipv6_key->ipv6_hlimit = output->ip.ttl;
964 ipv6_key->ipv6_frag = output->ip.frag;
965 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
966 swkey->eth.type == htons(ETH_P_RARP)) {
967 struct ovs_key_arp *arp_key;
968
969 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
970 if (!nla)
971 goto nla_put_failure;
972 arp_key = nla_data(nla);
973 memset(arp_key, 0, sizeof(struct ovs_key_arp));
974 arp_key->arp_sip = output->ipv4.addr.src;
975 arp_key->arp_tip = output->ipv4.addr.dst;
976 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -0800977 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
978 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Pravin B Shelare6445712013-10-03 18:16:47 -0700979 }
980
981 if ((swkey->eth.type == htons(ETH_P_IP) ||
982 swkey->eth.type == htons(ETH_P_IPV6)) &&
983 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
984
985 if (swkey->ip.proto == IPPROTO_TCP) {
986 struct ovs_key_tcp *tcp_key;
987
988 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
989 if (!nla)
990 goto nla_put_failure;
991 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700992 tcp_key->tcp_src = output->tp.src;
993 tcp_key->tcp_dst = output->tp.dst;
994 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
995 output->tp.flags))
996 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -0700997 } else if (swkey->ip.proto == IPPROTO_UDP) {
998 struct ovs_key_udp *udp_key;
999
1000 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1001 if (!nla)
1002 goto nla_put_failure;
1003 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001004 udp_key->udp_src = output->tp.src;
1005 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001006 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1007 struct ovs_key_sctp *sctp_key;
1008
1009 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1010 if (!nla)
1011 goto nla_put_failure;
1012 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001013 sctp_key->sctp_src = output->tp.src;
1014 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001015 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1016 swkey->ip.proto == IPPROTO_ICMP) {
1017 struct ovs_key_icmp *icmp_key;
1018
1019 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1020 if (!nla)
1021 goto nla_put_failure;
1022 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001023 icmp_key->icmp_type = ntohs(output->tp.src);
1024 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001025 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1026 swkey->ip.proto == IPPROTO_ICMPV6) {
1027 struct ovs_key_icmpv6 *icmpv6_key;
1028
1029 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1030 sizeof(*icmpv6_key));
1031 if (!nla)
1032 goto nla_put_failure;
1033 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001034 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1035 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001036
1037 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1038 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1039 struct ovs_key_nd *nd_key;
1040
1041 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1042 if (!nla)
1043 goto nla_put_failure;
1044 nd_key = nla_data(nla);
1045 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1046 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001047 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1048 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001049 }
1050 }
1051 }
1052
1053unencap:
1054 if (encap)
1055 nla_nest_end(skb, encap);
1056
1057 return 0;
1058
1059nla_put_failure:
1060 return -EMSGSIZE;
1061}
1062
1063#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1064
1065struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1066{
1067 struct sw_flow_actions *sfa;
1068
1069 if (size > MAX_ACTIONS_BUFSIZE)
1070 return ERR_PTR(-EINVAL);
1071
1072 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1073 if (!sfa)
1074 return ERR_PTR(-ENOMEM);
1075
1076 sfa->actions_len = 0;
1077 return sfa;
1078}
1079
Pravin B Shelare6445712013-10-03 18:16:47 -07001080/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1081 * The caller must hold rcu_read_lock for this to be sensible. */
1082void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1083{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001084 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001085}
1086
1087static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1088 int attr_len)
1089{
1090
1091 struct sw_flow_actions *acts;
1092 int new_acts_size;
1093 int req_size = NLA_ALIGN(attr_len);
1094 int next_offset = offsetof(struct sw_flow_actions, actions) +
1095 (*sfa)->actions_len;
1096
1097 if (req_size <= (ksize(*sfa) - next_offset))
1098 goto out;
1099
1100 new_acts_size = ksize(*sfa) * 2;
1101
1102 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1103 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1104 return ERR_PTR(-EMSGSIZE);
1105 new_acts_size = MAX_ACTIONS_BUFSIZE;
1106 }
1107
1108 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1109 if (IS_ERR(acts))
1110 return (void *)acts;
1111
1112 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1113 acts->actions_len = (*sfa)->actions_len;
1114 kfree(*sfa);
1115 *sfa = acts;
1116
1117out:
1118 (*sfa)->actions_len += req_size;
1119 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1120}
1121
1122static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1123{
1124 struct nlattr *a;
1125
1126 a = reserve_sfa_size(sfa, nla_attr_size(len));
1127 if (IS_ERR(a))
1128 return PTR_ERR(a);
1129
1130 a->nla_type = attrtype;
1131 a->nla_len = nla_attr_size(len);
1132
1133 if (data)
1134 memcpy(nla_data(a), data, len);
1135 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1136
1137 return 0;
1138}
1139
1140static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1141 int attrtype)
1142{
1143 int used = (*sfa)->actions_len;
1144 int err;
1145
1146 err = add_action(sfa, attrtype, NULL, 0);
1147 if (err)
1148 return err;
1149
1150 return used;
1151}
1152
1153static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1154 int st_offset)
1155{
1156 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1157 st_offset);
1158
1159 a->nla_len = sfa->actions_len - st_offset;
1160}
1161
1162static int validate_and_copy_sample(const struct nlattr *attr,
1163 const struct sw_flow_key *key, int depth,
1164 struct sw_flow_actions **sfa)
1165{
1166 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1167 const struct nlattr *probability, *actions;
1168 const struct nlattr *a;
1169 int rem, start, err, st_acts;
1170
1171 memset(attrs, 0, sizeof(attrs));
1172 nla_for_each_nested(a, attr, rem) {
1173 int type = nla_type(a);
1174 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1175 return -EINVAL;
1176 attrs[type] = a;
1177 }
1178 if (rem)
1179 return -EINVAL;
1180
1181 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1182 if (!probability || nla_len(probability) != sizeof(u32))
1183 return -EINVAL;
1184
1185 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1186 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1187 return -EINVAL;
1188
1189 /* validation done, copy sample action. */
1190 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1191 if (start < 0)
1192 return start;
1193 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1194 nla_data(probability), sizeof(u32));
1195 if (err)
1196 return err;
1197 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1198 if (st_acts < 0)
1199 return st_acts;
1200
1201 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1202 if (err)
1203 return err;
1204
1205 add_nested_action_end(*sfa, st_acts);
1206 add_nested_action_end(*sfa, start);
1207
1208 return 0;
1209}
1210
1211static int validate_tp_port(const struct sw_flow_key *flow_key)
1212{
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001213 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1214 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1215 (flow_key->tp.src || flow_key->tp.dst))
1216 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001217
1218 return -EINVAL;
1219}
1220
1221void ovs_match_init(struct sw_flow_match *match,
1222 struct sw_flow_key *key,
1223 struct sw_flow_mask *mask)
1224{
1225 memset(match, 0, sizeof(*match));
1226 match->key = key;
1227 match->mask = mask;
1228
1229 memset(key, 0, sizeof(*key));
1230
1231 if (mask) {
1232 memset(&mask->key, 0, sizeof(mask->key));
1233 mask->range.start = mask->range.end = 0;
1234 }
1235}
1236
1237static int validate_and_copy_set_tun(const struct nlattr *attr,
1238 struct sw_flow_actions **sfa)
1239{
1240 struct sw_flow_match match;
1241 struct sw_flow_key key;
1242 int err, start;
1243
1244 ovs_match_init(&match, &key, NULL);
1245 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1246 if (err)
1247 return err;
1248
1249 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1250 if (start < 0)
1251 return start;
1252
1253 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1254 sizeof(match.key->tun_key));
1255 add_nested_action_end(*sfa, start);
1256
1257 return err;
1258}
1259
1260static int validate_set(const struct nlattr *a,
1261 const struct sw_flow_key *flow_key,
1262 struct sw_flow_actions **sfa,
1263 bool *set_tun)
1264{
1265 const struct nlattr *ovs_key = nla_data(a);
1266 int key_type = nla_type(ovs_key);
1267
1268 /* There can be only one key in a action */
1269 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1270 return -EINVAL;
1271
1272 if (key_type > OVS_KEY_ATTR_MAX ||
1273 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1274 ovs_key_lens[key_type] != -1))
1275 return -EINVAL;
1276
1277 switch (key_type) {
1278 const struct ovs_key_ipv4 *ipv4_key;
1279 const struct ovs_key_ipv6 *ipv6_key;
1280 int err;
1281
1282 case OVS_KEY_ATTR_PRIORITY:
1283 case OVS_KEY_ATTR_SKB_MARK:
1284 case OVS_KEY_ATTR_ETHERNET:
1285 break;
1286
1287 case OVS_KEY_ATTR_TUNNEL:
1288 *set_tun = true;
1289 err = validate_and_copy_set_tun(a, sfa);
1290 if (err)
1291 return err;
1292 break;
1293
1294 case OVS_KEY_ATTR_IPV4:
1295 if (flow_key->eth.type != htons(ETH_P_IP))
1296 return -EINVAL;
1297
1298 if (!flow_key->ip.proto)
1299 return -EINVAL;
1300
1301 ipv4_key = nla_data(ovs_key);
1302 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1303 return -EINVAL;
1304
1305 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1306 return -EINVAL;
1307
1308 break;
1309
1310 case OVS_KEY_ATTR_IPV6:
1311 if (flow_key->eth.type != htons(ETH_P_IPV6))
1312 return -EINVAL;
1313
1314 if (!flow_key->ip.proto)
1315 return -EINVAL;
1316
1317 ipv6_key = nla_data(ovs_key);
1318 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1319 return -EINVAL;
1320
1321 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1322 return -EINVAL;
1323
1324 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1325 return -EINVAL;
1326
1327 break;
1328
1329 case OVS_KEY_ATTR_TCP:
1330 if (flow_key->ip.proto != IPPROTO_TCP)
1331 return -EINVAL;
1332
1333 return validate_tp_port(flow_key);
1334
1335 case OVS_KEY_ATTR_UDP:
1336 if (flow_key->ip.proto != IPPROTO_UDP)
1337 return -EINVAL;
1338
1339 return validate_tp_port(flow_key);
1340
1341 case OVS_KEY_ATTR_SCTP:
1342 if (flow_key->ip.proto != IPPROTO_SCTP)
1343 return -EINVAL;
1344
1345 return validate_tp_port(flow_key);
1346
1347 default:
1348 return -EINVAL;
1349 }
1350
1351 return 0;
1352}
1353
1354static int validate_userspace(const struct nlattr *attr)
1355{
1356 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1357 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1358 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1359 };
1360 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1361 int error;
1362
1363 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1364 attr, userspace_policy);
1365 if (error)
1366 return error;
1367
1368 if (!a[OVS_USERSPACE_ATTR_PID] ||
1369 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1370 return -EINVAL;
1371
1372 return 0;
1373}
1374
1375static int copy_action(const struct nlattr *from,
1376 struct sw_flow_actions **sfa)
1377{
1378 int totlen = NLA_ALIGN(from->nla_len);
1379 struct nlattr *to;
1380
1381 to = reserve_sfa_size(sfa, from->nla_len);
1382 if (IS_ERR(to))
1383 return PTR_ERR(to);
1384
1385 memcpy(to, from, totlen);
1386 return 0;
1387}
1388
1389int ovs_nla_copy_actions(const struct nlattr *attr,
1390 const struct sw_flow_key *key,
1391 int depth,
1392 struct sw_flow_actions **sfa)
1393{
1394 const struct nlattr *a;
1395 int rem, err;
1396
1397 if (depth >= SAMPLE_ACTION_DEPTH)
1398 return -EOVERFLOW;
1399
1400 nla_for_each_nested(a, attr, rem) {
1401 /* Expected argument lengths, (u32)-1 for variable length. */
1402 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1403 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1404 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1405 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1406 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1407 [OVS_ACTION_ATTR_SET] = (u32)-1,
1408 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1409 };
1410 const struct ovs_action_push_vlan *vlan;
1411 int type = nla_type(a);
1412 bool skip_copy;
1413
1414 if (type > OVS_ACTION_ATTR_MAX ||
1415 (action_lens[type] != nla_len(a) &&
1416 action_lens[type] != (u32)-1))
1417 return -EINVAL;
1418
1419 skip_copy = false;
1420 switch (type) {
1421 case OVS_ACTION_ATTR_UNSPEC:
1422 return -EINVAL;
1423
1424 case OVS_ACTION_ATTR_USERSPACE:
1425 err = validate_userspace(a);
1426 if (err)
1427 return err;
1428 break;
1429
1430 case OVS_ACTION_ATTR_OUTPUT:
1431 if (nla_get_u32(a) >= DP_MAX_PORTS)
1432 return -EINVAL;
1433 break;
1434
1435
1436 case OVS_ACTION_ATTR_POP_VLAN:
1437 break;
1438
1439 case OVS_ACTION_ATTR_PUSH_VLAN:
1440 vlan = nla_data(a);
1441 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1442 return -EINVAL;
1443 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1444 return -EINVAL;
1445 break;
1446
1447 case OVS_ACTION_ATTR_SET:
1448 err = validate_set(a, key, sfa, &skip_copy);
1449 if (err)
1450 return err;
1451 break;
1452
1453 case OVS_ACTION_ATTR_SAMPLE:
1454 err = validate_and_copy_sample(a, key, depth, sfa);
1455 if (err)
1456 return err;
1457 skip_copy = true;
1458 break;
1459
1460 default:
1461 return -EINVAL;
1462 }
1463 if (!skip_copy) {
1464 err = copy_action(a, sfa);
1465 if (err)
1466 return err;
1467 }
1468 }
1469
1470 if (rem > 0)
1471 return -EINVAL;
1472
1473 return 0;
1474}
1475
1476static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1477{
1478 const struct nlattr *a;
1479 struct nlattr *start;
1480 int err = 0, rem;
1481
1482 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1483 if (!start)
1484 return -EMSGSIZE;
1485
1486 nla_for_each_nested(a, attr, rem) {
1487 int type = nla_type(a);
1488 struct nlattr *st_sample;
1489
1490 switch (type) {
1491 case OVS_SAMPLE_ATTR_PROBABILITY:
1492 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1493 sizeof(u32), nla_data(a)))
1494 return -EMSGSIZE;
1495 break;
1496 case OVS_SAMPLE_ATTR_ACTIONS:
1497 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1498 if (!st_sample)
1499 return -EMSGSIZE;
1500 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1501 if (err)
1502 return err;
1503 nla_nest_end(skb, st_sample);
1504 break;
1505 }
1506 }
1507
1508 nla_nest_end(skb, start);
1509 return err;
1510}
1511
1512static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1513{
1514 const struct nlattr *ovs_key = nla_data(a);
1515 int key_type = nla_type(ovs_key);
1516 struct nlattr *start;
1517 int err;
1518
1519 switch (key_type) {
1520 case OVS_KEY_ATTR_IPV4_TUNNEL:
1521 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1522 if (!start)
1523 return -EMSGSIZE;
1524
1525 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1526 nla_data(ovs_key));
1527 if (err)
1528 return err;
1529 nla_nest_end(skb, start);
1530 break;
1531 default:
1532 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1533 return -EMSGSIZE;
1534 break;
1535 }
1536
1537 return 0;
1538}
1539
1540int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1541{
1542 const struct nlattr *a;
1543 int rem, err;
1544
1545 nla_for_each_attr(a, attr, len, rem) {
1546 int type = nla_type(a);
1547
1548 switch (type) {
1549 case OVS_ACTION_ATTR_SET:
1550 err = set_action_to_attr(a, skb);
1551 if (err)
1552 return err;
1553 break;
1554
1555 case OVS_ACTION_ATTR_SAMPLE:
1556 err = sample_action_to_attr(a, skb);
1557 if (err)
1558 return err;
1559 break;
1560 default:
1561 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1562 return -EMSGSIZE;
1563 break;
1564 }
1565 }
1566
1567 return 0;
1568}