blob: 569309c49cc0bb4284582fe80964f2be52599424 [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>
Jesse Grossf5796682014-10-03 15:35:33 -070045#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070046#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070049#include <net/mpls.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070050
51#include "flow_netlink.h"
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{
56 struct sw_flow_key_range *range = NULL;
57 size_t start = rounddown(offset, sizeof(long));
58 size_t end = roundup(offset + size, sizeof(long));
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76
77 if (range->end < end)
78 range->end = end;
79}
80
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask) { \
86 if ((match)->mask) \
87 (match)->mask->key.field = value; \
88 } else { \
89 (match)->key->field = value; \
90 } \
91 } while (0)
92
Jesse Grossf5796682014-10-03 15:35:33 -070093#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
94 do { \
95 update_range__(match, offset, len, is_mask); \
96 if (is_mask) \
97 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
98 len); \
99 else \
100 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700101 } while (0)
102
Jesse Grossf5796682014-10-03 15:35:33 -0700103#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
104 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
105 value_p, len, is_mask)
106
Pravin B Shelarf47de062014-10-16 21:55:45 -0700107#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
108 do { \
109 update_range__(match, offsetof(struct sw_flow_key, field), \
110 sizeof((match)->key->field), is_mask); \
111 if (is_mask) { \
112 if ((match)->mask) \
113 memset((u8 *)&(match)->mask->key.field, value,\
114 sizeof((match)->mask->key.field)); \
115 } else { \
116 memset((u8 *)&(match)->key->field, value, \
117 sizeof((match)->key->field)); \
118 } \
119 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700120
121static bool match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs)
123{
124 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
126
127 /* The following mask attributes allowed only if they
128 * pass the validation tests. */
129 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
130 | (1 << OVS_KEY_ATTR_IPV6)
131 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700132 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700133 | (1 << OVS_KEY_ATTR_UDP)
134 | (1 << OVS_KEY_ATTR_SCTP)
135 | (1 << OVS_KEY_ATTR_ICMP)
136 | (1 << OVS_KEY_ATTR_ICMPV6)
137 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700138 | (1 << OVS_KEY_ATTR_ND)
139 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700140
141 /* Always allowed mask fields. */
142 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
143 | (1 << OVS_KEY_ATTR_IN_PORT)
144 | (1 << OVS_KEY_ATTR_ETHERTYPE));
145
146 /* Check key attributes. */
147 if (match->key->eth.type == htons(ETH_P_ARP)
148 || match->key->eth.type == htons(ETH_P_RARP)) {
149 key_expected |= 1 << OVS_KEY_ATTR_ARP;
150 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
152 }
153
Simon Horman25cd9ba2014-10-06 05:05:13 -0700154 if (eth_p_mpls(match->key->eth.type)) {
155 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
156 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
157 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
158 }
159
Pravin B Shelare6445712013-10-03 18:16:47 -0700160 if (match->key->eth.type == htons(ETH_P_IP)) {
161 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
162 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
163 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
164
165 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
166 if (match->key->ip.proto == IPPROTO_UDP) {
167 key_expected |= 1 << OVS_KEY_ATTR_UDP;
168 if (match->mask && (match->mask->key.ip.proto == 0xff))
169 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
170 }
171
172 if (match->key->ip.proto == IPPROTO_SCTP) {
173 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
174 if (match->mask && (match->mask->key.ip.proto == 0xff))
175 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
176 }
177
178 if (match->key->ip.proto == IPPROTO_TCP) {
179 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700180 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
181 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700182 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700183 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
184 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700185 }
186
187 if (match->key->ip.proto == IPPROTO_ICMP) {
188 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
191 }
192 }
193 }
194
195 if (match->key->eth.type == htons(ETH_P_IPV6)) {
196 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
197 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
198 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
199
200 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
201 if (match->key->ip.proto == IPPROTO_UDP) {
202 key_expected |= 1 << OVS_KEY_ATTR_UDP;
203 if (match->mask && (match->mask->key.ip.proto == 0xff))
204 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
205 }
206
207 if (match->key->ip.proto == IPPROTO_SCTP) {
208 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_TCP) {
214 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700215 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
216 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700217 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700218 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
219 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700220 }
221
222 if (match->key->ip.proto == IPPROTO_ICMPV6) {
223 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
224 if (match->mask && (match->mask->key.ip.proto == 0xff))
225 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
226
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700227 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700228 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700229 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700230 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700231 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700232 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
233 }
234 }
235 }
236 }
237
238 if ((key_attrs & key_expected) != key_expected) {
239 /* Key attributes check failed. */
240 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800241 (unsigned long long)key_attrs, (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700242 return false;
243 }
244
245 if ((mask_attrs & mask_allowed) != mask_attrs) {
246 /* Mask attributes check failed. */
247 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800248 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700249 return false;
250 }
251
252 return true;
253}
254
255/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
256static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
257 [OVS_KEY_ATTR_ENCAP] = -1,
258 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
259 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
260 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
261 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
262 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
263 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
264 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
265 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
266 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700267 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -0700268 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
269 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
270 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
271 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
272 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
273 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Andy Zhou971427f32014-09-15 19:37:25 -0700274 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
275 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -0700276 [OVS_KEY_ATTR_TUNNEL] = -1,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700277 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
Pravin B Shelare6445712013-10-03 18:16:47 -0700278};
279
280static bool is_all_zero(const u8 *fp, size_t size)
281{
282 int i;
283
284 if (!fp)
285 return false;
286
287 for (i = 0; i < size; i++)
288 if (fp[i])
289 return false;
290
291 return true;
292}
293
294static int __parse_flow_nlattrs(const struct nlattr *attr,
295 const struct nlattr *a[],
296 u64 *attrsp, bool nz)
297{
298 const struct nlattr *nla;
299 u64 attrs;
300 int rem;
301
302 attrs = *attrsp;
303 nla_for_each_nested(nla, attr, rem) {
304 u16 type = nla_type(nla);
305 int expected_len;
306
307 if (type > OVS_KEY_ATTR_MAX) {
308 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
309 type, OVS_KEY_ATTR_MAX);
310 return -EINVAL;
311 }
312
313 if (attrs & (1 << type)) {
314 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
315 return -EINVAL;
316 }
317
318 expected_len = ovs_key_lens[type];
319 if (nla_len(nla) != expected_len && expected_len != -1) {
320 OVS_NLERR("Key attribute has unexpected length (type=%d"
321 ", length=%d, expected=%d).\n", type,
322 nla_len(nla), expected_len);
323 return -EINVAL;
324 }
325
326 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
327 attrs |= 1 << type;
328 a[type] = nla;
329 }
330 }
331 if (rem) {
332 OVS_NLERR("Message has %d unknown bytes.\n", rem);
333 return -EINVAL;
334 }
335
336 *attrsp = attrs;
337 return 0;
338}
339
340static int parse_flow_mask_nlattrs(const struct nlattr *attr,
341 const struct nlattr *a[], u64 *attrsp)
342{
343 return __parse_flow_nlattrs(attr, a, attrsp, true);
344}
345
346static int parse_flow_nlattrs(const struct nlattr *attr,
347 const struct nlattr *a[], u64 *attrsp)
348{
349 return __parse_flow_nlattrs(attr, a, attrsp, false);
350}
351
352static int ipv4_tun_from_nlattr(const struct nlattr *attr,
353 struct sw_flow_match *match, bool is_mask)
354{
355 struct nlattr *a;
356 int rem;
357 bool ttl = false;
358 __be16 tun_flags = 0;
Jesse Grossf5796682014-10-03 15:35:33 -0700359 unsigned long opt_key_offset;
Pravin B Shelare6445712013-10-03 18:16:47 -0700360
361 nla_for_each_nested(a, attr, rem) {
362 int type = nla_type(a);
363 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
364 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
365 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
366 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
367 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
368 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
369 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
370 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
Jesse Gross67fa0342014-10-03 15:35:30 -0700371 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
Jesse Grossf5796682014-10-03 15:35:33 -0700372 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
Pravin B Shelare6445712013-10-03 18:16:47 -0700373 };
374
375 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
376 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
377 type, OVS_TUNNEL_KEY_ATTR_MAX);
378 return -EINVAL;
379 }
380
Jesse Grossf5796682014-10-03 15:35:33 -0700381 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
382 ovs_tunnel_key_lens[type] != -1) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700383 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
384 " length (type=%d, length=%d, expected=%d).\n",
385 type, nla_len(a), ovs_tunnel_key_lens[type]);
386 return -EINVAL;
387 }
388
389 switch (type) {
390 case OVS_TUNNEL_KEY_ATTR_ID:
391 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
392 nla_get_be64(a), is_mask);
393 tun_flags |= TUNNEL_KEY;
394 break;
395 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
396 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
397 nla_get_be32(a), is_mask);
398 break;
399 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
400 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
401 nla_get_be32(a), is_mask);
402 break;
403 case OVS_TUNNEL_KEY_ATTR_TOS:
404 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
405 nla_get_u8(a), is_mask);
406 break;
407 case OVS_TUNNEL_KEY_ATTR_TTL:
408 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
409 nla_get_u8(a), is_mask);
410 ttl = true;
411 break;
412 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
413 tun_flags |= TUNNEL_DONT_FRAGMENT;
414 break;
415 case OVS_TUNNEL_KEY_ATTR_CSUM:
416 tun_flags |= TUNNEL_CSUM;
417 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700418 case OVS_TUNNEL_KEY_ATTR_OAM:
419 tun_flags |= TUNNEL_OAM;
420 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700421 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
422 tun_flags |= TUNNEL_OPTIONS_PRESENT;
423 if (nla_len(a) > sizeof(match->key->tun_opts)) {
424 OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
425 nla_len(a),
426 sizeof(match->key->tun_opts));
427 return -EINVAL;
428 }
429
430 if (nla_len(a) % 4 != 0) {
431 OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
432 nla_len(a));
433 return -EINVAL;
434 }
435
436 /* We need to record the length of the options passed
437 * down, otherwise packets with the same format but
438 * additional options will be silently matched.
439 */
440 if (!is_mask) {
441 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
442 false);
443 } else {
444 /* This is somewhat unusual because it looks at
445 * both the key and mask while parsing the
446 * attributes (and by extension assumes the key
447 * is parsed first). Normally, we would verify
448 * that each is the correct length and that the
449 * attributes line up in the validate function.
450 * However, that is difficult because this is
451 * variable length and we won't have the
452 * information later.
453 */
454 if (match->key->tun_opts_len != nla_len(a)) {
455 OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
456 match->key->tun_opts_len,
457 nla_len(a));
458 return -EINVAL;
459 }
460
461 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
462 true);
463 }
464
465 opt_key_offset = (unsigned long)GENEVE_OPTS(
466 (struct sw_flow_key *)0,
467 nla_len(a));
468 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset,
469 nla_data(a), nla_len(a),
470 is_mask);
471 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700472 default:
Jesse Grossf5796682014-10-03 15:35:33 -0700473 OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
474 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700475 return -EINVAL;
476 }
477 }
478
479 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
480
481 if (rem > 0) {
482 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
483 return -EINVAL;
484 }
485
486 if (!is_mask) {
487 if (!match->key->tun_key.ipv4_dst) {
488 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
489 return -EINVAL;
490 }
491
492 if (!ttl) {
493 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
494 return -EINVAL;
495 }
496 }
497
498 return 0;
499}
500
Jesse Grossf5796682014-10-03 15:35:33 -0700501static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
502 const struct ovs_key_ipv4_tunnel *output,
503 const struct geneve_opt *tun_opts,
504 int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700505{
Pravin B Shelare6445712013-10-03 18:16:47 -0700506 if (output->tun_flags & TUNNEL_KEY &&
507 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
508 return -EMSGSIZE;
509 if (output->ipv4_src &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700510 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700511 return -EMSGSIZE;
512 if (output->ipv4_dst &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700513 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700514 return -EMSGSIZE;
515 if (output->ipv4_tos &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700516 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700517 return -EMSGSIZE;
518 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
519 return -EMSGSIZE;
520 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700521 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700522 return -EMSGSIZE;
523 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700524 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
525 return -EMSGSIZE;
526 if ((output->tun_flags & TUNNEL_OAM) &&
527 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700528 return -EMSGSIZE;
Jesse Grossf5796682014-10-03 15:35:33 -0700529 if (tun_opts &&
530 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
531 swkey_tun_opts_len, tun_opts))
532 return -EMSGSIZE;
533
534 return 0;
535}
536
537
538static int ipv4_tun_to_nlattr(struct sk_buff *skb,
539 const struct ovs_key_ipv4_tunnel *output,
540 const struct geneve_opt *tun_opts,
541 int swkey_tun_opts_len)
542{
543 struct nlattr *nla;
544 int err;
545
546 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
547 if (!nla)
548 return -EMSGSIZE;
549
550 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
551 if (err)
552 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700553
554 nla_nest_end(skb, nla);
555 return 0;
556}
557
Pravin B Shelare6445712013-10-03 18:16:47 -0700558static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
559 const struct nlattr **a, bool is_mask)
560{
Andy Zhou971427f32014-09-15 19:37:25 -0700561 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
562 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
563
564 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
565 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
566 }
567
568 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
569 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
570
571 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
572 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
573 }
574
Pravin B Shelare6445712013-10-03 18:16:47 -0700575 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
576 SW_FLOW_KEY_PUT(match, phy.priority,
577 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
578 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
579 }
580
581 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
582 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
583
584 if (is_mask)
585 in_port = 0xffffffff; /* Always exact match in_port. */
586 else if (in_port >= DP_MAX_PORTS)
587 return -EINVAL;
588
589 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
590 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
591 } else if (!is_mask) {
592 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
593 }
594
595 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
596 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
597
598 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
599 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
600 }
601 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
602 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
603 is_mask))
604 return -EINVAL;
605 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
606 }
607 return 0;
608}
609
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700610static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
611 const struct nlattr **a, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700612{
613 int err;
614 u64 orig_attrs = attrs;
615
616 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
617 if (err)
618 return err;
619
620 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
621 const struct ovs_key_ethernet *eth_key;
622
623 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
624 SW_FLOW_KEY_MEMCPY(match, eth.src,
625 eth_key->eth_src, ETH_ALEN, is_mask);
626 SW_FLOW_KEY_MEMCPY(match, eth.dst,
627 eth_key->eth_dst, ETH_ALEN, is_mask);
628 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
629 }
630
631 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
632 __be16 tci;
633
634 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
635 if (!(tci & htons(VLAN_TAG_PRESENT))) {
636 if (is_mask)
637 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
638 else
639 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
640
641 return -EINVAL;
642 }
643
644 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
645 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
646 } else if (!is_mask)
647 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
648
649 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
650 __be16 eth_type;
651
652 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
653 if (is_mask) {
654 /* Always exact match EtherType. */
655 eth_type = htons(0xffff);
656 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
657 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
658 ntohs(eth_type), ETH_P_802_3_MIN);
659 return -EINVAL;
660 }
661
662 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
663 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
664 } else if (!is_mask) {
665 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
666 }
667
668 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
669 const struct ovs_key_ipv4 *ipv4_key;
670
671 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
672 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
673 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
674 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
675 return -EINVAL;
676 }
677 SW_FLOW_KEY_PUT(match, ip.proto,
678 ipv4_key->ipv4_proto, is_mask);
679 SW_FLOW_KEY_PUT(match, ip.tos,
680 ipv4_key->ipv4_tos, is_mask);
681 SW_FLOW_KEY_PUT(match, ip.ttl,
682 ipv4_key->ipv4_ttl, is_mask);
683 SW_FLOW_KEY_PUT(match, ip.frag,
684 ipv4_key->ipv4_frag, is_mask);
685 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
686 ipv4_key->ipv4_src, is_mask);
687 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
688 ipv4_key->ipv4_dst, is_mask);
689 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
690 }
691
692 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
693 const struct ovs_key_ipv6 *ipv6_key;
694
695 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
696 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
697 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
698 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
699 return -EINVAL;
700 }
701 SW_FLOW_KEY_PUT(match, ipv6.label,
702 ipv6_key->ipv6_label, is_mask);
703 SW_FLOW_KEY_PUT(match, ip.proto,
704 ipv6_key->ipv6_proto, is_mask);
705 SW_FLOW_KEY_PUT(match, ip.tos,
706 ipv6_key->ipv6_tclass, is_mask);
707 SW_FLOW_KEY_PUT(match, ip.ttl,
708 ipv6_key->ipv6_hlimit, is_mask);
709 SW_FLOW_KEY_PUT(match, ip.frag,
710 ipv6_key->ipv6_frag, is_mask);
711 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
712 ipv6_key->ipv6_src,
713 sizeof(match->key->ipv6.addr.src),
714 is_mask);
715 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
716 ipv6_key->ipv6_dst,
717 sizeof(match->key->ipv6.addr.dst),
718 is_mask);
719
720 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
721 }
722
723 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
724 const struct ovs_key_arp *arp_key;
725
726 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
727 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
728 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
729 arp_key->arp_op);
730 return -EINVAL;
731 }
732
733 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
734 arp_key->arp_sip, is_mask);
735 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
736 arp_key->arp_tip, is_mask);
737 SW_FLOW_KEY_PUT(match, ip.proto,
738 ntohs(arp_key->arp_op), is_mask);
739 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
740 arp_key->arp_sha, ETH_ALEN, is_mask);
741 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
742 arp_key->arp_tha, ETH_ALEN, is_mask);
743
744 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
745 }
746
Simon Horman25cd9ba2014-10-06 05:05:13 -0700747 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
748 const struct ovs_key_mpls *mpls_key;
749
750 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
751 SW_FLOW_KEY_PUT(match, mpls.top_lse,
752 mpls_key->mpls_lse, is_mask);
753
754 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
755 }
756
Pravin B Shelare6445712013-10-03 18:16:47 -0700757 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
758 const struct ovs_key_tcp *tcp_key;
759
760 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700761 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
762 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700763 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
764 }
765
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700766 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
767 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700768 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700769 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
770 is_mask);
771 } else {
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700772 SW_FLOW_KEY_PUT(match, tp.flags,
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700773 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
774 is_mask);
775 }
776 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
777 }
778
Pravin B Shelare6445712013-10-03 18:16:47 -0700779 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
780 const struct ovs_key_udp *udp_key;
781
782 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700783 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
784 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700785 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
786 }
787
788 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
789 const struct ovs_key_sctp *sctp_key;
790
791 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700792 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
793 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700794 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
795 }
796
797 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
798 const struct ovs_key_icmp *icmp_key;
799
800 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700801 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700802 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700803 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700804 htons(icmp_key->icmp_code), is_mask);
805 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
806 }
807
808 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
809 const struct ovs_key_icmpv6 *icmpv6_key;
810
811 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700812 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700813 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700814 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700815 htons(icmpv6_key->icmpv6_code), is_mask);
816 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
817 }
818
819 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
820 const struct ovs_key_nd *nd_key;
821
822 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
823 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
824 nd_key->nd_target,
825 sizeof(match->key->ipv6.nd.target),
826 is_mask);
827 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
828 nd_key->nd_sll, ETH_ALEN, is_mask);
829 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
830 nd_key->nd_tll, ETH_ALEN, is_mask);
831 attrs &= ~(1 << OVS_KEY_ATTR_ND);
832 }
833
834 if (attrs != 0)
835 return -EINVAL;
836
837 return 0;
838}
839
Pravin B Shelarf47de062014-10-16 21:55:45 -0700840static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700841{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700842 struct nlattr *nla;
843 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700844
Pravin B Shelarf47de062014-10-16 21:55:45 -0700845 /* The nlattr stream should already have been validated */
846 nla_for_each_nested(nla, attr, rem) {
847 /* We assume that ovs_key_lens[type] == -1 means that type is a
848 * nested attribute
849 */
850 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
851 nlattr_set(nla, val, false);
852 else
853 memset(nla_data(nla), val, nla_len(nla));
854 }
855}
856
857static void mask_set_nlattr(struct nlattr *attr, u8 val)
858{
859 nlattr_set(attr, val, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700860}
861
862/**
863 * ovs_nla_get_match - parses Netlink attributes into a flow key and
864 * mask. In case the 'mask' is NULL, the flow is treated as exact match
865 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
866 * does not include any don't care bit.
867 * @match: receives the extracted flow match information.
868 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
869 * sequence. The fields should of the packet that triggered the creation
870 * of this flow.
871 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
872 * attribute specifies the mask field of the wildcarded flow.
873 */
874int ovs_nla_get_match(struct sw_flow_match *match,
875 const struct nlattr *key,
876 const struct nlattr *mask)
877{
878 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
879 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700880 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700881 u64 key_attrs = 0;
882 u64 mask_attrs = 0;
883 bool encap_valid = false;
884 int err;
885
886 err = parse_flow_nlattrs(key, a, &key_attrs);
887 if (err)
888 return err;
889
890 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
891 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
892 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
893 __be16 tci;
894
895 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
896 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
897 OVS_NLERR("Invalid Vlan frame.\n");
898 return -EINVAL;
899 }
900
901 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
902 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
903 encap = a[OVS_KEY_ATTR_ENCAP];
904 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
905 encap_valid = true;
906
907 if (tci & htons(VLAN_TAG_PRESENT)) {
908 err = parse_flow_nlattrs(encap, a, &key_attrs);
909 if (err)
910 return err;
911 } else if (!tci) {
912 /* Corner case for truncated 802.1Q header. */
913 if (nla_len(encap)) {
914 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
915 return -EINVAL;
916 }
917 } else {
918 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
919 return -EINVAL;
920 }
921 }
922
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700923 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700924 if (err)
925 return err;
926
Pravin B Shelarf47de062014-10-16 21:55:45 -0700927 if (match->mask && !mask) {
928 /* Create an exact match mask. We need to set to 0xff all the
929 * 'match->mask' fields that have been touched in 'match->key'.
930 * We cannot simply memset 'match->mask', because padding bytes
931 * and fields not specified in 'match->key' should be left to 0.
932 * Instead, we use a stream of netlink attributes, copied from
933 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
934 * of filling 'match->mask' appropriately.
935 */
936 newmask = kmemdup(key, nla_total_size(nla_len(key)),
937 GFP_KERNEL);
938 if (!newmask)
939 return -ENOMEM;
940
941 mask_set_nlattr(newmask, 0xff);
942
943 /* The userspace does not send tunnel attributes that are 0,
944 * but we should not wildcard them nonetheless.
945 */
946 if (match->key->tun_key.ipv4_dst)
947 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
948
949 mask = newmask;
950 }
951
Pravin B Shelare6445712013-10-03 18:16:47 -0700952 if (mask) {
953 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
954 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700955 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700956
Pravin B Shelarf47de062014-10-16 21:55:45 -0700957 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700958 __be16 eth_type = 0;
959 __be16 tci = 0;
960
961 if (!encap_valid) {
962 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
Pravin B Shelarf47de062014-10-16 21:55:45 -0700963 err = -EINVAL;
964 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700965 }
966
967 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
968 if (a[OVS_KEY_ATTR_ETHERTYPE])
969 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
970
971 if (eth_type == htons(0xffff)) {
972 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
973 encap = a[OVS_KEY_ATTR_ENCAP];
974 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
Pravin B Shelarf47de062014-10-16 21:55:45 -0700975 if (err)
976 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700977 } else {
978 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
979 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -0700980 err = -EINVAL;
981 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700982 }
983
984 if (a[OVS_KEY_ATTR_VLAN])
985 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
986
987 if (!(tci & htons(VLAN_TAG_PRESENT))) {
988 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -0700989 err = -EINVAL;
990 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700991 }
992 }
993
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700994 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700995 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700996 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700997 }
998
999 if (!match_validate(match, key_attrs, mask_attrs))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001000 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001001
Pravin B Shelarf47de062014-10-16 21:55:45 -07001002free_newmask:
1003 kfree(newmask);
1004 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001005}
1006
1007/**
1008 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001009 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001010 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1011 * sequence.
1012 *
1013 * This parses a series of Netlink attributes that form a flow key, which must
1014 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1015 * get the metadata, that is, the parts of the flow key that cannot be
1016 * extracted from the packet itself.
1017 */
1018
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001019int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1020 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -07001021{
Pravin B Shelare6445712013-10-03 18:16:47 -07001022 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001023 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001024 u64 attrs = 0;
1025 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001026
1027 err = parse_flow_nlattrs(attr, a, &attrs);
1028 if (err)
1029 return -EINVAL;
1030
1031 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001032 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001033
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001034 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001035
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001036 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -07001037}
1038
1039int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1040 const struct sw_flow_key *output, struct sk_buff *skb)
1041{
1042 struct ovs_key_ethernet *eth_key;
1043 struct nlattr *nla, *encap;
1044 bool is_mask = (swkey != output);
1045
Andy Zhou971427f32014-09-15 19:37:25 -07001046 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1047 goto nla_put_failure;
1048
1049 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1050 goto nla_put_failure;
1051
Pravin B Shelare6445712013-10-03 18:16:47 -07001052 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1053 goto nla_put_failure;
1054
Jesse Grossf5796682014-10-03 15:35:33 -07001055 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1056 const struct geneve_opt *opts = NULL;
1057
1058 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1059 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1060
1061 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1062 swkey->tun_opts_len))
1063 goto nla_put_failure;
1064 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001065
1066 if (swkey->phy.in_port == DP_MAX_PORTS) {
1067 if (is_mask && (output->phy.in_port == 0xffff))
1068 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1069 goto nla_put_failure;
1070 } else {
1071 u16 upper_u16;
1072 upper_u16 = !is_mask ? 0 : 0xffff;
1073
1074 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1075 (upper_u16 << 16) | output->phy.in_port))
1076 goto nla_put_failure;
1077 }
1078
1079 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1080 goto nla_put_failure;
1081
1082 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1083 if (!nla)
1084 goto nla_put_failure;
1085
1086 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001087 ether_addr_copy(eth_key->eth_src, output->eth.src);
1088 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001089
1090 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1091 __be16 eth_type;
1092 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1093 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1094 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1095 goto nla_put_failure;
1096 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1097 if (!swkey->eth.tci)
1098 goto unencap;
1099 } else
1100 encap = NULL;
1101
1102 if (swkey->eth.type == htons(ETH_P_802_2)) {
1103 /*
1104 * Ethertype 802.2 is represented in the netlink with omitted
1105 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1106 * 0xffff in the mask attribute. Ethertype can also
1107 * be wildcarded.
1108 */
1109 if (is_mask && output->eth.type)
1110 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1111 output->eth.type))
1112 goto nla_put_failure;
1113 goto unencap;
1114 }
1115
1116 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1117 goto nla_put_failure;
1118
1119 if (swkey->eth.type == htons(ETH_P_IP)) {
1120 struct ovs_key_ipv4 *ipv4_key;
1121
1122 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1123 if (!nla)
1124 goto nla_put_failure;
1125 ipv4_key = nla_data(nla);
1126 ipv4_key->ipv4_src = output->ipv4.addr.src;
1127 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1128 ipv4_key->ipv4_proto = output->ip.proto;
1129 ipv4_key->ipv4_tos = output->ip.tos;
1130 ipv4_key->ipv4_ttl = output->ip.ttl;
1131 ipv4_key->ipv4_frag = output->ip.frag;
1132 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1133 struct ovs_key_ipv6 *ipv6_key;
1134
1135 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1136 if (!nla)
1137 goto nla_put_failure;
1138 ipv6_key = nla_data(nla);
1139 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1140 sizeof(ipv6_key->ipv6_src));
1141 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1142 sizeof(ipv6_key->ipv6_dst));
1143 ipv6_key->ipv6_label = output->ipv6.label;
1144 ipv6_key->ipv6_proto = output->ip.proto;
1145 ipv6_key->ipv6_tclass = output->ip.tos;
1146 ipv6_key->ipv6_hlimit = output->ip.ttl;
1147 ipv6_key->ipv6_frag = output->ip.frag;
1148 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1149 swkey->eth.type == htons(ETH_P_RARP)) {
1150 struct ovs_key_arp *arp_key;
1151
1152 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1153 if (!nla)
1154 goto nla_put_failure;
1155 arp_key = nla_data(nla);
1156 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1157 arp_key->arp_sip = output->ipv4.addr.src;
1158 arp_key->arp_tip = output->ipv4.addr.dst;
1159 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001160 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1161 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001162 } else if (eth_p_mpls(swkey->eth.type)) {
1163 struct ovs_key_mpls *mpls_key;
1164
1165 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1166 if (!nla)
1167 goto nla_put_failure;
1168 mpls_key = nla_data(nla);
1169 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001170 }
1171
1172 if ((swkey->eth.type == htons(ETH_P_IP) ||
1173 swkey->eth.type == htons(ETH_P_IPV6)) &&
1174 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1175
1176 if (swkey->ip.proto == IPPROTO_TCP) {
1177 struct ovs_key_tcp *tcp_key;
1178
1179 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1180 if (!nla)
1181 goto nla_put_failure;
1182 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001183 tcp_key->tcp_src = output->tp.src;
1184 tcp_key->tcp_dst = output->tp.dst;
1185 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1186 output->tp.flags))
1187 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001188 } else if (swkey->ip.proto == IPPROTO_UDP) {
1189 struct ovs_key_udp *udp_key;
1190
1191 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1192 if (!nla)
1193 goto nla_put_failure;
1194 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001195 udp_key->udp_src = output->tp.src;
1196 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001197 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1198 struct ovs_key_sctp *sctp_key;
1199
1200 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1201 if (!nla)
1202 goto nla_put_failure;
1203 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001204 sctp_key->sctp_src = output->tp.src;
1205 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001206 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1207 swkey->ip.proto == IPPROTO_ICMP) {
1208 struct ovs_key_icmp *icmp_key;
1209
1210 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1211 if (!nla)
1212 goto nla_put_failure;
1213 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001214 icmp_key->icmp_type = ntohs(output->tp.src);
1215 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001216 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1217 swkey->ip.proto == IPPROTO_ICMPV6) {
1218 struct ovs_key_icmpv6 *icmpv6_key;
1219
1220 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1221 sizeof(*icmpv6_key));
1222 if (!nla)
1223 goto nla_put_failure;
1224 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001225 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1226 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001227
1228 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1229 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1230 struct ovs_key_nd *nd_key;
1231
1232 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1233 if (!nla)
1234 goto nla_put_failure;
1235 nd_key = nla_data(nla);
1236 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1237 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001238 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1239 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001240 }
1241 }
1242 }
1243
1244unencap:
1245 if (encap)
1246 nla_nest_end(skb, encap);
1247
1248 return 0;
1249
1250nla_put_failure:
1251 return -EMSGSIZE;
1252}
1253
1254#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1255
1256struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1257{
1258 struct sw_flow_actions *sfa;
1259
1260 if (size > MAX_ACTIONS_BUFSIZE)
1261 return ERR_PTR(-EINVAL);
1262
1263 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1264 if (!sfa)
1265 return ERR_PTR(-ENOMEM);
1266
1267 sfa->actions_len = 0;
1268 return sfa;
1269}
1270
Pravin B Shelare6445712013-10-03 18:16:47 -07001271/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1272 * The caller must hold rcu_read_lock for this to be sensible. */
1273void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1274{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001275 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001276}
1277
1278static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1279 int attr_len)
1280{
1281
1282 struct sw_flow_actions *acts;
1283 int new_acts_size;
1284 int req_size = NLA_ALIGN(attr_len);
1285 int next_offset = offsetof(struct sw_flow_actions, actions) +
1286 (*sfa)->actions_len;
1287
1288 if (req_size <= (ksize(*sfa) - next_offset))
1289 goto out;
1290
1291 new_acts_size = ksize(*sfa) * 2;
1292
1293 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1294 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1295 return ERR_PTR(-EMSGSIZE);
1296 new_acts_size = MAX_ACTIONS_BUFSIZE;
1297 }
1298
1299 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1300 if (IS_ERR(acts))
1301 return (void *)acts;
1302
1303 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1304 acts->actions_len = (*sfa)->actions_len;
1305 kfree(*sfa);
1306 *sfa = acts;
1307
1308out:
1309 (*sfa)->actions_len += req_size;
1310 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1311}
1312
Jesse Grossf0b128c2014-10-03 15:35:31 -07001313static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1314 int attrtype, void *data, int len)
Pravin B Shelare6445712013-10-03 18:16:47 -07001315{
1316 struct nlattr *a;
1317
1318 a = reserve_sfa_size(sfa, nla_attr_size(len));
1319 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001320 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001321
1322 a->nla_type = attrtype;
1323 a->nla_len = nla_attr_size(len);
1324
1325 if (data)
1326 memcpy(nla_data(a), data, len);
1327 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1328
Jesse Grossf0b128c2014-10-03 15:35:31 -07001329 return a;
1330}
1331
1332static int add_action(struct sw_flow_actions **sfa, int attrtype,
1333 void *data, int len)
1334{
1335 struct nlattr *a;
1336
1337 a = __add_action(sfa, attrtype, data, len);
1338 if (IS_ERR(a))
1339 return PTR_ERR(a);
1340
Pravin B Shelare6445712013-10-03 18:16:47 -07001341 return 0;
1342}
1343
1344static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1345 int attrtype)
1346{
1347 int used = (*sfa)->actions_len;
1348 int err;
1349
1350 err = add_action(sfa, attrtype, NULL, 0);
1351 if (err)
1352 return err;
1353
1354 return used;
1355}
1356
1357static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1358 int st_offset)
1359{
1360 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1361 st_offset);
1362
1363 a->nla_len = sfa->actions_len - st_offset;
1364}
1365
Simon Horman25cd9ba2014-10-06 05:05:13 -07001366static int ovs_nla_copy_actions__(const struct nlattr *attr,
1367 const struct sw_flow_key *key,
1368 int depth, struct sw_flow_actions **sfa,
1369 __be16 eth_type, __be16 vlan_tci);
1370
Pravin B Shelare6445712013-10-03 18:16:47 -07001371static int validate_and_copy_sample(const struct nlattr *attr,
1372 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001373 struct sw_flow_actions **sfa,
1374 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001375{
1376 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1377 const struct nlattr *probability, *actions;
1378 const struct nlattr *a;
1379 int rem, start, err, st_acts;
1380
1381 memset(attrs, 0, sizeof(attrs));
1382 nla_for_each_nested(a, attr, rem) {
1383 int type = nla_type(a);
1384 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1385 return -EINVAL;
1386 attrs[type] = a;
1387 }
1388 if (rem)
1389 return -EINVAL;
1390
1391 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1392 if (!probability || nla_len(probability) != sizeof(u32))
1393 return -EINVAL;
1394
1395 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1396 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1397 return -EINVAL;
1398
1399 /* validation done, copy sample action. */
1400 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1401 if (start < 0)
1402 return start;
1403 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1404 nla_data(probability), sizeof(u32));
1405 if (err)
1406 return err;
1407 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1408 if (st_acts < 0)
1409 return st_acts;
1410
Simon Horman25cd9ba2014-10-06 05:05:13 -07001411 err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
1412 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001413 if (err)
1414 return err;
1415
1416 add_nested_action_end(*sfa, st_acts);
1417 add_nested_action_end(*sfa, start);
1418
1419 return 0;
1420}
1421
Simon Horman25cd9ba2014-10-06 05:05:13 -07001422static int validate_tp_port(const struct sw_flow_key *flow_key,
1423 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001424{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001425 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001426 (flow_key->tp.src || flow_key->tp.dst))
1427 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001428
1429 return -EINVAL;
1430}
1431
1432void ovs_match_init(struct sw_flow_match *match,
1433 struct sw_flow_key *key,
1434 struct sw_flow_mask *mask)
1435{
1436 memset(match, 0, sizeof(*match));
1437 match->key = key;
1438 match->mask = mask;
1439
1440 memset(key, 0, sizeof(*key));
1441
1442 if (mask) {
1443 memset(&mask->key, 0, sizeof(mask->key));
1444 mask->range.start = mask->range.end = 0;
1445 }
1446}
1447
1448static int validate_and_copy_set_tun(const struct nlattr *attr,
1449 struct sw_flow_actions **sfa)
1450{
1451 struct sw_flow_match match;
1452 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001453 struct ovs_tunnel_info *tun_info;
1454 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001455 int err, start;
1456
1457 ovs_match_init(&match, &key, NULL);
1458 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1459 if (err)
1460 return err;
1461
Jesse Grossf5796682014-10-03 15:35:33 -07001462 if (key.tun_opts_len) {
1463 struct geneve_opt *option = GENEVE_OPTS(&key,
1464 key.tun_opts_len);
1465 int opts_len = key.tun_opts_len;
1466 bool crit_opt = false;
1467
1468 while (opts_len > 0) {
1469 int len;
1470
1471 if (opts_len < sizeof(*option))
1472 return -EINVAL;
1473
1474 len = sizeof(*option) + option->length * 4;
1475 if (len > opts_len)
1476 return -EINVAL;
1477
1478 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1479
1480 option = (struct geneve_opt *)((u8 *)option + len);
1481 opts_len -= len;
1482 };
1483
1484 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1485 };
1486
Pravin B Shelare6445712013-10-03 18:16:47 -07001487 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1488 if (start < 0)
1489 return start;
1490
Jesse Grossf0b128c2014-10-03 15:35:31 -07001491 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jesse Grossf5796682014-10-03 15:35:33 -07001492 sizeof(*tun_info) + key.tun_opts_len);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001493 if (IS_ERR(a))
1494 return PTR_ERR(a);
1495
1496 tun_info = nla_data(a);
1497 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001498 tun_info->options_len = key.tun_opts_len;
1499
1500 if (tun_info->options_len) {
1501 /* We need to store the options in the action itself since
1502 * everything else will go away after flow setup. We can append
1503 * it to tun_info and then point there.
1504 */
1505 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1506 key.tun_opts_len);
1507 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1508 } else {
1509 tun_info->options = NULL;
1510 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001511
Pravin B Shelare6445712013-10-03 18:16:47 -07001512 add_nested_action_end(*sfa, start);
1513
1514 return err;
1515}
1516
1517static int validate_set(const struct nlattr *a,
1518 const struct sw_flow_key *flow_key,
1519 struct sw_flow_actions **sfa,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001520 bool *set_tun, __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001521{
1522 const struct nlattr *ovs_key = nla_data(a);
1523 int key_type = nla_type(ovs_key);
1524
1525 /* There can be only one key in a action */
1526 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1527 return -EINVAL;
1528
1529 if (key_type > OVS_KEY_ATTR_MAX ||
1530 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1531 ovs_key_lens[key_type] != -1))
1532 return -EINVAL;
1533
1534 switch (key_type) {
1535 const struct ovs_key_ipv4 *ipv4_key;
1536 const struct ovs_key_ipv6 *ipv6_key;
1537 int err;
1538
1539 case OVS_KEY_ATTR_PRIORITY:
1540 case OVS_KEY_ATTR_SKB_MARK:
1541 case OVS_KEY_ATTR_ETHERNET:
1542 break;
1543
1544 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001545 if (eth_p_mpls(eth_type))
1546 return -EINVAL;
1547
Pravin B Shelare6445712013-10-03 18:16:47 -07001548 *set_tun = true;
1549 err = validate_and_copy_set_tun(a, sfa);
1550 if (err)
1551 return err;
1552 break;
1553
1554 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001555 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001556 return -EINVAL;
1557
1558 if (!flow_key->ip.proto)
1559 return -EINVAL;
1560
1561 ipv4_key = nla_data(ovs_key);
1562 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1563 return -EINVAL;
1564
1565 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1566 return -EINVAL;
1567
1568 break;
1569
1570 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001571 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001572 return -EINVAL;
1573
1574 if (!flow_key->ip.proto)
1575 return -EINVAL;
1576
1577 ipv6_key = nla_data(ovs_key);
1578 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1579 return -EINVAL;
1580
1581 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1582 return -EINVAL;
1583
1584 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1585 return -EINVAL;
1586
1587 break;
1588
1589 case OVS_KEY_ATTR_TCP:
1590 if (flow_key->ip.proto != IPPROTO_TCP)
1591 return -EINVAL;
1592
Simon Horman25cd9ba2014-10-06 05:05:13 -07001593 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001594
1595 case OVS_KEY_ATTR_UDP:
1596 if (flow_key->ip.proto != IPPROTO_UDP)
1597 return -EINVAL;
1598
Simon Horman25cd9ba2014-10-06 05:05:13 -07001599 return validate_tp_port(flow_key, eth_type);
1600
1601 case OVS_KEY_ATTR_MPLS:
1602 if (!eth_p_mpls(eth_type))
1603 return -EINVAL;
1604 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001605
1606 case OVS_KEY_ATTR_SCTP:
1607 if (flow_key->ip.proto != IPPROTO_SCTP)
1608 return -EINVAL;
1609
Simon Horman25cd9ba2014-10-06 05:05:13 -07001610 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001611
1612 default:
1613 return -EINVAL;
1614 }
1615
1616 return 0;
1617}
1618
1619static int validate_userspace(const struct nlattr *attr)
1620{
1621 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1622 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1623 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1624 };
1625 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1626 int error;
1627
1628 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1629 attr, userspace_policy);
1630 if (error)
1631 return error;
1632
1633 if (!a[OVS_USERSPACE_ATTR_PID] ||
1634 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1635 return -EINVAL;
1636
1637 return 0;
1638}
1639
1640static int copy_action(const struct nlattr *from,
1641 struct sw_flow_actions **sfa)
1642{
1643 int totlen = NLA_ALIGN(from->nla_len);
1644 struct nlattr *to;
1645
1646 to = reserve_sfa_size(sfa, from->nla_len);
1647 if (IS_ERR(to))
1648 return PTR_ERR(to);
1649
1650 memcpy(to, from, totlen);
1651 return 0;
1652}
1653
Simon Horman25cd9ba2014-10-06 05:05:13 -07001654static int ovs_nla_copy_actions__(const struct nlattr *attr,
1655 const struct sw_flow_key *key,
1656 int depth, struct sw_flow_actions **sfa,
1657 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001658{
1659 const struct nlattr *a;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001660 bool out_tnl_port = false;
Pravin B Shelare6445712013-10-03 18:16:47 -07001661 int rem, err;
1662
1663 if (depth >= SAMPLE_ACTION_DEPTH)
1664 return -EOVERFLOW;
1665
1666 nla_for_each_nested(a, attr, rem) {
1667 /* Expected argument lengths, (u32)-1 for variable length. */
1668 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1669 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001670 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001671 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001672 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1673 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001674 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1675 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1676 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001677 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1678 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001679 };
1680 const struct ovs_action_push_vlan *vlan;
1681 int type = nla_type(a);
1682 bool skip_copy;
1683
1684 if (type > OVS_ACTION_ATTR_MAX ||
1685 (action_lens[type] != nla_len(a) &&
1686 action_lens[type] != (u32)-1))
1687 return -EINVAL;
1688
1689 skip_copy = false;
1690 switch (type) {
1691 case OVS_ACTION_ATTR_UNSPEC:
1692 return -EINVAL;
1693
1694 case OVS_ACTION_ATTR_USERSPACE:
1695 err = validate_userspace(a);
1696 if (err)
1697 return err;
1698 break;
1699
1700 case OVS_ACTION_ATTR_OUTPUT:
1701 if (nla_get_u32(a) >= DP_MAX_PORTS)
1702 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001703 out_tnl_port = false;
1704
Pravin B Shelare6445712013-10-03 18:16:47 -07001705 break;
1706
Andy Zhou971427f32014-09-15 19:37:25 -07001707 case OVS_ACTION_ATTR_HASH: {
1708 const struct ovs_action_hash *act_hash = nla_data(a);
1709
1710 switch (act_hash->hash_alg) {
1711 case OVS_HASH_ALG_L4:
1712 break;
1713 default:
1714 return -EINVAL;
1715 }
1716
1717 break;
1718 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001719
1720 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001721 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001722 break;
1723
1724 case OVS_ACTION_ATTR_PUSH_VLAN:
1725 vlan = nla_data(a);
1726 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1727 return -EINVAL;
1728 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1729 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001730 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001731 break;
1732
Andy Zhou971427f32014-09-15 19:37:25 -07001733 case OVS_ACTION_ATTR_RECIRC:
1734 break;
1735
Simon Horman25cd9ba2014-10-06 05:05:13 -07001736 case OVS_ACTION_ATTR_PUSH_MPLS: {
1737 const struct ovs_action_push_mpls *mpls = nla_data(a);
1738
1739 /* Networking stack do not allow simultaneous Tunnel
1740 * and MPLS GSO.
1741 */
1742 if (out_tnl_port)
1743 return -EINVAL;
1744
1745 if (!eth_p_mpls(mpls->mpls_ethertype))
1746 return -EINVAL;
1747 /* Prohibit push MPLS other than to a white list
1748 * for packets that have a known tag order.
1749 */
1750 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1751 (eth_type != htons(ETH_P_IP) &&
1752 eth_type != htons(ETH_P_IPV6) &&
1753 eth_type != htons(ETH_P_ARP) &&
1754 eth_type != htons(ETH_P_RARP) &&
1755 !eth_p_mpls(eth_type)))
1756 return -EINVAL;
1757 eth_type = mpls->mpls_ethertype;
1758 break;
1759 }
1760
1761 case OVS_ACTION_ATTR_POP_MPLS:
1762 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1763 !eth_p_mpls(eth_type))
1764 return -EINVAL;
1765
1766 /* Disallow subsequent L2.5+ set and mpls_pop actions
1767 * as there is no check here to ensure that the new
1768 * eth_type is valid and thus set actions could
1769 * write off the end of the packet or otherwise
1770 * corrupt it.
1771 *
1772 * Support for these actions is planned using packet
1773 * recirculation.
1774 */
1775 eth_type = htons(0);
1776 break;
1777
Pravin B Shelare6445712013-10-03 18:16:47 -07001778 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001779 err = validate_set(a, key, sfa,
1780 &out_tnl_port, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001781 if (err)
1782 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001783
1784 skip_copy = out_tnl_port;
Pravin B Shelare6445712013-10-03 18:16:47 -07001785 break;
1786
1787 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001788 err = validate_and_copy_sample(a, key, depth, sfa,
1789 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001790 if (err)
1791 return err;
1792 skip_copy = true;
1793 break;
1794
1795 default:
1796 return -EINVAL;
1797 }
1798 if (!skip_copy) {
1799 err = copy_action(a, sfa);
1800 if (err)
1801 return err;
1802 }
1803 }
1804
1805 if (rem > 0)
1806 return -EINVAL;
1807
1808 return 0;
1809}
1810
Simon Horman25cd9ba2014-10-06 05:05:13 -07001811int ovs_nla_copy_actions(const struct nlattr *attr,
1812 const struct sw_flow_key *key,
1813 struct sw_flow_actions **sfa)
1814{
1815 return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
1816 key->eth.tci);
1817}
1818
Pravin B Shelare6445712013-10-03 18:16:47 -07001819static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1820{
1821 const struct nlattr *a;
1822 struct nlattr *start;
1823 int err = 0, rem;
1824
1825 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1826 if (!start)
1827 return -EMSGSIZE;
1828
1829 nla_for_each_nested(a, attr, rem) {
1830 int type = nla_type(a);
1831 struct nlattr *st_sample;
1832
1833 switch (type) {
1834 case OVS_SAMPLE_ATTR_PROBABILITY:
1835 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1836 sizeof(u32), nla_data(a)))
1837 return -EMSGSIZE;
1838 break;
1839 case OVS_SAMPLE_ATTR_ACTIONS:
1840 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1841 if (!st_sample)
1842 return -EMSGSIZE;
1843 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1844 if (err)
1845 return err;
1846 nla_nest_end(skb, st_sample);
1847 break;
1848 }
1849 }
1850
1851 nla_nest_end(skb, start);
1852 return err;
1853}
1854
1855static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1856{
1857 const struct nlattr *ovs_key = nla_data(a);
1858 int key_type = nla_type(ovs_key);
1859 struct nlattr *start;
1860 int err;
1861
1862 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001863 case OVS_KEY_ATTR_TUNNEL_INFO: {
1864 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1865
Pravin B Shelare6445712013-10-03 18:16:47 -07001866 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1867 if (!start)
1868 return -EMSGSIZE;
1869
Jesse Grossf0b128c2014-10-03 15:35:31 -07001870 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001871 tun_info->options_len ?
1872 tun_info->options : NULL,
1873 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001874 if (err)
1875 return err;
1876 nla_nest_end(skb, start);
1877 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001878 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001879 default:
1880 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1881 return -EMSGSIZE;
1882 break;
1883 }
1884
1885 return 0;
1886}
1887
1888int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1889{
1890 const struct nlattr *a;
1891 int rem, err;
1892
1893 nla_for_each_attr(a, attr, len, rem) {
1894 int type = nla_type(a);
1895
1896 switch (type) {
1897 case OVS_ACTION_ATTR_SET:
1898 err = set_action_to_attr(a, skb);
1899 if (err)
1900 return err;
1901 break;
1902
1903 case OVS_ACTION_ATTR_SAMPLE:
1904 err = sample_action_to_attr(a, skb);
1905 if (err)
1906 return err;
1907 break;
1908 default:
1909 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1910 return -EMSGSIZE;
1911 break;
1912 }
1913 }
1914
1915 return 0;
1916}