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