blob: 1b29ea7069122aa62380c6ce2906f9881f0cfcc1 [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
Jesse Gross426cda52014-10-06 05:08:38 -0700584 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700585 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700586 } else if (in_port >= DP_MAX_PORTS) {
587 OVS_NLERR("Port (%d) exceeds maximum allowable (%d).\n",
588 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700589 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700590 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700591
592 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
593 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
594 } else if (!is_mask) {
595 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
596 }
597
598 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
599 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
600
601 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
602 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
603 }
604 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
605 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
606 is_mask))
607 return -EINVAL;
608 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
609 }
610 return 0;
611}
612
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700613static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
614 const struct nlattr **a, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700615{
616 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700617
618 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
619 if (err)
620 return err;
621
622 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
623 const struct ovs_key_ethernet *eth_key;
624
625 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
626 SW_FLOW_KEY_MEMCPY(match, eth.src,
627 eth_key->eth_src, ETH_ALEN, is_mask);
628 SW_FLOW_KEY_MEMCPY(match, eth.dst,
629 eth_key->eth_dst, ETH_ALEN, is_mask);
630 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
631 }
632
633 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
634 __be16 tci;
635
636 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
637 if (!(tci & htons(VLAN_TAG_PRESENT))) {
638 if (is_mask)
639 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
640 else
641 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
642
643 return -EINVAL;
644 }
645
646 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
647 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
648 } else if (!is_mask)
649 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
650
651 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
652 __be16 eth_type;
653
654 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
655 if (is_mask) {
656 /* Always exact match EtherType. */
657 eth_type = htons(0xffff);
658 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
659 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
660 ntohs(eth_type), ETH_P_802_3_MIN);
661 return -EINVAL;
662 }
663
664 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
665 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
666 } else if (!is_mask) {
667 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
668 }
669
670 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
671 const struct ovs_key_ipv4 *ipv4_key;
672
673 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
674 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
675 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
676 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
677 return -EINVAL;
678 }
679 SW_FLOW_KEY_PUT(match, ip.proto,
680 ipv4_key->ipv4_proto, is_mask);
681 SW_FLOW_KEY_PUT(match, ip.tos,
682 ipv4_key->ipv4_tos, is_mask);
683 SW_FLOW_KEY_PUT(match, ip.ttl,
684 ipv4_key->ipv4_ttl, is_mask);
685 SW_FLOW_KEY_PUT(match, ip.frag,
686 ipv4_key->ipv4_frag, is_mask);
687 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
688 ipv4_key->ipv4_src, is_mask);
689 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
690 ipv4_key->ipv4_dst, is_mask);
691 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
692 }
693
694 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
695 const struct ovs_key_ipv6 *ipv6_key;
696
697 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
698 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
699 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
700 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
701 return -EINVAL;
702 }
703 SW_FLOW_KEY_PUT(match, ipv6.label,
704 ipv6_key->ipv6_label, is_mask);
705 SW_FLOW_KEY_PUT(match, ip.proto,
706 ipv6_key->ipv6_proto, is_mask);
707 SW_FLOW_KEY_PUT(match, ip.tos,
708 ipv6_key->ipv6_tclass, is_mask);
709 SW_FLOW_KEY_PUT(match, ip.ttl,
710 ipv6_key->ipv6_hlimit, is_mask);
711 SW_FLOW_KEY_PUT(match, ip.frag,
712 ipv6_key->ipv6_frag, is_mask);
713 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
714 ipv6_key->ipv6_src,
715 sizeof(match->key->ipv6.addr.src),
716 is_mask);
717 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
718 ipv6_key->ipv6_dst,
719 sizeof(match->key->ipv6.addr.dst),
720 is_mask);
721
722 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
723 }
724
725 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
726 const struct ovs_key_arp *arp_key;
727
728 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
729 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
730 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
731 arp_key->arp_op);
732 return -EINVAL;
733 }
734
735 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
736 arp_key->arp_sip, is_mask);
737 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
738 arp_key->arp_tip, is_mask);
739 SW_FLOW_KEY_PUT(match, ip.proto,
740 ntohs(arp_key->arp_op), is_mask);
741 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
742 arp_key->arp_sha, ETH_ALEN, is_mask);
743 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
744 arp_key->arp_tha, ETH_ALEN, is_mask);
745
746 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
747 }
748
Simon Horman25cd9ba2014-10-06 05:05:13 -0700749 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
750 const struct ovs_key_mpls *mpls_key;
751
752 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
753 SW_FLOW_KEY_PUT(match, mpls.top_lse,
754 mpls_key->mpls_lse, is_mask);
755
756 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
757 }
758
Pravin B Shelare6445712013-10-03 18:16:47 -0700759 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
760 const struct ovs_key_tcp *tcp_key;
761
762 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700763 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
764 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700765 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
766 }
767
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700768 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700769 SW_FLOW_KEY_PUT(match, tp.flags,
770 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
771 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700772 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
773 }
774
Pravin B Shelare6445712013-10-03 18:16:47 -0700775 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
776 const struct ovs_key_udp *udp_key;
777
778 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700779 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
780 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700781 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
782 }
783
784 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
785 const struct ovs_key_sctp *sctp_key;
786
787 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700788 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
789 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700790 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
791 }
792
793 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
794 const struct ovs_key_icmp *icmp_key;
795
796 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700797 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700798 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700799 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700800 htons(icmp_key->icmp_code), is_mask);
801 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
802 }
803
804 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
805 const struct ovs_key_icmpv6 *icmpv6_key;
806
807 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700808 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700809 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700810 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700811 htons(icmpv6_key->icmpv6_code), is_mask);
812 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
813 }
814
815 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
816 const struct ovs_key_nd *nd_key;
817
818 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
819 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
820 nd_key->nd_target,
821 sizeof(match->key->ipv6.nd.target),
822 is_mask);
823 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
824 nd_key->nd_sll, ETH_ALEN, is_mask);
825 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
826 nd_key->nd_tll, ETH_ALEN, is_mask);
827 attrs &= ~(1 << OVS_KEY_ATTR_ND);
828 }
829
Jesse Gross426cda52014-10-06 05:08:38 -0700830 if (attrs != 0) {
831 OVS_NLERR("Unknown key attributes (%llx).\n",
832 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -0700833 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700834 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700835
836 return 0;
837}
838
Pravin B Shelarf47de062014-10-16 21:55:45 -0700839static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700840{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700841 struct nlattr *nla;
842 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700843
Pravin B Shelarf47de062014-10-16 21:55:45 -0700844 /* The nlattr stream should already have been validated */
845 nla_for_each_nested(nla, attr, rem) {
846 /* We assume that ovs_key_lens[type] == -1 means that type is a
847 * nested attribute
848 */
849 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
850 nlattr_set(nla, val, false);
851 else
852 memset(nla_data(nla), val, nla_len(nla));
853 }
854}
855
856static void mask_set_nlattr(struct nlattr *attr, u8 val)
857{
858 nlattr_set(attr, val, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700859}
860
861/**
862 * ovs_nla_get_match - parses Netlink attributes into a flow key and
863 * mask. In case the 'mask' is NULL, the flow is treated as exact match
864 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
865 * does not include any don't care bit.
866 * @match: receives the extracted flow match information.
867 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
868 * sequence. The fields should of the packet that triggered the creation
869 * of this flow.
870 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
871 * attribute specifies the mask field of the wildcarded flow.
872 */
873int ovs_nla_get_match(struct sw_flow_match *match,
874 const struct nlattr *key,
875 const struct nlattr *mask)
876{
877 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
878 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700879 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700880 u64 key_attrs = 0;
881 u64 mask_attrs = 0;
882 bool encap_valid = false;
883 int err;
884
885 err = parse_flow_nlattrs(key, a, &key_attrs);
886 if (err)
887 return err;
888
889 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
890 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
891 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
892 __be16 tci;
893
894 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
895 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
896 OVS_NLERR("Invalid Vlan frame.\n");
897 return -EINVAL;
898 }
899
900 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
901 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
902 encap = a[OVS_KEY_ATTR_ENCAP];
903 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
904 encap_valid = true;
905
906 if (tci & htons(VLAN_TAG_PRESENT)) {
907 err = parse_flow_nlattrs(encap, a, &key_attrs);
908 if (err)
909 return err;
910 } else if (!tci) {
911 /* Corner case for truncated 802.1Q header. */
912 if (nla_len(encap)) {
913 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
914 return -EINVAL;
915 }
916 } else {
917 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
918 return -EINVAL;
919 }
920 }
921
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700922 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700923 if (err)
924 return err;
925
Pravin B Shelarf47de062014-10-16 21:55:45 -0700926 if (match->mask && !mask) {
927 /* Create an exact match mask. We need to set to 0xff all the
928 * 'match->mask' fields that have been touched in 'match->key'.
929 * We cannot simply memset 'match->mask', because padding bytes
930 * and fields not specified in 'match->key' should be left to 0.
931 * Instead, we use a stream of netlink attributes, copied from
932 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
933 * of filling 'match->mask' appropriately.
934 */
935 newmask = kmemdup(key, nla_total_size(nla_len(key)),
936 GFP_KERNEL);
937 if (!newmask)
938 return -ENOMEM;
939
940 mask_set_nlattr(newmask, 0xff);
941
942 /* The userspace does not send tunnel attributes that are 0,
943 * but we should not wildcard them nonetheless.
944 */
945 if (match->key->tun_key.ipv4_dst)
946 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
947
948 mask = newmask;
949 }
950
Pravin B Shelare6445712013-10-03 18:16:47 -0700951 if (mask) {
952 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
953 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700954 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700955
Pravin B Shelarf47de062014-10-16 21:55:45 -0700956 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700957 __be16 eth_type = 0;
958 __be16 tci = 0;
959
960 if (!encap_valid) {
961 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
Pravin B Shelarf47de062014-10-16 21:55:45 -0700962 err = -EINVAL;
963 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700964 }
965
966 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
967 if (a[OVS_KEY_ATTR_ETHERTYPE])
968 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
969
970 if (eth_type == htons(0xffff)) {
971 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
972 encap = a[OVS_KEY_ATTR_ENCAP];
973 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
Pravin B Shelarf47de062014-10-16 21:55:45 -0700974 if (err)
975 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700976 } else {
977 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
978 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -0700979 err = -EINVAL;
980 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700981 }
982
983 if (a[OVS_KEY_ATTR_VLAN])
984 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
985
986 if (!(tci & htons(VLAN_TAG_PRESENT))) {
987 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 -0700988 err = -EINVAL;
989 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700990 }
991 }
992
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700993 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700994 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700995 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700996 }
997
998 if (!match_validate(match, key_attrs, mask_attrs))
Pravin B Shelarf47de062014-10-16 21:55:45 -0700999 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001000
Pravin B Shelarf47de062014-10-16 21:55:45 -07001001free_newmask:
1002 kfree(newmask);
1003 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001004}
1005
1006/**
1007 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001008 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001009 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1010 * sequence.
1011 *
1012 * This parses a series of Netlink attributes that form a flow key, which must
1013 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1014 * get the metadata, that is, the parts of the flow key that cannot be
1015 * extracted from the packet itself.
1016 */
1017
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001018int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1019 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -07001020{
Pravin B Shelare6445712013-10-03 18:16:47 -07001021 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001022 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001023 u64 attrs = 0;
1024 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001025
1026 err = parse_flow_nlattrs(attr, a, &attrs);
1027 if (err)
1028 return -EINVAL;
1029
1030 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001031 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001032
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001033 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001034
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001035 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -07001036}
1037
1038int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1039 const struct sw_flow_key *output, struct sk_buff *skb)
1040{
1041 struct ovs_key_ethernet *eth_key;
1042 struct nlattr *nla, *encap;
1043 bool is_mask = (swkey != output);
1044
Andy Zhou971427f32014-09-15 19:37:25 -07001045 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1046 goto nla_put_failure;
1047
1048 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1049 goto nla_put_failure;
1050
Pravin B Shelare6445712013-10-03 18:16:47 -07001051 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1052 goto nla_put_failure;
1053
Jesse Grossf5796682014-10-03 15:35:33 -07001054 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1055 const struct geneve_opt *opts = NULL;
1056
1057 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1058 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1059
1060 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1061 swkey->tun_opts_len))
1062 goto nla_put_failure;
1063 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001064
1065 if (swkey->phy.in_port == DP_MAX_PORTS) {
1066 if (is_mask && (output->phy.in_port == 0xffff))
1067 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1068 goto nla_put_failure;
1069 } else {
1070 u16 upper_u16;
1071 upper_u16 = !is_mask ? 0 : 0xffff;
1072
1073 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1074 (upper_u16 << 16) | output->phy.in_port))
1075 goto nla_put_failure;
1076 }
1077
1078 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1079 goto nla_put_failure;
1080
1081 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1082 if (!nla)
1083 goto nla_put_failure;
1084
1085 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001086 ether_addr_copy(eth_key->eth_src, output->eth.src);
1087 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001088
1089 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1090 __be16 eth_type;
1091 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1092 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1093 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1094 goto nla_put_failure;
1095 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1096 if (!swkey->eth.tci)
1097 goto unencap;
1098 } else
1099 encap = NULL;
1100
1101 if (swkey->eth.type == htons(ETH_P_802_2)) {
1102 /*
1103 * Ethertype 802.2 is represented in the netlink with omitted
1104 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1105 * 0xffff in the mask attribute. Ethertype can also
1106 * be wildcarded.
1107 */
1108 if (is_mask && output->eth.type)
1109 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1110 output->eth.type))
1111 goto nla_put_failure;
1112 goto unencap;
1113 }
1114
1115 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1116 goto nla_put_failure;
1117
1118 if (swkey->eth.type == htons(ETH_P_IP)) {
1119 struct ovs_key_ipv4 *ipv4_key;
1120
1121 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1122 if (!nla)
1123 goto nla_put_failure;
1124 ipv4_key = nla_data(nla);
1125 ipv4_key->ipv4_src = output->ipv4.addr.src;
1126 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1127 ipv4_key->ipv4_proto = output->ip.proto;
1128 ipv4_key->ipv4_tos = output->ip.tos;
1129 ipv4_key->ipv4_ttl = output->ip.ttl;
1130 ipv4_key->ipv4_frag = output->ip.frag;
1131 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1132 struct ovs_key_ipv6 *ipv6_key;
1133
1134 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1135 if (!nla)
1136 goto nla_put_failure;
1137 ipv6_key = nla_data(nla);
1138 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1139 sizeof(ipv6_key->ipv6_src));
1140 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1141 sizeof(ipv6_key->ipv6_dst));
1142 ipv6_key->ipv6_label = output->ipv6.label;
1143 ipv6_key->ipv6_proto = output->ip.proto;
1144 ipv6_key->ipv6_tclass = output->ip.tos;
1145 ipv6_key->ipv6_hlimit = output->ip.ttl;
1146 ipv6_key->ipv6_frag = output->ip.frag;
1147 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1148 swkey->eth.type == htons(ETH_P_RARP)) {
1149 struct ovs_key_arp *arp_key;
1150
1151 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1152 if (!nla)
1153 goto nla_put_failure;
1154 arp_key = nla_data(nla);
1155 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1156 arp_key->arp_sip = output->ipv4.addr.src;
1157 arp_key->arp_tip = output->ipv4.addr.dst;
1158 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001159 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1160 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001161 } else if (eth_p_mpls(swkey->eth.type)) {
1162 struct ovs_key_mpls *mpls_key;
1163
1164 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1165 if (!nla)
1166 goto nla_put_failure;
1167 mpls_key = nla_data(nla);
1168 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001169 }
1170
1171 if ((swkey->eth.type == htons(ETH_P_IP) ||
1172 swkey->eth.type == htons(ETH_P_IPV6)) &&
1173 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1174
1175 if (swkey->ip.proto == IPPROTO_TCP) {
1176 struct ovs_key_tcp *tcp_key;
1177
1178 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1179 if (!nla)
1180 goto nla_put_failure;
1181 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001182 tcp_key->tcp_src = output->tp.src;
1183 tcp_key->tcp_dst = output->tp.dst;
1184 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1185 output->tp.flags))
1186 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001187 } else if (swkey->ip.proto == IPPROTO_UDP) {
1188 struct ovs_key_udp *udp_key;
1189
1190 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1191 if (!nla)
1192 goto nla_put_failure;
1193 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001194 udp_key->udp_src = output->tp.src;
1195 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001196 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1197 struct ovs_key_sctp *sctp_key;
1198
1199 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1200 if (!nla)
1201 goto nla_put_failure;
1202 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001203 sctp_key->sctp_src = output->tp.src;
1204 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001205 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1206 swkey->ip.proto == IPPROTO_ICMP) {
1207 struct ovs_key_icmp *icmp_key;
1208
1209 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1210 if (!nla)
1211 goto nla_put_failure;
1212 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001213 icmp_key->icmp_type = ntohs(output->tp.src);
1214 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001215 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1216 swkey->ip.proto == IPPROTO_ICMPV6) {
1217 struct ovs_key_icmpv6 *icmpv6_key;
1218
1219 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1220 sizeof(*icmpv6_key));
1221 if (!nla)
1222 goto nla_put_failure;
1223 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001224 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1225 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001226
1227 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1228 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1229 struct ovs_key_nd *nd_key;
1230
1231 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1232 if (!nla)
1233 goto nla_put_failure;
1234 nd_key = nla_data(nla);
1235 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1236 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001237 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1238 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001239 }
1240 }
1241 }
1242
1243unencap:
1244 if (encap)
1245 nla_nest_end(skb, encap);
1246
1247 return 0;
1248
1249nla_put_failure:
1250 return -EMSGSIZE;
1251}
1252
1253#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1254
1255struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1256{
1257 struct sw_flow_actions *sfa;
1258
Jesse Gross426cda52014-10-06 05:08:38 -07001259 if (size > MAX_ACTIONS_BUFSIZE) {
1260 OVS_NLERR("Flow action size (%u bytes) exceeds maximum", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001261 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001262 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001263
1264 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1265 if (!sfa)
1266 return ERR_PTR(-ENOMEM);
1267
1268 sfa->actions_len = 0;
1269 return sfa;
1270}
1271
Pravin B Shelare6445712013-10-03 18:16:47 -07001272/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1273 * The caller must hold rcu_read_lock for this to be sensible. */
1274void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1275{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001276 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001277}
1278
1279static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1280 int attr_len)
1281{
1282
1283 struct sw_flow_actions *acts;
1284 int new_acts_size;
1285 int req_size = NLA_ALIGN(attr_len);
1286 int next_offset = offsetof(struct sw_flow_actions, actions) +
1287 (*sfa)->actions_len;
1288
1289 if (req_size <= (ksize(*sfa) - next_offset))
1290 goto out;
1291
1292 new_acts_size = ksize(*sfa) * 2;
1293
1294 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1295 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1296 return ERR_PTR(-EMSGSIZE);
1297 new_acts_size = MAX_ACTIONS_BUFSIZE;
1298 }
1299
1300 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1301 if (IS_ERR(acts))
1302 return (void *)acts;
1303
1304 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1305 acts->actions_len = (*sfa)->actions_len;
1306 kfree(*sfa);
1307 *sfa = acts;
1308
1309out:
1310 (*sfa)->actions_len += req_size;
1311 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1312}
1313
Jesse Grossf0b128c2014-10-03 15:35:31 -07001314static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1315 int attrtype, void *data, int len)
Pravin B Shelare6445712013-10-03 18:16:47 -07001316{
1317 struct nlattr *a;
1318
1319 a = reserve_sfa_size(sfa, nla_attr_size(len));
1320 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001321 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001322
1323 a->nla_type = attrtype;
1324 a->nla_len = nla_attr_size(len);
1325
1326 if (data)
1327 memcpy(nla_data(a), data, len);
1328 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1329
Jesse Grossf0b128c2014-10-03 15:35:31 -07001330 return a;
1331}
1332
1333static int add_action(struct sw_flow_actions **sfa, int attrtype,
1334 void *data, int len)
1335{
1336 struct nlattr *a;
1337
1338 a = __add_action(sfa, attrtype, data, len);
1339 if (IS_ERR(a))
1340 return PTR_ERR(a);
1341
Pravin B Shelare6445712013-10-03 18:16:47 -07001342 return 0;
1343}
1344
1345static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1346 int attrtype)
1347{
1348 int used = (*sfa)->actions_len;
1349 int err;
1350
1351 err = add_action(sfa, attrtype, NULL, 0);
1352 if (err)
1353 return err;
1354
1355 return used;
1356}
1357
1358static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1359 int st_offset)
1360{
1361 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1362 st_offset);
1363
1364 a->nla_len = sfa->actions_len - st_offset;
1365}
1366
Simon Horman25cd9ba2014-10-06 05:05:13 -07001367static int ovs_nla_copy_actions__(const struct nlattr *attr,
1368 const struct sw_flow_key *key,
1369 int depth, struct sw_flow_actions **sfa,
1370 __be16 eth_type, __be16 vlan_tci);
1371
Pravin B Shelare6445712013-10-03 18:16:47 -07001372static int validate_and_copy_sample(const struct nlattr *attr,
1373 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001374 struct sw_flow_actions **sfa,
1375 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001376{
1377 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1378 const struct nlattr *probability, *actions;
1379 const struct nlattr *a;
1380 int rem, start, err, st_acts;
1381
1382 memset(attrs, 0, sizeof(attrs));
1383 nla_for_each_nested(a, attr, rem) {
1384 int type = nla_type(a);
1385 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1386 return -EINVAL;
1387 attrs[type] = a;
1388 }
1389 if (rem)
1390 return -EINVAL;
1391
1392 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1393 if (!probability || nla_len(probability) != sizeof(u32))
1394 return -EINVAL;
1395
1396 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1397 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1398 return -EINVAL;
1399
1400 /* validation done, copy sample action. */
1401 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1402 if (start < 0)
1403 return start;
1404 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1405 nla_data(probability), sizeof(u32));
1406 if (err)
1407 return err;
1408 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1409 if (st_acts < 0)
1410 return st_acts;
1411
Simon Horman25cd9ba2014-10-06 05:05:13 -07001412 err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
1413 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001414 if (err)
1415 return err;
1416
1417 add_nested_action_end(*sfa, st_acts);
1418 add_nested_action_end(*sfa, start);
1419
1420 return 0;
1421}
1422
Simon Horman25cd9ba2014-10-06 05:05:13 -07001423static int validate_tp_port(const struct sw_flow_key *flow_key,
1424 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001425{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001426 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001427 (flow_key->tp.src || flow_key->tp.dst))
1428 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001429
1430 return -EINVAL;
1431}
1432
1433void ovs_match_init(struct sw_flow_match *match,
1434 struct sw_flow_key *key,
1435 struct sw_flow_mask *mask)
1436{
1437 memset(match, 0, sizeof(*match));
1438 match->key = key;
1439 match->mask = mask;
1440
1441 memset(key, 0, sizeof(*key));
1442
1443 if (mask) {
1444 memset(&mask->key, 0, sizeof(mask->key));
1445 mask->range.start = mask->range.end = 0;
1446 }
1447}
1448
1449static int validate_and_copy_set_tun(const struct nlattr *attr,
1450 struct sw_flow_actions **sfa)
1451{
1452 struct sw_flow_match match;
1453 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001454 struct ovs_tunnel_info *tun_info;
1455 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001456 int err, start;
1457
1458 ovs_match_init(&match, &key, NULL);
1459 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1460 if (err)
1461 return err;
1462
Jesse Grossf5796682014-10-03 15:35:33 -07001463 if (key.tun_opts_len) {
1464 struct geneve_opt *option = GENEVE_OPTS(&key,
1465 key.tun_opts_len);
1466 int opts_len = key.tun_opts_len;
1467 bool crit_opt = false;
1468
1469 while (opts_len > 0) {
1470 int len;
1471
1472 if (opts_len < sizeof(*option))
1473 return -EINVAL;
1474
1475 len = sizeof(*option) + option->length * 4;
1476 if (len > opts_len)
1477 return -EINVAL;
1478
1479 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1480
1481 option = (struct geneve_opt *)((u8 *)option + len);
1482 opts_len -= len;
1483 };
1484
1485 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1486 };
1487
Pravin B Shelare6445712013-10-03 18:16:47 -07001488 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1489 if (start < 0)
1490 return start;
1491
Jesse Grossf0b128c2014-10-03 15:35:31 -07001492 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jesse Grossf5796682014-10-03 15:35:33 -07001493 sizeof(*tun_info) + key.tun_opts_len);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001494 if (IS_ERR(a))
1495 return PTR_ERR(a);
1496
1497 tun_info = nla_data(a);
1498 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001499 tun_info->options_len = key.tun_opts_len;
1500
1501 if (tun_info->options_len) {
1502 /* We need to store the options in the action itself since
1503 * everything else will go away after flow setup. We can append
1504 * it to tun_info and then point there.
1505 */
1506 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1507 key.tun_opts_len);
1508 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1509 } else {
1510 tun_info->options = NULL;
1511 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001512
Pravin B Shelare6445712013-10-03 18:16:47 -07001513 add_nested_action_end(*sfa, start);
1514
1515 return err;
1516}
1517
1518static int validate_set(const struct nlattr *a,
1519 const struct sw_flow_key *flow_key,
1520 struct sw_flow_actions **sfa,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001521 bool *set_tun, __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001522{
1523 const struct nlattr *ovs_key = nla_data(a);
1524 int key_type = nla_type(ovs_key);
1525
1526 /* There can be only one key in a action */
1527 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1528 return -EINVAL;
1529
1530 if (key_type > OVS_KEY_ATTR_MAX ||
1531 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1532 ovs_key_lens[key_type] != -1))
1533 return -EINVAL;
1534
1535 switch (key_type) {
1536 const struct ovs_key_ipv4 *ipv4_key;
1537 const struct ovs_key_ipv6 *ipv6_key;
1538 int err;
1539
1540 case OVS_KEY_ATTR_PRIORITY:
1541 case OVS_KEY_ATTR_SKB_MARK:
1542 case OVS_KEY_ATTR_ETHERNET:
1543 break;
1544
1545 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001546 if (eth_p_mpls(eth_type))
1547 return -EINVAL;
1548
Pravin B Shelare6445712013-10-03 18:16:47 -07001549 *set_tun = true;
1550 err = validate_and_copy_set_tun(a, sfa);
1551 if (err)
1552 return err;
1553 break;
1554
1555 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001556 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001557 return -EINVAL;
1558
1559 if (!flow_key->ip.proto)
1560 return -EINVAL;
1561
1562 ipv4_key = nla_data(ovs_key);
1563 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1564 return -EINVAL;
1565
1566 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1567 return -EINVAL;
1568
1569 break;
1570
1571 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001572 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001573 return -EINVAL;
1574
1575 if (!flow_key->ip.proto)
1576 return -EINVAL;
1577
1578 ipv6_key = nla_data(ovs_key);
1579 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1580 return -EINVAL;
1581
1582 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1583 return -EINVAL;
1584
1585 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1586 return -EINVAL;
1587
1588 break;
1589
1590 case OVS_KEY_ATTR_TCP:
1591 if (flow_key->ip.proto != IPPROTO_TCP)
1592 return -EINVAL;
1593
Simon Horman25cd9ba2014-10-06 05:05:13 -07001594 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001595
1596 case OVS_KEY_ATTR_UDP:
1597 if (flow_key->ip.proto != IPPROTO_UDP)
1598 return -EINVAL;
1599
Simon Horman25cd9ba2014-10-06 05:05:13 -07001600 return validate_tp_port(flow_key, eth_type);
1601
1602 case OVS_KEY_ATTR_MPLS:
1603 if (!eth_p_mpls(eth_type))
1604 return -EINVAL;
1605 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001606
1607 case OVS_KEY_ATTR_SCTP:
1608 if (flow_key->ip.proto != IPPROTO_SCTP)
1609 return -EINVAL;
1610
Simon Horman25cd9ba2014-10-06 05:05:13 -07001611 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001612
1613 default:
1614 return -EINVAL;
1615 }
1616
1617 return 0;
1618}
1619
1620static int validate_userspace(const struct nlattr *attr)
1621{
1622 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1623 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1624 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1625 };
1626 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1627 int error;
1628
1629 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1630 attr, userspace_policy);
1631 if (error)
1632 return error;
1633
1634 if (!a[OVS_USERSPACE_ATTR_PID] ||
1635 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1636 return -EINVAL;
1637
1638 return 0;
1639}
1640
1641static int copy_action(const struct nlattr *from,
1642 struct sw_flow_actions **sfa)
1643{
1644 int totlen = NLA_ALIGN(from->nla_len);
1645 struct nlattr *to;
1646
1647 to = reserve_sfa_size(sfa, from->nla_len);
1648 if (IS_ERR(to))
1649 return PTR_ERR(to);
1650
1651 memcpy(to, from, totlen);
1652 return 0;
1653}
1654
Simon Horman25cd9ba2014-10-06 05:05:13 -07001655static int ovs_nla_copy_actions__(const struct nlattr *attr,
1656 const struct sw_flow_key *key,
1657 int depth, struct sw_flow_actions **sfa,
1658 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001659{
1660 const struct nlattr *a;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001661 bool out_tnl_port = false;
Pravin B Shelare6445712013-10-03 18:16:47 -07001662 int rem, err;
1663
1664 if (depth >= SAMPLE_ACTION_DEPTH)
1665 return -EOVERFLOW;
1666
1667 nla_for_each_nested(a, attr, rem) {
1668 /* Expected argument lengths, (u32)-1 for variable length. */
1669 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1670 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001671 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001672 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001673 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1674 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001675 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1676 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1677 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001678 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1679 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001680 };
1681 const struct ovs_action_push_vlan *vlan;
1682 int type = nla_type(a);
1683 bool skip_copy;
1684
1685 if (type > OVS_ACTION_ATTR_MAX ||
1686 (action_lens[type] != nla_len(a) &&
1687 action_lens[type] != (u32)-1))
1688 return -EINVAL;
1689
1690 skip_copy = false;
1691 switch (type) {
1692 case OVS_ACTION_ATTR_UNSPEC:
1693 return -EINVAL;
1694
1695 case OVS_ACTION_ATTR_USERSPACE:
1696 err = validate_userspace(a);
1697 if (err)
1698 return err;
1699 break;
1700
1701 case OVS_ACTION_ATTR_OUTPUT:
1702 if (nla_get_u32(a) >= DP_MAX_PORTS)
1703 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001704 out_tnl_port = false;
1705
Pravin B Shelare6445712013-10-03 18:16:47 -07001706 break;
1707
Andy Zhou971427f32014-09-15 19:37:25 -07001708 case OVS_ACTION_ATTR_HASH: {
1709 const struct ovs_action_hash *act_hash = nla_data(a);
1710
1711 switch (act_hash->hash_alg) {
1712 case OVS_HASH_ALG_L4:
1713 break;
1714 default:
1715 return -EINVAL;
1716 }
1717
1718 break;
1719 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001720
1721 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001722 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001723 break;
1724
1725 case OVS_ACTION_ATTR_PUSH_VLAN:
1726 vlan = nla_data(a);
1727 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1728 return -EINVAL;
1729 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1730 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001731 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001732 break;
1733
Andy Zhou971427f32014-09-15 19:37:25 -07001734 case OVS_ACTION_ATTR_RECIRC:
1735 break;
1736
Simon Horman25cd9ba2014-10-06 05:05:13 -07001737 case OVS_ACTION_ATTR_PUSH_MPLS: {
1738 const struct ovs_action_push_mpls *mpls = nla_data(a);
1739
1740 /* Networking stack do not allow simultaneous Tunnel
1741 * and MPLS GSO.
1742 */
1743 if (out_tnl_port)
1744 return -EINVAL;
1745
1746 if (!eth_p_mpls(mpls->mpls_ethertype))
1747 return -EINVAL;
1748 /* Prohibit push MPLS other than to a white list
1749 * for packets that have a known tag order.
1750 */
1751 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1752 (eth_type != htons(ETH_P_IP) &&
1753 eth_type != htons(ETH_P_IPV6) &&
1754 eth_type != htons(ETH_P_ARP) &&
1755 eth_type != htons(ETH_P_RARP) &&
1756 !eth_p_mpls(eth_type)))
1757 return -EINVAL;
1758 eth_type = mpls->mpls_ethertype;
1759 break;
1760 }
1761
1762 case OVS_ACTION_ATTR_POP_MPLS:
1763 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1764 !eth_p_mpls(eth_type))
1765 return -EINVAL;
1766
1767 /* Disallow subsequent L2.5+ set and mpls_pop actions
1768 * as there is no check here to ensure that the new
1769 * eth_type is valid and thus set actions could
1770 * write off the end of the packet or otherwise
1771 * corrupt it.
1772 *
1773 * Support for these actions is planned using packet
1774 * recirculation.
1775 */
1776 eth_type = htons(0);
1777 break;
1778
Pravin B Shelare6445712013-10-03 18:16:47 -07001779 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001780 err = validate_set(a, key, sfa,
1781 &out_tnl_port, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001782 if (err)
1783 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001784
1785 skip_copy = out_tnl_port;
Pravin B Shelare6445712013-10-03 18:16:47 -07001786 break;
1787
1788 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001789 err = validate_and_copy_sample(a, key, depth, sfa,
1790 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001791 if (err)
1792 return err;
1793 skip_copy = true;
1794 break;
1795
1796 default:
Jesse Gross426cda52014-10-06 05:08:38 -07001797 OVS_NLERR("Unknown tunnel attribute (%d).\n", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001798 return -EINVAL;
1799 }
1800 if (!skip_copy) {
1801 err = copy_action(a, sfa);
1802 if (err)
1803 return err;
1804 }
1805 }
1806
1807 if (rem > 0)
1808 return -EINVAL;
1809
1810 return 0;
1811}
1812
Simon Horman25cd9ba2014-10-06 05:05:13 -07001813int ovs_nla_copy_actions(const struct nlattr *attr,
1814 const struct sw_flow_key *key,
1815 struct sw_flow_actions **sfa)
1816{
1817 return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
1818 key->eth.tci);
1819}
1820
Pravin B Shelare6445712013-10-03 18:16:47 -07001821static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1822{
1823 const struct nlattr *a;
1824 struct nlattr *start;
1825 int err = 0, rem;
1826
1827 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1828 if (!start)
1829 return -EMSGSIZE;
1830
1831 nla_for_each_nested(a, attr, rem) {
1832 int type = nla_type(a);
1833 struct nlattr *st_sample;
1834
1835 switch (type) {
1836 case OVS_SAMPLE_ATTR_PROBABILITY:
1837 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1838 sizeof(u32), nla_data(a)))
1839 return -EMSGSIZE;
1840 break;
1841 case OVS_SAMPLE_ATTR_ACTIONS:
1842 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1843 if (!st_sample)
1844 return -EMSGSIZE;
1845 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1846 if (err)
1847 return err;
1848 nla_nest_end(skb, st_sample);
1849 break;
1850 }
1851 }
1852
1853 nla_nest_end(skb, start);
1854 return err;
1855}
1856
1857static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1858{
1859 const struct nlattr *ovs_key = nla_data(a);
1860 int key_type = nla_type(ovs_key);
1861 struct nlattr *start;
1862 int err;
1863
1864 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001865 case OVS_KEY_ATTR_TUNNEL_INFO: {
1866 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1867
Pravin B Shelare6445712013-10-03 18:16:47 -07001868 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1869 if (!start)
1870 return -EMSGSIZE;
1871
Jesse Grossf0b128c2014-10-03 15:35:31 -07001872 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001873 tun_info->options_len ?
1874 tun_info->options : NULL,
1875 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001876 if (err)
1877 return err;
1878 nla_nest_end(skb, start);
1879 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001880 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001881 default:
1882 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1883 return -EMSGSIZE;
1884 break;
1885 }
1886
1887 return 0;
1888}
1889
1890int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1891{
1892 const struct nlattr *a;
1893 int rem, err;
1894
1895 nla_for_each_attr(a, attr, len, rem) {
1896 int type = nla_type(a);
1897
1898 switch (type) {
1899 case OVS_ACTION_ATTR_SET:
1900 err = set_action_to_attr(a, skb);
1901 if (err)
1902 return err;
1903 break;
1904
1905 case OVS_ACTION_ATTR_SAMPLE:
1906 err = sample_action_to_attr(a, skb);
1907 if (err)
1908 return err;
1909 break;
1910 default:
1911 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1912 return -EMSGSIZE;
1913 break;
1914 }
1915 }
1916
1917 return 0;
1918}