blob: 98a3e96b7d930aac9689b86b25f994c6ea2f1a6d [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
Pravin B Shelara85311b2014-10-19 12:03:40 -070053static void update_range(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070055{
Pravin B Shelara85311b2014-10-19 12:03:40 -070056 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070057 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;
Pravin B Shelara85311b2014-10-19 12:03:40 -070062 else
Pravin B Shelare6445712013-10-03 18:16:47 -070063 range = &match->mask->range;
64
Pravin B Shelare6445712013-10-03 18:16:47 -070065 if (range->start == range->end) {
66 range->start = start;
67 range->end = end;
68 return;
69 }
70
71 if (range->start > start)
72 range->start = start;
73
74 if (range->end < end)
75 range->end = end;
76}
77
78#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
79 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070080 update_range(match, offsetof(struct sw_flow_key, field), \
81 sizeof((match)->key->field), is_mask); \
82 if (is_mask) \
83 (match)->mask->key.field = value; \
84 else \
Pravin B Shelare6445712013-10-03 18:16:47 -070085 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -070086 } while (0)
87
Jesse Grossf5796682014-10-03 15:35:33 -070088#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
89 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070090 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -070091 if (is_mask) \
92 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -070093 len); \
Jesse Grossf5796682014-10-03 15:35:33 -070094 else \
95 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -070096 } while (0)
97
Jesse Grossf5796682014-10-03 15:35:33 -070098#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
99 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
100 value_p, len, is_mask)
101
Pravin B Shelara85311b2014-10-19 12:03:40 -0700102#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
103 do { \
104 update_range(match, offsetof(struct sw_flow_key, field), \
105 sizeof((match)->key->field), is_mask); \
106 if (is_mask) \
107 memset((u8 *)&(match)->mask->key.field, value, \
108 sizeof((match)->mask->key.field)); \
109 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700110 memset((u8 *)&(match)->key->field, value, \
111 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700112 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700113
114static bool match_validate(const struct sw_flow_match *match,
115 u64 key_attrs, u64 mask_attrs)
116{
117 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
118 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
119
120 /* The following mask attributes allowed only if they
121 * pass the validation tests. */
122 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
123 | (1 << OVS_KEY_ATTR_IPV6)
124 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700125 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700126 | (1 << OVS_KEY_ATTR_UDP)
127 | (1 << OVS_KEY_ATTR_SCTP)
128 | (1 << OVS_KEY_ATTR_ICMP)
129 | (1 << OVS_KEY_ATTR_ICMPV6)
130 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700131 | (1 << OVS_KEY_ATTR_ND)
132 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700133
134 /* Always allowed mask fields. */
135 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
136 | (1 << OVS_KEY_ATTR_IN_PORT)
137 | (1 << OVS_KEY_ATTR_ETHERTYPE));
138
139 /* Check key attributes. */
140 if (match->key->eth.type == htons(ETH_P_ARP)
141 || match->key->eth.type == htons(ETH_P_RARP)) {
142 key_expected |= 1 << OVS_KEY_ATTR_ARP;
143 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
145 }
146
Simon Horman25cd9ba2014-10-06 05:05:13 -0700147 if (eth_p_mpls(match->key->eth.type)) {
148 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
149 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
150 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
151 }
152
Pravin B Shelare6445712013-10-03 18:16:47 -0700153 if (match->key->eth.type == htons(ETH_P_IP)) {
154 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
155 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
156 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
157
158 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
159 if (match->key->ip.proto == IPPROTO_UDP) {
160 key_expected |= 1 << OVS_KEY_ATTR_UDP;
161 if (match->mask && (match->mask->key.ip.proto == 0xff))
162 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
163 }
164
165 if (match->key->ip.proto == IPPROTO_SCTP) {
166 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
167 if (match->mask && (match->mask->key.ip.proto == 0xff))
168 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
169 }
170
171 if (match->key->ip.proto == IPPROTO_TCP) {
172 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700173 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
174 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700175 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700176 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
177 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700178 }
179
180 if (match->key->ip.proto == IPPROTO_ICMP) {
181 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
182 if (match->mask && (match->mask->key.ip.proto == 0xff))
183 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
184 }
185 }
186 }
187
188 if (match->key->eth.type == htons(ETH_P_IPV6)) {
189 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
190 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
191 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
192
193 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
194 if (match->key->ip.proto == IPPROTO_UDP) {
195 key_expected |= 1 << OVS_KEY_ATTR_UDP;
196 if (match->mask && (match->mask->key.ip.proto == 0xff))
197 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
198 }
199
200 if (match->key->ip.proto == IPPROTO_SCTP) {
201 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
202 if (match->mask && (match->mask->key.ip.proto == 0xff))
203 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
204 }
205
206 if (match->key->ip.proto == IPPROTO_TCP) {
207 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700208 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
209 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700210 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700211 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
212 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700213 }
214
215 if (match->key->ip.proto == IPPROTO_ICMPV6) {
216 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
217 if (match->mask && (match->mask->key.ip.proto == 0xff))
218 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
219
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700220 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700221 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700222 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700223 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700224 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700225 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
226 }
227 }
228 }
229 }
230
231 if ((key_attrs & key_expected) != key_expected) {
232 /* Key attributes check failed. */
233 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800234 (unsigned long long)key_attrs, (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700235 return false;
236 }
237
238 if ((mask_attrs & mask_allowed) != mask_attrs) {
239 /* Mask attributes check failed. */
240 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800241 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700242 return false;
243 }
244
245 return true;
246}
247
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800248size_t ovs_tun_key_attr_size(void)
249{
250 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
251 * updating this function.
252 */
253 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
254 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
255 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
256 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
257 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
258 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
259 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
260 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
261 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
262 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
263 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
264}
265
Joe Stringer41af73e2014-10-18 16:14:14 -0700266size_t ovs_key_attr_size(void)
267{
268 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
269 * updating this function.
270 */
271 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
272
273 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
274 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800275 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700276 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
277 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
278 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
279 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
280 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
281 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
282 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
283 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
284 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
285 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
286 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
287 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
288}
289
Pravin B Shelare6445712013-10-03 18:16:47 -0700290/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
291static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
292 [OVS_KEY_ATTR_ENCAP] = -1,
293 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
294 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
295 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
296 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
297 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
298 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
299 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
300 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
301 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700302 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -0700303 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
304 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
305 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
306 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
307 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
308 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Andy Zhou971427f32014-09-15 19:37:25 -0700309 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
310 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -0700311 [OVS_KEY_ATTR_TUNNEL] = -1,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700312 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
Pravin B Shelare6445712013-10-03 18:16:47 -0700313};
314
315static bool is_all_zero(const u8 *fp, size_t size)
316{
317 int i;
318
319 if (!fp)
320 return false;
321
322 for (i = 0; i < size; i++)
323 if (fp[i])
324 return false;
325
326 return true;
327}
328
329static int __parse_flow_nlattrs(const struct nlattr *attr,
330 const struct nlattr *a[],
331 u64 *attrsp, bool nz)
332{
333 const struct nlattr *nla;
334 u64 attrs;
335 int rem;
336
337 attrs = *attrsp;
338 nla_for_each_nested(nla, attr, rem) {
339 u16 type = nla_type(nla);
340 int expected_len;
341
342 if (type > OVS_KEY_ATTR_MAX) {
343 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
344 type, OVS_KEY_ATTR_MAX);
345 return -EINVAL;
346 }
347
348 if (attrs & (1 << type)) {
349 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
350 return -EINVAL;
351 }
352
353 expected_len = ovs_key_lens[type];
354 if (nla_len(nla) != expected_len && expected_len != -1) {
355 OVS_NLERR("Key attribute has unexpected length (type=%d"
356 ", length=%d, expected=%d).\n", type,
357 nla_len(nla), expected_len);
358 return -EINVAL;
359 }
360
361 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
362 attrs |= 1 << type;
363 a[type] = nla;
364 }
365 }
366 if (rem) {
367 OVS_NLERR("Message has %d unknown bytes.\n", rem);
368 return -EINVAL;
369 }
370
371 *attrsp = attrs;
372 return 0;
373}
374
375static int parse_flow_mask_nlattrs(const struct nlattr *attr,
376 const struct nlattr *a[], u64 *attrsp)
377{
378 return __parse_flow_nlattrs(attr, a, attrsp, true);
379}
380
381static int parse_flow_nlattrs(const struct nlattr *attr,
382 const struct nlattr *a[], u64 *attrsp)
383{
384 return __parse_flow_nlattrs(attr, a, attrsp, false);
385}
386
387static int ipv4_tun_from_nlattr(const struct nlattr *attr,
388 struct sw_flow_match *match, bool is_mask)
389{
390 struct nlattr *a;
391 int rem;
392 bool ttl = false;
393 __be16 tun_flags = 0;
Jesse Grossf5796682014-10-03 15:35:33 -0700394 unsigned long opt_key_offset;
Pravin B Shelare6445712013-10-03 18:16:47 -0700395
396 nla_for_each_nested(a, attr, rem) {
397 int type = nla_type(a);
398 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
399 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
400 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
401 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
402 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
403 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
404 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
405 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800406 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = sizeof(u16),
407 [OVS_TUNNEL_KEY_ATTR_TP_DST] = sizeof(u16),
Jesse Gross67fa0342014-10-03 15:35:30 -0700408 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
Jesse Grossf5796682014-10-03 15:35:33 -0700409 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
Pravin B Shelare6445712013-10-03 18:16:47 -0700410 };
411
412 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
413 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
414 type, OVS_TUNNEL_KEY_ATTR_MAX);
415 return -EINVAL;
416 }
417
Jesse Grossf5796682014-10-03 15:35:33 -0700418 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
419 ovs_tunnel_key_lens[type] != -1) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700420 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
421 " length (type=%d, length=%d, expected=%d).\n",
422 type, nla_len(a), ovs_tunnel_key_lens[type]);
423 return -EINVAL;
424 }
425
426 switch (type) {
427 case OVS_TUNNEL_KEY_ATTR_ID:
428 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
429 nla_get_be64(a), is_mask);
430 tun_flags |= TUNNEL_KEY;
431 break;
432 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
433 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
434 nla_get_be32(a), is_mask);
435 break;
436 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
437 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
438 nla_get_be32(a), is_mask);
439 break;
440 case OVS_TUNNEL_KEY_ATTR_TOS:
441 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
442 nla_get_u8(a), is_mask);
443 break;
444 case OVS_TUNNEL_KEY_ATTR_TTL:
445 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
446 nla_get_u8(a), is_mask);
447 ttl = true;
448 break;
449 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
450 tun_flags |= TUNNEL_DONT_FRAGMENT;
451 break;
452 case OVS_TUNNEL_KEY_ATTR_CSUM:
453 tun_flags |= TUNNEL_CSUM;
454 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800455 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
456 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
457 nla_get_be16(a), is_mask);
458 break;
459 case OVS_TUNNEL_KEY_ATTR_TP_DST:
460 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
461 nla_get_be16(a), is_mask);
462 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700463 case OVS_TUNNEL_KEY_ATTR_OAM:
464 tun_flags |= TUNNEL_OAM;
465 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700466 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
467 tun_flags |= TUNNEL_OPTIONS_PRESENT;
468 if (nla_len(a) > sizeof(match->key->tun_opts)) {
469 OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
470 nla_len(a),
471 sizeof(match->key->tun_opts));
472 return -EINVAL;
473 }
474
475 if (nla_len(a) % 4 != 0) {
476 OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
477 nla_len(a));
478 return -EINVAL;
479 }
480
481 /* We need to record the length of the options passed
482 * down, otherwise packets with the same format but
483 * additional options will be silently matched.
484 */
485 if (!is_mask) {
486 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
487 false);
488 } else {
489 /* This is somewhat unusual because it looks at
490 * both the key and mask while parsing the
491 * attributes (and by extension assumes the key
492 * is parsed first). Normally, we would verify
493 * that each is the correct length and that the
494 * attributes line up in the validate function.
495 * However, that is difficult because this is
496 * variable length and we won't have the
497 * information later.
498 */
499 if (match->key->tun_opts_len != nla_len(a)) {
500 OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
501 match->key->tun_opts_len,
502 nla_len(a));
503 return -EINVAL;
504 }
505
506 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
507 true);
508 }
509
510 opt_key_offset = (unsigned long)GENEVE_OPTS(
511 (struct sw_flow_key *)0,
512 nla_len(a));
513 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset,
514 nla_data(a), nla_len(a),
515 is_mask);
516 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700517 default:
Jesse Grossf5796682014-10-03 15:35:33 -0700518 OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
519 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700520 return -EINVAL;
521 }
522 }
523
524 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
525
526 if (rem > 0) {
527 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
528 return -EINVAL;
529 }
530
531 if (!is_mask) {
532 if (!match->key->tun_key.ipv4_dst) {
533 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
534 return -EINVAL;
535 }
536
537 if (!ttl) {
538 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
539 return -EINVAL;
540 }
541 }
542
543 return 0;
544}
545
Jesse Grossf5796682014-10-03 15:35:33 -0700546static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
547 const struct ovs_key_ipv4_tunnel *output,
548 const struct geneve_opt *tun_opts,
549 int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700550{
Pravin B Shelare6445712013-10-03 18:16:47 -0700551 if (output->tun_flags & TUNNEL_KEY &&
552 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
553 return -EMSGSIZE;
554 if (output->ipv4_src &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700555 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700556 return -EMSGSIZE;
557 if (output->ipv4_dst &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700558 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700559 return -EMSGSIZE;
560 if (output->ipv4_tos &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700561 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700562 return -EMSGSIZE;
563 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
564 return -EMSGSIZE;
565 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700566 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700567 return -EMSGSIZE;
568 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700569 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
570 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800571 if (output->tp_src &&
572 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
573 return -EMSGSIZE;
574 if (output->tp_dst &&
575 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
576 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700577 if ((output->tun_flags & TUNNEL_OAM) &&
578 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700579 return -EMSGSIZE;
Jesse Grossf5796682014-10-03 15:35:33 -0700580 if (tun_opts &&
581 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
582 swkey_tun_opts_len, tun_opts))
583 return -EMSGSIZE;
584
585 return 0;
586}
587
Jesse Grossf5796682014-10-03 15:35:33 -0700588static int ipv4_tun_to_nlattr(struct sk_buff *skb,
589 const struct ovs_key_ipv4_tunnel *output,
590 const struct geneve_opt *tun_opts,
591 int swkey_tun_opts_len)
592{
593 struct nlattr *nla;
594 int err;
595
596 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
597 if (!nla)
598 return -EMSGSIZE;
599
600 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
601 if (err)
602 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700603
604 nla_nest_end(skb, nla);
605 return 0;
606}
607
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800608int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
609 const struct ovs_tunnel_info *egress_tun_info)
610{
611 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
612 egress_tun_info->options,
613 egress_tun_info->options_len);
614}
615
Pravin B Shelare6445712013-10-03 18:16:47 -0700616static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
617 const struct nlattr **a, bool is_mask)
618{
Andy Zhou971427f32014-09-15 19:37:25 -0700619 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
620 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
621
622 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
623 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
624 }
625
626 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
627 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
628
629 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
630 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
631 }
632
Pravin B Shelare6445712013-10-03 18:16:47 -0700633 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
634 SW_FLOW_KEY_PUT(match, phy.priority,
635 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
636 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
637 }
638
639 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
640 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
641
Jesse Gross426cda52014-10-06 05:08:38 -0700642 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700643 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700644 } else if (in_port >= DP_MAX_PORTS) {
645 OVS_NLERR("Port (%d) exceeds maximum allowable (%d).\n",
646 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700647 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700648 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700649
650 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
651 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
652 } else if (!is_mask) {
653 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
654 }
655
656 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
657 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
658
659 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
660 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
661 }
662 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
663 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
664 is_mask))
665 return -EINVAL;
666 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
667 }
668 return 0;
669}
670
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700671static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
672 const struct nlattr **a, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700673{
674 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700675
676 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
677 if (err)
678 return err;
679
680 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
681 const struct ovs_key_ethernet *eth_key;
682
683 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
684 SW_FLOW_KEY_MEMCPY(match, eth.src,
685 eth_key->eth_src, ETH_ALEN, is_mask);
686 SW_FLOW_KEY_MEMCPY(match, eth.dst,
687 eth_key->eth_dst, ETH_ALEN, is_mask);
688 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
689 }
690
691 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
692 __be16 tci;
693
694 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
695 if (!(tci & htons(VLAN_TAG_PRESENT))) {
696 if (is_mask)
697 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
698 else
699 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
700
701 return -EINVAL;
702 }
703
704 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
705 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700706 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700707
708 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
709 __be16 eth_type;
710
711 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
712 if (is_mask) {
713 /* Always exact match EtherType. */
714 eth_type = htons(0xffff);
715 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
716 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
717 ntohs(eth_type), ETH_P_802_3_MIN);
718 return -EINVAL;
719 }
720
721 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
722 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
723 } else if (!is_mask) {
724 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
725 }
726
727 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
728 const struct ovs_key_ipv4 *ipv4_key;
729
730 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
731 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
732 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
733 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
734 return -EINVAL;
735 }
736 SW_FLOW_KEY_PUT(match, ip.proto,
737 ipv4_key->ipv4_proto, is_mask);
738 SW_FLOW_KEY_PUT(match, ip.tos,
739 ipv4_key->ipv4_tos, is_mask);
740 SW_FLOW_KEY_PUT(match, ip.ttl,
741 ipv4_key->ipv4_ttl, is_mask);
742 SW_FLOW_KEY_PUT(match, ip.frag,
743 ipv4_key->ipv4_frag, is_mask);
744 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
745 ipv4_key->ipv4_src, is_mask);
746 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
747 ipv4_key->ipv4_dst, is_mask);
748 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
749 }
750
751 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
752 const struct ovs_key_ipv6 *ipv6_key;
753
754 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
755 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
756 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
757 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
758 return -EINVAL;
759 }
760 SW_FLOW_KEY_PUT(match, ipv6.label,
761 ipv6_key->ipv6_label, is_mask);
762 SW_FLOW_KEY_PUT(match, ip.proto,
763 ipv6_key->ipv6_proto, is_mask);
764 SW_FLOW_KEY_PUT(match, ip.tos,
765 ipv6_key->ipv6_tclass, is_mask);
766 SW_FLOW_KEY_PUT(match, ip.ttl,
767 ipv6_key->ipv6_hlimit, is_mask);
768 SW_FLOW_KEY_PUT(match, ip.frag,
769 ipv6_key->ipv6_frag, is_mask);
770 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
771 ipv6_key->ipv6_src,
772 sizeof(match->key->ipv6.addr.src),
773 is_mask);
774 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
775 ipv6_key->ipv6_dst,
776 sizeof(match->key->ipv6.addr.dst),
777 is_mask);
778
779 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
780 }
781
782 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
783 const struct ovs_key_arp *arp_key;
784
785 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
786 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
787 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
788 arp_key->arp_op);
789 return -EINVAL;
790 }
791
792 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
793 arp_key->arp_sip, is_mask);
794 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
795 arp_key->arp_tip, is_mask);
796 SW_FLOW_KEY_PUT(match, ip.proto,
797 ntohs(arp_key->arp_op), is_mask);
798 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
799 arp_key->arp_sha, ETH_ALEN, is_mask);
800 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
801 arp_key->arp_tha, ETH_ALEN, is_mask);
802
803 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
804 }
805
Simon Horman25cd9ba2014-10-06 05:05:13 -0700806 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
807 const struct ovs_key_mpls *mpls_key;
808
809 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
810 SW_FLOW_KEY_PUT(match, mpls.top_lse,
811 mpls_key->mpls_lse, is_mask);
812
813 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
814 }
815
Pravin B Shelare6445712013-10-03 18:16:47 -0700816 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
817 const struct ovs_key_tcp *tcp_key;
818
819 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700820 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
821 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700822 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
823 }
824
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700825 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700826 SW_FLOW_KEY_PUT(match, tp.flags,
827 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
828 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700829 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
830 }
831
Pravin B Shelare6445712013-10-03 18:16:47 -0700832 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
833 const struct ovs_key_udp *udp_key;
834
835 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700836 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
837 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700838 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
839 }
840
841 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
842 const struct ovs_key_sctp *sctp_key;
843
844 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700845 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
846 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700847 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
848 }
849
850 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
851 const struct ovs_key_icmp *icmp_key;
852
853 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700854 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700855 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700856 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700857 htons(icmp_key->icmp_code), is_mask);
858 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
859 }
860
861 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
862 const struct ovs_key_icmpv6 *icmpv6_key;
863
864 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700865 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700866 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700867 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700868 htons(icmpv6_key->icmpv6_code), is_mask);
869 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
870 }
871
872 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
873 const struct ovs_key_nd *nd_key;
874
875 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
876 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
877 nd_key->nd_target,
878 sizeof(match->key->ipv6.nd.target),
879 is_mask);
880 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
881 nd_key->nd_sll, ETH_ALEN, is_mask);
882 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
883 nd_key->nd_tll, ETH_ALEN, is_mask);
884 attrs &= ~(1 << OVS_KEY_ATTR_ND);
885 }
886
Jesse Gross426cda52014-10-06 05:08:38 -0700887 if (attrs != 0) {
888 OVS_NLERR("Unknown key attributes (%llx).\n",
889 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -0700890 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700891 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700892
893 return 0;
894}
895
Pravin B Shelarf47de062014-10-16 21:55:45 -0700896static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700897{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700898 struct nlattr *nla;
899 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700900
Pravin B Shelarf47de062014-10-16 21:55:45 -0700901 /* The nlattr stream should already have been validated */
902 nla_for_each_nested(nla, attr, rem) {
903 /* We assume that ovs_key_lens[type] == -1 means that type is a
904 * nested attribute
905 */
906 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
907 nlattr_set(nla, val, false);
908 else
909 memset(nla_data(nla), val, nla_len(nla));
910 }
911}
912
913static void mask_set_nlattr(struct nlattr *attr, u8 val)
914{
915 nlattr_set(attr, val, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700916}
917
918/**
919 * ovs_nla_get_match - parses Netlink attributes into a flow key and
920 * mask. In case the 'mask' is NULL, the flow is treated as exact match
921 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
922 * does not include any don't care bit.
923 * @match: receives the extracted flow match information.
924 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
925 * sequence. The fields should of the packet that triggered the creation
926 * of this flow.
927 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
928 * attribute specifies the mask field of the wildcarded flow.
929 */
930int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -0700931 const struct nlattr *nla_key,
932 const struct nlattr *nla_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700933{
934 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
935 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700936 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700937 u64 key_attrs = 0;
938 u64 mask_attrs = 0;
939 bool encap_valid = false;
940 int err;
941
Pravin B Shelara85311b2014-10-19 12:03:40 -0700942 err = parse_flow_nlattrs(nla_key, a, &key_attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -0700943 if (err)
944 return err;
945
946 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
947 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
948 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
949 __be16 tci;
950
951 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
952 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
953 OVS_NLERR("Invalid Vlan frame.\n");
954 return -EINVAL;
955 }
956
957 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
958 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
959 encap = a[OVS_KEY_ATTR_ENCAP];
960 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
961 encap_valid = true;
962
963 if (tci & htons(VLAN_TAG_PRESENT)) {
964 err = parse_flow_nlattrs(encap, a, &key_attrs);
965 if (err)
966 return err;
967 } else if (!tci) {
968 /* Corner case for truncated 802.1Q header. */
969 if (nla_len(encap)) {
970 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
971 return -EINVAL;
972 }
973 } else {
974 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
975 return -EINVAL;
976 }
977 }
978
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700979 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700980 if (err)
981 return err;
982
Pravin B Shelara85311b2014-10-19 12:03:40 -0700983 if (match->mask) {
984 if (!nla_mask) {
985 /* Create an exact match mask. We need to set to 0xff
986 * all the 'match->mask' fields that have been touched
987 * in 'match->key'. We cannot simply memset
988 * 'match->mask', because padding bytes and fields not
989 * specified in 'match->key' should be left to 0.
990 * Instead, we use a stream of netlink attributes,
991 * copied from 'key' and set to 0xff.
992 * ovs_key_from_nlattrs() will take care of filling
993 * 'match->mask' appropriately.
994 */
995 newmask = kmemdup(nla_key,
996 nla_total_size(nla_len(nla_key)),
997 GFP_KERNEL);
998 if (!newmask)
999 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001000
Pravin B Shelara85311b2014-10-19 12:03:40 -07001001 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001002
Pravin B Shelara85311b2014-10-19 12:03:40 -07001003 /* The userspace does not send tunnel attributes that
1004 * are 0, but we should not wildcard them nonetheless.
1005 */
1006 if (match->key->tun_key.ipv4_dst)
1007 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1008 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001009
Pravin B Shelara85311b2014-10-19 12:03:40 -07001010 nla_mask = newmask;
1011 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001012
Pravin B Shelara85311b2014-10-19 12:03:40 -07001013 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001014 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001015 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001016
Pravin B Shelara85311b2014-10-19 12:03:40 -07001017 /* Always match on tci. */
1018 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1019
Pravin B Shelarf47de062014-10-16 21:55:45 -07001020 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001021 __be16 eth_type = 0;
1022 __be16 tci = 0;
1023
1024 if (!encap_valid) {
1025 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001026 err = -EINVAL;
1027 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001028 }
1029
1030 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1031 if (a[OVS_KEY_ATTR_ETHERTYPE])
1032 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1033
1034 if (eth_type == htons(0xffff)) {
1035 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1036 encap = a[OVS_KEY_ATTR_ENCAP];
1037 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001038 if (err)
1039 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001040 } else {
1041 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1042 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001043 err = -EINVAL;
1044 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001045 }
1046
1047 if (a[OVS_KEY_ATTR_VLAN])
1048 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1049
1050 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1051 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 -07001052 err = -EINVAL;
1053 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001054 }
1055 }
1056
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001057 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -07001058 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001059 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001060 }
1061
1062 if (!match_validate(match, key_attrs, mask_attrs))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001063 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001064
Pravin B Shelarf47de062014-10-16 21:55:45 -07001065free_newmask:
1066 kfree(newmask);
1067 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001068}
1069
1070/**
1071 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001072 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001073 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1074 * sequence.
1075 *
1076 * This parses a series of Netlink attributes that form a flow key, which must
1077 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1078 * get the metadata, that is, the parts of the flow key that cannot be
1079 * extracted from the packet itself.
1080 */
1081
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001082int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1083 struct sw_flow_key *key)
Pravin B Shelare6445712013-10-03 18:16:47 -07001084{
Pravin B Shelare6445712013-10-03 18:16:47 -07001085 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001086 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001087 u64 attrs = 0;
1088 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001089
1090 err = parse_flow_nlattrs(attr, a, &attrs);
1091 if (err)
1092 return -EINVAL;
1093
1094 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001095 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001096
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001097 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001098
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001099 return metadata_from_nlattrs(&match, &attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -07001100}
1101
1102int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1103 const struct sw_flow_key *output, struct sk_buff *skb)
1104{
1105 struct ovs_key_ethernet *eth_key;
1106 struct nlattr *nla, *encap;
1107 bool is_mask = (swkey != output);
1108
Andy Zhou971427f32014-09-15 19:37:25 -07001109 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1110 goto nla_put_failure;
1111
1112 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1113 goto nla_put_failure;
1114
Pravin B Shelare6445712013-10-03 18:16:47 -07001115 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1116 goto nla_put_failure;
1117
Jesse Grossf5796682014-10-03 15:35:33 -07001118 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1119 const struct geneve_opt *opts = NULL;
1120
1121 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1122 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1123
1124 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1125 swkey->tun_opts_len))
1126 goto nla_put_failure;
1127 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001128
1129 if (swkey->phy.in_port == DP_MAX_PORTS) {
1130 if (is_mask && (output->phy.in_port == 0xffff))
1131 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1132 goto nla_put_failure;
1133 } else {
1134 u16 upper_u16;
1135 upper_u16 = !is_mask ? 0 : 0xffff;
1136
1137 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1138 (upper_u16 << 16) | output->phy.in_port))
1139 goto nla_put_failure;
1140 }
1141
1142 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1143 goto nla_put_failure;
1144
1145 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1146 if (!nla)
1147 goto nla_put_failure;
1148
1149 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001150 ether_addr_copy(eth_key->eth_src, output->eth.src);
1151 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001152
1153 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1154 __be16 eth_type;
1155 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1156 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1157 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1158 goto nla_put_failure;
1159 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1160 if (!swkey->eth.tci)
1161 goto unencap;
1162 } else
1163 encap = NULL;
1164
1165 if (swkey->eth.type == htons(ETH_P_802_2)) {
1166 /*
1167 * Ethertype 802.2 is represented in the netlink with omitted
1168 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1169 * 0xffff in the mask attribute. Ethertype can also
1170 * be wildcarded.
1171 */
1172 if (is_mask && output->eth.type)
1173 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1174 output->eth.type))
1175 goto nla_put_failure;
1176 goto unencap;
1177 }
1178
1179 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1180 goto nla_put_failure;
1181
1182 if (swkey->eth.type == htons(ETH_P_IP)) {
1183 struct ovs_key_ipv4 *ipv4_key;
1184
1185 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1186 if (!nla)
1187 goto nla_put_failure;
1188 ipv4_key = nla_data(nla);
1189 ipv4_key->ipv4_src = output->ipv4.addr.src;
1190 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1191 ipv4_key->ipv4_proto = output->ip.proto;
1192 ipv4_key->ipv4_tos = output->ip.tos;
1193 ipv4_key->ipv4_ttl = output->ip.ttl;
1194 ipv4_key->ipv4_frag = output->ip.frag;
1195 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1196 struct ovs_key_ipv6 *ipv6_key;
1197
1198 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1199 if (!nla)
1200 goto nla_put_failure;
1201 ipv6_key = nla_data(nla);
1202 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1203 sizeof(ipv6_key->ipv6_src));
1204 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1205 sizeof(ipv6_key->ipv6_dst));
1206 ipv6_key->ipv6_label = output->ipv6.label;
1207 ipv6_key->ipv6_proto = output->ip.proto;
1208 ipv6_key->ipv6_tclass = output->ip.tos;
1209 ipv6_key->ipv6_hlimit = output->ip.ttl;
1210 ipv6_key->ipv6_frag = output->ip.frag;
1211 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1212 swkey->eth.type == htons(ETH_P_RARP)) {
1213 struct ovs_key_arp *arp_key;
1214
1215 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1216 if (!nla)
1217 goto nla_put_failure;
1218 arp_key = nla_data(nla);
1219 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1220 arp_key->arp_sip = output->ipv4.addr.src;
1221 arp_key->arp_tip = output->ipv4.addr.dst;
1222 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001223 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1224 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001225 } else if (eth_p_mpls(swkey->eth.type)) {
1226 struct ovs_key_mpls *mpls_key;
1227
1228 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1229 if (!nla)
1230 goto nla_put_failure;
1231 mpls_key = nla_data(nla);
1232 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001233 }
1234
1235 if ((swkey->eth.type == htons(ETH_P_IP) ||
1236 swkey->eth.type == htons(ETH_P_IPV6)) &&
1237 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1238
1239 if (swkey->ip.proto == IPPROTO_TCP) {
1240 struct ovs_key_tcp *tcp_key;
1241
1242 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1243 if (!nla)
1244 goto nla_put_failure;
1245 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001246 tcp_key->tcp_src = output->tp.src;
1247 tcp_key->tcp_dst = output->tp.dst;
1248 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1249 output->tp.flags))
1250 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001251 } else if (swkey->ip.proto == IPPROTO_UDP) {
1252 struct ovs_key_udp *udp_key;
1253
1254 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1255 if (!nla)
1256 goto nla_put_failure;
1257 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001258 udp_key->udp_src = output->tp.src;
1259 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001260 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1261 struct ovs_key_sctp *sctp_key;
1262
1263 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1264 if (!nla)
1265 goto nla_put_failure;
1266 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001267 sctp_key->sctp_src = output->tp.src;
1268 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001269 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1270 swkey->ip.proto == IPPROTO_ICMP) {
1271 struct ovs_key_icmp *icmp_key;
1272
1273 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1274 if (!nla)
1275 goto nla_put_failure;
1276 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001277 icmp_key->icmp_type = ntohs(output->tp.src);
1278 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001279 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1280 swkey->ip.proto == IPPROTO_ICMPV6) {
1281 struct ovs_key_icmpv6 *icmpv6_key;
1282
1283 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1284 sizeof(*icmpv6_key));
1285 if (!nla)
1286 goto nla_put_failure;
1287 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001288 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1289 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001290
1291 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1292 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1293 struct ovs_key_nd *nd_key;
1294
1295 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1296 if (!nla)
1297 goto nla_put_failure;
1298 nd_key = nla_data(nla);
1299 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1300 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001301 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1302 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001303 }
1304 }
1305 }
1306
1307unencap:
1308 if (encap)
1309 nla_nest_end(skb, encap);
1310
1311 return 0;
1312
1313nla_put_failure:
1314 return -EMSGSIZE;
1315}
1316
1317#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1318
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001319static struct sw_flow_actions *nla_alloc_flow_actions(int size)
Pravin B Shelare6445712013-10-03 18:16:47 -07001320{
1321 struct sw_flow_actions *sfa;
1322
Jesse Gross426cda52014-10-06 05:08:38 -07001323 if (size > MAX_ACTIONS_BUFSIZE) {
1324 OVS_NLERR("Flow action size (%u bytes) exceeds maximum", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001325 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001326 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001327
1328 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1329 if (!sfa)
1330 return ERR_PTR(-ENOMEM);
1331
1332 sfa->actions_len = 0;
1333 return sfa;
1334}
1335
Pravin B Shelare6445712013-10-03 18:16:47 -07001336/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1337 * The caller must hold rcu_read_lock for this to be sensible. */
1338void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1339{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001340 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001341}
1342
1343static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1344 int attr_len)
1345{
1346
1347 struct sw_flow_actions *acts;
1348 int new_acts_size;
1349 int req_size = NLA_ALIGN(attr_len);
1350 int next_offset = offsetof(struct sw_flow_actions, actions) +
1351 (*sfa)->actions_len;
1352
1353 if (req_size <= (ksize(*sfa) - next_offset))
1354 goto out;
1355
1356 new_acts_size = ksize(*sfa) * 2;
1357
1358 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1359 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1360 return ERR_PTR(-EMSGSIZE);
1361 new_acts_size = MAX_ACTIONS_BUFSIZE;
1362 }
1363
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001364 acts = nla_alloc_flow_actions(new_acts_size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001365 if (IS_ERR(acts))
1366 return (void *)acts;
1367
1368 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1369 acts->actions_len = (*sfa)->actions_len;
1370 kfree(*sfa);
1371 *sfa = acts;
1372
1373out:
1374 (*sfa)->actions_len += req_size;
1375 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1376}
1377
Jesse Grossf0b128c2014-10-03 15:35:31 -07001378static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1379 int attrtype, void *data, int len)
Pravin B Shelare6445712013-10-03 18:16:47 -07001380{
1381 struct nlattr *a;
1382
1383 a = reserve_sfa_size(sfa, nla_attr_size(len));
1384 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001385 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001386
1387 a->nla_type = attrtype;
1388 a->nla_len = nla_attr_size(len);
1389
1390 if (data)
1391 memcpy(nla_data(a), data, len);
1392 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1393
Jesse Grossf0b128c2014-10-03 15:35:31 -07001394 return a;
1395}
1396
1397static int add_action(struct sw_flow_actions **sfa, int attrtype,
1398 void *data, int len)
1399{
1400 struct nlattr *a;
1401
1402 a = __add_action(sfa, attrtype, data, len);
1403 if (IS_ERR(a))
1404 return PTR_ERR(a);
1405
Pravin B Shelare6445712013-10-03 18:16:47 -07001406 return 0;
1407}
1408
1409static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1410 int attrtype)
1411{
1412 int used = (*sfa)->actions_len;
1413 int err;
1414
1415 err = add_action(sfa, attrtype, NULL, 0);
1416 if (err)
1417 return err;
1418
1419 return used;
1420}
1421
1422static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1423 int st_offset)
1424{
1425 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1426 st_offset);
1427
1428 a->nla_len = sfa->actions_len - st_offset;
1429}
1430
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001431static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001432 const struct sw_flow_key *key,
1433 int depth, struct sw_flow_actions **sfa,
1434 __be16 eth_type, __be16 vlan_tci);
1435
Pravin B Shelare6445712013-10-03 18:16:47 -07001436static int validate_and_copy_sample(const struct nlattr *attr,
1437 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001438 struct sw_flow_actions **sfa,
1439 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001440{
1441 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1442 const struct nlattr *probability, *actions;
1443 const struct nlattr *a;
1444 int rem, start, err, st_acts;
1445
1446 memset(attrs, 0, sizeof(attrs));
1447 nla_for_each_nested(a, attr, rem) {
1448 int type = nla_type(a);
1449 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1450 return -EINVAL;
1451 attrs[type] = a;
1452 }
1453 if (rem)
1454 return -EINVAL;
1455
1456 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1457 if (!probability || nla_len(probability) != sizeof(u32))
1458 return -EINVAL;
1459
1460 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1461 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1462 return -EINVAL;
1463
1464 /* validation done, copy sample action. */
1465 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1466 if (start < 0)
1467 return start;
1468 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1469 nla_data(probability), sizeof(u32));
1470 if (err)
1471 return err;
1472 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1473 if (st_acts < 0)
1474 return st_acts;
1475
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001476 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001477 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001478 if (err)
1479 return err;
1480
1481 add_nested_action_end(*sfa, st_acts);
1482 add_nested_action_end(*sfa, start);
1483
1484 return 0;
1485}
1486
Simon Horman25cd9ba2014-10-06 05:05:13 -07001487static int validate_tp_port(const struct sw_flow_key *flow_key,
1488 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001489{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001490 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001491 (flow_key->tp.src || flow_key->tp.dst))
1492 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001493
1494 return -EINVAL;
1495}
1496
1497void ovs_match_init(struct sw_flow_match *match,
1498 struct sw_flow_key *key,
1499 struct sw_flow_mask *mask)
1500{
1501 memset(match, 0, sizeof(*match));
1502 match->key = key;
1503 match->mask = mask;
1504
1505 memset(key, 0, sizeof(*key));
1506
1507 if (mask) {
1508 memset(&mask->key, 0, sizeof(mask->key));
1509 mask->range.start = mask->range.end = 0;
1510 }
1511}
1512
1513static int validate_and_copy_set_tun(const struct nlattr *attr,
1514 struct sw_flow_actions **sfa)
1515{
1516 struct sw_flow_match match;
1517 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001518 struct ovs_tunnel_info *tun_info;
1519 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001520 int err, start;
1521
1522 ovs_match_init(&match, &key, NULL);
1523 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1524 if (err)
1525 return err;
1526
Jesse Grossf5796682014-10-03 15:35:33 -07001527 if (key.tun_opts_len) {
1528 struct geneve_opt *option = GENEVE_OPTS(&key,
1529 key.tun_opts_len);
1530 int opts_len = key.tun_opts_len;
1531 bool crit_opt = false;
1532
1533 while (opts_len > 0) {
1534 int len;
1535
1536 if (opts_len < sizeof(*option))
1537 return -EINVAL;
1538
1539 len = sizeof(*option) + option->length * 4;
1540 if (len > opts_len)
1541 return -EINVAL;
1542
1543 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1544
1545 option = (struct geneve_opt *)((u8 *)option + len);
1546 opts_len -= len;
1547 };
1548
1549 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1550 };
1551
Pravin B Shelare6445712013-10-03 18:16:47 -07001552 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1553 if (start < 0)
1554 return start;
1555
Jesse Grossf0b128c2014-10-03 15:35:31 -07001556 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jesse Grossf5796682014-10-03 15:35:33 -07001557 sizeof(*tun_info) + key.tun_opts_len);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001558 if (IS_ERR(a))
1559 return PTR_ERR(a);
1560
1561 tun_info = nla_data(a);
1562 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001563 tun_info->options_len = key.tun_opts_len;
1564
1565 if (tun_info->options_len) {
1566 /* We need to store the options in the action itself since
1567 * everything else will go away after flow setup. We can append
1568 * it to tun_info and then point there.
1569 */
1570 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1571 key.tun_opts_len);
1572 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1573 } else {
1574 tun_info->options = NULL;
1575 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001576
Pravin B Shelare6445712013-10-03 18:16:47 -07001577 add_nested_action_end(*sfa, start);
1578
1579 return err;
1580}
1581
1582static int validate_set(const struct nlattr *a,
1583 const struct sw_flow_key *flow_key,
1584 struct sw_flow_actions **sfa,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001585 bool *set_tun, __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001586{
1587 const struct nlattr *ovs_key = nla_data(a);
1588 int key_type = nla_type(ovs_key);
1589
1590 /* There can be only one key in a action */
1591 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1592 return -EINVAL;
1593
1594 if (key_type > OVS_KEY_ATTR_MAX ||
1595 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1596 ovs_key_lens[key_type] != -1))
1597 return -EINVAL;
1598
1599 switch (key_type) {
1600 const struct ovs_key_ipv4 *ipv4_key;
1601 const struct ovs_key_ipv6 *ipv6_key;
1602 int err;
1603
1604 case OVS_KEY_ATTR_PRIORITY:
1605 case OVS_KEY_ATTR_SKB_MARK:
1606 case OVS_KEY_ATTR_ETHERNET:
1607 break;
1608
1609 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001610 if (eth_p_mpls(eth_type))
1611 return -EINVAL;
1612
Pravin B Shelare6445712013-10-03 18:16:47 -07001613 *set_tun = true;
1614 err = validate_and_copy_set_tun(a, sfa);
1615 if (err)
1616 return err;
1617 break;
1618
1619 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001620 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001621 return -EINVAL;
1622
1623 if (!flow_key->ip.proto)
1624 return -EINVAL;
1625
1626 ipv4_key = nla_data(ovs_key);
1627 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1628 return -EINVAL;
1629
1630 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1631 return -EINVAL;
1632
1633 break;
1634
1635 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001636 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001637 return -EINVAL;
1638
1639 if (!flow_key->ip.proto)
1640 return -EINVAL;
1641
1642 ipv6_key = nla_data(ovs_key);
1643 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1644 return -EINVAL;
1645
1646 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1647 return -EINVAL;
1648
1649 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1650 return -EINVAL;
1651
1652 break;
1653
1654 case OVS_KEY_ATTR_TCP:
1655 if (flow_key->ip.proto != IPPROTO_TCP)
1656 return -EINVAL;
1657
Simon Horman25cd9ba2014-10-06 05:05:13 -07001658 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001659
1660 case OVS_KEY_ATTR_UDP:
1661 if (flow_key->ip.proto != IPPROTO_UDP)
1662 return -EINVAL;
1663
Simon Horman25cd9ba2014-10-06 05:05:13 -07001664 return validate_tp_port(flow_key, eth_type);
1665
1666 case OVS_KEY_ATTR_MPLS:
1667 if (!eth_p_mpls(eth_type))
1668 return -EINVAL;
1669 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001670
1671 case OVS_KEY_ATTR_SCTP:
1672 if (flow_key->ip.proto != IPPROTO_SCTP)
1673 return -EINVAL;
1674
Simon Horman25cd9ba2014-10-06 05:05:13 -07001675 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001676
1677 default:
1678 return -EINVAL;
1679 }
1680
1681 return 0;
1682}
1683
1684static int validate_userspace(const struct nlattr *attr)
1685{
1686 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1687 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1688 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08001689 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07001690 };
1691 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1692 int error;
1693
1694 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1695 attr, userspace_policy);
1696 if (error)
1697 return error;
1698
1699 if (!a[OVS_USERSPACE_ATTR_PID] ||
1700 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1701 return -EINVAL;
1702
1703 return 0;
1704}
1705
1706static int copy_action(const struct nlattr *from,
1707 struct sw_flow_actions **sfa)
1708{
1709 int totlen = NLA_ALIGN(from->nla_len);
1710 struct nlattr *to;
1711
1712 to = reserve_sfa_size(sfa, from->nla_len);
1713 if (IS_ERR(to))
1714 return PTR_ERR(to);
1715
1716 memcpy(to, from, totlen);
1717 return 0;
1718}
1719
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001720static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001721 const struct sw_flow_key *key,
1722 int depth, struct sw_flow_actions **sfa,
1723 __be16 eth_type, __be16 vlan_tci)
Pravin B Shelare6445712013-10-03 18:16:47 -07001724{
1725 const struct nlattr *a;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001726 bool out_tnl_port = false;
Pravin B Shelare6445712013-10-03 18:16:47 -07001727 int rem, err;
1728
1729 if (depth >= SAMPLE_ACTION_DEPTH)
1730 return -EOVERFLOW;
1731
1732 nla_for_each_nested(a, attr, rem) {
1733 /* Expected argument lengths, (u32)-1 for variable length. */
1734 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1735 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001736 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001737 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001738 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1739 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001740 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1741 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1742 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001743 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1744 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001745 };
1746 const struct ovs_action_push_vlan *vlan;
1747 int type = nla_type(a);
1748 bool skip_copy;
1749
1750 if (type > OVS_ACTION_ATTR_MAX ||
1751 (action_lens[type] != nla_len(a) &&
1752 action_lens[type] != (u32)-1))
1753 return -EINVAL;
1754
1755 skip_copy = false;
1756 switch (type) {
1757 case OVS_ACTION_ATTR_UNSPEC:
1758 return -EINVAL;
1759
1760 case OVS_ACTION_ATTR_USERSPACE:
1761 err = validate_userspace(a);
1762 if (err)
1763 return err;
1764 break;
1765
1766 case OVS_ACTION_ATTR_OUTPUT:
1767 if (nla_get_u32(a) >= DP_MAX_PORTS)
1768 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001769 out_tnl_port = false;
1770
Pravin B Shelare6445712013-10-03 18:16:47 -07001771 break;
1772
Andy Zhou971427f32014-09-15 19:37:25 -07001773 case OVS_ACTION_ATTR_HASH: {
1774 const struct ovs_action_hash *act_hash = nla_data(a);
1775
1776 switch (act_hash->hash_alg) {
1777 case OVS_HASH_ALG_L4:
1778 break;
1779 default:
1780 return -EINVAL;
1781 }
1782
1783 break;
1784 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001785
1786 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001787 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001788 break;
1789
1790 case OVS_ACTION_ATTR_PUSH_VLAN:
1791 vlan = nla_data(a);
1792 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1793 return -EINVAL;
1794 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1795 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001796 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001797 break;
1798
Andy Zhou971427f32014-09-15 19:37:25 -07001799 case OVS_ACTION_ATTR_RECIRC:
1800 break;
1801
Simon Horman25cd9ba2014-10-06 05:05:13 -07001802 case OVS_ACTION_ATTR_PUSH_MPLS: {
1803 const struct ovs_action_push_mpls *mpls = nla_data(a);
1804
1805 /* Networking stack do not allow simultaneous Tunnel
1806 * and MPLS GSO.
1807 */
1808 if (out_tnl_port)
1809 return -EINVAL;
1810
1811 if (!eth_p_mpls(mpls->mpls_ethertype))
1812 return -EINVAL;
1813 /* Prohibit push MPLS other than to a white list
1814 * for packets that have a known tag order.
1815 */
1816 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1817 (eth_type != htons(ETH_P_IP) &&
1818 eth_type != htons(ETH_P_IPV6) &&
1819 eth_type != htons(ETH_P_ARP) &&
1820 eth_type != htons(ETH_P_RARP) &&
1821 !eth_p_mpls(eth_type)))
1822 return -EINVAL;
1823 eth_type = mpls->mpls_ethertype;
1824 break;
1825 }
1826
1827 case OVS_ACTION_ATTR_POP_MPLS:
1828 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1829 !eth_p_mpls(eth_type))
1830 return -EINVAL;
1831
1832 /* Disallow subsequent L2.5+ set and mpls_pop actions
1833 * as there is no check here to ensure that the new
1834 * eth_type is valid and thus set actions could
1835 * write off the end of the packet or otherwise
1836 * corrupt it.
1837 *
1838 * Support for these actions is planned using packet
1839 * recirculation.
1840 */
1841 eth_type = htons(0);
1842 break;
1843
Pravin B Shelare6445712013-10-03 18:16:47 -07001844 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001845 err = validate_set(a, key, sfa,
1846 &out_tnl_port, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001847 if (err)
1848 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001849
1850 skip_copy = out_tnl_port;
Pravin B Shelare6445712013-10-03 18:16:47 -07001851 break;
1852
1853 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001854 err = validate_and_copy_sample(a, key, depth, sfa,
1855 eth_type, vlan_tci);
Pravin B Shelare6445712013-10-03 18:16:47 -07001856 if (err)
1857 return err;
1858 skip_copy = true;
1859 break;
1860
1861 default:
Jesse Gross426cda52014-10-06 05:08:38 -07001862 OVS_NLERR("Unknown tunnel attribute (%d).\n", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001863 return -EINVAL;
1864 }
1865 if (!skip_copy) {
1866 err = copy_action(a, sfa);
1867 if (err)
1868 return err;
1869 }
1870 }
1871
1872 if (rem > 0)
1873 return -EINVAL;
1874
1875 return 0;
1876}
1877
Simon Horman25cd9ba2014-10-06 05:05:13 -07001878int ovs_nla_copy_actions(const struct nlattr *attr,
1879 const struct sw_flow_key *key,
1880 struct sw_flow_actions **sfa)
1881{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001882 int err;
1883
1884 *sfa = nla_alloc_flow_actions(nla_len(attr));
1885 if (IS_ERR(*sfa))
1886 return PTR_ERR(*sfa);
1887
1888 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
1889 key->eth.tci);
1890 if (err)
1891 kfree(*sfa);
1892
1893 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001894}
1895
Pravin B Shelare6445712013-10-03 18:16:47 -07001896static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1897{
1898 const struct nlattr *a;
1899 struct nlattr *start;
1900 int err = 0, rem;
1901
1902 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1903 if (!start)
1904 return -EMSGSIZE;
1905
1906 nla_for_each_nested(a, attr, rem) {
1907 int type = nla_type(a);
1908 struct nlattr *st_sample;
1909
1910 switch (type) {
1911 case OVS_SAMPLE_ATTR_PROBABILITY:
1912 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1913 sizeof(u32), nla_data(a)))
1914 return -EMSGSIZE;
1915 break;
1916 case OVS_SAMPLE_ATTR_ACTIONS:
1917 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1918 if (!st_sample)
1919 return -EMSGSIZE;
1920 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1921 if (err)
1922 return err;
1923 nla_nest_end(skb, st_sample);
1924 break;
1925 }
1926 }
1927
1928 nla_nest_end(skb, start);
1929 return err;
1930}
1931
1932static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1933{
1934 const struct nlattr *ovs_key = nla_data(a);
1935 int key_type = nla_type(ovs_key);
1936 struct nlattr *start;
1937 int err;
1938
1939 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001940 case OVS_KEY_ATTR_TUNNEL_INFO: {
1941 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1942
Pravin B Shelare6445712013-10-03 18:16:47 -07001943 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1944 if (!start)
1945 return -EMSGSIZE;
1946
Jesse Grossf0b128c2014-10-03 15:35:31 -07001947 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001948 tun_info->options_len ?
1949 tun_info->options : NULL,
1950 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001951 if (err)
1952 return err;
1953 nla_nest_end(skb, start);
1954 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001955 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001956 default:
1957 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1958 return -EMSGSIZE;
1959 break;
1960 }
1961
1962 return 0;
1963}
1964
1965int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1966{
1967 const struct nlattr *a;
1968 int rem, err;
1969
1970 nla_for_each_attr(a, attr, len, rem) {
1971 int type = nla_type(a);
1972
1973 switch (type) {
1974 case OVS_ACTION_ATTR_SET:
1975 err = set_action_to_attr(a, skb);
1976 if (err)
1977 return err;
1978 break;
1979
1980 case OVS_ACTION_ATTR_SAMPLE:
1981 err = sample_action_to_attr(a, skb);
1982 if (err)
1983 return err;
1984 break;
1985 default:
1986 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1987 return -EMSGSIZE;
1988 break;
1989 }
1990 }
1991
1992 return 0;
1993}