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