Merge tag 'master-2014-11-20' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[deliverable/linux.git] / net / openvswitch / flow_netlink.c
CommitLineData
e6445719 1/*
971427f3 2 * Copyright (c) 2007-2014 Nicira, Inc.
e6445719
PS
3 *
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
2235ad1c
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
e6445719
PS
21#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>
f5796684 45#include <net/geneve.h>
e6445719
PS
46#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
25cd9ba0 49#include <net/mpls.h>
e6445719
PS
50
51#include "flow_netlink.h"
52
a85311bf
PS
53static void update_range(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
e6445719 55{
a85311bf 56 struct sw_flow_key_range *range;
e6445719
PS
57 size_t start = rounddown(offset, sizeof(long));
58 size_t end = roundup(offset + size, sizeof(long));
59
60 if (!is_mask)
61 range = &match->range;
a85311bf 62 else
e6445719
PS
63 range = &match->mask->range;
64
e6445719
PS
65 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 { \
a85311bf
PS
80 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 \
e6445719 85 (match)->key->field = value; \
e6445719
PS
86 } while (0)
87
f5796684
JG
88#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
89 do { \
a85311bf 90 update_range(match, offset, len, is_mask); \
f5796684
JG
91 if (is_mask) \
92 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
a85311bf 93 len); \
f5796684
JG
94 else \
95 memcpy((u8 *)(match)->key + offset, value_p, len); \
e6445719
PS
96 } while (0)
97
f5796684
JG
98#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
a85311bf
PS
102#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 \
f47de068
PS
110 memset((u8 *)&(match)->key->field, value, \
111 sizeof((match)->key->field)); \
f47de068 112 } while (0)
e6445719
PS
113
114static bool match_validate(const struct sw_flow_match *match,
05da5898 115 u64 key_attrs, u64 mask_attrs, bool log)
e6445719
PS
116{
117 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
118 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
119
120 /* The following mask attributes allowed only if they
121 * pass the validation tests. */
122 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
123 | (1 << OVS_KEY_ATTR_IPV6)
124 | (1 << OVS_KEY_ATTR_TCP)
5eb26b15 125 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
e6445719
PS
126 | (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)
25cd9ba0
SH
131 | (1 << OVS_KEY_ATTR_ND)
132 | (1 << OVS_KEY_ATTR_MPLS));
e6445719
PS
133
134 /* Always allowed mask fields. */
135 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
136 | (1 << OVS_KEY_ATTR_IN_PORT)
137 | (1 << OVS_KEY_ATTR_ETHERTYPE));
138
139 /* Check key attributes. */
140 if (match->key->eth.type == htons(ETH_P_ARP)
141 || match->key->eth.type == htons(ETH_P_RARP)) {
142 key_expected |= 1 << OVS_KEY_ATTR_ARP;
143 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
145 }
146
25cd9ba0
SH
147 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
e6445719
PS
153 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;
5eb26b15
JR
173 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
174 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 175 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
176 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
177 }
e6445719
PS
178 }
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;
5eb26b15
JR
208 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
209 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 210 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
211 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
212 }
e6445719
PS
213 }
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
1139e241 220 if (match->key->tp.src ==
e6445719 221 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1139e241 222 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e6445719 223 key_expected |= 1 << OVS_KEY_ATTR_ND;
1139e241 224 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
e6445719
PS
225 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. */
05da5898
JR
233 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
234 (unsigned long long)key_attrs,
235 (unsigned long long)key_expected);
e6445719
PS
236 return false;
237 }
238
239 if ((mask_attrs & mask_allowed) != mask_attrs) {
240 /* Mask attributes check failed. */
05da5898
JR
241 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
242 (unsigned long long)mask_attrs,
243 (unsigned long long)mask_allowed);
e6445719
PS
244 return false;
245 }
246
247 return true;
248}
249
8f0aad6f
WZ
250size_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
41af73e9
JS
268size_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 */
8f0aad6f 277 + ovs_tun_key_attr_size()
41af73e9
JS
278 + 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
e6445719
PS
292/* 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),
5eb26b15 304 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
e6445719
PS
305 [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),
971427f3
AZ
311 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
312 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
e6445719 313 [OVS_KEY_ATTR_TUNNEL] = -1,
25cd9ba0 314 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
e6445719
PS
315};
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[],
05da5898 333 u64 *attrsp, bool log, bool nz)
e6445719
PS
334{
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) {
05da5898 345 OVS_NLERR(log, "Key type %d is out of range max %d",
e6445719
PS
346 type, OVS_KEY_ATTR_MAX);
347 return -EINVAL;
348 }
349
350 if (attrs & (1 << type)) {
05da5898 351 OVS_NLERR(log, "Duplicate key (type %d).", type);
e6445719
PS
352 return -EINVAL;
353 }
354
355 expected_len = ovs_key_lens[type];
356 if (nla_len(nla) != expected_len && expected_len != -1) {
05da5898
JR
357 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
358 type, nla_len(nla), expected_len);
e6445719
PS
359 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) {
05da5898 368 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
e6445719
PS
369 return -EINVAL;
370 }
371
372 *attrsp = attrs;
373 return 0;
374}
375
376static int parse_flow_mask_nlattrs(const struct nlattr *attr,
05da5898
JR
377 const struct nlattr *a[], u64 *attrsp,
378 bool log)
e6445719 379{
05da5898 380 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
e6445719
PS
381}
382
383static int parse_flow_nlattrs(const struct nlattr *attr,
05da5898
JR
384 const struct nlattr *a[], u64 *attrsp,
385 bool log)
e6445719 386{
05da5898
JR
387 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
435 opt_key_offset = (unsigned long)GENEVE_OPTS((struct sw_flow_key *)0,
436 nla_len(a));
437 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
438 nla_len(a), is_mask);
439 return 0;
e6445719
PS
440}
441
442static int ipv4_tun_from_nlattr(const struct nlattr *attr,
05da5898
JR
443 struct sw_flow_match *match, bool is_mask,
444 bool log)
e6445719
PS
445{
446 struct nlattr *a;
447 int rem;
448 bool ttl = false;
449 __be16 tun_flags = 0;
450
451 nla_for_each_nested(a, attr, rem) {
452 int type = nla_type(a);
05da5898
JR
453 int err;
454
e6445719
PS
455 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
456 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
457 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
458 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
459 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
460 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
461 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
462 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
8f0aad6f
WZ
463 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = sizeof(u16),
464 [OVS_TUNNEL_KEY_ATTR_TP_DST] = sizeof(u16),
67fa0341 465 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
f5796684 466 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
e6445719
PS
467 };
468
469 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
05da5898
JR
470 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
471 type, OVS_TUNNEL_KEY_ATTR_MAX);
e6445719
PS
472 return -EINVAL;
473 }
474
f5796684
JG
475 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
476 ovs_tunnel_key_lens[type] != -1) {
05da5898 477 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
e6445719
PS
478 type, nla_len(a), ovs_tunnel_key_lens[type]);
479 return -EINVAL;
480 }
481
482 switch (type) {
483 case OVS_TUNNEL_KEY_ATTR_ID:
484 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
485 nla_get_be64(a), is_mask);
486 tun_flags |= TUNNEL_KEY;
487 break;
488 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
489 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
490 nla_get_be32(a), is_mask);
491 break;
492 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
493 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
494 nla_get_be32(a), is_mask);
495 break;
496 case OVS_TUNNEL_KEY_ATTR_TOS:
497 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
498 nla_get_u8(a), is_mask);
499 break;
500 case OVS_TUNNEL_KEY_ATTR_TTL:
501 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
502 nla_get_u8(a), is_mask);
503 ttl = true;
504 break;
505 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
506 tun_flags |= TUNNEL_DONT_FRAGMENT;
507 break;
508 case OVS_TUNNEL_KEY_ATTR_CSUM:
509 tun_flags |= TUNNEL_CSUM;
510 break;
8f0aad6f
WZ
511 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
512 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
513 nla_get_be16(a), is_mask);
514 break;
515 case OVS_TUNNEL_KEY_ATTR_TP_DST:
516 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
517 nla_get_be16(a), is_mask);
518 break;
67fa0341
JG
519 case OVS_TUNNEL_KEY_ATTR_OAM:
520 tun_flags |= TUNNEL_OAM;
521 break;
f5796684 522 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
05da5898
JR
523 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
524 if (err)
525 return err;
f5796684 526
05da5898 527 tun_flags |= TUNNEL_OPTIONS_PRESENT;
f5796684 528 break;
e6445719 529 default:
05da5898 530 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
f5796684 531 type);
e6445719
PS
532 return -EINVAL;
533 }
534 }
535
536 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
537
538 if (rem > 0) {
05da5898
JR
539 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
540 rem);
e6445719
PS
541 return -EINVAL;
542 }
543
544 if (!is_mask) {
545 if (!match->key->tun_key.ipv4_dst) {
05da5898 546 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
e6445719
PS
547 return -EINVAL;
548 }
549
550 if (!ttl) {
05da5898 551 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
e6445719
PS
552 return -EINVAL;
553 }
554 }
555
556 return 0;
557}
558
f5796684
JG
559static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
560 const struct ovs_key_ipv4_tunnel *output,
561 const struct geneve_opt *tun_opts,
562 int swkey_tun_opts_len)
e6445719 563{
e6445719
PS
564 if (output->tun_flags & TUNNEL_KEY &&
565 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
566 return -EMSGSIZE;
567 if (output->ipv4_src &&
67fa0341 568 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
e6445719
PS
569 return -EMSGSIZE;
570 if (output->ipv4_dst &&
67fa0341 571 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
e6445719
PS
572 return -EMSGSIZE;
573 if (output->ipv4_tos &&
67fa0341 574 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
e6445719
PS
575 return -EMSGSIZE;
576 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
577 return -EMSGSIZE;
578 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
67fa0341 579 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
e6445719
PS
580 return -EMSGSIZE;
581 if ((output->tun_flags & TUNNEL_CSUM) &&
67fa0341
JG
582 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
583 return -EMSGSIZE;
8f0aad6f
WZ
584 if (output->tp_src &&
585 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
586 return -EMSGSIZE;
587 if (output->tp_dst &&
588 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
589 return -EMSGSIZE;
67fa0341
JG
590 if ((output->tun_flags & TUNNEL_OAM) &&
591 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
e6445719 592 return -EMSGSIZE;
f5796684
JG
593 if (tun_opts &&
594 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
595 swkey_tun_opts_len, tun_opts))
596 return -EMSGSIZE;
e6445719 597
e6445719
PS
598 return 0;
599}
600
f5796684
JG
601static int ipv4_tun_to_nlattr(struct sk_buff *skb,
602 const struct ovs_key_ipv4_tunnel *output,
603 const struct geneve_opt *tun_opts,
604 int swkey_tun_opts_len)
605{
606 struct nlattr *nla;
607 int err;
608
609 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
610 if (!nla)
611 return -EMSGSIZE;
612
613 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
614 if (err)
615 return err;
616
617 nla_nest_end(skb, nla);
618 return 0;
619}
620
8f0aad6f
WZ
621int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
622 const struct ovs_tunnel_info *egress_tun_info)
623{
624 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
625 egress_tun_info->options,
626 egress_tun_info->options_len);
627}
628
e6445719 629static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
05da5898
JR
630 const struct nlattr **a, bool is_mask,
631 bool log)
e6445719 632{
971427f3
AZ
633 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
634 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
635
636 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
637 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
638 }
639
640 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
641 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
642
643 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
644 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
645 }
646
e6445719
PS
647 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
648 SW_FLOW_KEY_PUT(match, phy.priority,
649 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
650 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
651 }
652
653 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
654 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
655
426cda5c 656 if (is_mask) {
e6445719 657 in_port = 0xffffffff; /* Always exact match in_port. */
426cda5c 658 } else if (in_port >= DP_MAX_PORTS) {
05da5898 659 OVS_NLERR(log, "Port %d exceeds max allowable %d",
426cda5c 660 in_port, DP_MAX_PORTS);
e6445719 661 return -EINVAL;
426cda5c 662 }
e6445719
PS
663
664 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
665 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
666 } else if (!is_mask) {
667 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
668 }
669
670 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
671 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
672
673 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
674 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
675 }
676 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
677 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
05da5898 678 is_mask, log))
e6445719
PS
679 return -EINVAL;
680 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
681 }
682 return 0;
683}
684
23dabf88 685static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
05da5898
JR
686 const struct nlattr **a, bool is_mask,
687 bool log)
e6445719
PS
688{
689 int err;
e6445719 690
05da5898 691 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
e6445719
PS
692 if (err)
693 return err;
694
695 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
696 const struct ovs_key_ethernet *eth_key;
697
698 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
699 SW_FLOW_KEY_MEMCPY(match, eth.src,
700 eth_key->eth_src, ETH_ALEN, is_mask);
701 SW_FLOW_KEY_MEMCPY(match, eth.dst,
702 eth_key->eth_dst, ETH_ALEN, is_mask);
703 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
704 }
705
706 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
707 __be16 tci;
708
709 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
710 if (!(tci & htons(VLAN_TAG_PRESENT))) {
711 if (is_mask)
05da5898 712 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
e6445719 713 else
05da5898 714 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
e6445719
PS
715
716 return -EINVAL;
717 }
718
719 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
720 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
a85311bf 721 }
e6445719
PS
722
723 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
724 __be16 eth_type;
725
726 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
727 if (is_mask) {
728 /* Always exact match EtherType. */
729 eth_type = htons(0xffff);
730 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
05da5898
JR
731 OVS_NLERR(log, "EtherType %x is less than min %x",
732 ntohs(eth_type), ETH_P_802_3_MIN);
e6445719
PS
733 return -EINVAL;
734 }
735
736 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
737 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
738 } else if (!is_mask) {
739 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
740 }
741
742 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
743 const struct ovs_key_ipv4 *ipv4_key;
744
745 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
746 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
747 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
748 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
749 return -EINVAL;
750 }
751 SW_FLOW_KEY_PUT(match, ip.proto,
752 ipv4_key->ipv4_proto, is_mask);
753 SW_FLOW_KEY_PUT(match, ip.tos,
754 ipv4_key->ipv4_tos, is_mask);
755 SW_FLOW_KEY_PUT(match, ip.ttl,
756 ipv4_key->ipv4_ttl, is_mask);
757 SW_FLOW_KEY_PUT(match, ip.frag,
758 ipv4_key->ipv4_frag, is_mask);
759 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
760 ipv4_key->ipv4_src, is_mask);
761 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
762 ipv4_key->ipv4_dst, is_mask);
763 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
764 }
765
766 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
767 const struct ovs_key_ipv6 *ipv6_key;
768
769 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
770 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
771 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
772 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
773 return -EINVAL;
774 }
775 SW_FLOW_KEY_PUT(match, ipv6.label,
776 ipv6_key->ipv6_label, is_mask);
777 SW_FLOW_KEY_PUT(match, ip.proto,
778 ipv6_key->ipv6_proto, is_mask);
779 SW_FLOW_KEY_PUT(match, ip.tos,
780 ipv6_key->ipv6_tclass, is_mask);
781 SW_FLOW_KEY_PUT(match, ip.ttl,
782 ipv6_key->ipv6_hlimit, is_mask);
783 SW_FLOW_KEY_PUT(match, ip.frag,
784 ipv6_key->ipv6_frag, is_mask);
785 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
786 ipv6_key->ipv6_src,
787 sizeof(match->key->ipv6.addr.src),
788 is_mask);
789 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
790 ipv6_key->ipv6_dst,
791 sizeof(match->key->ipv6.addr.dst),
792 is_mask);
793
794 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
795 }
796
797 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
798 const struct ovs_key_arp *arp_key;
799
800 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
801 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
05da5898 802 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
e6445719
PS
803 arp_key->arp_op);
804 return -EINVAL;
805 }
806
807 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
808 arp_key->arp_sip, is_mask);
809 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
810 arp_key->arp_tip, is_mask);
811 SW_FLOW_KEY_PUT(match, ip.proto,
812 ntohs(arp_key->arp_op), is_mask);
813 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
814 arp_key->arp_sha, ETH_ALEN, is_mask);
815 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
816 arp_key->arp_tha, ETH_ALEN, is_mask);
817
818 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
819 }
820
25cd9ba0
SH
821 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
822 const struct ovs_key_mpls *mpls_key;
823
824 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
825 SW_FLOW_KEY_PUT(match, mpls.top_lse,
826 mpls_key->mpls_lse, is_mask);
827
828 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
829 }
830
e6445719
PS
831 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
832 const struct ovs_key_tcp *tcp_key;
833
834 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
835 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
836 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
837 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
838 }
839
5eb26b15 840 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1b760fb9
JS
841 SW_FLOW_KEY_PUT(match, tp.flags,
842 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
843 is_mask);
5eb26b15
JR
844 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
845 }
846
e6445719
PS
847 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
848 const struct ovs_key_udp *udp_key;
849
850 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
851 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
852 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
853 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
854 }
855
856 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
857 const struct ovs_key_sctp *sctp_key;
858
859 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
860 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
861 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
862 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
863 }
864
865 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
866 const struct ovs_key_icmp *icmp_key;
867
868 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 869 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 870 htons(icmp_key->icmp_type), is_mask);
1139e241 871 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
872 htons(icmp_key->icmp_code), is_mask);
873 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
874 }
875
876 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
877 const struct ovs_key_icmpv6 *icmpv6_key;
878
879 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 880 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 881 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 882 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
883 htons(icmpv6_key->icmpv6_code), is_mask);
884 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
885 }
886
887 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
888 const struct ovs_key_nd *nd_key;
889
890 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
891 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
892 nd_key->nd_target,
893 sizeof(match->key->ipv6.nd.target),
894 is_mask);
895 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
896 nd_key->nd_sll, ETH_ALEN, is_mask);
897 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
898 nd_key->nd_tll, ETH_ALEN, is_mask);
899 attrs &= ~(1 << OVS_KEY_ATTR_ND);
900 }
901
426cda5c 902 if (attrs != 0) {
05da5898 903 OVS_NLERR(log, "Unknown key attributes %llx",
426cda5c 904 (unsigned long long)attrs);
e6445719 905 return -EINVAL;
426cda5c 906 }
e6445719
PS
907
908 return 0;
909}
910
f47de068 911static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
e6445719 912{
f47de068
PS
913 struct nlattr *nla;
914 int rem;
e6445719 915
f47de068
PS
916 /* The nlattr stream should already have been validated */
917 nla_for_each_nested(nla, attr, rem) {
918 /* We assume that ovs_key_lens[type] == -1 means that type is a
919 * nested attribute
920 */
921 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
922 nlattr_set(nla, val, false);
923 else
924 memset(nla_data(nla), val, nla_len(nla));
925 }
926}
927
928static void mask_set_nlattr(struct nlattr *attr, u8 val)
929{
930 nlattr_set(attr, val, true);
e6445719
PS
931}
932
933/**
934 * ovs_nla_get_match - parses Netlink attributes into a flow key and
935 * mask. In case the 'mask' is NULL, the flow is treated as exact match
936 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
937 * does not include any don't care bit.
938 * @match: receives the extracted flow match information.
939 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
940 * sequence. The fields should of the packet that triggered the creation
941 * of this flow.
942 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
943 * attribute specifies the mask field of the wildcarded flow.
05da5898
JR
944 * @log: Boolean to allow kernel error logging. Normally true, but when
945 * probing for feature compatibility this should be passed in as false to
946 * suppress unnecessary error logging.
e6445719
PS
947 */
948int ovs_nla_get_match(struct sw_flow_match *match,
a85311bf 949 const struct nlattr *nla_key,
05da5898
JR
950 const struct nlattr *nla_mask,
951 bool log)
e6445719
PS
952{
953 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
954 const struct nlattr *encap;
f47de068 955 struct nlattr *newmask = NULL;
e6445719
PS
956 u64 key_attrs = 0;
957 u64 mask_attrs = 0;
958 bool encap_valid = false;
959 int err;
960
05da5898 961 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
e6445719
PS
962 if (err)
963 return err;
964
965 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
966 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
967 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
968 __be16 tci;
969
970 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
971 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
05da5898 972 OVS_NLERR(log, "Invalid Vlan frame.");
e6445719
PS
973 return -EINVAL;
974 }
975
976 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
977 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
978 encap = a[OVS_KEY_ATTR_ENCAP];
979 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
980 encap_valid = true;
981
982 if (tci & htons(VLAN_TAG_PRESENT)) {
05da5898 983 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
e6445719
PS
984 if (err)
985 return err;
986 } else if (!tci) {
987 /* Corner case for truncated 802.1Q header. */
988 if (nla_len(encap)) {
05da5898 989 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
e6445719
PS
990 return -EINVAL;
991 }
992 } else {
05da5898 993 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
e6445719
PS
994 return -EINVAL;
995 }
996 }
997
05da5898 998 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
e6445719
PS
999 if (err)
1000 return err;
1001
a85311bf
PS
1002 if (match->mask) {
1003 if (!nla_mask) {
1004 /* Create an exact match mask. We need to set to 0xff
1005 * all the 'match->mask' fields that have been touched
1006 * in 'match->key'. We cannot simply memset
1007 * 'match->mask', because padding bytes and fields not
1008 * specified in 'match->key' should be left to 0.
1009 * Instead, we use a stream of netlink attributes,
1010 * copied from 'key' and set to 0xff.
1011 * ovs_key_from_nlattrs() will take care of filling
1012 * 'match->mask' appropriately.
1013 */
1014 newmask = kmemdup(nla_key,
1015 nla_total_size(nla_len(nla_key)),
1016 GFP_KERNEL);
1017 if (!newmask)
1018 return -ENOMEM;
f47de068 1019
a85311bf 1020 mask_set_nlattr(newmask, 0xff);
f47de068 1021
a85311bf
PS
1022 /* The userspace does not send tunnel attributes that
1023 * are 0, but we should not wildcard them nonetheless.
1024 */
1025 if (match->key->tun_key.ipv4_dst)
1026 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1027 0xff, true);
f47de068 1028
a85311bf
PS
1029 nla_mask = newmask;
1030 }
f47de068 1031
05da5898 1032 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
e6445719 1033 if (err)
f47de068 1034 goto free_newmask;
e6445719 1035
a85311bf
PS
1036 /* Always match on tci. */
1037 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1038
f47de068 1039 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
e6445719
PS
1040 __be16 eth_type = 0;
1041 __be16 tci = 0;
1042
1043 if (!encap_valid) {
05da5898 1044 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
f47de068
PS
1045 err = -EINVAL;
1046 goto free_newmask;
e6445719
PS
1047 }
1048
1049 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1050 if (a[OVS_KEY_ATTR_ETHERTYPE])
1051 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1052
1053 if (eth_type == htons(0xffff)) {
1054 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1055 encap = a[OVS_KEY_ATTR_ENCAP];
05da5898
JR
1056 err = parse_flow_mask_nlattrs(encap, a,
1057 &mask_attrs, log);
f47de068
PS
1058 if (err)
1059 goto free_newmask;
e6445719 1060 } else {
05da5898
JR
1061 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1062 ntohs(eth_type));
f47de068
PS
1063 err = -EINVAL;
1064 goto free_newmask;
e6445719
PS
1065 }
1066
1067 if (a[OVS_KEY_ATTR_VLAN])
1068 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1069
1070 if (!(tci & htons(VLAN_TAG_PRESENT))) {
05da5898
JR
1071 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1072 ntohs(tci));
f47de068
PS
1073 err = -EINVAL;
1074 goto free_newmask;
e6445719
PS
1075 }
1076 }
1077
05da5898 1078 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
e6445719 1079 if (err)
f47de068 1080 goto free_newmask;
e6445719
PS
1081 }
1082
05da5898 1083 if (!match_validate(match, key_attrs, mask_attrs, log))
f47de068 1084 err = -EINVAL;
e6445719 1085
f47de068
PS
1086free_newmask:
1087 kfree(newmask);
1088 return err;
e6445719
PS
1089}
1090
1091/**
1092 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
83c8df26 1093 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
e6445719
PS
1094 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1095 * sequence.
05da5898
JR
1096 * @log: Boolean to allow kernel error logging. Normally true, but when
1097 * probing for feature compatibility this should be passed in as false to
1098 * suppress unnecessary error logging.
e6445719
PS
1099 *
1100 * This parses a series of Netlink attributes that form a flow key, which must
1101 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1102 * get the metadata, that is, the parts of the flow key that cannot be
1103 * extracted from the packet itself.
1104 */
1105
83c8df26 1106int ovs_nla_get_flow_metadata(const struct nlattr *attr,
05da5898
JR
1107 struct sw_flow_key *key,
1108 bool log)
e6445719 1109{
e6445719 1110 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
83c8df26 1111 struct sw_flow_match match;
e6445719
PS
1112 u64 attrs = 0;
1113 int err;
e6445719 1114
05da5898 1115 err = parse_flow_nlattrs(attr, a, &attrs, log);
e6445719
PS
1116 if (err)
1117 return -EINVAL;
1118
1119 memset(&match, 0, sizeof(match));
83c8df26 1120 match.key = key;
e6445719 1121
83c8df26 1122 key->phy.in_port = DP_MAX_PORTS;
e6445719 1123
05da5898 1124 return metadata_from_nlattrs(&match, &attrs, a, false, log);
e6445719
PS
1125}
1126
1127int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1128 const struct sw_flow_key *output, struct sk_buff *skb)
1129{
1130 struct ovs_key_ethernet *eth_key;
1131 struct nlattr *nla, *encap;
1132 bool is_mask = (swkey != output);
1133
971427f3
AZ
1134 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1135 goto nla_put_failure;
1136
1137 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1138 goto nla_put_failure;
1139
e6445719
PS
1140 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1141 goto nla_put_failure;
1142
f5796684
JG
1143 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1144 const struct geneve_opt *opts = NULL;
1145
1146 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1147 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1148
1149 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1150 swkey->tun_opts_len))
1151 goto nla_put_failure;
1152 }
e6445719
PS
1153
1154 if (swkey->phy.in_port == DP_MAX_PORTS) {
1155 if (is_mask && (output->phy.in_port == 0xffff))
1156 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1157 goto nla_put_failure;
1158 } else {
1159 u16 upper_u16;
1160 upper_u16 = !is_mask ? 0 : 0xffff;
1161
1162 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1163 (upper_u16 << 16) | output->phy.in_port))
1164 goto nla_put_failure;
1165 }
1166
1167 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1168 goto nla_put_failure;
1169
1170 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1171 if (!nla)
1172 goto nla_put_failure;
1173
1174 eth_key = nla_data(nla);
8c63ff09
JP
1175 ether_addr_copy(eth_key->eth_src, output->eth.src);
1176 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
e6445719
PS
1177
1178 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1179 __be16 eth_type;
1180 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1181 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1182 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1183 goto nla_put_failure;
1184 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1185 if (!swkey->eth.tci)
1186 goto unencap;
1187 } else
1188 encap = NULL;
1189
1190 if (swkey->eth.type == htons(ETH_P_802_2)) {
1191 /*
1192 * Ethertype 802.2 is represented in the netlink with omitted
1193 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1194 * 0xffff in the mask attribute. Ethertype can also
1195 * be wildcarded.
1196 */
1197 if (is_mask && output->eth.type)
1198 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1199 output->eth.type))
1200 goto nla_put_failure;
1201 goto unencap;
1202 }
1203
1204 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1205 goto nla_put_failure;
1206
1207 if (swkey->eth.type == htons(ETH_P_IP)) {
1208 struct ovs_key_ipv4 *ipv4_key;
1209
1210 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1211 if (!nla)
1212 goto nla_put_failure;
1213 ipv4_key = nla_data(nla);
1214 ipv4_key->ipv4_src = output->ipv4.addr.src;
1215 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1216 ipv4_key->ipv4_proto = output->ip.proto;
1217 ipv4_key->ipv4_tos = output->ip.tos;
1218 ipv4_key->ipv4_ttl = output->ip.ttl;
1219 ipv4_key->ipv4_frag = output->ip.frag;
1220 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1221 struct ovs_key_ipv6 *ipv6_key;
1222
1223 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1224 if (!nla)
1225 goto nla_put_failure;
1226 ipv6_key = nla_data(nla);
1227 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1228 sizeof(ipv6_key->ipv6_src));
1229 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1230 sizeof(ipv6_key->ipv6_dst));
1231 ipv6_key->ipv6_label = output->ipv6.label;
1232 ipv6_key->ipv6_proto = output->ip.proto;
1233 ipv6_key->ipv6_tclass = output->ip.tos;
1234 ipv6_key->ipv6_hlimit = output->ip.ttl;
1235 ipv6_key->ipv6_frag = output->ip.frag;
1236 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1237 swkey->eth.type == htons(ETH_P_RARP)) {
1238 struct ovs_key_arp *arp_key;
1239
1240 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1241 if (!nla)
1242 goto nla_put_failure;
1243 arp_key = nla_data(nla);
1244 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1245 arp_key->arp_sip = output->ipv4.addr.src;
1246 arp_key->arp_tip = output->ipv4.addr.dst;
1247 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
1248 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1249 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
25cd9ba0
SH
1250 } else if (eth_p_mpls(swkey->eth.type)) {
1251 struct ovs_key_mpls *mpls_key;
1252
1253 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1254 if (!nla)
1255 goto nla_put_failure;
1256 mpls_key = nla_data(nla);
1257 mpls_key->mpls_lse = output->mpls.top_lse;
e6445719
PS
1258 }
1259
1260 if ((swkey->eth.type == htons(ETH_P_IP) ||
1261 swkey->eth.type == htons(ETH_P_IPV6)) &&
1262 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1263
1264 if (swkey->ip.proto == IPPROTO_TCP) {
1265 struct ovs_key_tcp *tcp_key;
1266
1267 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1268 if (!nla)
1269 goto nla_put_failure;
1270 tcp_key = nla_data(nla);
1139e241
JR
1271 tcp_key->tcp_src = output->tp.src;
1272 tcp_key->tcp_dst = output->tp.dst;
1273 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1274 output->tp.flags))
1275 goto nla_put_failure;
e6445719
PS
1276 } else if (swkey->ip.proto == IPPROTO_UDP) {
1277 struct ovs_key_udp *udp_key;
1278
1279 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1280 if (!nla)
1281 goto nla_put_failure;
1282 udp_key = nla_data(nla);
1139e241
JR
1283 udp_key->udp_src = output->tp.src;
1284 udp_key->udp_dst = output->tp.dst;
e6445719
PS
1285 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1286 struct ovs_key_sctp *sctp_key;
1287
1288 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1289 if (!nla)
1290 goto nla_put_failure;
1291 sctp_key = nla_data(nla);
1139e241
JR
1292 sctp_key->sctp_src = output->tp.src;
1293 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
1294 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1295 swkey->ip.proto == IPPROTO_ICMP) {
1296 struct ovs_key_icmp *icmp_key;
1297
1298 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1299 if (!nla)
1300 goto nla_put_failure;
1301 icmp_key = nla_data(nla);
1139e241
JR
1302 icmp_key->icmp_type = ntohs(output->tp.src);
1303 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
1304 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1305 swkey->ip.proto == IPPROTO_ICMPV6) {
1306 struct ovs_key_icmpv6 *icmpv6_key;
1307
1308 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1309 sizeof(*icmpv6_key));
1310 if (!nla)
1311 goto nla_put_failure;
1312 icmpv6_key = nla_data(nla);
1139e241
JR
1313 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1314 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
1315
1316 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1317 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1318 struct ovs_key_nd *nd_key;
1319
1320 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1321 if (!nla)
1322 goto nla_put_failure;
1323 nd_key = nla_data(nla);
1324 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1325 sizeof(nd_key->nd_target));
8c63ff09
JP
1326 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1327 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
1328 }
1329 }
1330 }
1331
1332unencap:
1333 if (encap)
1334 nla_nest_end(skb, encap);
1335
1336 return 0;
1337
1338nla_put_failure:
1339 return -EMSGSIZE;
1340}
1341
1342#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1343
05da5898 1344static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
e6445719
PS
1345{
1346 struct sw_flow_actions *sfa;
1347
426cda5c 1348 if (size > MAX_ACTIONS_BUFSIZE) {
05da5898 1349 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
e6445719 1350 return ERR_PTR(-EINVAL);
426cda5c 1351 }
e6445719
PS
1352
1353 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1354 if (!sfa)
1355 return ERR_PTR(-ENOMEM);
1356
1357 sfa->actions_len = 0;
1358 return sfa;
1359}
1360
e6445719
PS
1361/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1362 * The caller must hold rcu_read_lock for this to be sensible. */
1363void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1364{
11d6c461 1365 kfree_rcu(sf_acts, rcu);
e6445719
PS
1366}
1367
1368static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
05da5898 1369 int attr_len, bool log)
e6445719
PS
1370{
1371
1372 struct sw_flow_actions *acts;
1373 int new_acts_size;
1374 int req_size = NLA_ALIGN(attr_len);
1375 int next_offset = offsetof(struct sw_flow_actions, actions) +
1376 (*sfa)->actions_len;
1377
1378 if (req_size <= (ksize(*sfa) - next_offset))
1379 goto out;
1380
1381 new_acts_size = ksize(*sfa) * 2;
1382
1383 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1384 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1385 return ERR_PTR(-EMSGSIZE);
1386 new_acts_size = MAX_ACTIONS_BUFSIZE;
1387 }
1388
05da5898 1389 acts = nla_alloc_flow_actions(new_acts_size, log);
e6445719
PS
1390 if (IS_ERR(acts))
1391 return (void *)acts;
1392
1393 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1394 acts->actions_len = (*sfa)->actions_len;
1395 kfree(*sfa);
1396 *sfa = acts;
1397
1398out:
1399 (*sfa)->actions_len += req_size;
1400 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1401}
1402
f0b128c1 1403static struct nlattr *__add_action(struct sw_flow_actions **sfa,
05da5898 1404 int attrtype, void *data, int len, bool log)
e6445719
PS
1405{
1406 struct nlattr *a;
1407
05da5898 1408 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
e6445719 1409 if (IS_ERR(a))
f0b128c1 1410 return a;
e6445719
PS
1411
1412 a->nla_type = attrtype;
1413 a->nla_len = nla_attr_size(len);
1414
1415 if (data)
1416 memcpy(nla_data(a), data, len);
1417 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1418
f0b128c1
JG
1419 return a;
1420}
1421
1422static int add_action(struct sw_flow_actions **sfa, int attrtype,
05da5898 1423 void *data, int len, bool log)
f0b128c1
JG
1424{
1425 struct nlattr *a;
1426
05da5898 1427 a = __add_action(sfa, attrtype, data, len, log);
f0b128c1 1428
f35423c1 1429 return PTR_ERR_OR_ZERO(a);
e6445719
PS
1430}
1431
1432static inline int add_nested_action_start(struct sw_flow_actions **sfa,
05da5898 1433 int attrtype, bool log)
e6445719
PS
1434{
1435 int used = (*sfa)->actions_len;
1436 int err;
1437
05da5898 1438 err = add_action(sfa, attrtype, NULL, 0, log);
e6445719
PS
1439 if (err)
1440 return err;
1441
1442 return used;
1443}
1444
1445static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1446 int st_offset)
1447{
1448 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1449 st_offset);
1450
1451 a->nla_len = sfa->actions_len - st_offset;
1452}
1453
2fdb957d 1454static int __ovs_nla_copy_actions(const struct nlattr *attr,
25cd9ba0
SH
1455 const struct sw_flow_key *key,
1456 int depth, struct sw_flow_actions **sfa,
05da5898 1457 __be16 eth_type, __be16 vlan_tci, bool log);
25cd9ba0 1458
e6445719
PS
1459static int validate_and_copy_sample(const struct nlattr *attr,
1460 const struct sw_flow_key *key, int depth,
25cd9ba0 1461 struct sw_flow_actions **sfa,
05da5898 1462 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719
PS
1463{
1464 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1465 const struct nlattr *probability, *actions;
1466 const struct nlattr *a;
1467 int rem, start, err, st_acts;
1468
1469 memset(attrs, 0, sizeof(attrs));
1470 nla_for_each_nested(a, attr, rem) {
1471 int type = nla_type(a);
1472 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1473 return -EINVAL;
1474 attrs[type] = a;
1475 }
1476 if (rem)
1477 return -EINVAL;
1478
1479 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1480 if (!probability || nla_len(probability) != sizeof(u32))
1481 return -EINVAL;
1482
1483 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1484 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1485 return -EINVAL;
1486
1487 /* validation done, copy sample action. */
05da5898 1488 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
e6445719
PS
1489 if (start < 0)
1490 return start;
1491 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
05da5898 1492 nla_data(probability), sizeof(u32), log);
e6445719
PS
1493 if (err)
1494 return err;
05da5898 1495 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
e6445719
PS
1496 if (st_acts < 0)
1497 return st_acts;
1498
2fdb957d 1499 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
05da5898 1500 eth_type, vlan_tci, log);
e6445719
PS
1501 if (err)
1502 return err;
1503
1504 add_nested_action_end(*sfa, st_acts);
1505 add_nested_action_end(*sfa, start);
1506
1507 return 0;
1508}
1509
25cd9ba0
SH
1510static int validate_tp_port(const struct sw_flow_key *flow_key,
1511 __be16 eth_type)
e6445719 1512{
25cd9ba0 1513 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1139e241
JR
1514 (flow_key->tp.src || flow_key->tp.dst))
1515 return 0;
e6445719
PS
1516
1517 return -EINVAL;
1518}
1519
1520void ovs_match_init(struct sw_flow_match *match,
1521 struct sw_flow_key *key,
1522 struct sw_flow_mask *mask)
1523{
1524 memset(match, 0, sizeof(*match));
1525 match->key = key;
1526 match->mask = mask;
1527
1528 memset(key, 0, sizeof(*key));
1529
1530 if (mask) {
1531 memset(&mask->key, 0, sizeof(mask->key));
1532 mask->range.start = mask->range.end = 0;
1533 }
1534}
1535
1536static int validate_and_copy_set_tun(const struct nlattr *attr,
05da5898 1537 struct sw_flow_actions **sfa, bool log)
e6445719
PS
1538{
1539 struct sw_flow_match match;
1540 struct sw_flow_key key;
f0b128c1
JG
1541 struct ovs_tunnel_info *tun_info;
1542 struct nlattr *a;
e6445719
PS
1543 int err, start;
1544
1545 ovs_match_init(&match, &key, NULL);
05da5898 1546 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
e6445719
PS
1547 if (err)
1548 return err;
1549
f5796684
JG
1550 if (key.tun_opts_len) {
1551 struct geneve_opt *option = GENEVE_OPTS(&key,
1552 key.tun_opts_len);
1553 int opts_len = key.tun_opts_len;
1554 bool crit_opt = false;
1555
1556 while (opts_len > 0) {
1557 int len;
1558
1559 if (opts_len < sizeof(*option))
1560 return -EINVAL;
1561
1562 len = sizeof(*option) + option->length * 4;
1563 if (len > opts_len)
1564 return -EINVAL;
1565
1566 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1567
1568 option = (struct geneve_opt *)((u8 *)option + len);
1569 opts_len -= len;
1570 };
1571
1572 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1573 };
1574
05da5898 1575 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
e6445719
PS
1576 if (start < 0)
1577 return start;
1578
f0b128c1 1579 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
05da5898 1580 sizeof(*tun_info) + key.tun_opts_len, log);
f0b128c1
JG
1581 if (IS_ERR(a))
1582 return PTR_ERR(a);
1583
1584 tun_info = nla_data(a);
1585 tun_info->tunnel = key.tun_key;
f5796684
JG
1586 tun_info->options_len = key.tun_opts_len;
1587
1588 if (tun_info->options_len) {
1589 /* We need to store the options in the action itself since
1590 * everything else will go away after flow setup. We can append
1591 * it to tun_info and then point there.
1592 */
1593 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1594 key.tun_opts_len);
1595 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1596 } else {
1597 tun_info->options = NULL;
1598 }
f0b128c1 1599
e6445719
PS
1600 add_nested_action_end(*sfa, start);
1601
1602 return err;
1603}
1604
1605static int validate_set(const struct nlattr *a,
1606 const struct sw_flow_key *flow_key,
1607 struct sw_flow_actions **sfa,
05da5898 1608 bool *set_tun, __be16 eth_type, bool log)
e6445719
PS
1609{
1610 const struct nlattr *ovs_key = nla_data(a);
1611 int key_type = nla_type(ovs_key);
1612
1613 /* There can be only one key in a action */
1614 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1615 return -EINVAL;
1616
1617 if (key_type > OVS_KEY_ATTR_MAX ||
1618 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1619 ovs_key_lens[key_type] != -1))
1620 return -EINVAL;
1621
1622 switch (key_type) {
1623 const struct ovs_key_ipv4 *ipv4_key;
1624 const struct ovs_key_ipv6 *ipv6_key;
1625 int err;
1626
1627 case OVS_KEY_ATTR_PRIORITY:
1628 case OVS_KEY_ATTR_SKB_MARK:
1629 case OVS_KEY_ATTR_ETHERNET:
1630 break;
1631
1632 case OVS_KEY_ATTR_TUNNEL:
25cd9ba0
SH
1633 if (eth_p_mpls(eth_type))
1634 return -EINVAL;
1635
e6445719 1636 *set_tun = true;
05da5898 1637 err = validate_and_copy_set_tun(a, sfa, log);
e6445719
PS
1638 if (err)
1639 return err;
1640 break;
1641
1642 case OVS_KEY_ATTR_IPV4:
25cd9ba0 1643 if (eth_type != htons(ETH_P_IP))
e6445719
PS
1644 return -EINVAL;
1645
1646 if (!flow_key->ip.proto)
1647 return -EINVAL;
1648
1649 ipv4_key = nla_data(ovs_key);
1650 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1651 return -EINVAL;
1652
1653 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1654 return -EINVAL;
1655
1656 break;
1657
1658 case OVS_KEY_ATTR_IPV6:
25cd9ba0 1659 if (eth_type != htons(ETH_P_IPV6))
e6445719
PS
1660 return -EINVAL;
1661
1662 if (!flow_key->ip.proto)
1663 return -EINVAL;
1664
1665 ipv6_key = nla_data(ovs_key);
1666 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1667 return -EINVAL;
1668
1669 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1670 return -EINVAL;
1671
1672 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1673 return -EINVAL;
1674
1675 break;
1676
1677 case OVS_KEY_ATTR_TCP:
1678 if (flow_key->ip.proto != IPPROTO_TCP)
1679 return -EINVAL;
1680
25cd9ba0 1681 return validate_tp_port(flow_key, eth_type);
e6445719
PS
1682
1683 case OVS_KEY_ATTR_UDP:
1684 if (flow_key->ip.proto != IPPROTO_UDP)
1685 return -EINVAL;
1686
25cd9ba0
SH
1687 return validate_tp_port(flow_key, eth_type);
1688
1689 case OVS_KEY_ATTR_MPLS:
1690 if (!eth_p_mpls(eth_type))
1691 return -EINVAL;
1692 break;
e6445719
PS
1693
1694 case OVS_KEY_ATTR_SCTP:
1695 if (flow_key->ip.proto != IPPROTO_SCTP)
1696 return -EINVAL;
1697
25cd9ba0 1698 return validate_tp_port(flow_key, eth_type);
e6445719
PS
1699
1700 default:
1701 return -EINVAL;
1702 }
1703
1704 return 0;
1705}
1706
1707static int validate_userspace(const struct nlattr *attr)
1708{
1709 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1710 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1711 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
8f0aad6f 1712 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
e6445719
PS
1713 };
1714 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1715 int error;
1716
1717 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1718 attr, userspace_policy);
1719 if (error)
1720 return error;
1721
1722 if (!a[OVS_USERSPACE_ATTR_PID] ||
1723 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1724 return -EINVAL;
1725
1726 return 0;
1727}
1728
1729static int copy_action(const struct nlattr *from,
05da5898 1730 struct sw_flow_actions **sfa, bool log)
e6445719
PS
1731{
1732 int totlen = NLA_ALIGN(from->nla_len);
1733 struct nlattr *to;
1734
05da5898 1735 to = reserve_sfa_size(sfa, from->nla_len, log);
e6445719
PS
1736 if (IS_ERR(to))
1737 return PTR_ERR(to);
1738
1739 memcpy(to, from, totlen);
1740 return 0;
1741}
1742
2fdb957d 1743static int __ovs_nla_copy_actions(const struct nlattr *attr,
25cd9ba0
SH
1744 const struct sw_flow_key *key,
1745 int depth, struct sw_flow_actions **sfa,
05da5898 1746 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719
PS
1747{
1748 const struct nlattr *a;
25cd9ba0 1749 bool out_tnl_port = false;
e6445719
PS
1750 int rem, err;
1751
1752 if (depth >= SAMPLE_ACTION_DEPTH)
1753 return -EOVERFLOW;
1754
1755 nla_for_each_nested(a, attr, rem) {
1756 /* Expected argument lengths, (u32)-1 for variable length. */
1757 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1758 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 1759 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719 1760 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
25cd9ba0
SH
1761 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1762 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
e6445719
PS
1763 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1764 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1765 [OVS_ACTION_ATTR_SET] = (u32)-1,
971427f3
AZ
1766 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1767 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
e6445719
PS
1768 };
1769 const struct ovs_action_push_vlan *vlan;
1770 int type = nla_type(a);
1771 bool skip_copy;
1772
1773 if (type > OVS_ACTION_ATTR_MAX ||
1774 (action_lens[type] != nla_len(a) &&
1775 action_lens[type] != (u32)-1))
1776 return -EINVAL;
1777
1778 skip_copy = false;
1779 switch (type) {
1780 case OVS_ACTION_ATTR_UNSPEC:
1781 return -EINVAL;
1782
1783 case OVS_ACTION_ATTR_USERSPACE:
1784 err = validate_userspace(a);
1785 if (err)
1786 return err;
1787 break;
1788
1789 case OVS_ACTION_ATTR_OUTPUT:
1790 if (nla_get_u32(a) >= DP_MAX_PORTS)
1791 return -EINVAL;
25cd9ba0
SH
1792 out_tnl_port = false;
1793
e6445719
PS
1794 break;
1795
971427f3
AZ
1796 case OVS_ACTION_ATTR_HASH: {
1797 const struct ovs_action_hash *act_hash = nla_data(a);
1798
1799 switch (act_hash->hash_alg) {
1800 case OVS_HASH_ALG_L4:
1801 break;
1802 default:
1803 return -EINVAL;
1804 }
1805
1806 break;
1807 }
e6445719
PS
1808
1809 case OVS_ACTION_ATTR_POP_VLAN:
25cd9ba0 1810 vlan_tci = htons(0);
e6445719
PS
1811 break;
1812
1813 case OVS_ACTION_ATTR_PUSH_VLAN:
1814 vlan = nla_data(a);
1815 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1816 return -EINVAL;
1817 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1818 return -EINVAL;
25cd9ba0 1819 vlan_tci = vlan->vlan_tci;
e6445719
PS
1820 break;
1821
971427f3
AZ
1822 case OVS_ACTION_ATTR_RECIRC:
1823 break;
1824
25cd9ba0
SH
1825 case OVS_ACTION_ATTR_PUSH_MPLS: {
1826 const struct ovs_action_push_mpls *mpls = nla_data(a);
1827
1828 /* Networking stack do not allow simultaneous Tunnel
1829 * and MPLS GSO.
1830 */
1831 if (out_tnl_port)
1832 return -EINVAL;
1833
1834 if (!eth_p_mpls(mpls->mpls_ethertype))
1835 return -EINVAL;
1836 /* Prohibit push MPLS other than to a white list
1837 * for packets that have a known tag order.
1838 */
1839 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1840 (eth_type != htons(ETH_P_IP) &&
1841 eth_type != htons(ETH_P_IPV6) &&
1842 eth_type != htons(ETH_P_ARP) &&
1843 eth_type != htons(ETH_P_RARP) &&
1844 !eth_p_mpls(eth_type)))
1845 return -EINVAL;
1846 eth_type = mpls->mpls_ethertype;
1847 break;
1848 }
1849
1850 case OVS_ACTION_ATTR_POP_MPLS:
1851 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1852 !eth_p_mpls(eth_type))
1853 return -EINVAL;
1854
1855 /* Disallow subsequent L2.5+ set and mpls_pop actions
1856 * as there is no check here to ensure that the new
1857 * eth_type is valid and thus set actions could
1858 * write off the end of the packet or otherwise
1859 * corrupt it.
1860 *
1861 * Support for these actions is planned using packet
1862 * recirculation.
1863 */
1864 eth_type = htons(0);
1865 break;
1866
e6445719 1867 case OVS_ACTION_ATTR_SET:
25cd9ba0 1868 err = validate_set(a, key, sfa,
05da5898 1869 &out_tnl_port, eth_type, log);
e6445719
PS
1870 if (err)
1871 return err;
25cd9ba0
SH
1872
1873 skip_copy = out_tnl_port;
e6445719
PS
1874 break;
1875
1876 case OVS_ACTION_ATTR_SAMPLE:
25cd9ba0 1877 err = validate_and_copy_sample(a, key, depth, sfa,
05da5898 1878 eth_type, vlan_tci, log);
e6445719
PS
1879 if (err)
1880 return err;
1881 skip_copy = true;
1882 break;
1883
1884 default:
05da5898 1885 OVS_NLERR(log, "Unknown Action type %d", type);
e6445719
PS
1886 return -EINVAL;
1887 }
1888 if (!skip_copy) {
05da5898 1889 err = copy_action(a, sfa, log);
e6445719
PS
1890 if (err)
1891 return err;
1892 }
1893 }
1894
1895 if (rem > 0)
1896 return -EINVAL;
1897
1898 return 0;
1899}
1900
25cd9ba0
SH
1901int ovs_nla_copy_actions(const struct nlattr *attr,
1902 const struct sw_flow_key *key,
05da5898 1903 struct sw_flow_actions **sfa, bool log)
25cd9ba0 1904{
2fdb957d
PS
1905 int err;
1906
05da5898 1907 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2fdb957d
PS
1908 if (IS_ERR(*sfa))
1909 return PTR_ERR(*sfa);
1910
1911 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
05da5898 1912 key->eth.tci, log);
2fdb957d
PS
1913 if (err)
1914 kfree(*sfa);
1915
1916 return err;
25cd9ba0
SH
1917}
1918
e6445719
PS
1919static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1920{
1921 const struct nlattr *a;
1922 struct nlattr *start;
1923 int err = 0, rem;
1924
1925 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1926 if (!start)
1927 return -EMSGSIZE;
1928
1929 nla_for_each_nested(a, attr, rem) {
1930 int type = nla_type(a);
1931 struct nlattr *st_sample;
1932
1933 switch (type) {
1934 case OVS_SAMPLE_ATTR_PROBABILITY:
1935 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1936 sizeof(u32), nla_data(a)))
1937 return -EMSGSIZE;
1938 break;
1939 case OVS_SAMPLE_ATTR_ACTIONS:
1940 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1941 if (!st_sample)
1942 return -EMSGSIZE;
1943 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1944 if (err)
1945 return err;
1946 nla_nest_end(skb, st_sample);
1947 break;
1948 }
1949 }
1950
1951 nla_nest_end(skb, start);
1952 return err;
1953}
1954
1955static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1956{
1957 const struct nlattr *ovs_key = nla_data(a);
1958 int key_type = nla_type(ovs_key);
1959 struct nlattr *start;
1960 int err;
1961
1962 switch (key_type) {
f0b128c1
JG
1963 case OVS_KEY_ATTR_TUNNEL_INFO: {
1964 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1965
e6445719
PS
1966 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1967 if (!start)
1968 return -EMSGSIZE;
1969
f0b128c1 1970 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
f5796684
JG
1971 tun_info->options_len ?
1972 tun_info->options : NULL,
1973 tun_info->options_len);
e6445719
PS
1974 if (err)
1975 return err;
1976 nla_nest_end(skb, start);
1977 break;
f0b128c1 1978 }
e6445719
PS
1979 default:
1980 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1981 return -EMSGSIZE;
1982 break;
1983 }
1984
1985 return 0;
1986}
1987
1988int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1989{
1990 const struct nlattr *a;
1991 int rem, err;
1992
1993 nla_for_each_attr(a, attr, len, rem) {
1994 int type = nla_type(a);
1995
1996 switch (type) {
1997 case OVS_ACTION_ATTR_SET:
1998 err = set_action_to_attr(a, skb);
1999 if (err)
2000 return err;
2001 break;
2002
2003 case OVS_ACTION_ATTR_SAMPLE:
2004 err = sample_action_to_attr(a, skb);
2005 if (err)
2006 return err;
2007 break;
2008 default:
2009 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2010 return -EMSGSIZE;
2011 break;
2012 }
2013 }
2014
2015 return 0;
2016}
This page took 0.163687 seconds and 5 git commands to generate.