blob: 2e8a9cd1080257cc743102313106fa1072a6c81f [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,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800115 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700116{
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;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800143 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700144 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;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800224 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
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. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800233 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
234 (unsigned long long)key_attrs,
235 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700236 return false;
237 }
238
239 if ((mask_attrs & mask_allowed) != mask_attrs) {
240 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800241 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
242 (unsigned long long)mask_attrs,
243 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700244 return false;
245 }
246
247 return true;
248}
249
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800250size_t ovs_tun_key_attr_size(void)
251{
252 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
253 * updating this function.
254 */
255 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
256 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
257 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
258 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
259 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
260 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
261 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
262 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
263 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
264 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
265 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
266}
267
Joe Stringer41af73e2014-10-18 16:14:14 -0700268size_t ovs_key_attr_size(void)
269{
270 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
271 * updating this function.
272 */
273 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
274
275 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
276 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800277 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700278 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
279 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
280 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
281 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
282 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
283 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
284 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
285 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
286 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
287 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
288 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
289 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
290}
291
Pravin B Shelare6445712013-10-03 18:16:47 -0700292/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
293static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
294 [OVS_KEY_ATTR_ENCAP] = -1,
295 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
296 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
297 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
298 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
299 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
300 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
301 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
302 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
303 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700304 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -0700305 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
306 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
307 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
308 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
309 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
310 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
Andy Zhou971427f32014-09-15 19:37:25 -0700311 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
312 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -0700313 [OVS_KEY_ATTR_TUNNEL] = -1,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700314 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
Pravin B Shelare6445712013-10-03 18:16:47 -0700315};
316
317static bool is_all_zero(const u8 *fp, size_t size)
318{
319 int i;
320
321 if (!fp)
322 return false;
323
324 for (i = 0; i < size; i++)
325 if (fp[i])
326 return false;
327
328 return true;
329}
330
331static int __parse_flow_nlattrs(const struct nlattr *attr,
332 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800333 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700334{
335 const struct nlattr *nla;
336 u64 attrs;
337 int rem;
338
339 attrs = *attrsp;
340 nla_for_each_nested(nla, attr, rem) {
341 u16 type = nla_type(nla);
342 int expected_len;
343
344 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800345 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700346 type, OVS_KEY_ATTR_MAX);
347 return -EINVAL;
348 }
349
350 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800351 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700352 return -EINVAL;
353 }
354
355 expected_len = ovs_key_lens[type];
356 if (nla_len(nla) != expected_len && expected_len != -1) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800357 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
358 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700359 return -EINVAL;
360 }
361
362 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
363 attrs |= 1 << type;
364 a[type] = nla;
365 }
366 }
367 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800368 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700369 return -EINVAL;
370 }
371
372 *attrsp = attrs;
373 return 0;
374}
375
376static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800377 const struct nlattr *a[], u64 *attrsp,
378 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700379{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800380 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700381}
382
383static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800384 const struct nlattr *a[], u64 *attrsp,
385 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700386{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800387 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
388}
389
390static int genev_tun_opt_from_nlattr(const struct nlattr *a,
391 struct sw_flow_match *match, bool is_mask,
392 bool log)
393{
394 unsigned long opt_key_offset;
395
396 if (nla_len(a) > sizeof(match->key->tun_opts)) {
397 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
398 nla_len(a), sizeof(match->key->tun_opts));
399 return -EINVAL;
400 }
401
402 if (nla_len(a) % 4 != 0) {
403 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
404 nla_len(a));
405 return -EINVAL;
406 }
407
408 /* We need to record the length of the options passed
409 * down, otherwise packets with the same format but
410 * additional options will be silently matched.
411 */
412 if (!is_mask) {
413 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
414 false);
415 } else {
416 /* This is somewhat unusual because it looks at
417 * both the key and mask while parsing the
418 * attributes (and by extension assumes the key
419 * is parsed first). Normally, we would verify
420 * that each is the correct length and that the
421 * attributes line up in the validate function.
422 * However, that is difficult because this is
423 * variable length and we won't have the
424 * information later.
425 */
426 if (match->key->tun_opts_len != nla_len(a)) {
427 OVS_NLERR(log, "Geneve option len %d != mask len %d",
428 match->key->tun_opts_len, nla_len(a));
429 return -EINVAL;
430 }
431
432 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
433 }
434
Thomas Grafd91641d2015-01-15 03:53:57 +0100435 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800436 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
437 nla_len(a), is_mask);
438 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700439}
440
441static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800442 struct sw_flow_match *match, bool is_mask,
443 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700444{
445 struct nlattr *a;
446 int rem;
447 bool ttl = false;
448 __be16 tun_flags = 0;
449
450 nla_for_each_nested(a, attr, rem) {
451 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800452 int err;
453
Pravin B Shelare6445712013-10-03 18:16:47 -0700454 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
455 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
456 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
457 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
458 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
459 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
460 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
461 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800462 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = sizeof(u16),
463 [OVS_TUNNEL_KEY_ATTR_TP_DST] = sizeof(u16),
Jesse Gross67fa0342014-10-03 15:35:30 -0700464 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
Jesse Grossf5796682014-10-03 15:35:33 -0700465 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
Pravin B Shelare6445712013-10-03 18:16:47 -0700466 };
467
468 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800469 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
470 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700471 return -EINVAL;
472 }
473
Jesse Grossf5796682014-10-03 15:35:33 -0700474 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
475 ovs_tunnel_key_lens[type] != -1) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800476 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700477 type, nla_len(a), ovs_tunnel_key_lens[type]);
478 return -EINVAL;
479 }
480
481 switch (type) {
482 case OVS_TUNNEL_KEY_ATTR_ID:
483 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
484 nla_get_be64(a), is_mask);
485 tun_flags |= TUNNEL_KEY;
486 break;
487 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
488 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
489 nla_get_be32(a), is_mask);
490 break;
491 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
492 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
493 nla_get_be32(a), is_mask);
494 break;
495 case OVS_TUNNEL_KEY_ATTR_TOS:
496 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
497 nla_get_u8(a), is_mask);
498 break;
499 case OVS_TUNNEL_KEY_ATTR_TTL:
500 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
501 nla_get_u8(a), is_mask);
502 ttl = true;
503 break;
504 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
505 tun_flags |= TUNNEL_DONT_FRAGMENT;
506 break;
507 case OVS_TUNNEL_KEY_ATTR_CSUM:
508 tun_flags |= TUNNEL_CSUM;
509 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800510 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
511 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
512 nla_get_be16(a), is_mask);
513 break;
514 case OVS_TUNNEL_KEY_ATTR_TP_DST:
515 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
516 nla_get_be16(a), is_mask);
517 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700518 case OVS_TUNNEL_KEY_ATTR_OAM:
519 tun_flags |= TUNNEL_OAM;
520 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700521 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800522 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
523 if (err)
524 return err;
525
Jesse Grossf5796682014-10-03 15:35:33 -0700526 tun_flags |= TUNNEL_OPTIONS_PRESENT;
Jesse Grossf5796682014-10-03 15:35:33 -0700527 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700528 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800529 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700530 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700531 return -EINVAL;
532 }
533 }
534
535 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
536
537 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800538 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
539 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700540 return -EINVAL;
541 }
542
543 if (!is_mask) {
544 if (!match->key->tun_key.ipv4_dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800545 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700546 return -EINVAL;
547 }
548
549 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800550 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700551 return -EINVAL;
552 }
553 }
554
555 return 0;
556}
557
Jesse Grossf5796682014-10-03 15:35:33 -0700558static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
559 const struct ovs_key_ipv4_tunnel *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100560 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700561{
Pravin B Shelare6445712013-10-03 18:16:47 -0700562 if (output->tun_flags & TUNNEL_KEY &&
563 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
564 return -EMSGSIZE;
565 if (output->ipv4_src &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700566 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700567 return -EMSGSIZE;
568 if (output->ipv4_dst &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700569 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700570 return -EMSGSIZE;
571 if (output->ipv4_tos &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700572 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700573 return -EMSGSIZE;
574 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
575 return -EMSGSIZE;
576 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700577 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700578 return -EMSGSIZE;
579 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700580 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
581 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800582 if (output->tp_src &&
583 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
584 return -EMSGSIZE;
585 if (output->tp_dst &&
586 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
587 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700588 if ((output->tun_flags & TUNNEL_OAM) &&
589 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700590 return -EMSGSIZE;
Jesse Grossf5796682014-10-03 15:35:33 -0700591 if (tun_opts &&
592 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
593 swkey_tun_opts_len, tun_opts))
594 return -EMSGSIZE;
595
596 return 0;
597}
598
Jesse Grossf5796682014-10-03 15:35:33 -0700599static int ipv4_tun_to_nlattr(struct sk_buff *skb,
600 const struct ovs_key_ipv4_tunnel *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100601 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700602{
603 struct nlattr *nla;
604 int err;
605
606 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
607 if (!nla)
608 return -EMSGSIZE;
609
610 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
611 if (err)
612 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700613
614 nla_nest_end(skb, nla);
615 return 0;
616}
617
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800618int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
619 const struct ovs_tunnel_info *egress_tun_info)
620{
621 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
622 egress_tun_info->options,
623 egress_tun_info->options_len);
624}
625
Pravin B Shelare6445712013-10-03 18:16:47 -0700626static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800627 const struct nlattr **a, bool is_mask,
628 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700629{
Andy Zhou971427f32014-09-15 19:37:25 -0700630 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
631 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
632
633 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
634 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
635 }
636
637 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
638 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
639
640 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
641 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
642 }
643
Pravin B Shelare6445712013-10-03 18:16:47 -0700644 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
645 SW_FLOW_KEY_PUT(match, phy.priority,
646 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
647 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
648 }
649
650 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
651 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
652
Jesse Gross426cda52014-10-06 05:08:38 -0700653 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700654 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700655 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800656 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700657 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700658 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700659 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700660
661 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
662 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
663 } else if (!is_mask) {
664 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
665 }
666
667 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
668 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
669
670 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
671 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
672 }
673 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
674 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800675 is_mask, log))
Pravin B Shelare6445712013-10-03 18:16:47 -0700676 return -EINVAL;
677 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
678 }
679 return 0;
680}
681
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700682static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800683 const struct nlattr **a, bool is_mask,
684 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700685{
686 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700687
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800688 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700689 if (err)
690 return err;
691
692 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
693 const struct ovs_key_ethernet *eth_key;
694
695 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
696 SW_FLOW_KEY_MEMCPY(match, eth.src,
697 eth_key->eth_src, ETH_ALEN, is_mask);
698 SW_FLOW_KEY_MEMCPY(match, eth.dst,
699 eth_key->eth_dst, ETH_ALEN, is_mask);
700 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
701 }
702
703 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
704 __be16 tci;
705
706 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
707 if (!(tci & htons(VLAN_TAG_PRESENT))) {
708 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800709 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700710 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800711 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700712
713 return -EINVAL;
714 }
715
716 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
717 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700718 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700719
720 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
721 __be16 eth_type;
722
723 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
724 if (is_mask) {
725 /* Always exact match EtherType. */
726 eth_type = htons(0xffff);
727 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800728 OVS_NLERR(log, "EtherType %x is less than min %x",
729 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700730 return -EINVAL;
731 }
732
733 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
734 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
735 } else if (!is_mask) {
736 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
737 }
738
739 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
740 const struct ovs_key_ipv4 *ipv4_key;
741
742 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
743 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800744 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
745 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700746 return -EINVAL;
747 }
748 SW_FLOW_KEY_PUT(match, ip.proto,
749 ipv4_key->ipv4_proto, is_mask);
750 SW_FLOW_KEY_PUT(match, ip.tos,
751 ipv4_key->ipv4_tos, is_mask);
752 SW_FLOW_KEY_PUT(match, ip.ttl,
753 ipv4_key->ipv4_ttl, is_mask);
754 SW_FLOW_KEY_PUT(match, ip.frag,
755 ipv4_key->ipv4_frag, is_mask);
756 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
757 ipv4_key->ipv4_src, is_mask);
758 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
759 ipv4_key->ipv4_dst, is_mask);
760 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
761 }
762
763 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
764 const struct ovs_key_ipv6 *ipv6_key;
765
766 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
767 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800768 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
769 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700770 return -EINVAL;
771 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800772
Joe Stringerd3052bb2014-11-19 13:54:49 -0800773 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500774 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800775 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
776 return -EINVAL;
777 }
778
Pravin B Shelare6445712013-10-03 18:16:47 -0700779 SW_FLOW_KEY_PUT(match, ipv6.label,
780 ipv6_key->ipv6_label, is_mask);
781 SW_FLOW_KEY_PUT(match, ip.proto,
782 ipv6_key->ipv6_proto, is_mask);
783 SW_FLOW_KEY_PUT(match, ip.tos,
784 ipv6_key->ipv6_tclass, is_mask);
785 SW_FLOW_KEY_PUT(match, ip.ttl,
786 ipv6_key->ipv6_hlimit, is_mask);
787 SW_FLOW_KEY_PUT(match, ip.frag,
788 ipv6_key->ipv6_frag, is_mask);
789 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
790 ipv6_key->ipv6_src,
791 sizeof(match->key->ipv6.addr.src),
792 is_mask);
793 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
794 ipv6_key->ipv6_dst,
795 sizeof(match->key->ipv6.addr.dst),
796 is_mask);
797
798 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
799 }
800
801 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
802 const struct ovs_key_arp *arp_key;
803
804 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
805 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800806 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700807 arp_key->arp_op);
808 return -EINVAL;
809 }
810
811 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
812 arp_key->arp_sip, is_mask);
813 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
814 arp_key->arp_tip, is_mask);
815 SW_FLOW_KEY_PUT(match, ip.proto,
816 ntohs(arp_key->arp_op), is_mask);
817 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
818 arp_key->arp_sha, ETH_ALEN, is_mask);
819 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
820 arp_key->arp_tha, ETH_ALEN, is_mask);
821
822 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
823 }
824
Simon Horman25cd9ba2014-10-06 05:05:13 -0700825 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
826 const struct ovs_key_mpls *mpls_key;
827
828 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
829 SW_FLOW_KEY_PUT(match, mpls.top_lse,
830 mpls_key->mpls_lse, is_mask);
831
832 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
833 }
834
Pravin B Shelare6445712013-10-03 18:16:47 -0700835 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
836 const struct ovs_key_tcp *tcp_key;
837
838 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700839 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
840 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700841 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
842 }
843
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700844 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700845 SW_FLOW_KEY_PUT(match, tp.flags,
846 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
847 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700848 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
849 }
850
Pravin B Shelare6445712013-10-03 18:16:47 -0700851 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
852 const struct ovs_key_udp *udp_key;
853
854 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700855 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
856 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700857 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
858 }
859
860 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
861 const struct ovs_key_sctp *sctp_key;
862
863 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700864 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
865 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700866 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
867 }
868
869 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
870 const struct ovs_key_icmp *icmp_key;
871
872 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700873 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700874 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700875 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700876 htons(icmp_key->icmp_code), is_mask);
877 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
878 }
879
880 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
881 const struct ovs_key_icmpv6 *icmpv6_key;
882
883 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700884 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700885 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700886 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700887 htons(icmpv6_key->icmpv6_code), is_mask);
888 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
889 }
890
891 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
892 const struct ovs_key_nd *nd_key;
893
894 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
895 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
896 nd_key->nd_target,
897 sizeof(match->key->ipv6.nd.target),
898 is_mask);
899 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
900 nd_key->nd_sll, ETH_ALEN, is_mask);
901 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
902 nd_key->nd_tll, ETH_ALEN, is_mask);
903 attrs &= ~(1 << OVS_KEY_ATTR_ND);
904 }
905
Jesse Gross426cda52014-10-06 05:08:38 -0700906 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800907 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -0700908 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -0700909 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700910 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700911
912 return 0;
913}
914
Pravin B Shelarf47de062014-10-16 21:55:45 -0700915static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
Pravin B Shelare6445712013-10-03 18:16:47 -0700916{
Pravin B Shelarf47de062014-10-16 21:55:45 -0700917 struct nlattr *nla;
918 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700919
Pravin B Shelarf47de062014-10-16 21:55:45 -0700920 /* The nlattr stream should already have been validated */
921 nla_for_each_nested(nla, attr, rem) {
922 /* We assume that ovs_key_lens[type] == -1 means that type is a
923 * nested attribute
924 */
925 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
926 nlattr_set(nla, val, false);
927 else
928 memset(nla_data(nla), val, nla_len(nla));
929 }
930}
931
932static void mask_set_nlattr(struct nlattr *attr, u8 val)
933{
934 nlattr_set(attr, val, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700935}
936
937/**
938 * ovs_nla_get_match - parses Netlink attributes into a flow key and
939 * mask. In case the 'mask' is NULL, the flow is treated as exact match
940 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
941 * does not include any don't care bit.
942 * @match: receives the extracted flow match information.
943 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
944 * sequence. The fields should of the packet that triggered the creation
945 * of this flow.
946 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
947 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800948 * @log: Boolean to allow kernel error logging. Normally true, but when
949 * probing for feature compatibility this should be passed in as false to
950 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -0700951 */
952int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -0700953 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800954 const struct nlattr *nla_mask,
955 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700956{
957 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
958 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -0700959 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700960 u64 key_attrs = 0;
961 u64 mask_attrs = 0;
962 bool encap_valid = false;
963 int err;
964
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800965 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700966 if (err)
967 return err;
968
969 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
970 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
971 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
972 __be16 tci;
973
974 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
975 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800976 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700977 return -EINVAL;
978 }
979
980 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
981 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
982 encap = a[OVS_KEY_ATTR_ENCAP];
983 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
984 encap_valid = true;
985
986 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800987 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700988 if (err)
989 return err;
990 } else if (!tci) {
991 /* Corner case for truncated 802.1Q header. */
992 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800993 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700994 return -EINVAL;
995 }
996 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800997 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -0700998 return -EINVAL;
999 }
1000 }
1001
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001002 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001003 if (err)
1004 return err;
1005
Pravin B Shelara85311b2014-10-19 12:03:40 -07001006 if (match->mask) {
1007 if (!nla_mask) {
1008 /* Create an exact match mask. We need to set to 0xff
1009 * all the 'match->mask' fields that have been touched
1010 * in 'match->key'. We cannot simply memset
1011 * 'match->mask', because padding bytes and fields not
1012 * specified in 'match->key' should be left to 0.
1013 * Instead, we use a stream of netlink attributes,
1014 * copied from 'key' and set to 0xff.
1015 * ovs_key_from_nlattrs() will take care of filling
1016 * 'match->mask' appropriately.
1017 */
1018 newmask = kmemdup(nla_key,
1019 nla_total_size(nla_len(nla_key)),
1020 GFP_KERNEL);
1021 if (!newmask)
1022 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001023
Pravin B Shelara85311b2014-10-19 12:03:40 -07001024 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001025
Pravin B Shelara85311b2014-10-19 12:03:40 -07001026 /* The userspace does not send tunnel attributes that
1027 * are 0, but we should not wildcard them nonetheless.
1028 */
1029 if (match->key->tun_key.ipv4_dst)
1030 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1031 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001032
Pravin B Shelara85311b2014-10-19 12:03:40 -07001033 nla_mask = newmask;
1034 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001035
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001036 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001037 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001038 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001039
Pravin B Shelara85311b2014-10-19 12:03:40 -07001040 /* Always match on tci. */
1041 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1042
Pravin B Shelarf47de062014-10-16 21:55:45 -07001043 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001044 __be16 eth_type = 0;
1045 __be16 tci = 0;
1046
1047 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001048 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001049 err = -EINVAL;
1050 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001051 }
1052
1053 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1054 if (a[OVS_KEY_ATTR_ETHERTYPE])
1055 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1056
1057 if (eth_type == htons(0xffff)) {
1058 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1059 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001060 err = parse_flow_mask_nlattrs(encap, a,
1061 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001062 if (err)
1063 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001064 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001065 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1066 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001067 err = -EINVAL;
1068 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001069 }
1070
1071 if (a[OVS_KEY_ATTR_VLAN])
1072 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1073
1074 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001075 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1076 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001077 err = -EINVAL;
1078 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001079 }
1080 }
1081
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001082 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001083 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001084 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001085 }
1086
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001087 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001088 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001089
Pravin B Shelarf47de062014-10-16 21:55:45 -07001090free_newmask:
1091 kfree(newmask);
1092 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001093}
1094
1095/**
1096 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001097 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001098 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1099 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001100 * @log: Boolean to allow kernel error logging. Normally true, but when
1101 * probing for feature compatibility this should be passed in as false to
1102 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001103 *
1104 * This parses a series of Netlink attributes that form a flow key, which must
1105 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1106 * get the metadata, that is, the parts of the flow key that cannot be
1107 * extracted from the packet itself.
1108 */
1109
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001110int ovs_nla_get_flow_metadata(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001111 struct sw_flow_key *key,
1112 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001113{
Pravin B Shelare6445712013-10-03 18:16:47 -07001114 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001115 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001116 u64 attrs = 0;
1117 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001118
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001119 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001120 if (err)
1121 return -EINVAL;
1122
1123 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001124 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001125
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001126 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001127
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001128 return metadata_from_nlattrs(&match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001129}
1130
1131int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1132 const struct sw_flow_key *output, struct sk_buff *skb)
1133{
1134 struct ovs_key_ethernet *eth_key;
1135 struct nlattr *nla, *encap;
1136 bool is_mask = (swkey != output);
1137
Andy Zhou971427f32014-09-15 19:37:25 -07001138 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1139 goto nla_put_failure;
1140
1141 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1142 goto nla_put_failure;
1143
Pravin B Shelare6445712013-10-03 18:16:47 -07001144 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1145 goto nla_put_failure;
1146
Jesse Grossf5796682014-10-03 15:35:33 -07001147 if ((swkey->tun_key.ipv4_dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001148 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001149
1150 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001151 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001152
1153 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1154 swkey->tun_opts_len))
1155 goto nla_put_failure;
1156 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001157
1158 if (swkey->phy.in_port == DP_MAX_PORTS) {
1159 if (is_mask && (output->phy.in_port == 0xffff))
1160 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1161 goto nla_put_failure;
1162 } else {
1163 u16 upper_u16;
1164 upper_u16 = !is_mask ? 0 : 0xffff;
1165
1166 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1167 (upper_u16 << 16) | output->phy.in_port))
1168 goto nla_put_failure;
1169 }
1170
1171 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1172 goto nla_put_failure;
1173
1174 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1175 if (!nla)
1176 goto nla_put_failure;
1177
1178 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001179 ether_addr_copy(eth_key->eth_src, output->eth.src);
1180 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001181
1182 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1183 __be16 eth_type;
1184 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1185 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1186 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1187 goto nla_put_failure;
1188 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1189 if (!swkey->eth.tci)
1190 goto unencap;
1191 } else
1192 encap = NULL;
1193
1194 if (swkey->eth.type == htons(ETH_P_802_2)) {
1195 /*
1196 * Ethertype 802.2 is represented in the netlink with omitted
1197 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1198 * 0xffff in the mask attribute. Ethertype can also
1199 * be wildcarded.
1200 */
1201 if (is_mask && output->eth.type)
1202 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1203 output->eth.type))
1204 goto nla_put_failure;
1205 goto unencap;
1206 }
1207
1208 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1209 goto nla_put_failure;
1210
1211 if (swkey->eth.type == htons(ETH_P_IP)) {
1212 struct ovs_key_ipv4 *ipv4_key;
1213
1214 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1215 if (!nla)
1216 goto nla_put_failure;
1217 ipv4_key = nla_data(nla);
1218 ipv4_key->ipv4_src = output->ipv4.addr.src;
1219 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1220 ipv4_key->ipv4_proto = output->ip.proto;
1221 ipv4_key->ipv4_tos = output->ip.tos;
1222 ipv4_key->ipv4_ttl = output->ip.ttl;
1223 ipv4_key->ipv4_frag = output->ip.frag;
1224 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1225 struct ovs_key_ipv6 *ipv6_key;
1226
1227 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1228 if (!nla)
1229 goto nla_put_failure;
1230 ipv6_key = nla_data(nla);
1231 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1232 sizeof(ipv6_key->ipv6_src));
1233 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1234 sizeof(ipv6_key->ipv6_dst));
1235 ipv6_key->ipv6_label = output->ipv6.label;
1236 ipv6_key->ipv6_proto = output->ip.proto;
1237 ipv6_key->ipv6_tclass = output->ip.tos;
1238 ipv6_key->ipv6_hlimit = output->ip.ttl;
1239 ipv6_key->ipv6_frag = output->ip.frag;
1240 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1241 swkey->eth.type == htons(ETH_P_RARP)) {
1242 struct ovs_key_arp *arp_key;
1243
1244 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1245 if (!nla)
1246 goto nla_put_failure;
1247 arp_key = nla_data(nla);
1248 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1249 arp_key->arp_sip = output->ipv4.addr.src;
1250 arp_key->arp_tip = output->ipv4.addr.dst;
1251 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001252 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1253 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001254 } else if (eth_p_mpls(swkey->eth.type)) {
1255 struct ovs_key_mpls *mpls_key;
1256
1257 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1258 if (!nla)
1259 goto nla_put_failure;
1260 mpls_key = nla_data(nla);
1261 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001262 }
1263
1264 if ((swkey->eth.type == htons(ETH_P_IP) ||
1265 swkey->eth.type == htons(ETH_P_IPV6)) &&
1266 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1267
1268 if (swkey->ip.proto == IPPROTO_TCP) {
1269 struct ovs_key_tcp *tcp_key;
1270
1271 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1272 if (!nla)
1273 goto nla_put_failure;
1274 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001275 tcp_key->tcp_src = output->tp.src;
1276 tcp_key->tcp_dst = output->tp.dst;
1277 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1278 output->tp.flags))
1279 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001280 } else if (swkey->ip.proto == IPPROTO_UDP) {
1281 struct ovs_key_udp *udp_key;
1282
1283 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1284 if (!nla)
1285 goto nla_put_failure;
1286 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001287 udp_key->udp_src = output->tp.src;
1288 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001289 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1290 struct ovs_key_sctp *sctp_key;
1291
1292 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1293 if (!nla)
1294 goto nla_put_failure;
1295 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001296 sctp_key->sctp_src = output->tp.src;
1297 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001298 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1299 swkey->ip.proto == IPPROTO_ICMP) {
1300 struct ovs_key_icmp *icmp_key;
1301
1302 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1303 if (!nla)
1304 goto nla_put_failure;
1305 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001306 icmp_key->icmp_type = ntohs(output->tp.src);
1307 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001308 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1309 swkey->ip.proto == IPPROTO_ICMPV6) {
1310 struct ovs_key_icmpv6 *icmpv6_key;
1311
1312 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1313 sizeof(*icmpv6_key));
1314 if (!nla)
1315 goto nla_put_failure;
1316 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001317 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1318 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001319
1320 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1321 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1322 struct ovs_key_nd *nd_key;
1323
1324 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1325 if (!nla)
1326 goto nla_put_failure;
1327 nd_key = nla_data(nla);
1328 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1329 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001330 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1331 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001332 }
1333 }
1334 }
1335
1336unencap:
1337 if (encap)
1338 nla_nest_end(skb, encap);
1339
1340 return 0;
1341
1342nla_put_failure:
1343 return -EMSGSIZE;
1344}
1345
1346#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1347
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001348static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001349{
1350 struct sw_flow_actions *sfa;
1351
Jesse Gross426cda52014-10-06 05:08:38 -07001352 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001353 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001354 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001355 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001356
1357 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1358 if (!sfa)
1359 return ERR_PTR(-ENOMEM);
1360
1361 sfa->actions_len = 0;
1362 return sfa;
1363}
1364
Pravin B Shelare6445712013-10-03 18:16:47 -07001365/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1366 * The caller must hold rcu_read_lock for this to be sensible. */
1367void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1368{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001369 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001370}
1371
1372static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001373 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001374{
1375
1376 struct sw_flow_actions *acts;
1377 int new_acts_size;
1378 int req_size = NLA_ALIGN(attr_len);
1379 int next_offset = offsetof(struct sw_flow_actions, actions) +
1380 (*sfa)->actions_len;
1381
1382 if (req_size <= (ksize(*sfa) - next_offset))
1383 goto out;
1384
1385 new_acts_size = ksize(*sfa) * 2;
1386
1387 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1388 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1389 return ERR_PTR(-EMSGSIZE);
1390 new_acts_size = MAX_ACTIONS_BUFSIZE;
1391 }
1392
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001393 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001394 if (IS_ERR(acts))
1395 return (void *)acts;
1396
1397 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1398 acts->actions_len = (*sfa)->actions_len;
1399 kfree(*sfa);
1400 *sfa = acts;
1401
1402out:
1403 (*sfa)->actions_len += req_size;
1404 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1405}
1406
Jesse Grossf0b128c2014-10-03 15:35:31 -07001407static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001408 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001409{
1410 struct nlattr *a;
1411
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001412 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001413 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001414 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001415
1416 a->nla_type = attrtype;
1417 a->nla_len = nla_attr_size(len);
1418
1419 if (data)
1420 memcpy(nla_data(a), data, len);
1421 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1422
Jesse Grossf0b128c2014-10-03 15:35:31 -07001423 return a;
1424}
1425
1426static int add_action(struct sw_flow_actions **sfa, int attrtype,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001427 void *data, int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001428{
1429 struct nlattr *a;
1430
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001431 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001432
Fabian Frederickf35423c12014-11-14 19:32:58 +01001433 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001434}
1435
1436static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001437 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001438{
1439 int used = (*sfa)->actions_len;
1440 int err;
1441
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001442 err = add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001443 if (err)
1444 return err;
1445
1446 return used;
1447}
1448
1449static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1450 int st_offset)
1451{
1452 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1453 st_offset);
1454
1455 a->nla_len = sfa->actions_len - st_offset;
1456}
1457
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001458static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001459 const struct sw_flow_key *key,
1460 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001461 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001462
Pravin B Shelare6445712013-10-03 18:16:47 -07001463static int validate_and_copy_sample(const struct nlattr *attr,
1464 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001465 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001466 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001467{
1468 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1469 const struct nlattr *probability, *actions;
1470 const struct nlattr *a;
1471 int rem, start, err, st_acts;
1472
1473 memset(attrs, 0, sizeof(attrs));
1474 nla_for_each_nested(a, attr, rem) {
1475 int type = nla_type(a);
1476 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1477 return -EINVAL;
1478 attrs[type] = a;
1479 }
1480 if (rem)
1481 return -EINVAL;
1482
1483 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1484 if (!probability || nla_len(probability) != sizeof(u32))
1485 return -EINVAL;
1486
1487 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1488 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1489 return -EINVAL;
1490
1491 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001492 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001493 if (start < 0)
1494 return start;
1495 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001496 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001497 if (err)
1498 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001499 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001500 if (st_acts < 0)
1501 return st_acts;
1502
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001503 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001504 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001505 if (err)
1506 return err;
1507
1508 add_nested_action_end(*sfa, st_acts);
1509 add_nested_action_end(*sfa, start);
1510
1511 return 0;
1512}
1513
Simon Horman25cd9ba2014-10-06 05:05:13 -07001514static int validate_tp_port(const struct sw_flow_key *flow_key,
1515 __be16 eth_type)
Pravin B Shelare6445712013-10-03 18:16:47 -07001516{
Simon Horman25cd9ba2014-10-06 05:05:13 -07001517 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001518 (flow_key->tp.src || flow_key->tp.dst))
1519 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001520
1521 return -EINVAL;
1522}
1523
1524void ovs_match_init(struct sw_flow_match *match,
1525 struct sw_flow_key *key,
1526 struct sw_flow_mask *mask)
1527{
1528 memset(match, 0, sizeof(*match));
1529 match->key = key;
1530 match->mask = mask;
1531
1532 memset(key, 0, sizeof(*key));
1533
1534 if (mask) {
1535 memset(&mask->key, 0, sizeof(mask->key));
1536 mask->range.start = mask->range.end = 0;
1537 }
1538}
1539
Thomas Grafd91641d2015-01-15 03:53:57 +01001540static int validate_geneve_opts(struct sw_flow_key *key)
1541{
1542 struct geneve_opt *option;
1543 int opts_len = key->tun_opts_len;
1544 bool crit_opt = false;
1545
1546 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1547 while (opts_len > 0) {
1548 int len;
1549
1550 if (opts_len < sizeof(*option))
1551 return -EINVAL;
1552
1553 len = sizeof(*option) + option->length * 4;
1554 if (len > opts_len)
1555 return -EINVAL;
1556
1557 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1558
1559 option = (struct geneve_opt *)((u8 *)option + len);
1560 opts_len -= len;
1561 };
1562
1563 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1564
1565 return 0;
1566}
1567
Pravin B Shelare6445712013-10-03 18:16:47 -07001568static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001569 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001570{
1571 struct sw_flow_match match;
1572 struct sw_flow_key key;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001573 struct ovs_tunnel_info *tun_info;
1574 struct nlattr *a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001575 int err, start;
1576
1577 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001578 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001579 if (err)
1580 return err;
1581
Jesse Grossf5796682014-10-03 15:35:33 -07001582 if (key.tun_opts_len) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001583 err = validate_geneve_opts(&key);
1584 if (err < 0)
1585 return err;
Jesse Grossf5796682014-10-03 15:35:33 -07001586 };
1587
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001588 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001589 if (start < 0)
1590 return start;
1591
Jesse Grossf0b128c2014-10-03 15:35:31 -07001592 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001593 sizeof(*tun_info) + key.tun_opts_len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001594 if (IS_ERR(a))
1595 return PTR_ERR(a);
1596
1597 tun_info = nla_data(a);
1598 tun_info->tunnel = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001599 tun_info->options_len = key.tun_opts_len;
1600
1601 if (tun_info->options_len) {
1602 /* We need to store the options in the action itself since
1603 * everything else will go away after flow setup. We can append
1604 * it to tun_info and then point there.
1605 */
Thomas Grafd91641d2015-01-15 03:53:57 +01001606 memcpy((tun_info + 1),
1607 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1608 tun_info->options = (tun_info + 1);
Jesse Grossf5796682014-10-03 15:35:33 -07001609 } else {
1610 tun_info->options = NULL;
1611 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001612
Pravin B Shelare6445712013-10-03 18:16:47 -07001613 add_nested_action_end(*sfa, start);
1614
1615 return err;
1616}
1617
1618static int validate_set(const struct nlattr *a,
1619 const struct sw_flow_key *flow_key,
1620 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001621 bool *set_tun, __be16 eth_type, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001622{
1623 const struct nlattr *ovs_key = nla_data(a);
1624 int key_type = nla_type(ovs_key);
1625
1626 /* There can be only one key in a action */
1627 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1628 return -EINVAL;
1629
1630 if (key_type > OVS_KEY_ATTR_MAX ||
1631 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1632 ovs_key_lens[key_type] != -1))
1633 return -EINVAL;
1634
1635 switch (key_type) {
1636 const struct ovs_key_ipv4 *ipv4_key;
1637 const struct ovs_key_ipv6 *ipv6_key;
1638 int err;
1639
1640 case OVS_KEY_ATTR_PRIORITY:
1641 case OVS_KEY_ATTR_SKB_MARK:
1642 case OVS_KEY_ATTR_ETHERNET:
1643 break;
1644
1645 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001646 if (eth_p_mpls(eth_type))
1647 return -EINVAL;
1648
Pravin B Shelare6445712013-10-03 18:16:47 -07001649 *set_tun = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001650 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001651 if (err)
1652 return err;
1653 break;
1654
1655 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001656 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001657 return -EINVAL;
1658
1659 if (!flow_key->ip.proto)
1660 return -EINVAL;
1661
1662 ipv4_key = nla_data(ovs_key);
1663 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1664 return -EINVAL;
1665
1666 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1667 return -EINVAL;
1668
1669 break;
1670
1671 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001672 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001673 return -EINVAL;
1674
1675 if (!flow_key->ip.proto)
1676 return -EINVAL;
1677
1678 ipv6_key = nla_data(ovs_key);
1679 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1680 return -EINVAL;
1681
1682 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1683 return -EINVAL;
1684
1685 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1686 return -EINVAL;
1687
1688 break;
1689
1690 case OVS_KEY_ATTR_TCP:
1691 if (flow_key->ip.proto != IPPROTO_TCP)
1692 return -EINVAL;
1693
Simon Horman25cd9ba2014-10-06 05:05:13 -07001694 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001695
1696 case OVS_KEY_ATTR_UDP:
1697 if (flow_key->ip.proto != IPPROTO_UDP)
1698 return -EINVAL;
1699
Simon Horman25cd9ba2014-10-06 05:05:13 -07001700 return validate_tp_port(flow_key, eth_type);
1701
1702 case OVS_KEY_ATTR_MPLS:
1703 if (!eth_p_mpls(eth_type))
1704 return -EINVAL;
1705 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001706
1707 case OVS_KEY_ATTR_SCTP:
1708 if (flow_key->ip.proto != IPPROTO_SCTP)
1709 return -EINVAL;
1710
Simon Horman25cd9ba2014-10-06 05:05:13 -07001711 return validate_tp_port(flow_key, eth_type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001712
1713 default:
1714 return -EINVAL;
1715 }
1716
1717 return 0;
1718}
1719
1720static int validate_userspace(const struct nlattr *attr)
1721{
1722 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1723 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1724 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08001725 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07001726 };
1727 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1728 int error;
1729
1730 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1731 attr, userspace_policy);
1732 if (error)
1733 return error;
1734
1735 if (!a[OVS_USERSPACE_ATTR_PID] ||
1736 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1737 return -EINVAL;
1738
1739 return 0;
1740}
1741
1742static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001743 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001744{
1745 int totlen = NLA_ALIGN(from->nla_len);
1746 struct nlattr *to;
1747
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001748 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001749 if (IS_ERR(to))
1750 return PTR_ERR(to);
1751
1752 memcpy(to, from, totlen);
1753 return 0;
1754}
1755
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001756static int __ovs_nla_copy_actions(const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001757 const struct sw_flow_key *key,
1758 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001759 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001760{
1761 const struct nlattr *a;
1762 int rem, err;
1763
1764 if (depth >= SAMPLE_ACTION_DEPTH)
1765 return -EOVERFLOW;
1766
1767 nla_for_each_nested(a, attr, rem) {
1768 /* Expected argument lengths, (u32)-1 for variable length. */
1769 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1770 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07001771 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07001772 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001773 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1774 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07001775 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1776 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1777 [OVS_ACTION_ATTR_SET] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07001778 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1779 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
Pravin B Shelare6445712013-10-03 18:16:47 -07001780 };
1781 const struct ovs_action_push_vlan *vlan;
1782 int type = nla_type(a);
1783 bool skip_copy;
1784
1785 if (type > OVS_ACTION_ATTR_MAX ||
1786 (action_lens[type] != nla_len(a) &&
1787 action_lens[type] != (u32)-1))
1788 return -EINVAL;
1789
1790 skip_copy = false;
1791 switch (type) {
1792 case OVS_ACTION_ATTR_UNSPEC:
1793 return -EINVAL;
1794
1795 case OVS_ACTION_ATTR_USERSPACE:
1796 err = validate_userspace(a);
1797 if (err)
1798 return err;
1799 break;
1800
1801 case OVS_ACTION_ATTR_OUTPUT:
1802 if (nla_get_u32(a) >= DP_MAX_PORTS)
1803 return -EINVAL;
1804 break;
1805
Andy Zhou971427f32014-09-15 19:37:25 -07001806 case OVS_ACTION_ATTR_HASH: {
1807 const struct ovs_action_hash *act_hash = nla_data(a);
1808
1809 switch (act_hash->hash_alg) {
1810 case OVS_HASH_ALG_L4:
1811 break;
1812 default:
1813 return -EINVAL;
1814 }
1815
1816 break;
1817 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001818
1819 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001820 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07001821 break;
1822
1823 case OVS_ACTION_ATTR_PUSH_VLAN:
1824 vlan = nla_data(a);
1825 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1826 return -EINVAL;
1827 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1828 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001829 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07001830 break;
1831
Andy Zhou971427f32014-09-15 19:37:25 -07001832 case OVS_ACTION_ATTR_RECIRC:
1833 break;
1834
Simon Horman25cd9ba2014-10-06 05:05:13 -07001835 case OVS_ACTION_ATTR_PUSH_MPLS: {
1836 const struct ovs_action_push_mpls *mpls = nla_data(a);
1837
Simon Horman25cd9ba2014-10-06 05:05:13 -07001838 if (!eth_p_mpls(mpls->mpls_ethertype))
1839 return -EINVAL;
1840 /* Prohibit push MPLS other than to a white list
1841 * for packets that have a known tag order.
1842 */
1843 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1844 (eth_type != htons(ETH_P_IP) &&
1845 eth_type != htons(ETH_P_IPV6) &&
1846 eth_type != htons(ETH_P_ARP) &&
1847 eth_type != htons(ETH_P_RARP) &&
1848 !eth_p_mpls(eth_type)))
1849 return -EINVAL;
1850 eth_type = mpls->mpls_ethertype;
1851 break;
1852 }
1853
1854 case OVS_ACTION_ATTR_POP_MPLS:
1855 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1856 !eth_p_mpls(eth_type))
1857 return -EINVAL;
1858
1859 /* Disallow subsequent L2.5+ set and mpls_pop actions
1860 * as there is no check here to ensure that the new
1861 * eth_type is valid and thus set actions could
1862 * write off the end of the packet or otherwise
1863 * corrupt it.
1864 *
1865 * Support for these actions is planned using packet
1866 * recirculation.
1867 */
1868 eth_type = htons(0);
1869 break;
1870
Pravin B Shelare6445712013-10-03 18:16:47 -07001871 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001872 err = validate_set(a, key, sfa,
Pravin B Shelarec449f42014-12-23 16:20:20 -08001873 &skip_copy, eth_type, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001874 if (err)
1875 return err;
1876 break;
1877
1878 case OVS_ACTION_ATTR_SAMPLE:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001879 err = validate_and_copy_sample(a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001880 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001881 if (err)
1882 return err;
1883 skip_copy = true;
1884 break;
1885
1886 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001887 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07001888 return -EINVAL;
1889 }
1890 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001891 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001892 if (err)
1893 return err;
1894 }
1895 }
1896
1897 if (rem > 0)
1898 return -EINVAL;
1899
1900 return 0;
1901}
1902
Simon Horman25cd9ba2014-10-06 05:05:13 -07001903int ovs_nla_copy_actions(const struct nlattr *attr,
1904 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001905 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07001906{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001907 int err;
1908
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001909 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001910 if (IS_ERR(*sfa))
1911 return PTR_ERR(*sfa);
1912
1913 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001914 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001915 if (err)
1916 kfree(*sfa);
1917
1918 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07001919}
1920
Pravin B Shelare6445712013-10-03 18:16:47 -07001921static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1922{
1923 const struct nlattr *a;
1924 struct nlattr *start;
1925 int err = 0, rem;
1926
1927 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1928 if (!start)
1929 return -EMSGSIZE;
1930
1931 nla_for_each_nested(a, attr, rem) {
1932 int type = nla_type(a);
1933 struct nlattr *st_sample;
1934
1935 switch (type) {
1936 case OVS_SAMPLE_ATTR_PROBABILITY:
1937 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1938 sizeof(u32), nla_data(a)))
1939 return -EMSGSIZE;
1940 break;
1941 case OVS_SAMPLE_ATTR_ACTIONS:
1942 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1943 if (!st_sample)
1944 return -EMSGSIZE;
1945 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1946 if (err)
1947 return err;
1948 nla_nest_end(skb, st_sample);
1949 break;
1950 }
1951 }
1952
1953 nla_nest_end(skb, start);
1954 return err;
1955}
1956
1957static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1958{
1959 const struct nlattr *ovs_key = nla_data(a);
1960 int key_type = nla_type(ovs_key);
1961 struct nlattr *start;
1962 int err;
1963
1964 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07001965 case OVS_KEY_ATTR_TUNNEL_INFO: {
1966 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1967
Pravin B Shelare6445712013-10-03 18:16:47 -07001968 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1969 if (!start)
1970 return -EMSGSIZE;
1971
Jesse Grossf0b128c2014-10-03 15:35:31 -07001972 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
Jesse Grossf5796682014-10-03 15:35:33 -07001973 tun_info->options_len ?
1974 tun_info->options : NULL,
1975 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001976 if (err)
1977 return err;
1978 nla_nest_end(skb, start);
1979 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001980 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001981 default:
1982 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1983 return -EMSGSIZE;
1984 break;
1985 }
1986
1987 return 0;
1988}
1989
1990int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1991{
1992 const struct nlattr *a;
1993 int rem, err;
1994
1995 nla_for_each_attr(a, attr, len, rem) {
1996 int type = nla_type(a);
1997
1998 switch (type) {
1999 case OVS_ACTION_ATTR_SET:
2000 err = set_action_to_attr(a, skb);
2001 if (err)
2002 return err;
2003 break;
2004
2005 case OVS_ACTION_ATTR_SAMPLE:
2006 err = sample_action_to_attr(a, skb);
2007 if (err)
2008 return err;
2009 break;
2010 default:
2011 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2012 return -EMSGSIZE;
2013 break;
2014 }
2015 }
2016
2017 return 0;
2018}