]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/flow_netlink.c
datapath: Add conntrack action
[mirror_ovs.git] / datapath / flow_netlink.c
1 /*
2 * Copyright (c) 2007-2015 Nicira, Inc.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/geneve.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
47 #include <net/mpls.h>
48 #include <net/vxlan.h>
49
50 #include "datapath.h"
51 #include "conntrack.h"
52 #include "flow.h"
53 #include "flow_netlink.h"
54 #include "gso.h"
55
56 struct ovs_len_tbl {
57 int len;
58 const struct ovs_len_tbl *next;
59 };
60
61 #define OVS_ATTR_NESTED -1
62 #define OVS_ATTR_VARIABLE -2
63
64 static void update_range(struct sw_flow_match *match,
65 size_t offset, size_t size, bool is_mask)
66 {
67 struct sw_flow_key_range *range;
68 size_t start = rounddown(offset, sizeof(long));
69 size_t end = roundup(offset + size, sizeof(long));
70
71 if (!is_mask)
72 range = &match->range;
73 else
74 range = &match->mask->range;
75
76 if (range->start == range->end) {
77 range->start = start;
78 range->end = end;
79 return;
80 }
81
82 if (range->start > start)
83 range->start = start;
84
85 if (range->end < end)
86 range->end = end;
87 }
88
89 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
90 do { \
91 update_range(match, offsetof(struct sw_flow_key, field), \
92 sizeof((match)->key->field), is_mask); \
93 if (is_mask) \
94 (match)->mask->key.field = value; \
95 else \
96 (match)->key->field = value; \
97 } while (0)
98
99 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
100 do { \
101 update_range(match, offset, len, is_mask); \
102 if (is_mask) \
103 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
104 else \
105 memcpy((u8 *)(match)->key + offset, value_p, len); \
106 } while (0)
107
108 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
109 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
110 value_p, len, is_mask)
111
112 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
113 do { \
114 update_range(match, offsetof(struct sw_flow_key, field), \
115 sizeof((match)->key->field), is_mask); \
116 if (is_mask) \
117 memset((u8 *)&(match)->mask->key.field, value, \
118 sizeof((match)->mask->key.field)); \
119 else \
120 memset((u8 *)&(match)->key->field, value, \
121 sizeof((match)->key->field)); \
122 } while (0)
123
124 static bool match_validate(const struct sw_flow_match *match,
125 u64 key_attrs, u64 mask_attrs, bool log)
126 {
127 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
128 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
129
130 /* The following mask attributes allowed only if they
131 * pass the validation tests.
132 */
133 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
134 | (1ULL << OVS_KEY_ATTR_IPV6)
135 | (1ULL << OVS_KEY_ATTR_TCP)
136 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
137 | (1ULL << OVS_KEY_ATTR_UDP)
138 | (1ULL << OVS_KEY_ATTR_SCTP)
139 | (1ULL << OVS_KEY_ATTR_ICMP)
140 | (1ULL << OVS_KEY_ATTR_ICMPV6)
141 | (1ULL << OVS_KEY_ATTR_ARP)
142 | (1ULL << OVS_KEY_ATTR_ND)
143 | (1ULL << OVS_KEY_ATTR_MPLS));
144
145 /* Always allowed mask fields. */
146 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
147 | (1ULL << OVS_KEY_ATTR_IN_PORT)
148 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
149
150 /* Check key attributes. */
151 if (match->key->eth.type == htons(ETH_P_ARP)
152 || match->key->eth.type == htons(ETH_P_RARP)) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
156 }
157
158 if (eth_p_mpls(match->key->eth.type)) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
160 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
161 mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
162 }
163
164 if (match->key->eth.type == htons(ETH_P_IP)) {
165 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
166 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
167 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
168
169 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
170 if (match->key->ip.proto == IPPROTO_UDP) {
171 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
172 if (match->mask && (match->mask->key.ip.proto == 0xff))
173 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
174 }
175
176 if (match->key->ip.proto == IPPROTO_SCTP) {
177 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
178 if (match->mask && (match->mask->key.ip.proto == 0xff))
179 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
180 }
181
182 if (match->key->ip.proto == IPPROTO_TCP) {
183 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
184 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
185 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
186 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
187 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
188 }
189 }
190
191 if (match->key->ip.proto == IPPROTO_ICMP) {
192 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
193 if (match->mask && (match->mask->key.ip.proto == 0xff))
194 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
195 }
196 }
197 }
198
199 if (match->key->eth.type == htons(ETH_P_IPV6)) {
200 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
201 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
202 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
203
204 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
205 if (match->key->ip.proto == IPPROTO_UDP) {
206 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
207 if (match->mask && (match->mask->key.ip.proto == 0xff))
208 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
209 }
210
211 if (match->key->ip.proto == IPPROTO_SCTP) {
212 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
213 if (match->mask && (match->mask->key.ip.proto == 0xff))
214 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
215 }
216
217 if (match->key->ip.proto == IPPROTO_TCP) {
218 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
219 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
220 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
221 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
222 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
223 }
224 }
225
226 if (match->key->ip.proto == IPPROTO_ICMPV6) {
227 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
228 if (match->mask && (match->mask->key.ip.proto == 0xff))
229 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
230
231 if (match->key->tp.src ==
232 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
233 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
234 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
235 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
236 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
237 }
238 }
239 }
240 }
241
242 if ((key_attrs & key_expected) != key_expected) {
243 /* Key attributes check failed. */
244 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
245 (unsigned long long)key_attrs,
246 (unsigned long long)key_expected);
247 return false;
248 }
249
250 if ((mask_attrs & mask_allowed) != mask_attrs) {
251 /* Mask attributes check failed. */
252 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
253 (unsigned long long)mask_attrs,
254 (unsigned long long)mask_allowed);
255 return false;
256 }
257
258 return true;
259 }
260
261 size_t ovs_tun_key_attr_size(void)
262 {
263 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
264 * updating this function.
265 */
266 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
267 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
268 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
269 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
270 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
271 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
272 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
273 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
274 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
275 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
276 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
277 */
278 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
279 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
280 }
281
282 size_t ovs_key_attr_size(void)
283 {
284 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
285 * updating this function.
286 */
287 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
288
289 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
290 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
291 + ovs_tun_key_attr_size()
292 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
293 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
294 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
295 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
296 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
297 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
298 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
300 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
301 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
302 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
303 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
304 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
305 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
306 }
307
308 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
309 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
310 };
311
312 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
313 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
314 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
315 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
316 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
317 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
318 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
319 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
320 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
321 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
322 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
323 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
324 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
325 .next = ovs_vxlan_ext_key_lens },
326 };
327
328 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
329 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
330 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
331 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
332 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
333 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
334 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
335 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
336 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
337 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
338 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
339 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
340 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
341 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
342 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
343 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
344 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
345 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
346 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
347 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
348 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
349 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
350 .next = ovs_tunnel_key_lens, },
351 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
352 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
353 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
354 };
355
356 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
357 {
358 return expected_len == attr_len ||
359 expected_len == OVS_ATTR_NESTED ||
360 expected_len == OVS_ATTR_VARIABLE;
361 }
362
363 static bool is_all_zero(const u8 *fp, size_t size)
364 {
365 int i;
366
367 if (!fp)
368 return false;
369
370 for (i = 0; i < size; i++)
371 if (fp[i])
372 return false;
373
374 return true;
375 }
376
377 static int __parse_flow_nlattrs(const struct nlattr *attr,
378 const struct nlattr *a[],
379 u64 *attrsp, bool log, bool nz)
380 {
381 const struct nlattr *nla;
382 u64 attrs;
383 int rem;
384
385 attrs = *attrsp;
386 nla_for_each_nested(nla, attr, rem) {
387 u16 type = nla_type(nla);
388 int expected_len;
389
390 if (type > OVS_KEY_ATTR_MAX) {
391 OVS_NLERR(log, "Key type %d is out of range max %d",
392 type, OVS_KEY_ATTR_MAX);
393 return -EINVAL;
394 }
395
396 if (attrs & (1ULL << type)) {
397 OVS_NLERR(log, "Duplicate key (type %d).", type);
398 return -EINVAL;
399 }
400
401 expected_len = ovs_key_lens[type].len;
402 if (!check_attr_len(nla_len(nla), expected_len)) {
403 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
404 type, nla_len(nla), expected_len);
405 return -EINVAL;
406 }
407
408 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
409 attrs |= 1ULL << type;
410 a[type] = nla;
411 }
412 }
413 if (rem) {
414 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
415 return -EINVAL;
416 }
417
418 *attrsp = attrs;
419 return 0;
420 }
421
422 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
423 const struct nlattr *a[], u64 *attrsp,
424 bool log)
425 {
426 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
427 }
428
429 static int parse_flow_nlattrs(const struct nlattr *attr,
430 const struct nlattr *a[], u64 *attrsp,
431 bool log)
432 {
433 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
434 }
435
436 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
437 struct sw_flow_match *match, bool is_mask,
438 bool log)
439 {
440 unsigned long opt_key_offset;
441
442 if (nla_len(a) > sizeof(match->key->tun_opts)) {
443 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
444 nla_len(a), sizeof(match->key->tun_opts));
445 return -EINVAL;
446 }
447
448 if (nla_len(a) % 4 != 0) {
449 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
450 nla_len(a));
451 return -EINVAL;
452 }
453
454 /* We need to record the length of the options passed
455 * down, otherwise packets with the same format but
456 * additional options will be silently matched.
457 */
458 if (!is_mask) {
459 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
460 false);
461 } else {
462 /* This is somewhat unusual because it looks at
463 * both the key and mask while parsing the
464 * attributes (and by extension assumes the key
465 * is parsed first). Normally, we would verify
466 * that each is the correct length and that the
467 * attributes line up in the validate function.
468 * However, that is difficult because this is
469 * variable length and we won't have the
470 * information later.
471 */
472 if (match->key->tun_opts_len != nla_len(a)) {
473 OVS_NLERR(log, "Geneve option len %d != mask len %d",
474 match->key->tun_opts_len, nla_len(a));
475 return -EINVAL;
476 }
477
478 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
479 }
480
481 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
482 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
483 nla_len(a), is_mask);
484 return 0;
485 }
486
487 static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
488 struct sw_flow_match *match, bool is_mask,
489 bool log)
490 {
491 struct nlattr *a;
492 int rem;
493 unsigned long opt_key_offset;
494 struct vxlan_metadata opts;
495
496 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
497
498 memset(&opts, 0, sizeof(opts));
499 nla_for_each_nested(a, attr, rem) {
500 int type = nla_type(a);
501
502 if (type > OVS_VXLAN_EXT_MAX) {
503 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
504 type, OVS_VXLAN_EXT_MAX);
505 return -EINVAL;
506 }
507
508 if (!check_attr_len(nla_len(a),
509 ovs_vxlan_ext_key_lens[type].len)) {
510 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
511 type, nla_len(a),
512 ovs_vxlan_ext_key_lens[type].len);
513 return -EINVAL;
514 }
515
516 switch (type) {
517 case OVS_VXLAN_EXT_GBP:
518 opts.gbp = nla_get_u32(a);
519 break;
520 default:
521 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
522 type);
523 return -EINVAL;
524 }
525 }
526 if (rem) {
527 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
528 rem);
529 return -EINVAL;
530 }
531
532 if (!is_mask)
533 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
534 else
535 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
536
537 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
538 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
539 is_mask);
540 return 0;
541 }
542
543 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
544 struct sw_flow_match *match, bool is_mask,
545 bool log)
546 {
547 struct nlattr *a;
548 int rem;
549 bool ttl = false;
550 __be16 tun_flags = 0;
551 int opts_type = 0;
552
553 nla_for_each_nested(a, attr, rem) {
554 int type = nla_type(a);
555 int err;
556
557 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
558 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
559 type, OVS_TUNNEL_KEY_ATTR_MAX);
560 return -EINVAL;
561 }
562
563 if (!check_attr_len(nla_len(a),
564 ovs_tunnel_key_lens[type].len)) {
565 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
566 type, nla_len(a), ovs_tunnel_key_lens[type].len);
567 return -EINVAL;
568 }
569
570 switch (type) {
571 case OVS_TUNNEL_KEY_ATTR_ID:
572 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
573 nla_get_be64(a), is_mask);
574 tun_flags |= TUNNEL_KEY;
575 break;
576 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
577 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
578 nla_get_in_addr(a), is_mask);
579 break;
580 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
581 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
582 nla_get_in_addr(a), is_mask);
583 break;
584 case OVS_TUNNEL_KEY_ATTR_TOS:
585 SW_FLOW_KEY_PUT(match, tun_key.tos,
586 nla_get_u8(a), is_mask);
587 break;
588 case OVS_TUNNEL_KEY_ATTR_TTL:
589 SW_FLOW_KEY_PUT(match, tun_key.ttl,
590 nla_get_u8(a), is_mask);
591 ttl = true;
592 break;
593 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
594 tun_flags |= TUNNEL_DONT_FRAGMENT;
595 break;
596 case OVS_TUNNEL_KEY_ATTR_CSUM:
597 tun_flags |= TUNNEL_CSUM;
598 break;
599 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
600 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
601 nla_get_be16(a), is_mask);
602 break;
603 case OVS_TUNNEL_KEY_ATTR_TP_DST:
604 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
605 nla_get_be16(a), is_mask);
606 break;
607 case OVS_TUNNEL_KEY_ATTR_OAM:
608 tun_flags |= TUNNEL_OAM;
609 break;
610 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
611 if (opts_type) {
612 OVS_NLERR(log, "Multiple metadata blocks provided");
613 return -EINVAL;
614 }
615
616 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
617 if (err)
618 return err;
619
620 tun_flags |= TUNNEL_GENEVE_OPT;
621 opts_type = type;
622 break;
623 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
624 if (opts_type) {
625 OVS_NLERR(log, "Multiple metadata blocks provided");
626 return -EINVAL;
627 }
628
629 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
630 if (err)
631 return err;
632
633 tun_flags |= TUNNEL_VXLAN_OPT;
634 opts_type = type;
635 break;
636 default:
637 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
638 type);
639 return -EINVAL;
640 }
641 }
642
643 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
644
645 if (rem > 0) {
646 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
647 rem);
648 return -EINVAL;
649 }
650
651 if (!is_mask) {
652 if (!match->key->tun_key.u.ipv4.dst) {
653 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
654 return -EINVAL;
655 }
656
657 if (!ttl) {
658 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
659 return -EINVAL;
660 }
661 }
662
663 return opts_type;
664 }
665
666 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
667 const void *tun_opts, int swkey_tun_opts_len)
668 {
669 const struct vxlan_metadata *opts = tun_opts;
670 struct nlattr *nla;
671
672 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
673 if (!nla)
674 return -EMSGSIZE;
675
676 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
677 return -EMSGSIZE;
678
679 nla_nest_end(skb, nla);
680 return 0;
681 }
682
683 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
684 const struct ip_tunnel_key *output,
685 const void *tun_opts, int swkey_tun_opts_len)
686 {
687 if (output->tun_flags & TUNNEL_KEY &&
688 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
689 return -EMSGSIZE;
690 if (output->u.ipv4.src &&
691 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
692 output->u.ipv4.src))
693 return -EMSGSIZE;
694 if (output->u.ipv4.dst &&
695 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
696 output->u.ipv4.dst))
697 return -EMSGSIZE;
698 if (output->tos &&
699 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
700 return -EMSGSIZE;
701 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
702 return -EMSGSIZE;
703 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
704 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
705 return -EMSGSIZE;
706 if ((output->tun_flags & TUNNEL_CSUM) &&
707 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
708 return -EMSGSIZE;
709 if (output->tp_src &&
710 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
711 return -EMSGSIZE;
712 if (output->tp_dst &&
713 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
714 return -EMSGSIZE;
715 if ((output->tun_flags & TUNNEL_OAM) &&
716 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
717 return -EMSGSIZE;
718 if (tun_opts) {
719 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
720 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
721 swkey_tun_opts_len, tun_opts))
722 return -EMSGSIZE;
723 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
724 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
725 return -EMSGSIZE;
726 }
727
728 return 0;
729 }
730
731 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
732 const struct ip_tunnel_key *output,
733 const void *tun_opts, int swkey_tun_opts_len)
734 {
735 struct nlattr *nla;
736 int err;
737
738 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
739 if (!nla)
740 return -EMSGSIZE;
741
742 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
743 if (err)
744 return err;
745
746 nla_nest_end(skb, nla);
747 return 0;
748 }
749
750 int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
751 const struct ip_tunnel_info *egress_tun_info,
752 const void *egress_tun_opts)
753 {
754 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
755 egress_tun_opts,
756 egress_tun_info->options_len);
757 }
758
759 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
760 const struct nlattr **a, bool is_mask,
761 bool log)
762 {
763 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
764 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
765
766 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
767 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
768 }
769
770 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
771 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
772
773 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
774 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
775 }
776
777 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
778 SW_FLOW_KEY_PUT(match, phy.priority,
779 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
780 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
781 }
782
783 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
784 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
785
786 if (is_mask) {
787 in_port = 0xffffffff; /* Always exact match in_port. */
788 } else if (in_port >= DP_MAX_PORTS) {
789 OVS_NLERR(log, "Port %d exceeds max allowable %d",
790 in_port, DP_MAX_PORTS);
791 return -EINVAL;
792 }
793
794 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
795 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
796 } else if (!is_mask) {
797 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
798 }
799
800 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
801 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
802
803 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
804 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
805 }
806 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
807 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
808 is_mask, log) < 0)
809 return -EINVAL;
810 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
811 }
812
813 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
814 ovs_ct_verify(OVS_KEY_ATTR_CT_STATE)) {
815 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
816
817 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
818 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
819 }
820 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
821 ovs_ct_verify(OVS_KEY_ATTR_CT_ZONE)) {
822 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
823
824 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
825 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
826 }
827 return 0;
828 }
829
830 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
831 const struct nlattr **a, bool is_mask,
832 bool log)
833 {
834 int err;
835
836 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
837 if (err)
838 return err;
839
840 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
841 const struct ovs_key_ethernet *eth_key;
842
843 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
844 SW_FLOW_KEY_MEMCPY(match, eth.src,
845 eth_key->eth_src, ETH_ALEN, is_mask);
846 SW_FLOW_KEY_MEMCPY(match, eth.dst,
847 eth_key->eth_dst, ETH_ALEN, is_mask);
848 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
849 }
850
851 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
852 __be16 tci;
853
854 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
855 if (!(tci & htons(VLAN_TAG_PRESENT))) {
856 if (is_mask)
857 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
858 else
859 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
860
861 return -EINVAL;
862 }
863
864 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
865 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
866 }
867
868 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
869 __be16 eth_type;
870
871 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
872 if (is_mask) {
873 /* Always exact match EtherType. */
874 eth_type = htons(0xffff);
875 } else if (!eth_proto_is_802_3(eth_type)) {
876 OVS_NLERR(log, "EtherType %x is less than min %x",
877 ntohs(eth_type), ETH_P_802_3_MIN);
878 return -EINVAL;
879 }
880
881 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
882 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
883 } else if (!is_mask) {
884 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
885 }
886
887 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
888 const struct ovs_key_ipv4 *ipv4_key;
889
890 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
891 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
892 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
893 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
894 return -EINVAL;
895 }
896 SW_FLOW_KEY_PUT(match, ip.proto,
897 ipv4_key->ipv4_proto, is_mask);
898 SW_FLOW_KEY_PUT(match, ip.tos,
899 ipv4_key->ipv4_tos, is_mask);
900 SW_FLOW_KEY_PUT(match, ip.ttl,
901 ipv4_key->ipv4_ttl, is_mask);
902 SW_FLOW_KEY_PUT(match, ip.frag,
903 ipv4_key->ipv4_frag, is_mask);
904 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
905 ipv4_key->ipv4_src, is_mask);
906 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
907 ipv4_key->ipv4_dst, is_mask);
908 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
909 }
910
911 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
912 const struct ovs_key_ipv6 *ipv6_key;
913
914 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
915 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
916 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
917 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
918 return -EINVAL;
919 }
920
921 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
922 OVS_NLERR(log,
923 "Invalid IPv6 flow label value (value=%x, max=%x).",
924 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
925 return -EINVAL;
926 }
927
928 SW_FLOW_KEY_PUT(match, ipv6.label,
929 ipv6_key->ipv6_label, is_mask);
930 SW_FLOW_KEY_PUT(match, ip.proto,
931 ipv6_key->ipv6_proto, is_mask);
932 SW_FLOW_KEY_PUT(match, ip.tos,
933 ipv6_key->ipv6_tclass, is_mask);
934 SW_FLOW_KEY_PUT(match, ip.ttl,
935 ipv6_key->ipv6_hlimit, is_mask);
936 SW_FLOW_KEY_PUT(match, ip.frag,
937 ipv6_key->ipv6_frag, is_mask);
938 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
939 ipv6_key->ipv6_src,
940 sizeof(match->key->ipv6.addr.src),
941 is_mask);
942 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
943 ipv6_key->ipv6_dst,
944 sizeof(match->key->ipv6.addr.dst),
945 is_mask);
946
947 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
948 }
949
950 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
951 const struct ovs_key_arp *arp_key;
952
953 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
954 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
955 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
956 arp_key->arp_op);
957 return -EINVAL;
958 }
959
960 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
961 arp_key->arp_sip, is_mask);
962 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
963 arp_key->arp_tip, is_mask);
964 SW_FLOW_KEY_PUT(match, ip.proto,
965 ntohs(arp_key->arp_op), is_mask);
966 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
967 arp_key->arp_sha, ETH_ALEN, is_mask);
968 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
969 arp_key->arp_tha, ETH_ALEN, is_mask);
970
971 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
972 }
973
974 if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
975 const struct ovs_key_mpls *mpls_key;
976
977 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
978 SW_FLOW_KEY_PUT(match, mpls.top_lse,
979 mpls_key->mpls_lse, is_mask);
980
981 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
982 }
983
984 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
985 const struct ovs_key_tcp *tcp_key;
986
987 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
988 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
989 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
990 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
991 }
992
993 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
994 SW_FLOW_KEY_PUT(match, tp.flags,
995 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
996 is_mask);
997 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
998 }
999
1000 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1001 const struct ovs_key_udp *udp_key;
1002
1003 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1004 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1005 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
1006 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1007 }
1008
1009 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1010 const struct ovs_key_sctp *sctp_key;
1011
1012 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1013 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1014 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
1015 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1016 }
1017
1018 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1019 const struct ovs_key_icmp *icmp_key;
1020
1021 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1022 SW_FLOW_KEY_PUT(match, tp.src,
1023 htons(icmp_key->icmp_type), is_mask);
1024 SW_FLOW_KEY_PUT(match, tp.dst,
1025 htons(icmp_key->icmp_code), is_mask);
1026 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1027 }
1028
1029 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1030 const struct ovs_key_icmpv6 *icmpv6_key;
1031
1032 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1033 SW_FLOW_KEY_PUT(match, tp.src,
1034 htons(icmpv6_key->icmpv6_type), is_mask);
1035 SW_FLOW_KEY_PUT(match, tp.dst,
1036 htons(icmpv6_key->icmpv6_code), is_mask);
1037 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1038 }
1039
1040 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1041 const struct ovs_key_nd *nd_key;
1042
1043 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1044 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1045 nd_key->nd_target,
1046 sizeof(match->key->ipv6.nd.target),
1047 is_mask);
1048 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1049 nd_key->nd_sll, ETH_ALEN, is_mask);
1050 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1051 nd_key->nd_tll, ETH_ALEN, is_mask);
1052 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1053 }
1054
1055 if (attrs != 0) {
1056 OVS_NLERR(log, "Unknown key attributes %llx",
1057 (unsigned long long)attrs);
1058 return -EINVAL;
1059 }
1060
1061 return 0;
1062 }
1063
1064 static void nlattr_set(struct nlattr *attr, u8 val,
1065 const struct ovs_len_tbl *tbl)
1066 {
1067 struct nlattr *nla;
1068 int rem;
1069
1070 /* The nlattr stream should already have been validated */
1071 nla_for_each_nested(nla, attr, rem) {
1072 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1073 if (tbl[nla_type(nla)].next)
1074 tbl = tbl[nla_type(nla)].next;
1075 nlattr_set(nla, val, tbl);
1076 } else {
1077 memset(nla_data(nla), val, nla_len(nla));
1078 }
1079 }
1080 }
1081
1082 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1083 {
1084 nlattr_set(attr, val, ovs_key_lens);
1085 }
1086
1087 /**
1088 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1089 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1090 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1091 * does not include any don't care bit.
1092 * @match: receives the extracted flow match information.
1093 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1094 * sequence. The fields should of the packet that triggered the creation
1095 * of this flow.
1096 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1097 * attribute specifies the mask field of the wildcarded flow.
1098 * @log: Boolean to allow kernel error logging. Normally true, but when
1099 * probing for feature compatibility this should be passed in as false to
1100 * suppress unnecessary error logging.
1101 */
1102 int ovs_nla_get_match(struct sw_flow_match *match,
1103 const struct nlattr *nla_key,
1104 const struct nlattr *nla_mask,
1105 bool log)
1106 {
1107 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1108 const struct nlattr *encap;
1109 struct nlattr *newmask = NULL;
1110 u64 key_attrs = 0;
1111 u64 mask_attrs = 0;
1112 bool encap_valid = false;
1113 int err;
1114
1115 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1116 if (err)
1117 return err;
1118
1119 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1120 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
1121 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1122 __be16 tci;
1123
1124 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
1125 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
1126 OVS_NLERR(log, "Invalid Vlan frame.");
1127 return -EINVAL;
1128 }
1129
1130 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1131 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1132 encap = a[OVS_KEY_ATTR_ENCAP];
1133 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1134 encap_valid = true;
1135
1136 if (tci & htons(VLAN_TAG_PRESENT)) {
1137 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
1138 if (err)
1139 return err;
1140 } else if (!tci) {
1141 /* Corner case for truncated 802.1Q header. */
1142 if (nla_len(encap)) {
1143 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
1144 return -EINVAL;
1145 }
1146 } else {
1147 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
1148 return -EINVAL;
1149 }
1150 }
1151
1152 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
1153 if (err)
1154 return err;
1155
1156 if (match->mask) {
1157 if (!nla_mask) {
1158 /* Create an exact match mask. We need to set to 0xff
1159 * all the 'match->mask' fields that have been touched
1160 * in 'match->key'. We cannot simply memset
1161 * 'match->mask', because padding bytes and fields not
1162 * specified in 'match->key' should be left to 0.
1163 * Instead, we use a stream of netlink attributes,
1164 * copied from 'key' and set to 0xff.
1165 * ovs_key_from_nlattrs() will take care of filling
1166 * 'match->mask' appropriately.
1167 */
1168 newmask = kmemdup(nla_key,
1169 nla_total_size(nla_len(nla_key)),
1170 GFP_KERNEL);
1171 if (!newmask)
1172 return -ENOMEM;
1173
1174 mask_set_nlattr(newmask, 0xff);
1175
1176 /* The userspace does not send tunnel attributes that
1177 * are 0, but we should not wildcard them nonetheless.
1178 */
1179 if (match->key->tun_key.u.ipv4.dst)
1180 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1181 0xff, true);
1182
1183 nla_mask = newmask;
1184 }
1185
1186 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1187 if (err)
1188 goto free_newmask;
1189
1190 /* Always match on tci. */
1191 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1192
1193 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1194 __be16 eth_type = 0;
1195 __be16 tci = 0;
1196
1197 if (!encap_valid) {
1198 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
1199 err = -EINVAL;
1200 goto free_newmask;
1201 }
1202
1203 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1204 if (a[OVS_KEY_ATTR_ETHERTYPE])
1205 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1206
1207 if (eth_type == htons(0xffff)) {
1208 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1209 encap = a[OVS_KEY_ATTR_ENCAP];
1210 err = parse_flow_mask_nlattrs(encap, a,
1211 &mask_attrs, log);
1212 if (err)
1213 goto free_newmask;
1214 } else {
1215 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1216 ntohs(eth_type));
1217 err = -EINVAL;
1218 goto free_newmask;
1219 }
1220
1221 if (a[OVS_KEY_ATTR_VLAN])
1222 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1223
1224 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1225 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1226 ntohs(tci));
1227 err = -EINVAL;
1228 goto free_newmask;
1229 }
1230 }
1231
1232 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
1233 if (err)
1234 goto free_newmask;
1235 }
1236
1237 if (!match_validate(match, key_attrs, mask_attrs, log))
1238 err = -EINVAL;
1239
1240 free_newmask:
1241 kfree(newmask);
1242 return err;
1243 }
1244
1245 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1246 {
1247 size_t len;
1248
1249 if (!attr)
1250 return 0;
1251
1252 len = nla_len(attr);
1253 if (len < 1 || len > MAX_UFID_LENGTH) {
1254 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1255 nla_len(attr), MAX_UFID_LENGTH);
1256 return 0;
1257 }
1258
1259 return len;
1260 }
1261
1262 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1263 * or false otherwise.
1264 */
1265 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1266 bool log)
1267 {
1268 sfid->ufid_len = get_ufid_len(attr, log);
1269 if (sfid->ufid_len)
1270 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1271
1272 return sfid->ufid_len;
1273 }
1274
1275 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1276 const struct sw_flow_key *key, bool log)
1277 {
1278 struct sw_flow_key *new_key;
1279
1280 if (ovs_nla_get_ufid(sfid, ufid, log))
1281 return 0;
1282
1283 /* If UFID was not provided, use unmasked key. */
1284 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1285 if (!new_key)
1286 return -ENOMEM;
1287 memcpy(new_key, key, sizeof(*key));
1288 sfid->unmasked_key = new_key;
1289
1290 return 0;
1291 }
1292
1293 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1294 {
1295 return attr ? nla_get_u32(attr) : 0;
1296 }
1297
1298 /**
1299 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1300 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1301 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1302 * sequence.
1303 * @log: Boolean to allow kernel error logging. Normally true, but when
1304 * probing for feature compatibility this should be passed in as false to
1305 * suppress unnecessary error logging.
1306 *
1307 * This parses a series of Netlink attributes that form a flow key, which must
1308 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1309 * get the metadata, that is, the parts of the flow key that cannot be
1310 * extracted from the packet itself.
1311 */
1312
1313 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1314 struct sw_flow_key *key,
1315 bool log)
1316 {
1317 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1318 struct sw_flow_match match;
1319 u64 attrs = 0;
1320 int err;
1321
1322 err = parse_flow_nlattrs(attr, a, &attrs, log);
1323 if (err)
1324 return -EINVAL;
1325
1326 memset(&match, 0, sizeof(match));
1327 match.key = key;
1328
1329 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1330 memset(&key->ct, 0, sizeof(key->ct));
1331 key->phy.in_port = DP_MAX_PORTS;
1332
1333 return metadata_from_nlattrs(&match, &attrs, a, false, log);
1334 }
1335
1336 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1337 const struct sw_flow_key *output, bool is_mask,
1338 struct sk_buff *skb)
1339 {
1340 struct ovs_key_ethernet *eth_key;
1341 struct nlattr *nla, *encap;
1342
1343 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1344 goto nla_put_failure;
1345
1346 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1347 goto nla_put_failure;
1348
1349 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1350 goto nla_put_failure;
1351
1352 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
1353 const void *opts = NULL;
1354
1355 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1356 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
1357
1358 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1359 swkey->tun_opts_len))
1360 goto nla_put_failure;
1361 }
1362
1363 if (swkey->phy.in_port == DP_MAX_PORTS) {
1364 if (is_mask && (output->phy.in_port == 0xffff))
1365 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1366 goto nla_put_failure;
1367 } else {
1368 u16 upper_u16;
1369 upper_u16 = !is_mask ? 0 : 0xffff;
1370
1371 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1372 (upper_u16 << 16) | output->phy.in_port))
1373 goto nla_put_failure;
1374 }
1375
1376 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1377 goto nla_put_failure;
1378
1379 if (ovs_ct_put_key(output, skb))
1380 goto nla_put_failure;
1381
1382 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1383 if (!nla)
1384 goto nla_put_failure;
1385
1386 eth_key = nla_data(nla);
1387 ether_addr_copy(eth_key->eth_src, output->eth.src);
1388 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1389
1390 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1391 __be16 eth_type;
1392 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1393 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1394 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1395 goto nla_put_failure;
1396 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1397 if (!swkey->eth.tci)
1398 goto unencap;
1399 } else
1400 encap = NULL;
1401
1402 if (swkey->eth.type == htons(ETH_P_802_2)) {
1403 /*
1404 * Ethertype 802.2 is represented in the netlink with omitted
1405 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1406 * 0xffff in the mask attribute. Ethertype can also
1407 * be wildcarded.
1408 */
1409 if (is_mask && output->eth.type)
1410 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1411 output->eth.type))
1412 goto nla_put_failure;
1413 goto unencap;
1414 }
1415
1416 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1417 goto nla_put_failure;
1418
1419 if (swkey->eth.type == htons(ETH_P_IP)) {
1420 struct ovs_key_ipv4 *ipv4_key;
1421
1422 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1423 if (!nla)
1424 goto nla_put_failure;
1425 ipv4_key = nla_data(nla);
1426 ipv4_key->ipv4_src = output->ipv4.addr.src;
1427 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1428 ipv4_key->ipv4_proto = output->ip.proto;
1429 ipv4_key->ipv4_tos = output->ip.tos;
1430 ipv4_key->ipv4_ttl = output->ip.ttl;
1431 ipv4_key->ipv4_frag = output->ip.frag;
1432 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1433 struct ovs_key_ipv6 *ipv6_key;
1434
1435 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1436 if (!nla)
1437 goto nla_put_failure;
1438 ipv6_key = nla_data(nla);
1439 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1440 sizeof(ipv6_key->ipv6_src));
1441 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1442 sizeof(ipv6_key->ipv6_dst));
1443 ipv6_key->ipv6_label = output->ipv6.label;
1444 ipv6_key->ipv6_proto = output->ip.proto;
1445 ipv6_key->ipv6_tclass = output->ip.tos;
1446 ipv6_key->ipv6_hlimit = output->ip.ttl;
1447 ipv6_key->ipv6_frag = output->ip.frag;
1448 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1449 swkey->eth.type == htons(ETH_P_RARP)) {
1450 struct ovs_key_arp *arp_key;
1451
1452 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1453 if (!nla)
1454 goto nla_put_failure;
1455 arp_key = nla_data(nla);
1456 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1457 arp_key->arp_sip = output->ipv4.addr.src;
1458 arp_key->arp_tip = output->ipv4.addr.dst;
1459 arp_key->arp_op = htons(output->ip.proto);
1460 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1461 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1462 } else if (eth_p_mpls(swkey->eth.type)) {
1463 struct ovs_key_mpls *mpls_key;
1464
1465 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1466 if (!nla)
1467 goto nla_put_failure;
1468 mpls_key = nla_data(nla);
1469 mpls_key->mpls_lse = output->mpls.top_lse;
1470 }
1471
1472 if ((swkey->eth.type == htons(ETH_P_IP) ||
1473 swkey->eth.type == htons(ETH_P_IPV6)) &&
1474 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1475
1476 if (swkey->ip.proto == IPPROTO_TCP) {
1477 struct ovs_key_tcp *tcp_key;
1478
1479 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1480 if (!nla)
1481 goto nla_put_failure;
1482 tcp_key = nla_data(nla);
1483 tcp_key->tcp_src = output->tp.src;
1484 tcp_key->tcp_dst = output->tp.dst;
1485 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1486 output->tp.flags))
1487 goto nla_put_failure;
1488 } else if (swkey->ip.proto == IPPROTO_UDP) {
1489 struct ovs_key_udp *udp_key;
1490
1491 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1492 if (!nla)
1493 goto nla_put_failure;
1494 udp_key = nla_data(nla);
1495 udp_key->udp_src = output->tp.src;
1496 udp_key->udp_dst = output->tp.dst;
1497 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1498 struct ovs_key_sctp *sctp_key;
1499
1500 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1501 if (!nla)
1502 goto nla_put_failure;
1503 sctp_key = nla_data(nla);
1504 sctp_key->sctp_src = output->tp.src;
1505 sctp_key->sctp_dst = output->tp.dst;
1506 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1507 swkey->ip.proto == IPPROTO_ICMP) {
1508 struct ovs_key_icmp *icmp_key;
1509
1510 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1511 if (!nla)
1512 goto nla_put_failure;
1513 icmp_key = nla_data(nla);
1514 icmp_key->icmp_type = ntohs(output->tp.src);
1515 icmp_key->icmp_code = ntohs(output->tp.dst);
1516 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1517 swkey->ip.proto == IPPROTO_ICMPV6) {
1518 struct ovs_key_icmpv6 *icmpv6_key;
1519
1520 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1521 sizeof(*icmpv6_key));
1522 if (!nla)
1523 goto nla_put_failure;
1524 icmpv6_key = nla_data(nla);
1525 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1526 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1527
1528 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1529 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1530 struct ovs_key_nd *nd_key;
1531
1532 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1533 if (!nla)
1534 goto nla_put_failure;
1535 nd_key = nla_data(nla);
1536 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1537 sizeof(nd_key->nd_target));
1538 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1539 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1540 }
1541 }
1542 }
1543
1544 unencap:
1545 if (encap)
1546 nla_nest_end(skb, encap);
1547
1548 return 0;
1549
1550 nla_put_failure:
1551 return -EMSGSIZE;
1552 }
1553
1554 int ovs_nla_put_key(const struct sw_flow_key *swkey,
1555 const struct sw_flow_key *output, int attr, bool is_mask,
1556 struct sk_buff *skb)
1557 {
1558 int err;
1559 struct nlattr *nla;
1560
1561 nla = nla_nest_start(skb, attr);
1562 if (!nla)
1563 return -EMSGSIZE;
1564 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1565 if (err)
1566 return err;
1567 nla_nest_end(skb, nla);
1568
1569 return 0;
1570 }
1571
1572 /* Called with ovs_mutex or RCU read lock. */
1573 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1574 {
1575 if (ovs_identifier_is_ufid(&flow->id))
1576 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1577 flow->id.ufid);
1578
1579 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1580 OVS_FLOW_ATTR_KEY, false, skb);
1581 }
1582
1583 /* Called with ovs_mutex or RCU read lock. */
1584 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1585 {
1586 return ovs_nla_put_key(&flow->key, &flow->key,
1587 OVS_FLOW_ATTR_KEY, false, skb);
1588 }
1589
1590 /* Called with ovs_mutex or RCU read lock. */
1591 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1592 {
1593 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1594 OVS_FLOW_ATTR_MASK, true, skb);
1595 }
1596
1597 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1598
1599 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1600 {
1601 struct sw_flow_actions *sfa;
1602
1603 if (size > MAX_ACTIONS_BUFSIZE) {
1604 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1605 return ERR_PTR(-EINVAL);
1606 }
1607
1608 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1609 if (!sfa)
1610 return ERR_PTR(-ENOMEM);
1611
1612 sfa->actions_len = 0;
1613 return sfa;
1614 }
1615
1616 static void ovs_nla_free_set_action(const struct nlattr *a)
1617 {
1618 const struct nlattr *ovs_key = nla_data(a);
1619 struct ovs_tunnel_info *ovs_tun;
1620
1621 switch (nla_type(ovs_key)) {
1622 case OVS_KEY_ATTR_TUNNEL_INFO:
1623 ovs_tun = nla_data(ovs_key);
1624 ovs_dst_release((struct dst_entry *)ovs_tun->tun_dst);
1625 break;
1626 }
1627 }
1628
1629 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1630 {
1631 const struct nlattr *a;
1632 int rem;
1633
1634 if (!sf_acts)
1635 return;
1636
1637 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1638 switch (nla_type(a)) {
1639 case OVS_ACTION_ATTR_SET:
1640 ovs_nla_free_set_action(a);
1641 break;
1642 case OVS_ACTION_ATTR_CT:
1643 ovs_ct_free_action(a);
1644 break;
1645 }
1646 }
1647
1648 kfree(sf_acts);
1649 }
1650
1651 static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1652 {
1653 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1654 }
1655
1656 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1657 * The caller must hold rcu_read_lock for this to be sensible.
1658 */
1659 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1660 {
1661 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
1662 }
1663
1664 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1665 int attr_len, bool log)
1666 {
1667
1668 struct sw_flow_actions *acts;
1669 int new_acts_size;
1670 int req_size = NLA_ALIGN(attr_len);
1671 int next_offset = offsetof(struct sw_flow_actions, actions) +
1672 (*sfa)->actions_len;
1673
1674 if (req_size <= (ksize(*sfa) - next_offset))
1675 goto out;
1676
1677 new_acts_size = ksize(*sfa) * 2;
1678
1679 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1680 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1681 return ERR_PTR(-EMSGSIZE);
1682 new_acts_size = MAX_ACTIONS_BUFSIZE;
1683 }
1684
1685 acts = nla_alloc_flow_actions(new_acts_size, log);
1686 if (IS_ERR(acts))
1687 return (void *)acts;
1688
1689 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1690 acts->actions_len = (*sfa)->actions_len;
1691 acts->orig_len = (*sfa)->orig_len;
1692 kfree(*sfa);
1693 *sfa = acts;
1694
1695 out:
1696 (*sfa)->actions_len += req_size;
1697 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1698 }
1699
1700 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1701 int attrtype, void *data, int len, bool log)
1702 {
1703 struct nlattr *a;
1704
1705 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
1706 if (IS_ERR(a))
1707 return a;
1708
1709 a->nla_type = attrtype;
1710 a->nla_len = nla_attr_size(len);
1711
1712 if (data)
1713 memcpy(nla_data(a), data, len);
1714 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1715
1716 return a;
1717 }
1718
1719 int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1720 int len, bool log)
1721 {
1722 struct nlattr *a;
1723
1724 a = __add_action(sfa, attrtype, data, len, log);
1725 if (IS_ERR(a))
1726 return PTR_ERR(a);
1727
1728 return 0;
1729 }
1730
1731 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1732 int attrtype, bool log)
1733 {
1734 int used = (*sfa)->actions_len;
1735 int err;
1736
1737 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
1738 if (err)
1739 return err;
1740
1741 return used;
1742 }
1743
1744 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1745 int st_offset)
1746 {
1747 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1748 st_offset);
1749
1750 a->nla_len = sfa->actions_len - st_offset;
1751 }
1752
1753 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
1754 const struct sw_flow_key *key,
1755 int depth, struct sw_flow_actions **sfa,
1756 __be16 eth_type, __be16 vlan_tci, bool log);
1757
1758 static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
1759 const struct sw_flow_key *key, int depth,
1760 struct sw_flow_actions **sfa,
1761 __be16 eth_type, __be16 vlan_tci, bool log)
1762 {
1763 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1764 const struct nlattr *probability, *actions;
1765 const struct nlattr *a;
1766 int rem, start, err, st_acts;
1767
1768 memset(attrs, 0, sizeof(attrs));
1769 nla_for_each_nested(a, attr, rem) {
1770 int type = nla_type(a);
1771 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1772 return -EINVAL;
1773 attrs[type] = a;
1774 }
1775 if (rem)
1776 return -EINVAL;
1777
1778 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1779 if (!probability || nla_len(probability) != sizeof(u32))
1780 return -EINVAL;
1781
1782 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1783 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1784 return -EINVAL;
1785
1786 /* validation done, copy sample action. */
1787 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
1788 if (start < 0)
1789 return start;
1790 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1791 nla_data(probability), sizeof(u32), log);
1792 if (err)
1793 return err;
1794 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
1795 if (st_acts < 0)
1796 return st_acts;
1797
1798 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
1799 eth_type, vlan_tci, log);
1800 if (err)
1801 return err;
1802
1803 add_nested_action_end(*sfa, st_acts);
1804 add_nested_action_end(*sfa, start);
1805
1806 return 0;
1807 }
1808
1809 void ovs_match_init(struct sw_flow_match *match,
1810 struct sw_flow_key *key,
1811 struct sw_flow_mask *mask)
1812 {
1813 memset(match, 0, sizeof(*match));
1814 match->key = key;
1815 match->mask = mask;
1816
1817 memset(key, 0, sizeof(*key));
1818
1819 if (mask) {
1820 memset(&mask->key, 0, sizeof(mask->key));
1821 mask->range.start = mask->range.end = 0;
1822 }
1823 }
1824
1825 static int validate_geneve_opts(struct sw_flow_key *key)
1826 {
1827 struct geneve_opt *option;
1828 int opts_len = key->tun_opts_len;
1829 bool crit_opt = false;
1830
1831 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1832 while (opts_len > 0) {
1833 int len;
1834
1835 if (opts_len < sizeof(*option))
1836 return -EINVAL;
1837
1838 len = sizeof(*option) + option->length * 4;
1839 if (len > opts_len)
1840 return -EINVAL;
1841
1842 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1843
1844 option = (struct geneve_opt *)((u8 *)option + len);
1845 opts_len -= len;
1846 };
1847
1848 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1849
1850 return 0;
1851 }
1852
1853 static int validate_and_copy_set_tun(const struct nlattr *attr,
1854 struct sw_flow_actions **sfa, bool log)
1855 {
1856 struct sw_flow_match match;
1857 struct sw_flow_key key;
1858 struct metadata_dst *tun_dst;
1859 struct ip_tunnel_info *tun_info;
1860 struct ovs_tunnel_info *ovs_tun;
1861 struct nlattr *a;
1862 int err = 0, start, opts_type;
1863
1864 ovs_match_init(&match, &key, NULL);
1865 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1866 if (opts_type < 0)
1867 return opts_type;
1868
1869 if (key.tun_opts_len) {
1870 switch (opts_type) {
1871 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1872 err = validate_geneve_opts(&key);
1873 if (err < 0)
1874 return err;
1875 break;
1876 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1877 break;
1878 }
1879 };
1880
1881 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
1882 if (start < 0)
1883 return start;
1884
1885 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1886 if (!tun_dst)
1887 return -ENOMEM;
1888
1889 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1890 sizeof(*ovs_tun), log);
1891 if (IS_ERR(a)) {
1892 ovs_dst_release((struct dst_entry *)tun_dst);
1893 return PTR_ERR(a);
1894 }
1895
1896 ovs_tun = nla_data(a);
1897 ovs_tun->tun_dst = tun_dst;
1898
1899 tun_info = &tun_dst->u.tun_info;
1900 tun_info->mode = IP_TUNNEL_INFO_TX;
1901 tun_info->key = key.tun_key;
1902
1903 /* We need to store the options in the action itself since
1904 * everything else will go away after flow setup. We can append
1905 * it to tun_info and then point there.
1906 */
1907 ip_tunnel_info_opts_set(tun_info,
1908 TUN_METADATA_OPTS(&key, key.tun_opts_len),
1909 key.tun_opts_len);
1910 add_nested_action_end(*sfa, start);
1911
1912 return err;
1913 }
1914
1915 /* Return false if there are any non-masked bits set.
1916 * Mask follows data immediately, before any netlink padding.
1917 */
1918 static bool validate_masked(u8 *data, int len)
1919 {
1920 u8 *mask = data + len;
1921
1922 while (len--)
1923 if (*data++ & ~*mask++)
1924 return false;
1925
1926 return true;
1927 }
1928
1929 static int validate_set(const struct nlattr *a,
1930 const struct sw_flow_key *flow_key,
1931 struct sw_flow_actions **sfa,
1932 bool *skip_copy, __be16 eth_type, bool masked, bool log)
1933 {
1934 const struct nlattr *ovs_key = nla_data(a);
1935 int key_type = nla_type(ovs_key);
1936 size_t key_len;
1937
1938 /* There can be only one key in a action */
1939 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1940 return -EINVAL;
1941
1942 key_len = nla_len(ovs_key);
1943 if (masked)
1944 key_len /= 2;
1945
1946 if (key_type > OVS_KEY_ATTR_MAX ||
1947 !check_attr_len(key_len, ovs_key_lens[key_type].len))
1948 return -EINVAL;
1949
1950 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1951 return -EINVAL;
1952
1953 switch (key_type) {
1954 const struct ovs_key_ipv4 *ipv4_key;
1955 const struct ovs_key_ipv6 *ipv6_key;
1956 int err;
1957
1958 case OVS_KEY_ATTR_PRIORITY:
1959 case OVS_KEY_ATTR_SKB_MARK:
1960 case OVS_KEY_ATTR_ETHERNET:
1961 break;
1962
1963 case OVS_KEY_ATTR_TUNNEL:
1964 if (eth_p_mpls(eth_type))
1965 return -EINVAL;
1966
1967 if (masked)
1968 return -EINVAL; /* Masked tunnel set not supported. */
1969
1970 *skip_copy = true;
1971 err = validate_and_copy_set_tun(a, sfa, log);
1972 if (err)
1973 return err;
1974 break;
1975
1976 case OVS_KEY_ATTR_IPV4:
1977 if (eth_type != htons(ETH_P_IP))
1978 return -EINVAL;
1979
1980 ipv4_key = nla_data(ovs_key);
1981
1982 if (masked) {
1983 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
1984
1985 /* Non-writeable fields. */
1986 if (mask->ipv4_proto || mask->ipv4_frag)
1987 return -EINVAL;
1988 } else {
1989 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1990 return -EINVAL;
1991
1992 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1993 return -EINVAL;
1994 }
1995 break;
1996
1997 case OVS_KEY_ATTR_IPV6:
1998 if (eth_type != htons(ETH_P_IPV6))
1999 return -EINVAL;
2000
2001 ipv6_key = nla_data(ovs_key);
2002
2003 if (masked) {
2004 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2005
2006 /* Non-writeable fields. */
2007 if (mask->ipv6_proto || mask->ipv6_frag)
2008 return -EINVAL;
2009
2010 /* Invalid bits in the flow label mask? */
2011 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2012 return -EINVAL;
2013 } else {
2014 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2015 return -EINVAL;
2016
2017 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2018 return -EINVAL;
2019 }
2020 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2021 return -EINVAL;
2022
2023 break;
2024
2025 case OVS_KEY_ATTR_TCP:
2026 if ((eth_type != htons(ETH_P_IP) &&
2027 eth_type != htons(ETH_P_IPV6)) ||
2028 flow_key->ip.proto != IPPROTO_TCP)
2029 return -EINVAL;
2030
2031 break;
2032
2033 case OVS_KEY_ATTR_UDP:
2034 if ((eth_type != htons(ETH_P_IP) &&
2035 eth_type != htons(ETH_P_IPV6)) ||
2036 flow_key->ip.proto != IPPROTO_UDP)
2037 return -EINVAL;
2038
2039 break;
2040
2041 case OVS_KEY_ATTR_MPLS:
2042 if (!eth_p_mpls(eth_type))
2043 return -EINVAL;
2044 break;
2045
2046 case OVS_KEY_ATTR_SCTP:
2047 if ((eth_type != htons(ETH_P_IP) &&
2048 eth_type != htons(ETH_P_IPV6)) ||
2049 flow_key->ip.proto != IPPROTO_SCTP)
2050 return -EINVAL;
2051
2052 break;
2053
2054 default:
2055 return -EINVAL;
2056 }
2057
2058 /* Convert non-masked non-tunnel set actions to masked set actions. */
2059 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2060 int start, len = key_len * 2;
2061 struct nlattr *at;
2062
2063 *skip_copy = true;
2064
2065 start = add_nested_action_start(sfa,
2066 OVS_ACTION_ATTR_SET_TO_MASKED,
2067 log);
2068 if (start < 0)
2069 return start;
2070
2071 at = __add_action(sfa, key_type, NULL, len, log);
2072 if (IS_ERR(at))
2073 return PTR_ERR(at);
2074
2075 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2076 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2077 /* Clear non-writeable bits from otherwise writeable fields. */
2078 if (key_type == OVS_KEY_ATTR_IPV6) {
2079 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2080
2081 mask->ipv6_label &= htonl(0x000FFFFF);
2082 }
2083 add_nested_action_end(*sfa, start);
2084 }
2085
2086 return 0;
2087 }
2088
2089 static int validate_userspace(const struct nlattr *attr)
2090 {
2091 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2092 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2093 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
2094 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
2095 };
2096 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2097 int error;
2098
2099 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2100 attr, userspace_policy);
2101 if (error)
2102 return error;
2103
2104 if (!a[OVS_USERSPACE_ATTR_PID] ||
2105 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2106 return -EINVAL;
2107
2108 return 0;
2109 }
2110
2111 static int copy_action(const struct nlattr *from,
2112 struct sw_flow_actions **sfa, bool log)
2113 {
2114 int totlen = NLA_ALIGN(from->nla_len);
2115 struct nlattr *to;
2116
2117 to = reserve_sfa_size(sfa, from->nla_len, log);
2118 if (IS_ERR(to))
2119 return PTR_ERR(to);
2120
2121 memcpy(to, from, totlen);
2122 return 0;
2123 }
2124
2125 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2126 const struct sw_flow_key *key,
2127 int depth, struct sw_flow_actions **sfa,
2128 __be16 eth_type, __be16 vlan_tci, bool log)
2129 {
2130 const struct nlattr *a;
2131 int rem, err;
2132
2133 if (depth >= SAMPLE_ACTION_DEPTH)
2134 return -EOVERFLOW;
2135
2136 nla_for_each_nested(a, attr, rem) {
2137 /* Expected argument lengths, (u32)-1 for variable length. */
2138 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2139 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
2140 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
2141 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
2142 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2143 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
2144 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2145 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2146 [OVS_ACTION_ATTR_SET] = (u32)-1,
2147 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
2148 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
2149 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2150 [OVS_ACTION_ATTR_CT] = (u32)-1,
2151 };
2152 const struct ovs_action_push_vlan *vlan;
2153 int type = nla_type(a);
2154 bool skip_copy;
2155
2156 if (type > OVS_ACTION_ATTR_MAX ||
2157 (action_lens[type] != nla_len(a) &&
2158 action_lens[type] != (u32)-1))
2159 return -EINVAL;
2160
2161 skip_copy = false;
2162 switch (type) {
2163 case OVS_ACTION_ATTR_UNSPEC:
2164 return -EINVAL;
2165
2166 case OVS_ACTION_ATTR_USERSPACE:
2167 err = validate_userspace(a);
2168 if (err)
2169 return err;
2170 break;
2171
2172 case OVS_ACTION_ATTR_OUTPUT:
2173 if (nla_get_u32(a) >= DP_MAX_PORTS)
2174 return -EINVAL;
2175 break;
2176
2177 case OVS_ACTION_ATTR_HASH: {
2178 const struct ovs_action_hash *act_hash = nla_data(a);
2179
2180 switch (act_hash->hash_alg) {
2181 case OVS_HASH_ALG_L4:
2182 break;
2183 default:
2184 return -EINVAL;
2185 }
2186
2187 break;
2188 }
2189
2190 case OVS_ACTION_ATTR_POP_VLAN:
2191 vlan_tci = htons(0);
2192 break;
2193
2194 case OVS_ACTION_ATTR_PUSH_VLAN:
2195 vlan = nla_data(a);
2196 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2197 return -EINVAL;
2198 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2199 return -EINVAL;
2200 vlan_tci = vlan->vlan_tci;
2201 break;
2202
2203 case OVS_ACTION_ATTR_RECIRC:
2204 break;
2205
2206 case OVS_ACTION_ATTR_PUSH_MPLS: {
2207 const struct ovs_action_push_mpls *mpls = nla_data(a);
2208
2209 if (!eth_p_mpls(mpls->mpls_ethertype))
2210 return -EINVAL;
2211 /* Prohibit push MPLS other than to a white list
2212 * for packets that have a known tag order.
2213 */
2214 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2215 (eth_type != htons(ETH_P_IP) &&
2216 eth_type != htons(ETH_P_IPV6) &&
2217 eth_type != htons(ETH_P_ARP) &&
2218 eth_type != htons(ETH_P_RARP) &&
2219 !eth_p_mpls(eth_type)))
2220 return -EINVAL;
2221 eth_type = mpls->mpls_ethertype;
2222 break;
2223 }
2224
2225 case OVS_ACTION_ATTR_POP_MPLS:
2226 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2227 !eth_p_mpls(eth_type))
2228 return -EINVAL;
2229
2230 /* Disallow subsequent L2.5+ set and mpls_pop actions
2231 * as there is no check here to ensure that the new
2232 * eth_type is valid and thus set actions could
2233 * write off the end of the packet or otherwise
2234 * corrupt it.
2235 *
2236 * Support for these actions is planned using packet
2237 * recirculation.
2238 */
2239 eth_type = htons(0);
2240 break;
2241
2242 case OVS_ACTION_ATTR_SET:
2243 err = validate_set(a, key, sfa,
2244 &skip_copy, eth_type, false, log);
2245 if (err)
2246 return err;
2247 break;
2248
2249 case OVS_ACTION_ATTR_SET_MASKED:
2250 err = validate_set(a, key, sfa,
2251 &skip_copy, eth_type, true, log);
2252 if (err)
2253 return err;
2254 break;
2255
2256 case OVS_ACTION_ATTR_SAMPLE:
2257 err = validate_and_copy_sample(net, a, key, depth, sfa,
2258 eth_type, vlan_tci, log);
2259 if (err)
2260 return err;
2261 skip_copy = true;
2262 break;
2263
2264 case OVS_ACTION_ATTR_CT:
2265 err = ovs_ct_copy_action(net, a, key, sfa, log);
2266 if (err)
2267 return err;
2268 skip_copy = true;
2269 break;
2270
2271 default:
2272 OVS_NLERR(log, "Unknown Action type %d", type);
2273 return -EINVAL;
2274 }
2275 if (!skip_copy) {
2276 err = copy_action(a, sfa, log);
2277 if (err)
2278 return err;
2279 }
2280 }
2281
2282 if (rem > 0)
2283 return -EINVAL;
2284
2285 return 0;
2286 }
2287
2288 /* 'key' must be the masked key. */
2289 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2290 const struct sw_flow_key *key,
2291 struct sw_flow_actions **sfa, bool log)
2292 {
2293 int err;
2294
2295 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2296 if (IS_ERR(*sfa))
2297 return PTR_ERR(*sfa);
2298
2299 (*sfa)->orig_len = nla_len(attr);
2300 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
2301 key->eth.tci, log);
2302 if (err)
2303 ovs_nla_free_flow_actions(*sfa);
2304
2305 return err;
2306 }
2307
2308 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2309 {
2310 const struct nlattr *a;
2311 struct nlattr *start;
2312 int err = 0, rem;
2313
2314 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2315 if (!start)
2316 return -EMSGSIZE;
2317
2318 nla_for_each_nested(a, attr, rem) {
2319 int type = nla_type(a);
2320 struct nlattr *st_sample;
2321
2322 switch (type) {
2323 case OVS_SAMPLE_ATTR_PROBABILITY:
2324 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2325 sizeof(u32), nla_data(a)))
2326 return -EMSGSIZE;
2327 break;
2328 case OVS_SAMPLE_ATTR_ACTIONS:
2329 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2330 if (!st_sample)
2331 return -EMSGSIZE;
2332 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2333 if (err)
2334 return err;
2335 nla_nest_end(skb, st_sample);
2336 break;
2337 }
2338 }
2339
2340 nla_nest_end(skb, start);
2341 return err;
2342 }
2343
2344 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2345 {
2346 const struct nlattr *ovs_key = nla_data(a);
2347 int key_type = nla_type(ovs_key);
2348 struct nlattr *start;
2349 int err;
2350
2351 switch (key_type) {
2352 case OVS_KEY_ATTR_TUNNEL_INFO: {
2353 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2354 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
2355
2356 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2357 if (!start)
2358 return -EMSGSIZE;
2359
2360 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
2361 tun_info->options_len ?
2362 ip_tunnel_info_opts(tun_info) : NULL,
2363 tun_info->options_len);
2364 if (err)
2365 return err;
2366 nla_nest_end(skb, start);
2367 break;
2368 }
2369 default:
2370 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2371 return -EMSGSIZE;
2372 break;
2373 }
2374
2375 return 0;
2376 }
2377
2378 static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2379 struct sk_buff *skb)
2380 {
2381 const struct nlattr *ovs_key = nla_data(a);
2382 size_t key_len = nla_len(ovs_key) / 2;
2383
2384 /* Revert the conversion we did from a non-masked set action to
2385 * masked set action.
2386 */
2387 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a) - key_len, ovs_key))
2388 return -EMSGSIZE;
2389
2390 return 0;
2391 }
2392
2393 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2394 {
2395 const struct nlattr *a;
2396 int rem, err;
2397
2398 nla_for_each_attr(a, attr, len, rem) {
2399 int type = nla_type(a);
2400
2401 switch (type) {
2402 case OVS_ACTION_ATTR_SET:
2403 err = set_action_to_attr(a, skb);
2404 if (err)
2405 return err;
2406 break;
2407
2408 case OVS_ACTION_ATTR_SET_TO_MASKED:
2409 err = masked_set_action_to_set_action_attr(a, skb);
2410 if (err)
2411 return err;
2412 break;
2413
2414 case OVS_ACTION_ATTR_SAMPLE:
2415 err = sample_action_to_attr(a, skb);
2416 if (err)
2417 return err;
2418 break;
2419
2420 case OVS_ACTION_ATTR_CT:
2421 err = ovs_ct_action_to_attr(nla_data(a), skb);
2422 if (err)
2423 return err;
2424 break;
2425
2426 default:
2427 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2428 return -EMSGSIZE;
2429 break;
2430 }
2431 }
2432
2433 return 0;
2434 }