blob: 518941c5bdf11d928809a4be39dc8633c972ba7e [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
Thomas Graf81bfe3c32015-01-15 03:53:58 +010053struct ovs_len_tbl {
54 int len;
55 const struct ovs_len_tbl *next;
56};
57
58#define OVS_ATTR_NESTED -1
59
Pravin B Shelara85311b2014-10-19 12:03:40 -070060static void update_range(struct sw_flow_match *match,
61 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070062{
Pravin B Shelara85311b2014-10-19 12:03:40 -070063 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070064 size_t start = rounddown(offset, sizeof(long));
65 size_t end = roundup(offset + size, sizeof(long));
66
67 if (!is_mask)
68 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -070069 else
Pravin B Shelare6445712013-10-03 18:16:47 -070070 range = &match->mask->range;
71
Pravin B Shelare6445712013-10-03 18:16:47 -070072 if (range->start == range->end) {
73 range->start = start;
74 range->end = end;
75 return;
76 }
77
78 if (range->start > start)
79 range->start = start;
80
81 if (range->end < end)
82 range->end = end;
83}
84
85#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
86 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070087 update_range(match, offsetof(struct sw_flow_key, field), \
88 sizeof((match)->key->field), is_mask); \
89 if (is_mask) \
90 (match)->mask->key.field = value; \
91 else \
Pravin B Shelare6445712013-10-03 18:16:47 -070092 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -070093 } while (0)
94
Jesse Grossf5796682014-10-03 15:35:33 -070095#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
96 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070097 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -070098 if (is_mask) \
99 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700100 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700101 else \
102 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700103 } while (0)
104
Jesse Grossf5796682014-10-03 15:35:33 -0700105#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
106 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
107 value_p, len, is_mask)
108
Pravin B Shelara85311b2014-10-19 12:03:40 -0700109#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
110 do { \
111 update_range(match, offsetof(struct sw_flow_key, field), \
112 sizeof((match)->key->field), is_mask); \
113 if (is_mask) \
114 memset((u8 *)&(match)->mask->key.field, value, \
115 sizeof((match)->mask->key.field)); \
116 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700117 memset((u8 *)&(match)->key->field, value, \
118 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700119 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700120
121static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800122 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700123{
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;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800150 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700151 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;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800231 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
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. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800240 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
241 (unsigned long long)key_attrs,
242 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700243 return false;
244 }
245
246 if ((mask_attrs & mask_allowed) != mask_attrs) {
247 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800248 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
249 (unsigned long long)mask_attrs,
250 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700251 return false;
252 }
253
254 return true;
255}
256
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800257size_t ovs_tun_key_attr_size(void)
258{
259 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
260 * updating this function.
261 */
262 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
263 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
264 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
265 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
266 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
267 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
268 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
270 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
271 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
272 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
273}
274
Joe Stringer41af73e2014-10-18 16:14:14 -0700275size_t ovs_key_attr_size(void)
276{
277 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
278 * updating this function.
279 */
280 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
281
282 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
283 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800284 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700285 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
286 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
287 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
288 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
289 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
290 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
291 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
292 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
293 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
294 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
295 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
296 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
297}
298
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100299static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
300 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
301 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
302 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
303 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
304 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
305 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
306 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
307 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
308 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
309 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
310 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
311};
312
Pravin B Shelare6445712013-10-03 18:16:47 -0700313/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100314static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
315 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
316 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
317 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
318 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
319 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
320 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
321 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
322 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
323 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
324 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
325 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
326 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
327 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
328 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
329 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
330 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
331 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
332 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
333 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
334 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
335 .next = ovs_tunnel_key_lens, },
336 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700337};
338
339static bool is_all_zero(const u8 *fp, size_t size)
340{
341 int i;
342
343 if (!fp)
344 return false;
345
346 for (i = 0; i < size; i++)
347 if (fp[i])
348 return false;
349
350 return true;
351}
352
353static int __parse_flow_nlattrs(const struct nlattr *attr,
354 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800355 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700356{
357 const struct nlattr *nla;
358 u64 attrs;
359 int rem;
360
361 attrs = *attrsp;
362 nla_for_each_nested(nla, attr, rem) {
363 u16 type = nla_type(nla);
364 int expected_len;
365
366 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800367 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700368 type, OVS_KEY_ATTR_MAX);
369 return -EINVAL;
370 }
371
372 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800373 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700374 return -EINVAL;
375 }
376
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100377 expected_len = ovs_key_lens[type].len;
378 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800379 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
380 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700381 return -EINVAL;
382 }
383
384 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
385 attrs |= 1 << type;
386 a[type] = nla;
387 }
388 }
389 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800390 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700391 return -EINVAL;
392 }
393
394 *attrsp = attrs;
395 return 0;
396}
397
398static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800399 const struct nlattr *a[], u64 *attrsp,
400 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700401{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800402 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700403}
404
405static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800406 const struct nlattr *a[], u64 *attrsp,
407 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700408{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800409 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
410}
411
412static int genev_tun_opt_from_nlattr(const struct nlattr *a,
413 struct sw_flow_match *match, bool is_mask,
414 bool log)
415{
416 unsigned long opt_key_offset;
417
418 if (nla_len(a) > sizeof(match->key->tun_opts)) {
419 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
420 nla_len(a), sizeof(match->key->tun_opts));
421 return -EINVAL;
422 }
423
424 if (nla_len(a) % 4 != 0) {
425 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
426 nla_len(a));
427 return -EINVAL;
428 }
429
430 /* We need to record the length of the options passed
431 * down, otherwise packets with the same format but
432 * additional options will be silently matched.
433 */
434 if (!is_mask) {
435 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
436 false);
437 } else {
438 /* This is somewhat unusual because it looks at
439 * both the key and mask while parsing the
440 * attributes (and by extension assumes the key
441 * is parsed first). Normally, we would verify
442 * that each is the correct length and that the
443 * attributes line up in the validate function.
444 * However, that is difficult because this is
445 * variable length and we won't have the
446 * information later.
447 */
448 if (match->key->tun_opts_len != nla_len(a)) {
449 OVS_NLERR(log, "Geneve option len %d != mask len %d",
450 match->key->tun_opts_len, nla_len(a));
451 return -EINVAL;
452 }
453
454 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
455 }
456
Thomas Grafd91641d2015-01-15 03:53:57 +0100457 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800458 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
459 nla_len(a), is_mask);
460 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700461}
462
463static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800464 struct sw_flow_match *match, bool is_mask,
465 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700466{
467 struct nlattr *a;
468 int rem;
469 bool ttl = false;
470 __be16 tun_flags = 0;
471
472 nla_for_each_nested(a, attr, rem) {
473 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800474 int err;
475
Pravin B Shelare6445712013-10-03 18:16:47 -0700476 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800477 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
478 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700479 return -EINVAL;
480 }
481
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100482 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
483 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800484 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100485 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700486 return -EINVAL;
487 }
488
489 switch (type) {
490 case OVS_TUNNEL_KEY_ATTR_ID:
491 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
492 nla_get_be64(a), is_mask);
493 tun_flags |= TUNNEL_KEY;
494 break;
495 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
496 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
497 nla_get_be32(a), is_mask);
498 break;
499 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
500 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
501 nla_get_be32(a), is_mask);
502 break;
503 case OVS_TUNNEL_KEY_ATTR_TOS:
504 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
505 nla_get_u8(a), is_mask);
506 break;
507 case OVS_TUNNEL_KEY_ATTR_TTL:
508 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
509 nla_get_u8(a), is_mask);
510 ttl = true;
511 break;
512 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
513 tun_flags |= TUNNEL_DONT_FRAGMENT;
514 break;
515 case OVS_TUNNEL_KEY_ATTR_CSUM:
516 tun_flags |= TUNNEL_CSUM;
517 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800518 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
519 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
520 nla_get_be16(a), is_mask);
521 break;
522 case OVS_TUNNEL_KEY_ATTR_TP_DST:
523 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
524 nla_get_be16(a), is_mask);
525 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700526 case OVS_TUNNEL_KEY_ATTR_OAM:
527 tun_flags |= TUNNEL_OAM;
528 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700529 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800530 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
531 if (err)
532 return err;
533
Jesse Grossf5796682014-10-03 15:35:33 -0700534 tun_flags |= TUNNEL_OPTIONS_PRESENT;
Jesse Grossf5796682014-10-03 15:35:33 -0700535 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700536 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800537 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700538 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700539 return -EINVAL;
540 }
541 }
542
543 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
544
545 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800546 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
547 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700548 return -EINVAL;
549 }
550
551 if (!is_mask) {
552 if (!match->key->tun_key.ipv4_dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800553 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700554 return -EINVAL;
555 }
556
557 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800558 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700559 return -EINVAL;
560 }
561 }
562
563 return 0;
564}
565
Jesse Grossf5796682014-10-03 15:35:33 -0700566static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
567 const struct ovs_key_ipv4_tunnel *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100568 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700569{
Pravin B Shelare6445712013-10-03 18:16:47 -0700570 if (output->tun_flags & TUNNEL_KEY &&
571 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
572 return -EMSGSIZE;
573 if (output->ipv4_src &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700574 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700575 return -EMSGSIZE;
576 if (output->ipv4_dst &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700577 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700578 return -EMSGSIZE;
579 if (output->ipv4_tos &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700580 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700581 return -EMSGSIZE;
582 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
583 return -EMSGSIZE;
584 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700585 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700586 return -EMSGSIZE;
587 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700588 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
589 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800590 if (output->tp_src &&
591 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
592 return -EMSGSIZE;
593 if (output->tp_dst &&
594 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
595 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700596 if ((output->tun_flags & TUNNEL_OAM) &&
597 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700598 return -EMSGSIZE;
Jesse Grossf5796682014-10-03 15:35:33 -0700599 if (tun_opts &&
600 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
601 swkey_tun_opts_len, tun_opts))
602 return -EMSGSIZE;
603
604 return 0;
605}
606
Jesse Grossf5796682014-10-03 15:35:33 -0700607static int ipv4_tun_to_nlattr(struct sk_buff *skb,
608 const struct ovs_key_ipv4_tunnel *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100609 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700610{
611 struct nlattr *nla;
612 int err;
613
614 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
615 if (!nla)
616 return -EMSGSIZE;
617
618 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
619 if (err)
620 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700621
622 nla_nest_end(skb, nla);
623 return 0;
624}
625
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800626int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
627 const struct ovs_tunnel_info *egress_tun_info)
628{
629 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
630 egress_tun_info->options,
631 egress_tun_info->options_len);
632}
633
Pravin B Shelare6445712013-10-03 18:16:47 -0700634static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800635 const struct nlattr **a, bool is_mask,
636 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700637{
Andy Zhou971427f32014-09-15 19:37:25 -0700638 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
639 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
640
641 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
642 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
643 }
644
645 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
646 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
647
648 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
649 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
650 }
651
Pravin B Shelare6445712013-10-03 18:16:47 -0700652 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
653 SW_FLOW_KEY_PUT(match, phy.priority,
654 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
655 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
656 }
657
658 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
659 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
660
Jesse Gross426cda52014-10-06 05:08:38 -0700661 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700662 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700663 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800664 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700665 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700666 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700667 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700668
669 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
670 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
671 } else if (!is_mask) {
672 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
673 }
674
675 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
676 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
677
678 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
679 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
680 }
681 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
682 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800683 is_mask, log))
Pravin B Shelare6445712013-10-03 18:16:47 -0700684 return -EINVAL;
685 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
686 }
687 return 0;
688}
689
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700690static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800691 const struct nlattr **a, bool is_mask,
692 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700693{
694 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700695
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800696 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700697 if (err)
698 return err;
699
700 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
701 const struct ovs_key_ethernet *eth_key;
702
703 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
704 SW_FLOW_KEY_MEMCPY(match, eth.src,
705 eth_key->eth_src, ETH_ALEN, is_mask);
706 SW_FLOW_KEY_MEMCPY(match, eth.dst,
707 eth_key->eth_dst, ETH_ALEN, is_mask);
708 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
709 }
710
711 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
712 __be16 tci;
713
714 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
715 if (!(tci & htons(VLAN_TAG_PRESENT))) {
716 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800717 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700718 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800719 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700720
721 return -EINVAL;
722 }
723
724 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
725 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700726 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700727
728 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
729 __be16 eth_type;
730
731 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
732 if (is_mask) {
733 /* Always exact match EtherType. */
734 eth_type = htons(0xffff);
735 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800736 OVS_NLERR(log, "EtherType %x is less than min %x",
737 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700738 return -EINVAL;
739 }
740
741 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
742 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
743 } else if (!is_mask) {
744 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
745 }
746
747 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
748 const struct ovs_key_ipv4 *ipv4_key;
749
750 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
751 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800752 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
753 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700754 return -EINVAL;
755 }
756 SW_FLOW_KEY_PUT(match, ip.proto,
757 ipv4_key->ipv4_proto, is_mask);
758 SW_FLOW_KEY_PUT(match, ip.tos,
759 ipv4_key->ipv4_tos, is_mask);
760 SW_FLOW_KEY_PUT(match, ip.ttl,
761 ipv4_key->ipv4_ttl, is_mask);
762 SW_FLOW_KEY_PUT(match, ip.frag,
763 ipv4_key->ipv4_frag, is_mask);
764 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
765 ipv4_key->ipv4_src, is_mask);
766 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
767 ipv4_key->ipv4_dst, is_mask);
768 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
769 }
770
771 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
772 const struct ovs_key_ipv6 *ipv6_key;
773
774 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
775 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800776 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
777 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700778 return -EINVAL;
779 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800780
Joe Stringerd3052bb2014-11-19 13:54:49 -0800781 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500782 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800783 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
784 return -EINVAL;
785 }
786
Pravin B Shelare6445712013-10-03 18:16:47 -0700787 SW_FLOW_KEY_PUT(match, ipv6.label,
788 ipv6_key->ipv6_label, is_mask);
789 SW_FLOW_KEY_PUT(match, ip.proto,
790 ipv6_key->ipv6_proto, is_mask);
791 SW_FLOW_KEY_PUT(match, ip.tos,
792 ipv6_key->ipv6_tclass, is_mask);
793 SW_FLOW_KEY_PUT(match, ip.ttl,
794 ipv6_key->ipv6_hlimit, is_mask);
795 SW_FLOW_KEY_PUT(match, ip.frag,
796 ipv6_key->ipv6_frag, is_mask);
797 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
798 ipv6_key->ipv6_src,
799 sizeof(match->key->ipv6.addr.src),
800 is_mask);
801 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
802 ipv6_key->ipv6_dst,
803 sizeof(match->key->ipv6.addr.dst),
804 is_mask);
805
806 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
807 }
808
809 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
810 const struct ovs_key_arp *arp_key;
811
812 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
813 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800814 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700815 arp_key->arp_op);
816 return -EINVAL;
817 }
818
819 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
820 arp_key->arp_sip, is_mask);
821 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
822 arp_key->arp_tip, is_mask);
823 SW_FLOW_KEY_PUT(match, ip.proto,
824 ntohs(arp_key->arp_op), is_mask);
825 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
826 arp_key->arp_sha, ETH_ALEN, is_mask);
827 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
828 arp_key->arp_tha, ETH_ALEN, is_mask);
829
830 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
831 }
832
Simon Horman25cd9ba2014-10-06 05:05:13 -0700833 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
834 const struct ovs_key_mpls *mpls_key;
835
836 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
837 SW_FLOW_KEY_PUT(match, mpls.top_lse,
838 mpls_key->mpls_lse, is_mask);
839
840 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
841 }
842
Pravin B Shelare6445712013-10-03 18:16:47 -0700843 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
844 const struct ovs_key_tcp *tcp_key;
845
846 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700847 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
848 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700849 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
850 }
851
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700852 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700853 SW_FLOW_KEY_PUT(match, tp.flags,
854 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
855 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700856 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
857 }
858
Pravin B Shelare6445712013-10-03 18:16:47 -0700859 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
860 const struct ovs_key_udp *udp_key;
861
862 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700863 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
864 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700865 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
866 }
867
868 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
869 const struct ovs_key_sctp *sctp_key;
870
871 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700872 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
873 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700874 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
875 }
876
877 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
878 const struct ovs_key_icmp *icmp_key;
879
880 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700881 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700882 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700883 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700884 htons(icmp_key->icmp_code), is_mask);
885 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
886 }
887
888 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
889 const struct ovs_key_icmpv6 *icmpv6_key;
890
891 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700892 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700893 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700894 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700895 htons(icmpv6_key->icmpv6_code), is_mask);
896 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
897 }
898
899 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
900 const struct ovs_key_nd *nd_key;
901
902 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
903 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
904 nd_key->nd_target,
905 sizeof(match->key->ipv6.nd.target),
906 is_mask);
907 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
908 nd_key->nd_sll, ETH_ALEN, is_mask);
909 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
910 nd_key->nd_tll, ETH_ALEN, is_mask);
911 attrs &= ~(1 << OVS_KEY_ATTR_ND);
912 }
913
Jesse Gross426cda52014-10-06 05:08:38 -0700914 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800915 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -0700916 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -0700917 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700918 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700919
920 return 0;
921}
922
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100923static void nlattr_set(struct nlattr *attr, u8 val,
924 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -0700925{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700926 struct nlattr *nla;
927 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700928
Pravin B Shelarf47de062014-10-16 21:55:45 -0700929 /* The nlattr stream should already have been validated */
930 nla_for_each_nested(nla, attr, rem) {
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100931 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
932 nlattr_set(nla, val, tbl[nla_type(nla)].next);
Pravin B Shelarf47de062014-10-16 21:55:45 -0700933 else
934 memset(nla_data(nla), val, nla_len(nla));
935 }
936}
937
938static void mask_set_nlattr(struct nlattr *attr, u8 val)
939{
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100940 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -0700941}
942
943/**
944 * ovs_nla_get_match - parses Netlink attributes into a flow key and
945 * mask. In case the 'mask' is NULL, the flow is treated as exact match
946 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
947 * does not include any don't care bit.
948 * @match: receives the extracted flow match information.
949 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
950 * sequence. The fields should of the packet that triggered the creation
951 * of this flow.
952 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
953 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800954 * @log: Boolean to allow kernel error logging. Normally true, but when
955 * probing for feature compatibility this should be passed in as false to
956 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -0700957 */
958int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -0700959 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800960 const struct nlattr *nla_mask,
961 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700962{
963 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
964 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700965 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700966 u64 key_attrs = 0;
967 u64 mask_attrs = 0;
968 bool encap_valid = false;
969 int err;
970
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800971 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700972 if (err)
973 return err;
974
975 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
976 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
977 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
978 __be16 tci;
979
980 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
981 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800982 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700983 return -EINVAL;
984 }
985
986 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
987 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
988 encap = a[OVS_KEY_ATTR_ENCAP];
989 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
990 encap_valid = true;
991
992 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800993 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700994 if (err)
995 return err;
996 } else if (!tci) {
997 /* Corner case for truncated 802.1Q header. */
998 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800999 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001000 return -EINVAL;
1001 }
1002 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001003 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001004 return -EINVAL;
1005 }
1006 }
1007
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001008 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001009 if (err)
1010 return err;
1011
Pravin B Shelara85311b2014-10-19 12:03:40 -07001012 if (match->mask) {
1013 if (!nla_mask) {
1014 /* Create an exact match mask. We need to set to 0xff
1015 * all the 'match->mask' fields that have been touched
1016 * in 'match->key'. We cannot simply memset
1017 * 'match->mask', because padding bytes and fields not
1018 * specified in 'match->key' should be left to 0.
1019 * Instead, we use a stream of netlink attributes,
1020 * copied from 'key' and set to 0xff.
1021 * ovs_key_from_nlattrs() will take care of filling
1022 * 'match->mask' appropriately.
1023 */
1024 newmask = kmemdup(nla_key,
1025 nla_total_size(nla_len(nla_key)),
1026 GFP_KERNEL);
1027 if (!newmask)
1028 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001029
Pravin B Shelara85311b2014-10-19 12:03:40 -07001030 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001031
Pravin B Shelara85311b2014-10-19 12:03:40 -07001032 /* The userspace does not send tunnel attributes that
1033 * are 0, but we should not wildcard them nonetheless.
1034 */
1035 if (match->key->tun_key.ipv4_dst)
1036 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1037 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001038
Pravin B Shelara85311b2014-10-19 12:03:40 -07001039 nla_mask = newmask;
1040 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001041
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001042 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001043 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001044 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001045
Pravin B Shelara85311b2014-10-19 12:03:40 -07001046 /* Always match on tci. */
1047 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1048
Pravin B Shelarf47de062014-10-16 21:55:45 -07001049 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001050 __be16 eth_type = 0;
1051 __be16 tci = 0;
1052
1053 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001054 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001055 err = -EINVAL;
1056 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001057 }
1058
1059 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1060 if (a[OVS_KEY_ATTR_ETHERTYPE])
1061 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1062
1063 if (eth_type == htons(0xffff)) {
1064 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1065 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001066 err = parse_flow_mask_nlattrs(encap, a,
1067 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001068 if (err)
1069 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001070 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001071 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1072 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001073 err = -EINVAL;
1074 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001075 }
1076
1077 if (a[OVS_KEY_ATTR_VLAN])
1078 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1079
1080 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001081 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1082 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001083 err = -EINVAL;
1084 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001085 }
1086 }
1087
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001088 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001089 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001090 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001091 }
1092
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001093 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001094 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001095
Pravin B Shelarf47de062014-10-16 21:55:45 -07001096free_newmask:
1097 kfree(newmask);
1098 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001099}
1100
1101/**
1102 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001103 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001104 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1105 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001106 * @log: Boolean to allow kernel error logging. Normally true, but when
1107 * probing for feature compatibility this should be passed in as false to
1108 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001109 *
1110 * This parses a series of Netlink attributes that form a flow key, which must
1111 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1112 * get the metadata, that is, the parts of the flow key that cannot be
1113 * extracted from the packet itself.
1114 */
1115
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001116int ovs_nla_get_flow_metadata(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001117 struct sw_flow_key *key,
1118 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001119{
Pravin B Shelare6445712013-10-03 18:16:47 -07001120 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001121 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001122 u64 attrs = 0;
1123 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001124
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001125 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001126 if (err)
1127 return -EINVAL;
1128
1129 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001130 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001131
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001132 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001133
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001134 return metadata_from_nlattrs(&match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001135}
1136
1137int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1138 const struct sw_flow_key *output, struct sk_buff *skb)
1139{
1140 struct ovs_key_ethernet *eth_key;
1141 struct nlattr *nla, *encap;
1142 bool is_mask = (swkey != output);
1143
Andy Zhou971427f32014-09-15 19:37:25 -07001144 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1145 goto nla_put_failure;
1146
1147 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1148 goto nla_put_failure;
1149
Pravin B Shelare6445712013-10-03 18:16:47 -07001150 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1151 goto nla_put_failure;
1152
Jesse Grossf5796682014-10-03 15:35:33 -07001153 if ((swkey->tun_key.ipv4_dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001154 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001155
1156 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001157 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001158
1159 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1160 swkey->tun_opts_len))
1161 goto nla_put_failure;
1162 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001163
1164 if (swkey->phy.in_port == DP_MAX_PORTS) {
1165 if (is_mask && (output->phy.in_port == 0xffff))
1166 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1167 goto nla_put_failure;
1168 } else {
1169 u16 upper_u16;
1170 upper_u16 = !is_mask ? 0 : 0xffff;
1171
1172 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1173 (upper_u16 << 16) | output->phy.in_port))
1174 goto nla_put_failure;
1175 }
1176
1177 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1178 goto nla_put_failure;
1179
1180 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1181 if (!nla)
1182 goto nla_put_failure;
1183
1184 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001185 ether_addr_copy(eth_key->eth_src, output->eth.src);
1186 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001187
1188 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1189 __be16 eth_type;
1190 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1191 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1192 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1193 goto nla_put_failure;
1194 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1195 if (!swkey->eth.tci)
1196 goto unencap;
1197 } else
1198 encap = NULL;
1199
1200 if (swkey->eth.type == htons(ETH_P_802_2)) {
1201 /*
1202 * Ethertype 802.2 is represented in the netlink with omitted
1203 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1204 * 0xffff in the mask attribute. Ethertype can also
1205 * be wildcarded.
1206 */
1207 if (is_mask && output->eth.type)
1208 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1209 output->eth.type))
1210 goto nla_put_failure;
1211 goto unencap;
1212 }
1213
1214 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1215 goto nla_put_failure;
1216
1217 if (swkey->eth.type == htons(ETH_P_IP)) {
1218 struct ovs_key_ipv4 *ipv4_key;
1219
1220 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1221 if (!nla)
1222 goto nla_put_failure;
1223 ipv4_key = nla_data(nla);
1224 ipv4_key->ipv4_src = output->ipv4.addr.src;
1225 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1226 ipv4_key->ipv4_proto = output->ip.proto;
1227 ipv4_key->ipv4_tos = output->ip.tos;
1228 ipv4_key->ipv4_ttl = output->ip.ttl;
1229 ipv4_key->ipv4_frag = output->ip.frag;
1230 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1231 struct ovs_key_ipv6 *ipv6_key;
1232
1233 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1234 if (!nla)
1235 goto nla_put_failure;
1236 ipv6_key = nla_data(nla);
1237 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1238 sizeof(ipv6_key->ipv6_src));
1239 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1240 sizeof(ipv6_key->ipv6_dst));
1241 ipv6_key->ipv6_label = output->ipv6.label;
1242 ipv6_key->ipv6_proto = output->ip.proto;
1243 ipv6_key->ipv6_tclass = output->ip.tos;
1244 ipv6_key->ipv6_hlimit = output->ip.ttl;
1245 ipv6_key->ipv6_frag = output->ip.frag;
1246 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1247 swkey->eth.type == htons(ETH_P_RARP)) {
1248 struct ovs_key_arp *arp_key;
1249
1250 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1251 if (!nla)
1252 goto nla_put_failure;
1253 arp_key = nla_data(nla);
1254 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1255 arp_key->arp_sip = output->ipv4.addr.src;
1256 arp_key->arp_tip = output->ipv4.addr.dst;
1257 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001258 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1259 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001260 } else if (eth_p_mpls(swkey->eth.type)) {
1261 struct ovs_key_mpls *mpls_key;
1262
1263 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1264 if (!nla)
1265 goto nla_put_failure;
1266 mpls_key = nla_data(nla);
1267 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001268 }
1269
1270 if ((swkey->eth.type == htons(ETH_P_IP) ||
1271 swkey->eth.type == htons(ETH_P_IPV6)) &&
1272 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1273
1274 if (swkey->ip.proto == IPPROTO_TCP) {
1275 struct ovs_key_tcp *tcp_key;
1276
1277 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1278 if (!nla)
1279 goto nla_put_failure;
1280 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001281 tcp_key->tcp_src = output->tp.src;
1282 tcp_key->tcp_dst = output->tp.dst;
1283 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1284 output->tp.flags))
1285 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001286 } else if (swkey->ip.proto == IPPROTO_UDP) {
1287 struct ovs_key_udp *udp_key;
1288
1289 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1290 if (!nla)
1291 goto nla_put_failure;
1292 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001293 udp_key->udp_src = output->tp.src;
1294 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001295 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1296 struct ovs_key_sctp *sctp_key;
1297
1298 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1299 if (!nla)
1300 goto nla_put_failure;
1301 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001302 sctp_key->sctp_src = output->tp.src;
1303 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001304 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1305 swkey->ip.proto == IPPROTO_ICMP) {
1306 struct ovs_key_icmp *icmp_key;
1307
1308 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1309 if (!nla)
1310 goto nla_put_failure;
1311 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001312 icmp_key->icmp_type = ntohs(output->tp.src);
1313 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001314 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1315 swkey->ip.proto == IPPROTO_ICMPV6) {
1316 struct ovs_key_icmpv6 *icmpv6_key;
1317
1318 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1319 sizeof(*icmpv6_key));
1320 if (!nla)
1321 goto nla_put_failure;
1322 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001323 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1324 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001325
1326 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1327 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1328 struct ovs_key_nd *nd_key;
1329
1330 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1331 if (!nla)
1332 goto nla_put_failure;
1333 nd_key = nla_data(nla);
1334 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1335 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001336 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1337 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001338 }
1339 }
1340 }
1341
1342unencap:
1343 if (encap)
1344 nla_nest_end(skb, encap);
1345
1346 return 0;
1347
1348nla_put_failure:
1349 return -EMSGSIZE;
1350}
1351
1352#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1353
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001354static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001355{
1356 struct sw_flow_actions *sfa;
1357
Jesse Gross426cda52014-10-06 05:08:38 -07001358 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001359 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001360 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001361 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001362
1363 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1364 if (!sfa)
1365 return ERR_PTR(-ENOMEM);
1366
1367 sfa->actions_len = 0;
1368 return sfa;
1369}
1370
Pravin B Shelare6445712013-10-03 18:16:47 -07001371/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1372 * The caller must hold rcu_read_lock for this to be sensible. */
1373void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1374{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001375 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001376}
1377
1378static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001379 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001380{
1381
1382 struct sw_flow_actions *acts;
1383 int new_acts_size;
1384 int req_size = NLA_ALIGN(attr_len);
1385 int next_offset = offsetof(struct sw_flow_actions, actions) +
1386 (*sfa)->actions_len;
1387
1388 if (req_size <= (ksize(*sfa) - next_offset))
1389 goto out;
1390
1391 new_acts_size = ksize(*sfa) * 2;
1392
1393 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1394 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1395 return ERR_PTR(-EMSGSIZE);
1396 new_acts_size = MAX_ACTIONS_BUFSIZE;
1397 }
1398
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001399 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001400 if (IS_ERR(acts))
1401 return (void *)acts;
1402
1403 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1404 acts->actions_len = (*sfa)->actions_len;
1405 kfree(*sfa);
1406 *sfa = acts;
1407
1408out:
1409 (*sfa)->actions_len += req_size;
1410 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1411}
1412
Jesse Grossf0b128c2014-10-03 15:35:31 -07001413static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001414 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001415{
1416 struct nlattr *a;
1417
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001418 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001419 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001420 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001421
1422 a->nla_type = attrtype;
1423 a->nla_len = nla_attr_size(len);
1424
1425 if (data)
1426 memcpy(nla_data(a), data, len);
1427 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1428
Jesse Grossf0b128c2014-10-03 15:35:31 -07001429 return a;
1430}
1431
1432static int add_action(struct sw_flow_actions **sfa, int attrtype,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001433 void *data, int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001434{
1435 struct nlattr *a;
1436
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001437 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001438
Fabian Frederickf35423c12014-11-14 19:32:58 +01001439 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001440}
1441
1442static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001443 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001444{
1445 int used = (*sfa)->actions_len;
1446 int err;
1447
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001448 err = add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001449 if (err)
1450 return err;
1451
1452 return used;
1453}
1454
1455static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1456 int st_offset)
1457{
1458 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1459 st_offset);
1460
1461 a->nla_len = sfa->actions_len - st_offset;
1462}
1463
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001464static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001465 const struct sw_flow_key *key,
1466 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001467 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001468
Pravin B Shelare6445712013-10-03 18:16:47 -07001469static int validate_and_copy_sample(const struct nlattr *attr,
1470 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001471 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001472 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001473{
1474 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1475 const struct nlattr *probability, *actions;
1476 const struct nlattr *a;
1477 int rem, start, err, st_acts;
1478
1479 memset(attrs, 0, sizeof(attrs));
1480 nla_for_each_nested(a, attr, rem) {
1481 int type = nla_type(a);
1482 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1483 return -EINVAL;
1484 attrs[type] = a;
1485 }
1486 if (rem)
1487 return -EINVAL;
1488
1489 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1490 if (!probability || nla_len(probability) != sizeof(u32))
1491 return -EINVAL;
1492
1493 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1494 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1495 return -EINVAL;
1496
1497 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001498 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001499 if (start < 0)
1500 return start;
1501 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001502 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001503 if (err)
1504 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001505 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001506 if (st_acts < 0)
1507 return st_acts;
1508
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001509 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001510 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001511 if (err)
1512 return err;
1513
1514 add_nested_action_end(*sfa, st_acts);
1515 add_nested_action_end(*sfa, start);
1516
1517 return 0;
1518}
1519
Simon Horman25cd9ba2014-10-06 05:05:13 -07001520static int validate_tp_port(const struct sw_flow_key *flow_key,
1521 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001522{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001523 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001524 (flow_key->tp.src || flow_key->tp.dst))
1525 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001526
1527 return -EINVAL;
1528}
1529
1530void ovs_match_init(struct sw_flow_match *match,
1531 struct sw_flow_key *key,
1532 struct sw_flow_mask *mask)
1533{
1534 memset(match, 0, sizeof(*match));
1535 match->key = key;
1536 match->mask = mask;
1537
1538 memset(key, 0, sizeof(*key));
1539
1540 if (mask) {
1541 memset(&mask->key, 0, sizeof(mask->key));
1542 mask->range.start = mask->range.end = 0;
1543 }
1544}
1545
Thomas Grafd91641d2015-01-15 03:53:57 +01001546static int validate_geneve_opts(struct sw_flow_key *key)
1547{
1548 struct geneve_opt *option;
1549 int opts_len = key->tun_opts_len;
1550 bool crit_opt = false;
1551
1552 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1553 while (opts_len > 0) {
1554 int len;
1555
1556 if (opts_len < sizeof(*option))
1557 return -EINVAL;
1558
1559 len = sizeof(*option) + option->length * 4;
1560 if (len > opts_len)
1561 return -EINVAL;
1562
1563 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1564
1565 option = (struct geneve_opt *)((u8 *)option + len);
1566 opts_len -= len;
1567 };
1568
1569 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1570
1571 return 0;
1572}
1573
Pravin B Shelare6445712013-10-03 18:16:47 -07001574static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001575 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001576{
1577 struct sw_flow_match match;
1578 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001579 struct ovs_tunnel_info *tun_info;
1580 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001581 int err, start;
1582
1583 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001584 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001585 if (err)
1586 return err;
1587
Jesse Grossf5796682014-10-03 15:35:33 -07001588 if (key.tun_opts_len) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001589 err = validate_geneve_opts(&key);
1590 if (err < 0)
1591 return err;
Jesse Grossf5796682014-10-03 15:35:33 -07001592 };
1593
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001594 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001595 if (start < 0)
1596 return start;
1597
Jesse Grossf0b128c2014-10-03 15:35:31 -07001598 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001599 sizeof(*tun_info) + key.tun_opts_len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001600 if (IS_ERR(a))
1601 return PTR_ERR(a);
1602
1603 tun_info = nla_data(a);
1604 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001605 tun_info->options_len = key.tun_opts_len;
1606
1607 if (tun_info->options_len) {
1608 /* We need to store the options in the action itself since
1609 * everything else will go away after flow setup. We can append
1610 * it to tun_info and then point there.
1611 */
Thomas Grafd91641d2015-01-15 03:53:57 +01001612 memcpy((tun_info + 1),
1613 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1614 tun_info->options = (tun_info + 1);
Jesse Grossf5796682014-10-03 15:35:33 -07001615 } else {
1616 tun_info->options = NULL;
1617 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001618
Pravin B Shelare6445712013-10-03 18:16:47 -07001619 add_nested_action_end(*sfa, start);
1620
1621 return err;
1622}
1623
1624static int validate_set(const struct nlattr *a,
1625 const struct sw_flow_key *flow_key,
1626 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001627 bool *set_tun, __be16 eth_type, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001628{
1629 const struct nlattr *ovs_key = nla_data(a);
1630 int key_type = nla_type(ovs_key);
1631
1632 /* There can be only one key in a action */
1633 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1634 return -EINVAL;
1635
1636 if (key_type > OVS_KEY_ATTR_MAX ||
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001637 (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
1638 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
Pravin B Shelare6445712013-10-03 18:16:47 -07001639 return -EINVAL;
1640
1641 switch (key_type) {
1642 const struct ovs_key_ipv4 *ipv4_key;
1643 const struct ovs_key_ipv6 *ipv6_key;
1644 int err;
1645
1646 case OVS_KEY_ATTR_PRIORITY:
1647 case OVS_KEY_ATTR_SKB_MARK:
1648 case OVS_KEY_ATTR_ETHERNET:
1649 break;
1650
1651 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001652 if (eth_p_mpls(eth_type))
1653 return -EINVAL;
1654
Pravin B Shelare6445712013-10-03 18:16:47 -07001655 *set_tun = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001656 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001657 if (err)
1658 return err;
1659 break;
1660
1661 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001662 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001663 return -EINVAL;
1664
1665 if (!flow_key->ip.proto)
1666 return -EINVAL;
1667
1668 ipv4_key = nla_data(ovs_key);
1669 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1670 return -EINVAL;
1671
1672 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1673 return -EINVAL;
1674
1675 break;
1676
1677 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001678 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001679 return -EINVAL;
1680
1681 if (!flow_key->ip.proto)
1682 return -EINVAL;
1683
1684 ipv6_key = nla_data(ovs_key);
1685 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1686 return -EINVAL;
1687
1688 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1689 return -EINVAL;
1690
1691 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1692 return -EINVAL;
1693
1694 break;
1695
1696 case OVS_KEY_ATTR_TCP:
1697 if (flow_key->ip.proto != IPPROTO_TCP)
1698 return -EINVAL;
1699
Simon Horman25cd9ba2014-10-06 05:05:13 -07001700 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001701
1702 case OVS_KEY_ATTR_UDP:
1703 if (flow_key->ip.proto != IPPROTO_UDP)
1704 return -EINVAL;
1705
Simon Horman25cd9ba2014-10-06 05:05:13 -07001706 return validate_tp_port(flow_key, eth_type);
1707
1708 case OVS_KEY_ATTR_MPLS:
1709 if (!eth_p_mpls(eth_type))
1710 return -EINVAL;
1711 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001712
1713 case OVS_KEY_ATTR_SCTP:
1714 if (flow_key->ip.proto != IPPROTO_SCTP)
1715 return -EINVAL;
1716
Simon Horman25cd9ba2014-10-06 05:05:13 -07001717 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001718
1719 default:
1720 return -EINVAL;
1721 }
1722
1723 return 0;
1724}
1725
1726static int validate_userspace(const struct nlattr *attr)
1727{
1728 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1729 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1730 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08001731 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07001732 };
1733 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1734 int error;
1735
1736 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1737 attr, userspace_policy);
1738 if (error)
1739 return error;
1740
1741 if (!a[OVS_USERSPACE_ATTR_PID] ||
1742 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1743 return -EINVAL;
1744
1745 return 0;
1746}
1747
1748static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001749 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001750{
1751 int totlen = NLA_ALIGN(from->nla_len);
1752 struct nlattr *to;
1753
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001754 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001755 if (IS_ERR(to))
1756 return PTR_ERR(to);
1757
1758 memcpy(to, from, totlen);
1759 return 0;
1760}
1761
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001762static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001763 const struct sw_flow_key *key,
1764 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001765 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001766{
1767 const struct nlattr *a;
1768 int rem, err;
1769
1770 if (depth >= SAMPLE_ACTION_DEPTH)
1771 return -EOVERFLOW;
1772
1773 nla_for_each_nested(a, attr, rem) {
1774 /* Expected argument lengths, (u32)-1 for variable length. */
1775 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1776 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001777 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001778 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001779 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1780 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001781 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1782 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1783 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001784 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1785 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001786 };
1787 const struct ovs_action_push_vlan *vlan;
1788 int type = nla_type(a);
1789 bool skip_copy;
1790
1791 if (type > OVS_ACTION_ATTR_MAX ||
1792 (action_lens[type] != nla_len(a) &&
1793 action_lens[type] != (u32)-1))
1794 return -EINVAL;
1795
1796 skip_copy = false;
1797 switch (type) {
1798 case OVS_ACTION_ATTR_UNSPEC:
1799 return -EINVAL;
1800
1801 case OVS_ACTION_ATTR_USERSPACE:
1802 err = validate_userspace(a);
1803 if (err)
1804 return err;
1805 break;
1806
1807 case OVS_ACTION_ATTR_OUTPUT:
1808 if (nla_get_u32(a) >= DP_MAX_PORTS)
1809 return -EINVAL;
1810 break;
1811
Andy Zhou971427f32014-09-15 19:37:25 -07001812 case OVS_ACTION_ATTR_HASH: {
1813 const struct ovs_action_hash *act_hash = nla_data(a);
1814
1815 switch (act_hash->hash_alg) {
1816 case OVS_HASH_ALG_L4:
1817 break;
1818 default:
1819 return -EINVAL;
1820 }
1821
1822 break;
1823 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001824
1825 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001826 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001827 break;
1828
1829 case OVS_ACTION_ATTR_PUSH_VLAN:
1830 vlan = nla_data(a);
1831 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1832 return -EINVAL;
1833 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1834 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001835 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001836 break;
1837
Andy Zhou971427f32014-09-15 19:37:25 -07001838 case OVS_ACTION_ATTR_RECIRC:
1839 break;
1840
Simon Horman25cd9ba2014-10-06 05:05:13 -07001841 case OVS_ACTION_ATTR_PUSH_MPLS: {
1842 const struct ovs_action_push_mpls *mpls = nla_data(a);
1843
Simon Horman25cd9ba2014-10-06 05:05:13 -07001844 if (!eth_p_mpls(mpls->mpls_ethertype))
1845 return -EINVAL;
1846 /* Prohibit push MPLS other than to a white list
1847 * for packets that have a known tag order.
1848 */
1849 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1850 (eth_type != htons(ETH_P_IP) &&
1851 eth_type != htons(ETH_P_IPV6) &&
1852 eth_type != htons(ETH_P_ARP) &&
1853 eth_type != htons(ETH_P_RARP) &&
1854 !eth_p_mpls(eth_type)))
1855 return -EINVAL;
1856 eth_type = mpls->mpls_ethertype;
1857 break;
1858 }
1859
1860 case OVS_ACTION_ATTR_POP_MPLS:
1861 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1862 !eth_p_mpls(eth_type))
1863 return -EINVAL;
1864
1865 /* Disallow subsequent L2.5+ set and mpls_pop actions
1866 * as there is no check here to ensure that the new
1867 * eth_type is valid and thus set actions could
1868 * write off the end of the packet or otherwise
1869 * corrupt it.
1870 *
1871 * Support for these actions is planned using packet
1872 * recirculation.
1873 */
1874 eth_type = htons(0);
1875 break;
1876
Pravin B Shelare6445712013-10-03 18:16:47 -07001877 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001878 err = validate_set(a, key, sfa,
Pravin B Shelarec449f42014-12-23 16:20:20 -08001879 &skip_copy, eth_type, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001880 if (err)
1881 return err;
1882 break;
1883
1884 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001885 err = validate_and_copy_sample(a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001886 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001887 if (err)
1888 return err;
1889 skip_copy = true;
1890 break;
1891
1892 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001893 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001894 return -EINVAL;
1895 }
1896 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001897 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001898 if (err)
1899 return err;
1900 }
1901 }
1902
1903 if (rem > 0)
1904 return -EINVAL;
1905
1906 return 0;
1907}
1908
Simon Horman25cd9ba2014-10-06 05:05:13 -07001909int ovs_nla_copy_actions(const struct nlattr *attr,
1910 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001911 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07001912{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001913 int err;
1914
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001915 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001916 if (IS_ERR(*sfa))
1917 return PTR_ERR(*sfa);
1918
1919 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001920 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001921 if (err)
1922 kfree(*sfa);
1923
1924 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001925}
1926
Pravin B Shelare6445712013-10-03 18:16:47 -07001927static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1928{
1929 const struct nlattr *a;
1930 struct nlattr *start;
1931 int err = 0, rem;
1932
1933 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1934 if (!start)
1935 return -EMSGSIZE;
1936
1937 nla_for_each_nested(a, attr, rem) {
1938 int type = nla_type(a);
1939 struct nlattr *st_sample;
1940
1941 switch (type) {
1942 case OVS_SAMPLE_ATTR_PROBABILITY:
1943 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1944 sizeof(u32), nla_data(a)))
1945 return -EMSGSIZE;
1946 break;
1947 case OVS_SAMPLE_ATTR_ACTIONS:
1948 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1949 if (!st_sample)
1950 return -EMSGSIZE;
1951 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1952 if (err)
1953 return err;
1954 nla_nest_end(skb, st_sample);
1955 break;
1956 }
1957 }
1958
1959 nla_nest_end(skb, start);
1960 return err;
1961}
1962
1963static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1964{
1965 const struct nlattr *ovs_key = nla_data(a);
1966 int key_type = nla_type(ovs_key);
1967 struct nlattr *start;
1968 int err;
1969
1970 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001971 case OVS_KEY_ATTR_TUNNEL_INFO: {
1972 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1973
Pravin B Shelare6445712013-10-03 18:16:47 -07001974 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1975 if (!start)
1976 return -EMSGSIZE;
1977
Jesse Grossf0b128c2014-10-03 15:35:31 -07001978 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001979 tun_info->options_len ?
1980 tun_info->options : NULL,
1981 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001982 if (err)
1983 return err;
1984 nla_nest_end(skb, start);
1985 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001986 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001987 default:
1988 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1989 return -EMSGSIZE;
1990 break;
1991 }
1992
1993 return 0;
1994}
1995
1996int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1997{
1998 const struct nlattr *a;
1999 int rem, err;
2000
2001 nla_for_each_attr(a, attr, len, rem) {
2002 int type = nla_type(a);
2003
2004 switch (type) {
2005 case OVS_ACTION_ATTR_SET:
2006 err = set_action_to_attr(a, skb);
2007 if (err)
2008 return err;
2009 break;
2010
2011 case OVS_ACTION_ATTR_SAMPLE:
2012 err = sample_action_to_attr(a, skb);
2013 if (err)
2014 return err;
2015 break;
2016 default:
2017 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2018 return -EMSGSIZE;
2019 break;
2020 }
2021 }
2022
2023 return 0;
2024}