blob: 5a91d792505dbbc9118a0e5f30a831384cd238a0 [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;
Pravin B Shelare6445712013-10-03 18:16:47 -0700614
615 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
616 if (err)
617 return err;
618
619 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
620 const struct ovs_key_ethernet *eth_key;
621
622 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
623 SW_FLOW_KEY_MEMCPY(match, eth.src,
624 eth_key->eth_src, ETH_ALEN, is_mask);
625 SW_FLOW_KEY_MEMCPY(match, eth.dst,
626 eth_key->eth_dst, ETH_ALEN, is_mask);
627 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
628 }
629
630 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
631 __be16 tci;
632
633 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
634 if (!(tci & htons(VLAN_TAG_PRESENT))) {
635 if (is_mask)
636 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
637 else
638 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
639
640 return -EINVAL;
641 }
642
643 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
644 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
645 } else if (!is_mask)
646 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
647
648 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
649 __be16 eth_type;
650
651 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
652 if (is_mask) {
653 /* Always exact match EtherType. */
654 eth_type = htons(0xffff);
655 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
656 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
657 ntohs(eth_type), ETH_P_802_3_MIN);
658 return -EINVAL;
659 }
660
661 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
662 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
663 } else if (!is_mask) {
664 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
665 }
666
667 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
668 const struct ovs_key_ipv4 *ipv4_key;
669
670 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
671 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
672 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
673 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
674 return -EINVAL;
675 }
676 SW_FLOW_KEY_PUT(match, ip.proto,
677 ipv4_key->ipv4_proto, is_mask);
678 SW_FLOW_KEY_PUT(match, ip.tos,
679 ipv4_key->ipv4_tos, is_mask);
680 SW_FLOW_KEY_PUT(match, ip.ttl,
681 ipv4_key->ipv4_ttl, is_mask);
682 SW_FLOW_KEY_PUT(match, ip.frag,
683 ipv4_key->ipv4_frag, is_mask);
684 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
685 ipv4_key->ipv4_src, is_mask);
686 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
687 ipv4_key->ipv4_dst, is_mask);
688 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
689 }
690
691 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
692 const struct ovs_key_ipv6 *ipv6_key;
693
694 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
695 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
696 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
697 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
698 return -EINVAL;
699 }
700 SW_FLOW_KEY_PUT(match, ipv6.label,
701 ipv6_key->ipv6_label, is_mask);
702 SW_FLOW_KEY_PUT(match, ip.proto,
703 ipv6_key->ipv6_proto, is_mask);
704 SW_FLOW_KEY_PUT(match, ip.tos,
705 ipv6_key->ipv6_tclass, is_mask);
706 SW_FLOW_KEY_PUT(match, ip.ttl,
707 ipv6_key->ipv6_hlimit, is_mask);
708 SW_FLOW_KEY_PUT(match, ip.frag,
709 ipv6_key->ipv6_frag, is_mask);
710 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
711 ipv6_key->ipv6_src,
712 sizeof(match->key->ipv6.addr.src),
713 is_mask);
714 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
715 ipv6_key->ipv6_dst,
716 sizeof(match->key->ipv6.addr.dst),
717 is_mask);
718
719 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
720 }
721
722 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
723 const struct ovs_key_arp *arp_key;
724
725 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
726 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
727 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
728 arp_key->arp_op);
729 return -EINVAL;
730 }
731
732 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
733 arp_key->arp_sip, is_mask);
734 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
735 arp_key->arp_tip, is_mask);
736 SW_FLOW_KEY_PUT(match, ip.proto,
737 ntohs(arp_key->arp_op), is_mask);
738 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
739 arp_key->arp_sha, ETH_ALEN, is_mask);
740 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
741 arp_key->arp_tha, ETH_ALEN, is_mask);
742
743 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
744 }
745
Simon Horman25cd9ba2014-10-06 05:05:13 -0700746 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
747 const struct ovs_key_mpls *mpls_key;
748
749 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
750 SW_FLOW_KEY_PUT(match, mpls.top_lse,
751 mpls_key->mpls_lse, is_mask);
752
753 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
754 }
755
Pravin B Shelare6445712013-10-03 18:16:47 -0700756 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
757 const struct ovs_key_tcp *tcp_key;
758
759 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700760 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
761 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700762 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
763 }
764
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700765 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700766 SW_FLOW_KEY_PUT(match, tp.flags,
767 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
768 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700769 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
770 }
771
Pravin B Shelare6445712013-10-03 18:16:47 -0700772 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
773 const struct ovs_key_udp *udp_key;
774
775 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700776 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
777 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700778 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
779 }
780
781 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
782 const struct ovs_key_sctp *sctp_key;
783
784 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700785 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
786 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700787 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
788 }
789
790 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
791 const struct ovs_key_icmp *icmp_key;
792
793 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700794 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700795 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700796 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700797 htons(icmp_key->icmp_code), is_mask);
798 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
799 }
800
801 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
802 const struct ovs_key_icmpv6 *icmpv6_key;
803
804 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700805 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700806 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700807 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700808 htons(icmpv6_key->icmpv6_code), is_mask);
809 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
810 }
811
812 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
813 const struct ovs_key_nd *nd_key;
814
815 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
816 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
817 nd_key->nd_target,
818 sizeof(match->key->ipv6.nd.target),
819 is_mask);
820 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
821 nd_key->nd_sll, ETH_ALEN, is_mask);
822 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
823 nd_key->nd_tll, ETH_ALEN, is_mask);
824 attrs &= ~(1 << OVS_KEY_ATTR_ND);
825 }
826
827 if (attrs != 0)
828 return -EINVAL;
829
830 return 0;
831}
832
Pravin B Shelarf47de062014-10-16 21:55:45 -0700833static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700834{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700835 struct nlattr *nla;
836 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700837
Pravin B Shelarf47de062014-10-16 21:55:45 -0700838 /* The nlattr stream should already have been validated */
839 nla_for_each_nested(nla, attr, rem) {
840 /* We assume that ovs_key_lens[type] == -1 means that type is a
841 * nested attribute
842 */
843 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
844 nlattr_set(nla, val, false);
845 else
846 memset(nla_data(nla), val, nla_len(nla));
847 }
848}
849
850static void mask_set_nlattr(struct nlattr *attr, u8 val)
851{
852 nlattr_set(attr, val, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700853}
854
855/**
856 * ovs_nla_get_match - parses Netlink attributes into a flow key and
857 * mask. In case the 'mask' is NULL, the flow is treated as exact match
858 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
859 * does not include any don't care bit.
860 * @match: receives the extracted flow match information.
861 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
862 * sequence. The fields should of the packet that triggered the creation
863 * of this flow.
864 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
865 * attribute specifies the mask field of the wildcarded flow.
866 */
867int ovs_nla_get_match(struct sw_flow_match *match,
868 const struct nlattr *key,
869 const struct nlattr *mask)
870{
871 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
872 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700873 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700874 u64 key_attrs = 0;
875 u64 mask_attrs = 0;
876 bool encap_valid = false;
877 int err;
878
879 err = parse_flow_nlattrs(key, a, &key_attrs);
880 if (err)
881 return err;
882
883 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
884 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
885 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
886 __be16 tci;
887
888 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
889 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
890 OVS_NLERR("Invalid Vlan frame.\n");
891 return -EINVAL;
892 }
893
894 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
895 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
896 encap = a[OVS_KEY_ATTR_ENCAP];
897 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
898 encap_valid = true;
899
900 if (tci & htons(VLAN_TAG_PRESENT)) {
901 err = parse_flow_nlattrs(encap, a, &key_attrs);
902 if (err)
903 return err;
904 } else if (!tci) {
905 /* Corner case for truncated 802.1Q header. */
906 if (nla_len(encap)) {
907 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
908 return -EINVAL;
909 }
910 } else {
911 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
912 return -EINVAL;
913 }
914 }
915
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700916 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700917 if (err)
918 return err;
919
Pravin B Shelarf47de062014-10-16 21:55:45 -0700920 if (match->mask && !mask) {
921 /* Create an exact match mask. We need to set to 0xff all the
922 * 'match->mask' fields that have been touched in 'match->key'.
923 * We cannot simply memset 'match->mask', because padding bytes
924 * and fields not specified in 'match->key' should be left to 0.
925 * Instead, we use a stream of netlink attributes, copied from
926 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
927 * of filling 'match->mask' appropriately.
928 */
929 newmask = kmemdup(key, nla_total_size(nla_len(key)),
930 GFP_KERNEL);
931 if (!newmask)
932 return -ENOMEM;
933
934 mask_set_nlattr(newmask, 0xff);
935
936 /* The userspace does not send tunnel attributes that are 0,
937 * but we should not wildcard them nonetheless.
938 */
939 if (match->key->tun_key.ipv4_dst)
940 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
941
942 mask = newmask;
943 }
944
Pravin B Shelare6445712013-10-03 18:16:47 -0700945 if (mask) {
946 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
947 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700948 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700949
Pravin B Shelarf47de062014-10-16 21:55:45 -0700950 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700951 __be16 eth_type = 0;
952 __be16 tci = 0;
953
954 if (!encap_valid) {
955 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
Pravin B Shelarf47de062014-10-16 21:55:45 -0700956 err = -EINVAL;
957 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700958 }
959
960 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
961 if (a[OVS_KEY_ATTR_ETHERTYPE])
962 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
963
964 if (eth_type == htons(0xffff)) {
965 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
966 encap = a[OVS_KEY_ATTR_ENCAP];
967 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
Pravin B Shelarf47de062014-10-16 21:55:45 -0700968 if (err)
969 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700970 } else {
971 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
972 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -0700973 err = -EINVAL;
974 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700975 }
976
977 if (a[OVS_KEY_ATTR_VLAN])
978 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
979
980 if (!(tci & htons(VLAN_TAG_PRESENT))) {
981 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 -0700982 err = -EINVAL;
983 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700984 }
985 }
986
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700987 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700988 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -0700989 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -0700990 }
991
992 if (!match_validate(match, key_attrs, mask_attrs))
Pravin B Shelarf47de062014-10-16 21:55:45 -0700993 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700994
Pravin B Shelarf47de062014-10-16 21:55:45 -0700995free_newmask:
996 kfree(newmask);
997 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700998}
999
1000/**
1001 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001002 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001003 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1004 * sequence.
1005 *
1006 * This parses a series of Netlink attributes that form a flow key, which must
1007 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1008 * get the metadata, that is, the parts of the flow key that cannot be
1009 * extracted from the packet itself.
1010 */
1011
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001012int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1013 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -07001014{
Pravin B Shelare6445712013-10-03 18:16:47 -07001015 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001016 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001017 u64 attrs = 0;
1018 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001019
1020 err = parse_flow_nlattrs(attr, a, &attrs);
1021 if (err)
1022 return -EINVAL;
1023
1024 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001025 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001026
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001027 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001028
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001029 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -07001030}
1031
1032int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1033 const struct sw_flow_key *output, struct sk_buff *skb)
1034{
1035 struct ovs_key_ethernet *eth_key;
1036 struct nlattr *nla, *encap;
1037 bool is_mask = (swkey != output);
1038
Andy Zhou971427f32014-09-15 19:37:25 -07001039 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1040 goto nla_put_failure;
1041
1042 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1043 goto nla_put_failure;
1044
Pravin B Shelare6445712013-10-03 18:16:47 -07001045 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1046 goto nla_put_failure;
1047
Jesse Grossf5796682014-10-03 15:35:33 -07001048 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1049 const struct geneve_opt *opts = NULL;
1050
1051 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1052 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1053
1054 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1055 swkey->tun_opts_len))
1056 goto nla_put_failure;
1057 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001058
1059 if (swkey->phy.in_port == DP_MAX_PORTS) {
1060 if (is_mask && (output->phy.in_port == 0xffff))
1061 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1062 goto nla_put_failure;
1063 } else {
1064 u16 upper_u16;
1065 upper_u16 = !is_mask ? 0 : 0xffff;
1066
1067 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1068 (upper_u16 << 16) | output->phy.in_port))
1069 goto nla_put_failure;
1070 }
1071
1072 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1073 goto nla_put_failure;
1074
1075 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1076 if (!nla)
1077 goto nla_put_failure;
1078
1079 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001080 ether_addr_copy(eth_key->eth_src, output->eth.src);
1081 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001082
1083 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1084 __be16 eth_type;
1085 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1086 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1087 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1088 goto nla_put_failure;
1089 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1090 if (!swkey->eth.tci)
1091 goto unencap;
1092 } else
1093 encap = NULL;
1094
1095 if (swkey->eth.type == htons(ETH_P_802_2)) {
1096 /*
1097 * Ethertype 802.2 is represented in the netlink with omitted
1098 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1099 * 0xffff in the mask attribute. Ethertype can also
1100 * be wildcarded.
1101 */
1102 if (is_mask && output->eth.type)
1103 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1104 output->eth.type))
1105 goto nla_put_failure;
1106 goto unencap;
1107 }
1108
1109 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1110 goto nla_put_failure;
1111
1112 if (swkey->eth.type == htons(ETH_P_IP)) {
1113 struct ovs_key_ipv4 *ipv4_key;
1114
1115 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1116 if (!nla)
1117 goto nla_put_failure;
1118 ipv4_key = nla_data(nla);
1119 ipv4_key->ipv4_src = output->ipv4.addr.src;
1120 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1121 ipv4_key->ipv4_proto = output->ip.proto;
1122 ipv4_key->ipv4_tos = output->ip.tos;
1123 ipv4_key->ipv4_ttl = output->ip.ttl;
1124 ipv4_key->ipv4_frag = output->ip.frag;
1125 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1126 struct ovs_key_ipv6 *ipv6_key;
1127
1128 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1129 if (!nla)
1130 goto nla_put_failure;
1131 ipv6_key = nla_data(nla);
1132 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1133 sizeof(ipv6_key->ipv6_src));
1134 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1135 sizeof(ipv6_key->ipv6_dst));
1136 ipv6_key->ipv6_label = output->ipv6.label;
1137 ipv6_key->ipv6_proto = output->ip.proto;
1138 ipv6_key->ipv6_tclass = output->ip.tos;
1139 ipv6_key->ipv6_hlimit = output->ip.ttl;
1140 ipv6_key->ipv6_frag = output->ip.frag;
1141 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1142 swkey->eth.type == htons(ETH_P_RARP)) {
1143 struct ovs_key_arp *arp_key;
1144
1145 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1146 if (!nla)
1147 goto nla_put_failure;
1148 arp_key = nla_data(nla);
1149 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1150 arp_key->arp_sip = output->ipv4.addr.src;
1151 arp_key->arp_tip = output->ipv4.addr.dst;
1152 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001153 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1154 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001155 } else if (eth_p_mpls(swkey->eth.type)) {
1156 struct ovs_key_mpls *mpls_key;
1157
1158 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1159 if (!nla)
1160 goto nla_put_failure;
1161 mpls_key = nla_data(nla);
1162 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001163 }
1164
1165 if ((swkey->eth.type == htons(ETH_P_IP) ||
1166 swkey->eth.type == htons(ETH_P_IPV6)) &&
1167 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1168
1169 if (swkey->ip.proto == IPPROTO_TCP) {
1170 struct ovs_key_tcp *tcp_key;
1171
1172 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1173 if (!nla)
1174 goto nla_put_failure;
1175 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001176 tcp_key->tcp_src = output->tp.src;
1177 tcp_key->tcp_dst = output->tp.dst;
1178 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1179 output->tp.flags))
1180 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001181 } else if (swkey->ip.proto == IPPROTO_UDP) {
1182 struct ovs_key_udp *udp_key;
1183
1184 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1185 if (!nla)
1186 goto nla_put_failure;
1187 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001188 udp_key->udp_src = output->tp.src;
1189 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001190 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1191 struct ovs_key_sctp *sctp_key;
1192
1193 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1194 if (!nla)
1195 goto nla_put_failure;
1196 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001197 sctp_key->sctp_src = output->tp.src;
1198 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001199 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1200 swkey->ip.proto == IPPROTO_ICMP) {
1201 struct ovs_key_icmp *icmp_key;
1202
1203 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1204 if (!nla)
1205 goto nla_put_failure;
1206 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001207 icmp_key->icmp_type = ntohs(output->tp.src);
1208 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001209 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1210 swkey->ip.proto == IPPROTO_ICMPV6) {
1211 struct ovs_key_icmpv6 *icmpv6_key;
1212
1213 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1214 sizeof(*icmpv6_key));
1215 if (!nla)
1216 goto nla_put_failure;
1217 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001218 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1219 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001220
1221 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1222 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1223 struct ovs_key_nd *nd_key;
1224
1225 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1226 if (!nla)
1227 goto nla_put_failure;
1228 nd_key = nla_data(nla);
1229 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1230 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001231 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1232 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001233 }
1234 }
1235 }
1236
1237unencap:
1238 if (encap)
1239 nla_nest_end(skb, encap);
1240
1241 return 0;
1242
1243nla_put_failure:
1244 return -EMSGSIZE;
1245}
1246
1247#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1248
1249struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1250{
1251 struct sw_flow_actions *sfa;
1252
1253 if (size > MAX_ACTIONS_BUFSIZE)
1254 return ERR_PTR(-EINVAL);
1255
1256 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1257 if (!sfa)
1258 return ERR_PTR(-ENOMEM);
1259
1260 sfa->actions_len = 0;
1261 return sfa;
1262}
1263
Pravin B Shelare6445712013-10-03 18:16:47 -07001264/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1265 * The caller must hold rcu_read_lock for this to be sensible. */
1266void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1267{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001268 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001269}
1270
1271static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1272 int attr_len)
1273{
1274
1275 struct sw_flow_actions *acts;
1276 int new_acts_size;
1277 int req_size = NLA_ALIGN(attr_len);
1278 int next_offset = offsetof(struct sw_flow_actions, actions) +
1279 (*sfa)->actions_len;
1280
1281 if (req_size <= (ksize(*sfa) - next_offset))
1282 goto out;
1283
1284 new_acts_size = ksize(*sfa) * 2;
1285
1286 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1287 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1288 return ERR_PTR(-EMSGSIZE);
1289 new_acts_size = MAX_ACTIONS_BUFSIZE;
1290 }
1291
1292 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1293 if (IS_ERR(acts))
1294 return (void *)acts;
1295
1296 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1297 acts->actions_len = (*sfa)->actions_len;
1298 kfree(*sfa);
1299 *sfa = acts;
1300
1301out:
1302 (*sfa)->actions_len += req_size;
1303 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1304}
1305
Jesse Grossf0b128c2014-10-03 15:35:31 -07001306static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1307 int attrtype, void *data, int len)
Pravin B Shelare6445712013-10-03 18:16:47 -07001308{
1309 struct nlattr *a;
1310
1311 a = reserve_sfa_size(sfa, nla_attr_size(len));
1312 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001313 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001314
1315 a->nla_type = attrtype;
1316 a->nla_len = nla_attr_size(len);
1317
1318 if (data)
1319 memcpy(nla_data(a), data, len);
1320 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1321
Jesse Grossf0b128c2014-10-03 15:35:31 -07001322 return a;
1323}
1324
1325static int add_action(struct sw_flow_actions **sfa, int attrtype,
1326 void *data, int len)
1327{
1328 struct nlattr *a;
1329
1330 a = __add_action(sfa, attrtype, data, len);
1331 if (IS_ERR(a))
1332 return PTR_ERR(a);
1333
Pravin B Shelare6445712013-10-03 18:16:47 -07001334 return 0;
1335}
1336
1337static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1338 int attrtype)
1339{
1340 int used = (*sfa)->actions_len;
1341 int err;
1342
1343 err = add_action(sfa, attrtype, NULL, 0);
1344 if (err)
1345 return err;
1346
1347 return used;
1348}
1349
1350static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1351 int st_offset)
1352{
1353 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1354 st_offset);
1355
1356 a->nla_len = sfa->actions_len - st_offset;
1357}
1358
Simon Horman25cd9ba2014-10-06 05:05:13 -07001359static int ovs_nla_copy_actions__(const struct nlattr *attr,
1360 const struct sw_flow_key *key,
1361 int depth, struct sw_flow_actions **sfa,
1362 __be16 eth_type, __be16 vlan_tci);
1363
Pravin B Shelare6445712013-10-03 18:16:47 -07001364static int validate_and_copy_sample(const struct nlattr *attr,
1365 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001366 struct sw_flow_actions **sfa,
1367 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001368{
1369 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1370 const struct nlattr *probability, *actions;
1371 const struct nlattr *a;
1372 int rem, start, err, st_acts;
1373
1374 memset(attrs, 0, sizeof(attrs));
1375 nla_for_each_nested(a, attr, rem) {
1376 int type = nla_type(a);
1377 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1378 return -EINVAL;
1379 attrs[type] = a;
1380 }
1381 if (rem)
1382 return -EINVAL;
1383
1384 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1385 if (!probability || nla_len(probability) != sizeof(u32))
1386 return -EINVAL;
1387
1388 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1389 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1390 return -EINVAL;
1391
1392 /* validation done, copy sample action. */
1393 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1394 if (start < 0)
1395 return start;
1396 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1397 nla_data(probability), sizeof(u32));
1398 if (err)
1399 return err;
1400 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1401 if (st_acts < 0)
1402 return st_acts;
1403
Simon Horman25cd9ba2014-10-06 05:05:13 -07001404 err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
1405 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001406 if (err)
1407 return err;
1408
1409 add_nested_action_end(*sfa, st_acts);
1410 add_nested_action_end(*sfa, start);
1411
1412 return 0;
1413}
1414
Simon Horman25cd9ba2014-10-06 05:05:13 -07001415static int validate_tp_port(const struct sw_flow_key *flow_key,
1416 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001417{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001418 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001419 (flow_key->tp.src || flow_key->tp.dst))
1420 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001421
1422 return -EINVAL;
1423}
1424
1425void ovs_match_init(struct sw_flow_match *match,
1426 struct sw_flow_key *key,
1427 struct sw_flow_mask *mask)
1428{
1429 memset(match, 0, sizeof(*match));
1430 match->key = key;
1431 match->mask = mask;
1432
1433 memset(key, 0, sizeof(*key));
1434
1435 if (mask) {
1436 memset(&mask->key, 0, sizeof(mask->key));
1437 mask->range.start = mask->range.end = 0;
1438 }
1439}
1440
1441static int validate_and_copy_set_tun(const struct nlattr *attr,
1442 struct sw_flow_actions **sfa)
1443{
1444 struct sw_flow_match match;
1445 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001446 struct ovs_tunnel_info *tun_info;
1447 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001448 int err, start;
1449
1450 ovs_match_init(&match, &key, NULL);
1451 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1452 if (err)
1453 return err;
1454
Jesse Grossf5796682014-10-03 15:35:33 -07001455 if (key.tun_opts_len) {
1456 struct geneve_opt *option = GENEVE_OPTS(&key,
1457 key.tun_opts_len);
1458 int opts_len = key.tun_opts_len;
1459 bool crit_opt = false;
1460
1461 while (opts_len > 0) {
1462 int len;
1463
1464 if (opts_len < sizeof(*option))
1465 return -EINVAL;
1466
1467 len = sizeof(*option) + option->length * 4;
1468 if (len > opts_len)
1469 return -EINVAL;
1470
1471 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1472
1473 option = (struct geneve_opt *)((u8 *)option + len);
1474 opts_len -= len;
1475 };
1476
1477 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1478 };
1479
Pravin B Shelare6445712013-10-03 18:16:47 -07001480 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1481 if (start < 0)
1482 return start;
1483
Jesse Grossf0b128c2014-10-03 15:35:31 -07001484 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jesse Grossf5796682014-10-03 15:35:33 -07001485 sizeof(*tun_info) + key.tun_opts_len);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001486 if (IS_ERR(a))
1487 return PTR_ERR(a);
1488
1489 tun_info = nla_data(a);
1490 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001491 tun_info->options_len = key.tun_opts_len;
1492
1493 if (tun_info->options_len) {
1494 /* We need to store the options in the action itself since
1495 * everything else will go away after flow setup. We can append
1496 * it to tun_info and then point there.
1497 */
1498 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1499 key.tun_opts_len);
1500 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1501 } else {
1502 tun_info->options = NULL;
1503 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001504
Pravin B Shelare6445712013-10-03 18:16:47 -07001505 add_nested_action_end(*sfa, start);
1506
1507 return err;
1508}
1509
1510static int validate_set(const struct nlattr *a,
1511 const struct sw_flow_key *flow_key,
1512 struct sw_flow_actions **sfa,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001513 bool *set_tun, __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001514{
1515 const struct nlattr *ovs_key = nla_data(a);
1516 int key_type = nla_type(ovs_key);
1517
1518 /* There can be only one key in a action */
1519 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1520 return -EINVAL;
1521
1522 if (key_type > OVS_KEY_ATTR_MAX ||
1523 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1524 ovs_key_lens[key_type] != -1))
1525 return -EINVAL;
1526
1527 switch (key_type) {
1528 const struct ovs_key_ipv4 *ipv4_key;
1529 const struct ovs_key_ipv6 *ipv6_key;
1530 int err;
1531
1532 case OVS_KEY_ATTR_PRIORITY:
1533 case OVS_KEY_ATTR_SKB_MARK:
1534 case OVS_KEY_ATTR_ETHERNET:
1535 break;
1536
1537 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001538 if (eth_p_mpls(eth_type))
1539 return -EINVAL;
1540
Pravin B Shelare6445712013-10-03 18:16:47 -07001541 *set_tun = true;
1542 err = validate_and_copy_set_tun(a, sfa);
1543 if (err)
1544 return err;
1545 break;
1546
1547 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001548 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001549 return -EINVAL;
1550
1551 if (!flow_key->ip.proto)
1552 return -EINVAL;
1553
1554 ipv4_key = nla_data(ovs_key);
1555 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1556 return -EINVAL;
1557
1558 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1559 return -EINVAL;
1560
1561 break;
1562
1563 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001564 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001565 return -EINVAL;
1566
1567 if (!flow_key->ip.proto)
1568 return -EINVAL;
1569
1570 ipv6_key = nla_data(ovs_key);
1571 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1572 return -EINVAL;
1573
1574 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1575 return -EINVAL;
1576
1577 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1578 return -EINVAL;
1579
1580 break;
1581
1582 case OVS_KEY_ATTR_TCP:
1583 if (flow_key->ip.proto != IPPROTO_TCP)
1584 return -EINVAL;
1585
Simon Horman25cd9ba2014-10-06 05:05:13 -07001586 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001587
1588 case OVS_KEY_ATTR_UDP:
1589 if (flow_key->ip.proto != IPPROTO_UDP)
1590 return -EINVAL;
1591
Simon Horman25cd9ba2014-10-06 05:05:13 -07001592 return validate_tp_port(flow_key, eth_type);
1593
1594 case OVS_KEY_ATTR_MPLS:
1595 if (!eth_p_mpls(eth_type))
1596 return -EINVAL;
1597 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001598
1599 case OVS_KEY_ATTR_SCTP:
1600 if (flow_key->ip.proto != IPPROTO_SCTP)
1601 return -EINVAL;
1602
Simon Horman25cd9ba2014-10-06 05:05:13 -07001603 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001604
1605 default:
1606 return -EINVAL;
1607 }
1608
1609 return 0;
1610}
1611
1612static int validate_userspace(const struct nlattr *attr)
1613{
1614 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1615 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1616 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1617 };
1618 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1619 int error;
1620
1621 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1622 attr, userspace_policy);
1623 if (error)
1624 return error;
1625
1626 if (!a[OVS_USERSPACE_ATTR_PID] ||
1627 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1628 return -EINVAL;
1629
1630 return 0;
1631}
1632
1633static int copy_action(const struct nlattr *from,
1634 struct sw_flow_actions **sfa)
1635{
1636 int totlen = NLA_ALIGN(from->nla_len);
1637 struct nlattr *to;
1638
1639 to = reserve_sfa_size(sfa, from->nla_len);
1640 if (IS_ERR(to))
1641 return PTR_ERR(to);
1642
1643 memcpy(to, from, totlen);
1644 return 0;
1645}
1646
Simon Horman25cd9ba2014-10-06 05:05:13 -07001647static int ovs_nla_copy_actions__(const struct nlattr *attr,
1648 const struct sw_flow_key *key,
1649 int depth, struct sw_flow_actions **sfa,
1650 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001651{
1652 const struct nlattr *a;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001653 bool out_tnl_port = false;
Pravin B Shelare6445712013-10-03 18:16:47 -07001654 int rem, err;
1655
1656 if (depth >= SAMPLE_ACTION_DEPTH)
1657 return -EOVERFLOW;
1658
1659 nla_for_each_nested(a, attr, rem) {
1660 /* Expected argument lengths, (u32)-1 for variable length. */
1661 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1662 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001663 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001664 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001665 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1666 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001667 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1668 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1669 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001670 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1671 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001672 };
1673 const struct ovs_action_push_vlan *vlan;
1674 int type = nla_type(a);
1675 bool skip_copy;
1676
1677 if (type > OVS_ACTION_ATTR_MAX ||
1678 (action_lens[type] != nla_len(a) &&
1679 action_lens[type] != (u32)-1))
1680 return -EINVAL;
1681
1682 skip_copy = false;
1683 switch (type) {
1684 case OVS_ACTION_ATTR_UNSPEC:
1685 return -EINVAL;
1686
1687 case OVS_ACTION_ATTR_USERSPACE:
1688 err = validate_userspace(a);
1689 if (err)
1690 return err;
1691 break;
1692
1693 case OVS_ACTION_ATTR_OUTPUT:
1694 if (nla_get_u32(a) >= DP_MAX_PORTS)
1695 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001696 out_tnl_port = false;
1697
Pravin B Shelare6445712013-10-03 18:16:47 -07001698 break;
1699
Andy Zhou971427f32014-09-15 19:37:25 -07001700 case OVS_ACTION_ATTR_HASH: {
1701 const struct ovs_action_hash *act_hash = nla_data(a);
1702
1703 switch (act_hash->hash_alg) {
1704 case OVS_HASH_ALG_L4:
1705 break;
1706 default:
1707 return -EINVAL;
1708 }
1709
1710 break;
1711 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001712
1713 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001714 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001715 break;
1716
1717 case OVS_ACTION_ATTR_PUSH_VLAN:
1718 vlan = nla_data(a);
1719 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1720 return -EINVAL;
1721 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1722 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001723 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001724 break;
1725
Andy Zhou971427f32014-09-15 19:37:25 -07001726 case OVS_ACTION_ATTR_RECIRC:
1727 break;
1728
Simon Horman25cd9ba2014-10-06 05:05:13 -07001729 case OVS_ACTION_ATTR_PUSH_MPLS: {
1730 const struct ovs_action_push_mpls *mpls = nla_data(a);
1731
1732 /* Networking stack do not allow simultaneous Tunnel
1733 * and MPLS GSO.
1734 */
1735 if (out_tnl_port)
1736 return -EINVAL;
1737
1738 if (!eth_p_mpls(mpls->mpls_ethertype))
1739 return -EINVAL;
1740 /* Prohibit push MPLS other than to a white list
1741 * for packets that have a known tag order.
1742 */
1743 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1744 (eth_type != htons(ETH_P_IP) &&
1745 eth_type != htons(ETH_P_IPV6) &&
1746 eth_type != htons(ETH_P_ARP) &&
1747 eth_type != htons(ETH_P_RARP) &&
1748 !eth_p_mpls(eth_type)))
1749 return -EINVAL;
1750 eth_type = mpls->mpls_ethertype;
1751 break;
1752 }
1753
1754 case OVS_ACTION_ATTR_POP_MPLS:
1755 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1756 !eth_p_mpls(eth_type))
1757 return -EINVAL;
1758
1759 /* Disallow subsequent L2.5+ set and mpls_pop actions
1760 * as there is no check here to ensure that the new
1761 * eth_type is valid and thus set actions could
1762 * write off the end of the packet or otherwise
1763 * corrupt it.
1764 *
1765 * Support for these actions is planned using packet
1766 * recirculation.
1767 */
1768 eth_type = htons(0);
1769 break;
1770
Pravin B Shelare6445712013-10-03 18:16:47 -07001771 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001772 err = validate_set(a, key, sfa,
1773 &out_tnl_port, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001774 if (err)
1775 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001776
1777 skip_copy = out_tnl_port;
Pravin B Shelare6445712013-10-03 18:16:47 -07001778 break;
1779
1780 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001781 err = validate_and_copy_sample(a, key, depth, sfa,
1782 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001783 if (err)
1784 return err;
1785 skip_copy = true;
1786 break;
1787
1788 default:
1789 return -EINVAL;
1790 }
1791 if (!skip_copy) {
1792 err = copy_action(a, sfa);
1793 if (err)
1794 return err;
1795 }
1796 }
1797
1798 if (rem > 0)
1799 return -EINVAL;
1800
1801 return 0;
1802}
1803
Simon Horman25cd9ba2014-10-06 05:05:13 -07001804int ovs_nla_copy_actions(const struct nlattr *attr,
1805 const struct sw_flow_key *key,
1806 struct sw_flow_actions **sfa)
1807{
1808 return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
1809 key->eth.tci);
1810}
1811
Pravin B Shelare6445712013-10-03 18:16:47 -07001812static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1813{
1814 const struct nlattr *a;
1815 struct nlattr *start;
1816 int err = 0, rem;
1817
1818 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1819 if (!start)
1820 return -EMSGSIZE;
1821
1822 nla_for_each_nested(a, attr, rem) {
1823 int type = nla_type(a);
1824 struct nlattr *st_sample;
1825
1826 switch (type) {
1827 case OVS_SAMPLE_ATTR_PROBABILITY:
1828 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1829 sizeof(u32), nla_data(a)))
1830 return -EMSGSIZE;
1831 break;
1832 case OVS_SAMPLE_ATTR_ACTIONS:
1833 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1834 if (!st_sample)
1835 return -EMSGSIZE;
1836 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1837 if (err)
1838 return err;
1839 nla_nest_end(skb, st_sample);
1840 break;
1841 }
1842 }
1843
1844 nla_nest_end(skb, start);
1845 return err;
1846}
1847
1848static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1849{
1850 const struct nlattr *ovs_key = nla_data(a);
1851 int key_type = nla_type(ovs_key);
1852 struct nlattr *start;
1853 int err;
1854
1855 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001856 case OVS_KEY_ATTR_TUNNEL_INFO: {
1857 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1858
Pravin B Shelare6445712013-10-03 18:16:47 -07001859 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1860 if (!start)
1861 return -EMSGSIZE;
1862
Jesse Grossf0b128c2014-10-03 15:35:31 -07001863 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001864 tun_info->options_len ?
1865 tun_info->options : NULL,
1866 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001867 if (err)
1868 return err;
1869 nla_nest_end(skb, start);
1870 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001871 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001872 default:
1873 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1874 return -EMSGSIZE;
1875 break;
1876 }
1877
1878 return 0;
1879}
1880
1881int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1882{
1883 const struct nlattr *a;
1884 int rem, err;
1885
1886 nla_for_each_attr(a, attr, len, rem) {
1887 int type = nla_type(a);
1888
1889 switch (type) {
1890 case OVS_ACTION_ATTR_SET:
1891 err = set_action_to_attr(a, skb);
1892 if (err)
1893 return err;
1894 break;
1895
1896 case OVS_ACTION_ATTR_SAMPLE:
1897 err = sample_action_to_attr(a, skb);
1898 if (err)
1899 return err;
1900 break;
1901 default:
1902 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1903 return -EMSGSIZE;
1904 break;
1905 }
1906 }
1907
1908 return 0;
1909}