]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/openvswitch/flow_netlink.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / openvswitch / flow_netlink.c
CommitLineData
e6445719 1/*
798c1661 2 * Copyright (c) 2007-2017 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>
614732ea 50#include <net/vxlan.h>
b2d0f5d5 51#include <net/tun_proto.h>
e6445719
PS
52
53#include "flow_netlink.h"
54
81bfe3c3
TG
55struct ovs_len_tbl {
56 int len;
57 const struct ovs_len_tbl *next;
58};
59
60#define OVS_ATTR_NESTED -1
982b5270 61#define OVS_ATTR_VARIABLE -2
81bfe3c3 62
798c1661 63static bool actions_may_change_flow(const struct nlattr *actions)
64{
65 struct nlattr *nla;
66 int rem;
67
68 nla_for_each_nested(nla, actions, rem) {
69 u16 action = nla_type(nla);
70
71 switch (action) {
72 case OVS_ACTION_ATTR_OUTPUT:
73 case OVS_ACTION_ATTR_RECIRC:
74 case OVS_ACTION_ATTR_TRUNC:
75 case OVS_ACTION_ATTR_USERSPACE:
76 break;
77
78 case OVS_ACTION_ATTR_CT:
b8226962 79 case OVS_ACTION_ATTR_CT_CLEAR:
798c1661 80 case OVS_ACTION_ATTR_HASH:
81 case OVS_ACTION_ATTR_POP_ETH:
82 case OVS_ACTION_ATTR_POP_MPLS:
b2d0f5d5 83 case OVS_ACTION_ATTR_POP_NSH:
798c1661 84 case OVS_ACTION_ATTR_POP_VLAN:
85 case OVS_ACTION_ATTR_PUSH_ETH:
86 case OVS_ACTION_ATTR_PUSH_MPLS:
b2d0f5d5 87 case OVS_ACTION_ATTR_PUSH_NSH:
798c1661 88 case OVS_ACTION_ATTR_PUSH_VLAN:
89 case OVS_ACTION_ATTR_SAMPLE:
90 case OVS_ACTION_ATTR_SET:
91 case OVS_ACTION_ATTR_SET_MASKED:
cd8a6c33 92 case OVS_ACTION_ATTR_METER:
798c1661 93 default:
94 return true;
95 }
96 }
97 return false;
98}
99
a85311bf
PS
100static void update_range(struct sw_flow_match *match,
101 size_t offset, size_t size, bool is_mask)
e6445719 102{
a85311bf 103 struct sw_flow_key_range *range;
e6445719
PS
104 size_t start = rounddown(offset, sizeof(long));
105 size_t end = roundup(offset + size, sizeof(long));
106
107 if (!is_mask)
108 range = &match->range;
a85311bf 109 else
e6445719
PS
110 range = &match->mask->range;
111
e6445719
PS
112 if (range->start == range->end) {
113 range->start = start;
114 range->end = end;
115 return;
116 }
117
118 if (range->start > start)
119 range->start = start;
120
121 if (range->end < end)
122 range->end = end;
123}
124
125#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
126 do { \
a85311bf
PS
127 update_range(match, offsetof(struct sw_flow_key, field), \
128 sizeof((match)->key->field), is_mask); \
129 if (is_mask) \
130 (match)->mask->key.field = value; \
131 else \
e6445719 132 (match)->key->field = value; \
e6445719
PS
133 } while (0)
134
f5796684
JG
135#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
136 do { \
a85311bf 137 update_range(match, offset, len, is_mask); \
f5796684
JG
138 if (is_mask) \
139 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
a85311bf 140 len); \
f5796684
JG
141 else \
142 memcpy((u8 *)(match)->key + offset, value_p, len); \
e6445719
PS
143 } while (0)
144
f5796684
JG
145#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
146 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
147 value_p, len, is_mask)
148
a85311bf
PS
149#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
150 do { \
151 update_range(match, offsetof(struct sw_flow_key, field), \
152 sizeof((match)->key->field), is_mask); \
153 if (is_mask) \
154 memset((u8 *)&(match)->mask->key.field, value, \
155 sizeof((match)->mask->key.field)); \
156 else \
f47de068
PS
157 memset((u8 *)&(match)->key->field, value, \
158 sizeof((match)->key->field)); \
f47de068 159 } while (0)
e6445719
PS
160
161static bool match_validate(const struct sw_flow_match *match,
05da5898 162 u64 key_attrs, u64 mask_attrs, bool log)
e6445719 163{
0a6410fb 164 u64 key_expected = 0;
e6445719
PS
165 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
166
167 /* The following mask attributes allowed only if they
168 * pass the validation tests. */
169 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
9dd7f890 170 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
e6445719 171 | (1 << OVS_KEY_ATTR_IPV6)
9dd7f890 172 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
e6445719 173 | (1 << OVS_KEY_ATTR_TCP)
5eb26b15 174 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
e6445719
PS
175 | (1 << OVS_KEY_ATTR_UDP)
176 | (1 << OVS_KEY_ATTR_SCTP)
177 | (1 << OVS_KEY_ATTR_ICMP)
178 | (1 << OVS_KEY_ATTR_ICMPV6)
179 | (1 << OVS_KEY_ATTR_ARP)
25cd9ba0 180 | (1 << OVS_KEY_ATTR_ND)
b2d0f5d5
YY
181 | (1 << OVS_KEY_ATTR_MPLS)
182 | (1 << OVS_KEY_ATTR_NSH));
e6445719
PS
183
184 /* Always allowed mask fields. */
185 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
186 | (1 << OVS_KEY_ATTR_IN_PORT)
187 | (1 << OVS_KEY_ATTR_ETHERTYPE));
188
189 /* Check key attributes. */
190 if (match->key->eth.type == htons(ETH_P_ARP)
191 || match->key->eth.type == htons(ETH_P_RARP)) {
192 key_expected |= 1 << OVS_KEY_ATTR_ARP;
f2a01517 193 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
e6445719
PS
194 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
195 }
196
25cd9ba0
SH
197 if (eth_p_mpls(match->key->eth.type)) {
198 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
199 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
200 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
201 }
202
e6445719
PS
203 if (match->key->eth.type == htons(ETH_P_IP)) {
204 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
9dd7f890 205 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
e6445719 206 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
9dd7f890
JR
207 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
208 }
e6445719
PS
209
210 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
211 if (match->key->ip.proto == IPPROTO_UDP) {
212 key_expected |= 1 << OVS_KEY_ATTR_UDP;
213 if (match->mask && (match->mask->key.ip.proto == 0xff))
214 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
215 }
216
217 if (match->key->ip.proto == IPPROTO_SCTP) {
218 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
219 if (match->mask && (match->mask->key.ip.proto == 0xff))
220 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
221 }
222
223 if (match->key->ip.proto == IPPROTO_TCP) {
224 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
225 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
226 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 227 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
228 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
229 }
e6445719
PS
230 }
231
232 if (match->key->ip.proto == IPPROTO_ICMP) {
233 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
234 if (match->mask && (match->mask->key.ip.proto == 0xff))
235 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
236 }
237 }
238 }
239
240 if (match->key->eth.type == htons(ETH_P_IPV6)) {
241 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
9dd7f890 242 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
e6445719 243 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
9dd7f890
JR
244 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
245 }
e6445719
PS
246
247 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
248 if (match->key->ip.proto == IPPROTO_UDP) {
249 key_expected |= 1 << OVS_KEY_ATTR_UDP;
250 if (match->mask && (match->mask->key.ip.proto == 0xff))
251 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
252 }
253
254 if (match->key->ip.proto == IPPROTO_SCTP) {
255 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
256 if (match->mask && (match->mask->key.ip.proto == 0xff))
257 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
258 }
259
260 if (match->key->ip.proto == IPPROTO_TCP) {
261 key_expected |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
262 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
263 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
e6445719 264 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
5eb26b15
JR
265 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
266 }
e6445719
PS
267 }
268
269 if (match->key->ip.proto == IPPROTO_ICMPV6) {
270 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
271 if (match->mask && (match->mask->key.ip.proto == 0xff))
272 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
273
1139e241 274 if (match->key->tp.src ==
e6445719 275 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1139e241 276 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
e6445719 277 key_expected |= 1 << OVS_KEY_ATTR_ND;
9dd7f890
JR
278 /* Original direction conntrack tuple
279 * uses the same space as the ND fields
280 * in the key, so both are not allowed
281 * at the same time.
282 */
283 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
f2a01517 284 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
e6445719
PS
285 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
286 }
287 }
288 }
289 }
290
b2d0f5d5
YY
291 if (match->key->eth.type == htons(ETH_P_NSH)) {
292 key_expected |= 1 << OVS_KEY_ATTR_NSH;
293 if (match->mask &&
294 match->mask->key.eth.type == htons(0xffff)) {
295 mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
296 }
297 }
298
e6445719
PS
299 if ((key_attrs & key_expected) != key_expected) {
300 /* Key attributes check failed. */
05da5898
JR
301 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
302 (unsigned long long)key_attrs,
303 (unsigned long long)key_expected);
e6445719
PS
304 return false;
305 }
306
307 if ((mask_attrs & mask_allowed) != mask_attrs) {
308 /* Mask attributes check failed. */
05da5898
JR
309 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
310 (unsigned long long)mask_attrs,
311 (unsigned long long)mask_allowed);
e6445719
PS
312 return false;
313 }
314
315 return true;
316}
317
8f0aad6f
WZ
318size_t ovs_tun_key_attr_size(void)
319{
320 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
321 * updating this function.
322 */
b46f6ded 323 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
6b26ba3a
JB
324 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
325 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
8f0aad6f
WZ
326 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
327 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
328 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
329 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
330 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
331 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
1dd144cf
TG
332 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
333 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
334 */
8f0aad6f 335 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
95a33208 336 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
8f0aad6f
WZ
337}
338
06c2351f 339static size_t ovs_nsh_key_attr_size(void)
b2d0f5d5
YY
340{
341 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
342 * updating this function.
343 */
344 return nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
345 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
346 * mutually exclusive, so the bigger one can cover
347 * the small one.
348 */
349 + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
350}
351
41af73e9
JS
352size_t ovs_key_attr_size(void)
353{
354 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
355 * updating this function.
356 */
b2d0f5d5 357 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
41af73e9
JS
358
359 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
360 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
8f0aad6f 361 + ovs_tun_key_attr_size()
41af73e9
JS
362 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
363 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
364 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
365 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
fbccce59 366 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
7f8a436e 367 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
182e3042 368 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
33db4125 369 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
9dd7f890 370 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
b2d0f5d5
YY
371 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
372 + ovs_nsh_key_attr_size()
41af73e9
JS
373 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
374 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
375 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
376 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
378 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
380 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
381}
382
982b5270
JG
383static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
384 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
385};
386
81bfe3c3
TG
387static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
388 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
389 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
390 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
391 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
392 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
393 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
394 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
395 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
396 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
397 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
982b5270
JG
398 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
399 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
400 .next = ovs_vxlan_ext_key_lens },
6b26ba3a
JB
401 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
402 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
81bfe3c3
TG
403};
404
b2d0f5d5
YY
405static const struct ovs_len_tbl
406ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
407 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
408 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
409 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
410};
411
e6445719 412/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
81bfe3c3
TG
413static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
414 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
415 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
416 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
417 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
418 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
419 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
420 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
421 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
422 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
423 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
424 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
425 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
426 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
427 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
428 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
429 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
430 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
431 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
432 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
433 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
434 .next = ovs_tunnel_key_lens, },
435 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
fbccce59 436 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
7f8a436e 437 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
182e3042 438 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
33db4125 439 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
9dd7f890
JR
440 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
441 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
442 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
443 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
b2d0f5d5
YY
444 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
445 .next = ovs_nsh_key_attr_lens, },
e6445719
PS
446};
447
982b5270
JG
448static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
449{
450 return expected_len == attr_len ||
451 expected_len == OVS_ATTR_NESTED ||
452 expected_len == OVS_ATTR_VARIABLE;
453}
454
e6445719
PS
455static bool is_all_zero(const u8 *fp, size_t size)
456{
457 int i;
458
459 if (!fp)
460 return false;
461
462 for (i = 0; i < size; i++)
463 if (fp[i])
464 return false;
465
466 return true;
467}
468
469static int __parse_flow_nlattrs(const struct nlattr *attr,
470 const struct nlattr *a[],
05da5898 471 u64 *attrsp, bool log, bool nz)
e6445719
PS
472{
473 const struct nlattr *nla;
474 u64 attrs;
475 int rem;
476
477 attrs = *attrsp;
478 nla_for_each_nested(nla, attr, rem) {
479 u16 type = nla_type(nla);
480 int expected_len;
481
482 if (type > OVS_KEY_ATTR_MAX) {
05da5898 483 OVS_NLERR(log, "Key type %d is out of range max %d",
e6445719
PS
484 type, OVS_KEY_ATTR_MAX);
485 return -EINVAL;
486 }
487
488 if (attrs & (1 << type)) {
05da5898 489 OVS_NLERR(log, "Duplicate key (type %d).", type);
e6445719
PS
490 return -EINVAL;
491 }
492
81bfe3c3 493 expected_len = ovs_key_lens[type].len;
982b5270 494 if (!check_attr_len(nla_len(nla), expected_len)) {
05da5898
JR
495 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
496 type, nla_len(nla), expected_len);
e6445719
PS
497 return -EINVAL;
498 }
499
5b217bbe 500 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
e6445719
PS
501 attrs |= 1 << type;
502 a[type] = nla;
503 }
504 }
505 if (rem) {
05da5898 506 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
e6445719
PS
507 return -EINVAL;
508 }
509
510 *attrsp = attrs;
511 return 0;
512}
513
514static int parse_flow_mask_nlattrs(const struct nlattr *attr,
05da5898
JR
515 const struct nlattr *a[], u64 *attrsp,
516 bool log)
e6445719 517{
05da5898 518 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
e6445719
PS
519}
520
9dd7f890
JR
521int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
522 u64 *attrsp, bool log)
e6445719 523{
05da5898
JR
524 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
525}
526
527static int genev_tun_opt_from_nlattr(const struct nlattr *a,
528 struct sw_flow_match *match, bool is_mask,
529 bool log)
530{
531 unsigned long opt_key_offset;
532
533 if (nla_len(a) > sizeof(match->key->tun_opts)) {
534 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
535 nla_len(a), sizeof(match->key->tun_opts));
536 return -EINVAL;
537 }
538
539 if (nla_len(a) % 4 != 0) {
540 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
541 nla_len(a));
542 return -EINVAL;
543 }
544
545 /* We need to record the length of the options passed
546 * down, otherwise packets with the same format but
547 * additional options will be silently matched.
548 */
549 if (!is_mask) {
550 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
551 false);
552 } else {
553 /* This is somewhat unusual because it looks at
554 * both the key and mask while parsing the
555 * attributes (and by extension assumes the key
556 * is parsed first). Normally, we would verify
557 * that each is the correct length and that the
558 * attributes line up in the validate function.
559 * However, that is difficult because this is
560 * variable length and we won't have the
561 * information later.
562 */
563 if (match->key->tun_opts_len != nla_len(a)) {
564 OVS_NLERR(log, "Geneve option len %d != mask len %d",
565 match->key->tun_opts_len, nla_len(a));
566 return -EINVAL;
567 }
568
569 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
570 }
571
d91641d9 572 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
05da5898
JR
573 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
574 nla_len(a), is_mask);
575 return 0;
e6445719
PS
576}
577
982b5270 578static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
1dd144cf
TG
579 struct sw_flow_match *match, bool is_mask,
580 bool log)
581{
982b5270
JG
582 struct nlattr *a;
583 int rem;
1dd144cf 584 unsigned long opt_key_offset;
614732ea 585 struct vxlan_metadata opts;
1dd144cf
TG
586
587 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
588
1dd144cf 589 memset(&opts, 0, sizeof(opts));
982b5270
JG
590 nla_for_each_nested(a, attr, rem) {
591 int type = nla_type(a);
1dd144cf 592
982b5270
JG
593 if (type > OVS_VXLAN_EXT_MAX) {
594 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
595 type, OVS_VXLAN_EXT_MAX);
596 return -EINVAL;
597 }
598
599 if (!check_attr_len(nla_len(a),
600 ovs_vxlan_ext_key_lens[type].len)) {
601 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
602 type, nla_len(a),
603 ovs_vxlan_ext_key_lens[type].len);
604 return -EINVAL;
605 }
606
607 switch (type) {
608 case OVS_VXLAN_EXT_GBP:
609 opts.gbp = nla_get_u32(a);
610 break;
611 default:
612 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
613 type);
614 return -EINVAL;
615 }
616 }
617 if (rem) {
618 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
619 rem);
620 return -EINVAL;
621 }
1dd144cf
TG
622
623 if (!is_mask)
624 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
625 else
626 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
627
628 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
629 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
630 is_mask);
631 return 0;
632}
633
6b26ba3a
JB
634static int ip_tun_from_nlattr(const struct nlattr *attr,
635 struct sw_flow_match *match, bool is_mask,
636 bool log)
e6445719 637{
99e28f18
PS
638 bool ttl = false, ipv4 = false, ipv6 = false;
639 __be16 tun_flags = 0;
640 int opts_type = 0;
e6445719
PS
641 struct nlattr *a;
642 int rem;
e6445719
PS
643
644 nla_for_each_nested(a, attr, rem) {
645 int type = nla_type(a);
05da5898
JR
646 int err;
647
e6445719 648 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
05da5898
JR
649 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
650 type, OVS_TUNNEL_KEY_ATTR_MAX);
e6445719
PS
651 return -EINVAL;
652 }
653
982b5270
JG
654 if (!check_attr_len(nla_len(a),
655 ovs_tunnel_key_lens[type].len)) {
05da5898 656 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
81bfe3c3 657 type, nla_len(a), ovs_tunnel_key_lens[type].len);
e6445719
PS
658 return -EINVAL;
659 }
660
661 switch (type) {
662 case OVS_TUNNEL_KEY_ATTR_ID:
663 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
664 nla_get_be64(a), is_mask);
665 tun_flags |= TUNNEL_KEY;
666 break;
667 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
c1ea5d67 668 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
67b61f6c 669 nla_get_in_addr(a), is_mask);
6b26ba3a 670 ipv4 = true;
e6445719
PS
671 break;
672 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
c1ea5d67 673 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
67b61f6c 674 nla_get_in_addr(a), is_mask);
6b26ba3a
JB
675 ipv4 = true;
676 break;
677 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
3d20f1f7 678 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
6b26ba3a
JB
679 nla_get_in6_addr(a), is_mask);
680 ipv6 = true;
681 break;
682 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
683 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
684 nla_get_in6_addr(a), is_mask);
685 ipv6 = true;
e6445719
PS
686 break;
687 case OVS_TUNNEL_KEY_ATTR_TOS:
7c383fb2 688 SW_FLOW_KEY_PUT(match, tun_key.tos,
e6445719
PS
689 nla_get_u8(a), is_mask);
690 break;
691 case OVS_TUNNEL_KEY_ATTR_TTL:
7c383fb2 692 SW_FLOW_KEY_PUT(match, tun_key.ttl,
e6445719
PS
693 nla_get_u8(a), is_mask);
694 ttl = true;
695 break;
696 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
697 tun_flags |= TUNNEL_DONT_FRAGMENT;
698 break;
699 case OVS_TUNNEL_KEY_ATTR_CSUM:
700 tun_flags |= TUNNEL_CSUM;
701 break;
8f0aad6f
WZ
702 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
703 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
704 nla_get_be16(a), is_mask);
705 break;
706 case OVS_TUNNEL_KEY_ATTR_TP_DST:
707 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
708 nla_get_be16(a), is_mask);
709 break;
67fa0341
JG
710 case OVS_TUNNEL_KEY_ATTR_OAM:
711 tun_flags |= TUNNEL_OAM;
712 break;
f5796684 713 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1dd144cf
TG
714 if (opts_type) {
715 OVS_NLERR(log, "Multiple metadata blocks provided");
716 return -EINVAL;
717 }
718
05da5898
JR
719 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
720 if (err)
721 return err;
f5796684 722
1dd144cf
TG
723 tun_flags |= TUNNEL_GENEVE_OPT;
724 opts_type = type;
725 break;
726 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
727 if (opts_type) {
728 OVS_NLERR(log, "Multiple metadata blocks provided");
729 return -EINVAL;
730 }
731
732 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
733 if (err)
734 return err;
735
736 tun_flags |= TUNNEL_VXLAN_OPT;
737 opts_type = type;
f5796684 738 break;
8f3dbfd7
KM
739 case OVS_TUNNEL_KEY_ATTR_PAD:
740 break;
e6445719 741 default:
6b26ba3a 742 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
f5796684 743 type);
e6445719
PS
744 return -EINVAL;
745 }
746 }
747
748 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
00a93bab
JB
749 if (is_mask)
750 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
751 else
6b26ba3a
JB
752 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
753 false);
e6445719
PS
754
755 if (rem > 0) {
6b26ba3a 756 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
05da5898 757 rem);
e6445719
PS
758 return -EINVAL;
759 }
760
6b26ba3a
JB
761 if (ipv4 && ipv6) {
762 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
763 return -EINVAL;
764 }
765
e6445719 766 if (!is_mask) {
6b26ba3a
JB
767 if (!ipv4 && !ipv6) {
768 OVS_NLERR(log, "IP tunnel dst address not specified");
769 return -EINVAL;
770 }
771 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
05da5898 772 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
e6445719
PS
773 return -EINVAL;
774 }
6b26ba3a
JB
775 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
776 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
777 return -EINVAL;
778 }
e6445719
PS
779
780 if (!ttl) {
6b26ba3a 781 OVS_NLERR(log, "IP tunnel TTL not specified.");
e6445719
PS
782 return -EINVAL;
783 }
784 }
785
1dd144cf
TG
786 return opts_type;
787}
788
789static int vxlan_opt_to_nlattr(struct sk_buff *skb,
790 const void *tun_opts, int swkey_tun_opts_len)
791{
614732ea 792 const struct vxlan_metadata *opts = tun_opts;
1dd144cf
TG
793 struct nlattr *nla;
794
795 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
796 if (!nla)
797 return -EMSGSIZE;
798
799 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
800 return -EMSGSIZE;
801
802 nla_nest_end(skb, nla);
e6445719
PS
803 return 0;
804}
805
6b26ba3a
JB
806static int __ip_tun_to_nlattr(struct sk_buff *skb,
807 const struct ip_tunnel_key *output,
808 const void *tun_opts, int swkey_tun_opts_len,
809 unsigned short tun_proto)
e6445719 810{
e6445719 811 if (output->tun_flags & TUNNEL_KEY &&
b46f6ded
ND
812 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
813 OVS_TUNNEL_KEY_ATTR_PAD))
e6445719 814 return -EMSGSIZE;
6b26ba3a
JB
815 switch (tun_proto) {
816 case AF_INET:
817 if (output->u.ipv4.src &&
818 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
819 output->u.ipv4.src))
820 return -EMSGSIZE;
821 if (output->u.ipv4.dst &&
822 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
823 output->u.ipv4.dst))
824 return -EMSGSIZE;
825 break;
826 case AF_INET6:
827 if (!ipv6_addr_any(&output->u.ipv6.src) &&
828 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
829 &output->u.ipv6.src))
830 return -EMSGSIZE;
831 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
832 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
833 &output->u.ipv6.dst))
834 return -EMSGSIZE;
835 break;
836 }
7c383fb2
JB
837 if (output->tos &&
838 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
e6445719 839 return -EMSGSIZE;
7c383fb2 840 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
e6445719
PS
841 return -EMSGSIZE;
842 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
67fa0341 843 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
e6445719
PS
844 return -EMSGSIZE;
845 if ((output->tun_flags & TUNNEL_CSUM) &&
67fa0341
JG
846 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
847 return -EMSGSIZE;
8f0aad6f
WZ
848 if (output->tp_src &&
849 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
850 return -EMSGSIZE;
851 if (output->tp_dst &&
852 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
853 return -EMSGSIZE;
67fa0341
JG
854 if ((output->tun_flags & TUNNEL_OAM) &&
855 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
e6445719 856 return -EMSGSIZE;
fc4099f1 857 if (swkey_tun_opts_len) {
1dd144cf
TG
858 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
859 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
860 swkey_tun_opts_len, tun_opts))
861 return -EMSGSIZE;
862 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
863 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
864 return -EMSGSIZE;
865 }
e6445719 866
e6445719
PS
867 return 0;
868}
869
6b26ba3a
JB
870static int ip_tun_to_nlattr(struct sk_buff *skb,
871 const struct ip_tunnel_key *output,
872 const void *tun_opts, int swkey_tun_opts_len,
873 unsigned short tun_proto)
f5796684
JG
874{
875 struct nlattr *nla;
876 int err;
877
878 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
879 if (!nla)
880 return -EMSGSIZE;
881
6b26ba3a
JB
882 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
883 tun_proto);
f5796684
JG
884 if (err)
885 return err;
886
887 nla_nest_end(skb, nla);
888 return 0;
889}
890
fc4099f1
PS
891int ovs_nla_put_tunnel_info(struct sk_buff *skb,
892 struct ip_tunnel_info *tun_info)
8f0aad6f 893{
ba3e2084
DM
894 return __ip_tun_to_nlattr(skb, &tun_info->key,
895 ip_tunnel_info_opts(tun_info),
896 tun_info->options_len,
897 ip_tunnel_info_af(tun_info));
8f0aad6f
WZ
898}
899
018c1dda
EG
900static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
901 const struct nlattr *a[],
902 bool is_mask, bool inner)
903{
904 __be16 tci = 0;
905 __be16 tpid = 0;
906
907 if (a[OVS_KEY_ATTR_VLAN])
908 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
909
910 if (a[OVS_KEY_ATTR_ETHERTYPE])
911 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
912
913 if (likely(!inner)) {
914 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
915 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
916 } else {
917 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
918 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
919 }
920 return 0;
921}
922
923static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
924 u64 key_attrs, bool inner,
925 const struct nlattr **a, bool log)
926{
927 __be16 tci = 0;
928
929 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
930 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
931 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
932 /* Not a VLAN. */
933 return 0;
934 }
935
936 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
937 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
938 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
939 return -EINVAL;
940 }
941
942 if (a[OVS_KEY_ATTR_VLAN])
943 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
944
945 if (!(tci & htons(VLAN_TAG_PRESENT))) {
946 if (tci) {
947 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
948 (inner) ? "C-VLAN" : "VLAN");
949 return -EINVAL;
950 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
951 /* Corner case for truncated VLAN header. */
952 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
953 (inner) ? "C-VLAN" : "VLAN");
954 return -EINVAL;
955 }
956 }
957
958 return 1;
959}
960
961static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
962 u64 key_attrs, bool inner,
963 const struct nlattr **a, bool log)
964{
965 __be16 tci = 0;
966 __be16 tpid = 0;
967 bool encap_valid = !!(match->key->eth.vlan.tci &
968 htons(VLAN_TAG_PRESENT));
969 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
970 htons(VLAN_TAG_PRESENT));
971
972 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
973 /* Not a VLAN. */
974 return 0;
975 }
976
977 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
978 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
979 (inner) ? "C-VLAN" : "VLAN");
980 return -EINVAL;
981 }
982
983 if (a[OVS_KEY_ATTR_VLAN])
984 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
985
986 if (a[OVS_KEY_ATTR_ETHERTYPE])
987 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
988
989 if (tpid != htons(0xffff)) {
990 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
991 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
992 return -EINVAL;
993 }
994 if (!(tci & htons(VLAN_TAG_PRESENT))) {
995 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
996 (inner) ? "C-VLAN" : "VLAN");
997 return -EINVAL;
998 }
999
1000 return 1;
1001}
1002
1003static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1004 u64 *key_attrs, bool inner,
1005 const struct nlattr **a, bool is_mask,
1006 bool log)
1007{
1008 int err;
1009 const struct nlattr *encap;
1010
1011 if (!is_mask)
1012 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1013 a, log);
1014 else
1015 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1016 a, log);
1017 if (err <= 0)
1018 return err;
1019
1020 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1021 if (err)
1022 return err;
1023
1024 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1025 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1026 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1027
1028 encap = a[OVS_KEY_ATTR_ENCAP];
1029
1030 if (!is_mask)
1031 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1032 else
1033 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1034
1035 return err;
1036}
1037
1038static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1039 u64 *key_attrs, const struct nlattr **a,
1040 bool is_mask, bool log)
1041{
1042 int err;
1043 bool encap_valid = false;
1044
1045 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1046 is_mask, log);
1047 if (err)
1048 return err;
1049
1050 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1051 if (encap_valid) {
1052 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1053 is_mask, log);
1054 if (err)
1055 return err;
1056 }
1057
1058 return 0;
1059}
1060
0a6410fb
JB
1061static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1062 u64 *attrs, const struct nlattr **a,
1063 bool is_mask, bool log)
1064{
1065 __be16 eth_type;
1066
1067 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1068 if (is_mask) {
1069 /* Always exact match EtherType. */
1070 eth_type = htons(0xffff);
1071 } else if (!eth_proto_is_802_3(eth_type)) {
1072 OVS_NLERR(log, "EtherType %x is less than min %x",
1073 ntohs(eth_type), ETH_P_802_3_MIN);
1074 return -EINVAL;
1075 }
1076
1077 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1078 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1079 return 0;
1080}
1081
c2ac6673
JS
1082static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1083 u64 *attrs, const struct nlattr **a,
1084 bool is_mask, bool log)
e6445719 1085{
0a6410fb
JB
1086 u8 mac_proto = MAC_PROTO_ETHERNET;
1087
971427f3
AZ
1088 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1089 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1090
1091 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1092 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1093 }
1094
1095 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1096 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1097
1098 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1099 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1100 }
1101
e6445719
PS
1102 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1103 SW_FLOW_KEY_PUT(match, phy.priority,
1104 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1105 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1106 }
1107
1108 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1109 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1110
426cda5c 1111 if (is_mask) {
e6445719 1112 in_port = 0xffffffff; /* Always exact match in_port. */
426cda5c 1113 } else if (in_port >= DP_MAX_PORTS) {
05da5898 1114 OVS_NLERR(log, "Port %d exceeds max allowable %d",
426cda5c 1115 in_port, DP_MAX_PORTS);
e6445719 1116 return -EINVAL;
426cda5c 1117 }
e6445719
PS
1118
1119 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1120 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1121 } else if (!is_mask) {
1122 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1123 }
1124
1125 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1126 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1127
1128 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1129 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1130 }
1131 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
6b26ba3a
JB
1132 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1133 is_mask, log) < 0)
e6445719
PS
1134 return -EINVAL;
1135 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1136 }
7f8a436e
JS
1137
1138 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
c2ac6673 1139 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
fbccce59 1140 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
7f8a436e 1141
9e384715 1142 if (ct_state & ~CT_SUPPORTED_MASK) {
fbccce59 1143 OVS_NLERR(log, "ct_state flags %08x unsupported",
6f225952
JS
1144 ct_state);
1145 return -EINVAL;
1146 }
7f8a436e 1147
316d4d78 1148 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
7f8a436e
JS
1149 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1150 }
1151 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
c2ac6673 1152 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
7f8a436e
JS
1153 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1154
316d4d78 1155 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
7f8a436e
JS
1156 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1157 }
182e3042 1158 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
c2ac6673 1159 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
182e3042
JS
1160 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1161
1162 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1163 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1164 }
33db4125
JS
1165 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1166 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1167 const struct ovs_key_ct_labels *cl;
c2ac6673 1168
33db4125
JS
1169 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1170 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
c2ac6673 1171 sizeof(*cl), is_mask);
33db4125 1172 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
c2ac6673 1173 }
9dd7f890
JR
1174 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1175 const struct ovs_key_ct_tuple_ipv4 *ct;
1176
1177 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1178
1179 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1180 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1181 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1182 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
316d4d78 1183 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
9dd7f890
JR
1184 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1185 }
1186 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1187 const struct ovs_key_ct_tuple_ipv6 *ct;
1188
1189 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1190
1191 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1192 sizeof(match->key->ipv6.ct_orig.src),
1193 is_mask);
1194 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1195 sizeof(match->key->ipv6.ct_orig.dst),
1196 is_mask);
1197 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1198 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
316d4d78 1199 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
9dd7f890
JR
1200 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1201 }
329f45bc 1202
0a6410fb
JB
1203 /* For layer 3 packets the Ethernet type is provided
1204 * and treated as metadata but no MAC addresses are provided.
1205 */
1206 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1207 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1208 mac_proto = MAC_PROTO_NONE;
1209
329f45bc 1210 /* Always exact match mac_proto */
0a6410fb
JB
1211 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1212
1213 if (mac_proto == MAC_PROTO_NONE)
1214 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1215 log);
329f45bc 1216
e6445719
PS
1217 return 0;
1218}
1219
b2d0f5d5
YY
1220int nsh_hdr_from_nlattr(const struct nlattr *attr,
1221 struct nshhdr *nh, size_t size)
1222{
1223 struct nlattr *a;
1224 int rem;
1225 u8 flags = 0;
1226 u8 ttl = 0;
1227 int mdlen = 0;
1228
1229 /* validate_nsh has check this, so we needn't do duplicate check here
1230 */
1231 if (size < NSH_BASE_HDR_LEN)
1232 return -ENOBUFS;
1233
1234 nla_for_each_nested(a, attr, rem) {
1235 int type = nla_type(a);
1236
1237 switch (type) {
1238 case OVS_NSH_KEY_ATTR_BASE: {
1239 const struct ovs_nsh_key_base *base = nla_data(a);
1240
1241 flags = base->flags;
1242 ttl = base->ttl;
1243 nh->np = base->np;
1244 nh->mdtype = base->mdtype;
1245 nh->path_hdr = base->path_hdr;
1246 break;
1247 }
1248 case OVS_NSH_KEY_ATTR_MD1:
1249 mdlen = nla_len(a);
1250 if (mdlen > size - NSH_BASE_HDR_LEN)
1251 return -ENOBUFS;
1252 memcpy(&nh->md1, nla_data(a), mdlen);
1253 break;
1254
1255 case OVS_NSH_KEY_ATTR_MD2:
1256 mdlen = nla_len(a);
1257 if (mdlen > size - NSH_BASE_HDR_LEN)
1258 return -ENOBUFS;
1259 memcpy(&nh->md2, nla_data(a), mdlen);
1260 break;
1261
1262 default:
1263 return -EINVAL;
1264 }
1265 }
1266
1267 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1268 nh->ver_flags_ttl_len = 0;
1269 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1270
1271 return 0;
1272}
1273
1274int nsh_key_from_nlattr(const struct nlattr *attr,
1275 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1276{
1277 struct nlattr *a;
1278 int rem;
1279
1280 /* validate_nsh has check this, so we needn't do duplicate check here
1281 */
1282 nla_for_each_nested(a, attr, rem) {
1283 int type = nla_type(a);
1284
1285 switch (type) {
1286 case OVS_NSH_KEY_ATTR_BASE: {
1287 const struct ovs_nsh_key_base *base = nla_data(a);
1288 const struct ovs_nsh_key_base *base_mask = base + 1;
1289
1290 nsh->base = *base;
1291 nsh_mask->base = *base_mask;
1292 break;
1293 }
1294 case OVS_NSH_KEY_ATTR_MD1: {
1295 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1296 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1297
1298 memcpy(nsh->context, md1->context, sizeof(*md1));
1299 memcpy(nsh_mask->context, md1_mask->context,
1300 sizeof(*md1_mask));
1301 break;
1302 }
1303 case OVS_NSH_KEY_ATTR_MD2:
1304 /* Not supported yet */
1305 return -ENOTSUPP;
1306 default:
1307 return -EINVAL;
1308 }
1309 }
1310
1311 return 0;
1312}
1313
1314static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1315 struct sw_flow_match *match, bool is_mask,
1316 bool is_push_nsh, bool log)
1317{
1318 struct nlattr *a;
1319 int rem;
1320 bool has_base = false;
1321 bool has_md1 = false;
1322 bool has_md2 = false;
1323 u8 mdtype = 0;
1324 int mdlen = 0;
1325
1326 if (WARN_ON(is_push_nsh && is_mask))
1327 return -EINVAL;
1328
1329 nla_for_each_nested(a, attr, rem) {
1330 int type = nla_type(a);
1331 int i;
1332
1333 if (type > OVS_NSH_KEY_ATTR_MAX) {
1334 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1335 type, OVS_NSH_KEY_ATTR_MAX);
1336 return -EINVAL;
1337 }
1338
1339 if (!check_attr_len(nla_len(a),
1340 ovs_nsh_key_attr_lens[type].len)) {
1341 OVS_NLERR(
1342 log,
1343 "nsh attr %d has unexpected len %d expected %d",
1344 type,
1345 nla_len(a),
1346 ovs_nsh_key_attr_lens[type].len
1347 );
1348 return -EINVAL;
1349 }
1350
1351 switch (type) {
1352 case OVS_NSH_KEY_ATTR_BASE: {
1353 const struct ovs_nsh_key_base *base = nla_data(a);
1354
1355 has_base = true;
1356 mdtype = base->mdtype;
1357 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1358 base->flags, is_mask);
1359 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1360 base->ttl, is_mask);
1361 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1362 base->mdtype, is_mask);
1363 SW_FLOW_KEY_PUT(match, nsh.base.np,
1364 base->np, is_mask);
1365 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1366 base->path_hdr, is_mask);
1367 break;
1368 }
1369 case OVS_NSH_KEY_ATTR_MD1: {
1370 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1371
1372 has_md1 = true;
1373 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1374 SW_FLOW_KEY_PUT(match, nsh.context[i],
1375 md1->context[i], is_mask);
1376 break;
1377 }
1378 case OVS_NSH_KEY_ATTR_MD2:
1379 if (!is_push_nsh) /* Not supported MD type 2 yet */
1380 return -ENOTSUPP;
1381
1382 has_md2 = true;
1383 mdlen = nla_len(a);
1384 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1385 OVS_NLERR(
1386 log,
1387 "Invalid MD length %d for MD type %d",
1388 mdlen,
1389 mdtype
1390 );
1391 return -EINVAL;
1392 }
1393 break;
1394 default:
1395 OVS_NLERR(log, "Unknown nsh attribute %d",
1396 type);
1397 return -EINVAL;
1398 }
1399 }
1400
1401 if (rem > 0) {
1402 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1403 return -EINVAL;
1404 }
1405
1406 if (has_md1 && has_md2) {
1407 OVS_NLERR(
1408 1,
1409 "invalid nsh attribute: md1 and md2 are exclusive."
1410 );
1411 return -EINVAL;
1412 }
1413
1414 if (!is_mask) {
1415 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1416 (has_md2 && mdtype != NSH_M_TYPE2)) {
1417 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1418 mdtype);
1419 return -EINVAL;
1420 }
1421
1422 if (is_push_nsh &&
1423 (!has_base || (!has_md1 && !has_md2))) {
1424 OVS_NLERR(
1425 1,
1426 "push_nsh: missing base or metadata attributes"
1427 );
1428 return -EINVAL;
1429 }
1430 }
1431
1432 return 0;
1433}
1434
c2ac6673
JS
1435static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1436 u64 attrs, const struct nlattr **a,
1437 bool is_mask, bool log)
e6445719
PS
1438{
1439 int err;
e6445719 1440
c2ac6673 1441 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
e6445719
PS
1442 if (err)
1443 return err;
1444
1445 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1446 const struct ovs_key_ethernet *eth_key;
1447
1448 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1449 SW_FLOW_KEY_MEMCPY(match, eth.src,
1450 eth_key->eth_src, ETH_ALEN, is_mask);
1451 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1452 eth_key->eth_dst, ETH_ALEN, is_mask);
1453 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
e6445719 1454
0a6410fb
JB
1455 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1456 /* VLAN attribute is always parsed before getting here since it
1457 * may occur multiple times.
1458 */
1459 OVS_NLERR(log, "VLAN attribute unexpected.");
e6445719
PS
1460 return -EINVAL;
1461 }
1462
0a6410fb
JB
1463 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1464 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1465 log);
1466 if (err)
1467 return err;
1468 } else if (!is_mask) {
1469 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1470 }
1471 } else if (!match->key->eth.type) {
1472 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1473 return -EINVAL;
e6445719
PS
1474 }
1475
1476 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1477 const struct ovs_key_ipv4 *ipv4_key;
1478
1479 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1480 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
1481 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1482 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
1483 return -EINVAL;
1484 }
1485 SW_FLOW_KEY_PUT(match, ip.proto,
1486 ipv4_key->ipv4_proto, is_mask);
1487 SW_FLOW_KEY_PUT(match, ip.tos,
1488 ipv4_key->ipv4_tos, is_mask);
1489 SW_FLOW_KEY_PUT(match, ip.ttl,
1490 ipv4_key->ipv4_ttl, is_mask);
1491 SW_FLOW_KEY_PUT(match, ip.frag,
1492 ipv4_key->ipv4_frag, is_mask);
1493 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1494 ipv4_key->ipv4_src, is_mask);
1495 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1496 ipv4_key->ipv4_dst, is_mask);
1497 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1498 }
1499
1500 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1501 const struct ovs_key_ipv6 *ipv6_key;
1502
1503 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1504 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
05da5898
JR
1505 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1506 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
e6445719
PS
1507 return -EINVAL;
1508 }
fecaef85 1509
d3052bb5 1510 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
0ed80da5 1511 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
fecaef85
JR
1512 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1513 return -EINVAL;
1514 }
1515
e6445719
PS
1516 SW_FLOW_KEY_PUT(match, ipv6.label,
1517 ipv6_key->ipv6_label, is_mask);
1518 SW_FLOW_KEY_PUT(match, ip.proto,
1519 ipv6_key->ipv6_proto, is_mask);
1520 SW_FLOW_KEY_PUT(match, ip.tos,
1521 ipv6_key->ipv6_tclass, is_mask);
1522 SW_FLOW_KEY_PUT(match, ip.ttl,
1523 ipv6_key->ipv6_hlimit, is_mask);
1524 SW_FLOW_KEY_PUT(match, ip.frag,
1525 ipv6_key->ipv6_frag, is_mask);
1526 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1527 ipv6_key->ipv6_src,
1528 sizeof(match->key->ipv6.addr.src),
1529 is_mask);
1530 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1531 ipv6_key->ipv6_dst,
1532 sizeof(match->key->ipv6.addr.dst),
1533 is_mask);
1534
1535 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1536 }
1537
1538 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1539 const struct ovs_key_arp *arp_key;
1540
1541 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1542 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
05da5898 1543 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
e6445719
PS
1544 arp_key->arp_op);
1545 return -EINVAL;
1546 }
1547
1548 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1549 arp_key->arp_sip, is_mask);
1550 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1551 arp_key->arp_tip, is_mask);
1552 SW_FLOW_KEY_PUT(match, ip.proto,
1553 ntohs(arp_key->arp_op), is_mask);
1554 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1555 arp_key->arp_sha, ETH_ALEN, is_mask);
1556 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1557 arp_key->arp_tha, ETH_ALEN, is_mask);
1558
1559 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1560 }
1561
b2d0f5d5
YY
1562 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1563 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1564 is_mask, false, log) < 0)
1565 return -EINVAL;
1566 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1567 }
1568
25cd9ba0
SH
1569 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1570 const struct ovs_key_mpls *mpls_key;
1571
1572 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1573 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1574 mpls_key->mpls_lse, is_mask);
1575
1576 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1577 }
1578
e6445719
PS
1579 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1580 const struct ovs_key_tcp *tcp_key;
1581
1582 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1139e241
JR
1583 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1584 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
e6445719
PS
1585 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1586 }
1587
5eb26b15 1588 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1b760fb9
JS
1589 SW_FLOW_KEY_PUT(match, tp.flags,
1590 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1591 is_mask);
5eb26b15
JR
1592 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1593 }
1594
e6445719
PS
1595 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1596 const struct ovs_key_udp *udp_key;
1597
1598 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1139e241
JR
1599 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1600 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
e6445719
PS
1601 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1602 }
1603
1604 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1605 const struct ovs_key_sctp *sctp_key;
1606
1607 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1139e241
JR
1608 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1609 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
e6445719
PS
1610 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1611 }
1612
1613 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1614 const struct ovs_key_icmp *icmp_key;
1615
1616 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1139e241 1617 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1618 htons(icmp_key->icmp_type), is_mask);
1139e241 1619 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1620 htons(icmp_key->icmp_code), is_mask);
1621 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1622 }
1623
1624 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1625 const struct ovs_key_icmpv6 *icmpv6_key;
1626
1627 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1139e241 1628 SW_FLOW_KEY_PUT(match, tp.src,
e6445719 1629 htons(icmpv6_key->icmpv6_type), is_mask);
1139e241 1630 SW_FLOW_KEY_PUT(match, tp.dst,
e6445719
PS
1631 htons(icmpv6_key->icmpv6_code), is_mask);
1632 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1633 }
1634
1635 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1636 const struct ovs_key_nd *nd_key;
1637
1638 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1639 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1640 nd_key->nd_target,
1641 sizeof(match->key->ipv6.nd.target),
1642 is_mask);
1643 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1644 nd_key->nd_sll, ETH_ALEN, is_mask);
1645 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1646 nd_key->nd_tll, ETH_ALEN, is_mask);
1647 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1648 }
1649
426cda5c 1650 if (attrs != 0) {
05da5898 1651 OVS_NLERR(log, "Unknown key attributes %llx",
426cda5c 1652 (unsigned long long)attrs);
e6445719 1653 return -EINVAL;
426cda5c 1654 }
e6445719
PS
1655
1656 return 0;
1657}
1658
81bfe3c3
TG
1659static void nlattr_set(struct nlattr *attr, u8 val,
1660 const struct ovs_len_tbl *tbl)
e6445719 1661{
f47de068
PS
1662 struct nlattr *nla;
1663 int rem;
e6445719 1664
f47de068
PS
1665 /* The nlattr stream should already have been validated */
1666 nla_for_each_nested(nla, attr, rem) {
5984a7cc
SB
1667 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1668 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1669 else
f47de068 1670 memset(nla_data(nla), val, nla_len(nla));
9e384715
JS
1671
1672 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1673 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
f47de068
PS
1674 }
1675}
1676
1677static void mask_set_nlattr(struct nlattr *attr, u8 val)
1678{
81bfe3c3 1679 nlattr_set(attr, val, ovs_key_lens);
e6445719
PS
1680}
1681
1682/**
1683 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1684 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1685 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1686 * does not include any don't care bit.
c2ac6673 1687 * @net: Used to determine per-namespace field support.
e6445719
PS
1688 * @match: receives the extracted flow match information.
1689 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1690 * sequence. The fields should of the packet that triggered the creation
1691 * of this flow.
1692 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1693 * attribute specifies the mask field of the wildcarded flow.
05da5898
JR
1694 * @log: Boolean to allow kernel error logging. Normally true, but when
1695 * probing for feature compatibility this should be passed in as false to
1696 * suppress unnecessary error logging.
e6445719 1697 */
c2ac6673 1698int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
a85311bf 1699 const struct nlattr *nla_key,
05da5898
JR
1700 const struct nlattr *nla_mask,
1701 bool log)
e6445719
PS
1702{
1703 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
f47de068 1704 struct nlattr *newmask = NULL;
e6445719
PS
1705 u64 key_attrs = 0;
1706 u64 mask_attrs = 0;
e6445719
PS
1707 int err;
1708
05da5898 1709 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
e6445719
PS
1710 if (err)
1711 return err;
1712
018c1dda
EG
1713 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1714 if (err)
1715 return err;
e6445719 1716
c2ac6673 1717 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
e6445719
PS
1718 if (err)
1719 return err;
1720
a85311bf
PS
1721 if (match->mask) {
1722 if (!nla_mask) {
1723 /* Create an exact match mask. We need to set to 0xff
1724 * all the 'match->mask' fields that have been touched
1725 * in 'match->key'. We cannot simply memset
1726 * 'match->mask', because padding bytes and fields not
1727 * specified in 'match->key' should be left to 0.
1728 * Instead, we use a stream of netlink attributes,
1729 * copied from 'key' and set to 0xff.
1730 * ovs_key_from_nlattrs() will take care of filling
1731 * 'match->mask' appropriately.
1732 */
1733 newmask = kmemdup(nla_key,
1734 nla_total_size(nla_len(nla_key)),
1735 GFP_KERNEL);
1736 if (!newmask)
1737 return -ENOMEM;
f47de068 1738
a85311bf 1739 mask_set_nlattr(newmask, 0xff);
f47de068 1740
a85311bf
PS
1741 /* The userspace does not send tunnel attributes that
1742 * are 0, but we should not wildcard them nonetheless.
1743 */
00a93bab 1744 if (match->key->tun_proto)
a85311bf
PS
1745 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1746 0xff, true);
f47de068 1747
a85311bf
PS
1748 nla_mask = newmask;
1749 }
f47de068 1750
05da5898 1751 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
e6445719 1752 if (err)
f47de068 1753 goto free_newmask;
e6445719 1754
a85311bf 1755 /* Always match on tci. */
018c1dda
EG
1756 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1757 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
e6445719 1758
018c1dda
EG
1759 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1760 if (err)
1761 goto free_newmask;
e6445719 1762
c2ac6673
JS
1763 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1764 log);
e6445719 1765 if (err)
f47de068 1766 goto free_newmask;
e6445719
PS
1767 }
1768
05da5898 1769 if (!match_validate(match, key_attrs, mask_attrs, log))
f47de068 1770 err = -EINVAL;
e6445719 1771
f47de068
PS
1772free_newmask:
1773 kfree(newmask);
1774 return err;
e6445719
PS
1775}
1776
74ed7ab9
JS
1777static size_t get_ufid_len(const struct nlattr *attr, bool log)
1778{
1779 size_t len;
1780
1781 if (!attr)
1782 return 0;
1783
1784 len = nla_len(attr);
1785 if (len < 1 || len > MAX_UFID_LENGTH) {
1786 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1787 nla_len(attr), MAX_UFID_LENGTH);
1788 return 0;
1789 }
1790
1791 return len;
1792}
1793
1794/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1795 * or false otherwise.
1796 */
1797bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1798 bool log)
1799{
1800 sfid->ufid_len = get_ufid_len(attr, log);
1801 if (sfid->ufid_len)
1802 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1803
1804 return sfid->ufid_len;
1805}
1806
1807int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1808 const struct sw_flow_key *key, bool log)
1809{
1810 struct sw_flow_key *new_key;
1811
1812 if (ovs_nla_get_ufid(sfid, ufid, log))
1813 return 0;
1814
1815 /* If UFID was not provided, use unmasked key. */
1816 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1817 if (!new_key)
1818 return -ENOMEM;
1819 memcpy(new_key, key, sizeof(*key));
1820 sfid->unmasked_key = new_key;
1821
1822 return 0;
1823}
1824
1825u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1826{
1827 return attr ? nla_get_u32(attr) : 0;
1828}
1829
e6445719
PS
1830/**
1831 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
9dd7f890
JR
1832 * @net: Network namespace.
1833 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1834 * metadata.
1835 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1836 * attributes.
1837 * @attrs: Bit mask for the netlink attributes included in @a.
05da5898
JR
1838 * @log: Boolean to allow kernel error logging. Normally true, but when
1839 * probing for feature compatibility this should be passed in as false to
1840 * suppress unnecessary error logging.
e6445719
PS
1841 *
1842 * This parses a series of Netlink attributes that form a flow key, which must
1843 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1844 * get the metadata, that is, the parts of the flow key that cannot be
1845 * extracted from the packet itself.
9dd7f890
JR
1846 *
1847 * This must be called before the packet key fields are filled in 'key'.
e6445719
PS
1848 */
1849
9dd7f890
JR
1850int ovs_nla_get_flow_metadata(struct net *net,
1851 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1852 u64 attrs, struct sw_flow_key *key, bool log)
e6445719 1853{
83c8df26 1854 struct sw_flow_match match;
e6445719
PS
1855
1856 memset(&match, 0, sizeof(match));
83c8df26 1857 match.key = key;
e6445719 1858
316d4d78
JR
1859 key->ct_state = 0;
1860 key->ct_zone = 0;
1861 key->ct_orig_proto = 0;
7f8a436e 1862 memset(&key->ct, 0, sizeof(key->ct));
9dd7f890
JR
1863 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1864 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1865
83c8df26 1866 key->phy.in_port = DP_MAX_PORTS;
e6445719 1867
c2ac6673 1868 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
e6445719
PS
1869}
1870
018c1dda
EG
1871static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1872 bool is_mask)
1873{
1874 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1875
1876 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1877 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1878 return -EMSGSIZE;
1879 return 0;
1880}
1881
b2d0f5d5
YY
1882static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1883 struct sk_buff *skb)
1884{
1885 struct nlattr *start;
1886
1887 start = nla_nest_start(skb, OVS_KEY_ATTR_NSH);
1888 if (!start)
1889 return -EMSGSIZE;
1890
1891 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1892 goto nla_put_failure;
1893
1894 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1895 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1896 sizeof(nsh->context), nsh->context))
1897 goto nla_put_failure;
1898 }
1899
1900 /* Don't support MD type 2 yet */
1901
1902 nla_nest_end(skb, start);
1903
1904 return 0;
1905
1906nla_put_failure:
1907 return -EMSGSIZE;
1908}
1909
5b4237bb
JS
1910static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1911 const struct sw_flow_key *output, bool is_mask,
1912 struct sk_buff *skb)
e6445719
PS
1913{
1914 struct ovs_key_ethernet *eth_key;
018c1dda
EG
1915 struct nlattr *nla;
1916 struct nlattr *encap = NULL;
1917 struct nlattr *in_encap = NULL;
e6445719 1918
971427f3
AZ
1919 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1920 goto nla_put_failure;
1921
1922 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1923 goto nla_put_failure;
1924
e6445719
PS
1925 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1926 goto nla_put_failure;
1927
00a93bab 1928 if ((swkey->tun_proto || is_mask)) {
d91641d9 1929 const void *opts = NULL;
f5796684
JG
1930
1931 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
d91641d9 1932 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
f5796684 1933
6b26ba3a
JB
1934 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1935 swkey->tun_opts_len, swkey->tun_proto))
f5796684
JG
1936 goto nla_put_failure;
1937 }
e6445719
PS
1938
1939 if (swkey->phy.in_port == DP_MAX_PORTS) {
1940 if (is_mask && (output->phy.in_port == 0xffff))
1941 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1942 goto nla_put_failure;
1943 } else {
1944 u16 upper_u16;
1945 upper_u16 = !is_mask ? 0 : 0xffff;
1946
1947 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1948 (upper_u16 << 16) | output->phy.in_port))
1949 goto nla_put_failure;
1950 }
1951
1952 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1953 goto nla_put_failure;
1954
9dd7f890 1955 if (ovs_ct_put_key(swkey, output, skb))
7f8a436e
JS
1956 goto nla_put_failure;
1957
0a6410fb
JB
1958 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1959 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1960 if (!nla)
e6445719 1961 goto nla_put_failure;
018c1dda 1962
0a6410fb
JB
1963 eth_key = nla_data(nla);
1964 ether_addr_copy(eth_key->eth_src, output->eth.src);
1965 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1966
1967 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1968 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
018c1dda 1969 goto nla_put_failure;
0a6410fb
JB
1970 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1971 if (!swkey->eth.vlan.tci)
018c1dda 1972 goto unencap;
0a6410fb
JB
1973
1974 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1975 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1976 goto nla_put_failure;
1977 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1978 if (!swkey->eth.cvlan.tci)
1979 goto unencap;
1980 }
018c1dda 1981 }
e6445719 1982
0a6410fb
JB
1983 if (swkey->eth.type == htons(ETH_P_802_2)) {
1984 /*
1985 * Ethertype 802.2 is represented in the netlink with omitted
1986 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1987 * 0xffff in the mask attribute. Ethertype can also
1988 * be wildcarded.
1989 */
1990 if (is_mask && output->eth.type)
1991 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1992 output->eth.type))
1993 goto nla_put_failure;
1994 goto unencap;
1995 }
e6445719
PS
1996 }
1997
1998 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1999 goto nla_put_failure;
2000
018c1dda
EG
2001 if (eth_type_vlan(swkey->eth.type)) {
2002 /* There are 3 VLAN tags, we don't know anything about the rest
2003 * of the packet, so truncate here.
2004 */
2005 WARN_ON_ONCE(!(encap && in_encap));
2006 goto unencap;
2007 }
2008
e6445719
PS
2009 if (swkey->eth.type == htons(ETH_P_IP)) {
2010 struct ovs_key_ipv4 *ipv4_key;
2011
2012 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2013 if (!nla)
2014 goto nla_put_failure;
2015 ipv4_key = nla_data(nla);
2016 ipv4_key->ipv4_src = output->ipv4.addr.src;
2017 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2018 ipv4_key->ipv4_proto = output->ip.proto;
2019 ipv4_key->ipv4_tos = output->ip.tos;
2020 ipv4_key->ipv4_ttl = output->ip.ttl;
2021 ipv4_key->ipv4_frag = output->ip.frag;
2022 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2023 struct ovs_key_ipv6 *ipv6_key;
2024
2025 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2026 if (!nla)
2027 goto nla_put_failure;
2028 ipv6_key = nla_data(nla);
2029 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2030 sizeof(ipv6_key->ipv6_src));
2031 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2032 sizeof(ipv6_key->ipv6_dst));
2033 ipv6_key->ipv6_label = output->ipv6.label;
2034 ipv6_key->ipv6_proto = output->ip.proto;
2035 ipv6_key->ipv6_tclass = output->ip.tos;
2036 ipv6_key->ipv6_hlimit = output->ip.ttl;
2037 ipv6_key->ipv6_frag = output->ip.frag;
b2d0f5d5
YY
2038 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2039 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2040 goto nla_put_failure;
e6445719
PS
2041 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2042 swkey->eth.type == htons(ETH_P_RARP)) {
2043 struct ovs_key_arp *arp_key;
2044
2045 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2046 if (!nla)
2047 goto nla_put_failure;
2048 arp_key = nla_data(nla);
2049 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2050 arp_key->arp_sip = output->ipv4.addr.src;
2051 arp_key->arp_tip = output->ipv4.addr.dst;
2052 arp_key->arp_op = htons(output->ip.proto);
8c63ff09
JP
2053 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2054 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
25cd9ba0
SH
2055 } else if (eth_p_mpls(swkey->eth.type)) {
2056 struct ovs_key_mpls *mpls_key;
2057
2058 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
2059 if (!nla)
2060 goto nla_put_failure;
2061 mpls_key = nla_data(nla);
2062 mpls_key->mpls_lse = output->mpls.top_lse;
e6445719
PS
2063 }
2064
2065 if ((swkey->eth.type == htons(ETH_P_IP) ||
2066 swkey->eth.type == htons(ETH_P_IPV6)) &&
2067 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2068
2069 if (swkey->ip.proto == IPPROTO_TCP) {
2070 struct ovs_key_tcp *tcp_key;
2071
2072 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2073 if (!nla)
2074 goto nla_put_failure;
2075 tcp_key = nla_data(nla);
1139e241
JR
2076 tcp_key->tcp_src = output->tp.src;
2077 tcp_key->tcp_dst = output->tp.dst;
2078 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2079 output->tp.flags))
2080 goto nla_put_failure;
e6445719
PS
2081 } else if (swkey->ip.proto == IPPROTO_UDP) {
2082 struct ovs_key_udp *udp_key;
2083
2084 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2085 if (!nla)
2086 goto nla_put_failure;
2087 udp_key = nla_data(nla);
1139e241
JR
2088 udp_key->udp_src = output->tp.src;
2089 udp_key->udp_dst = output->tp.dst;
e6445719
PS
2090 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2091 struct ovs_key_sctp *sctp_key;
2092
2093 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2094 if (!nla)
2095 goto nla_put_failure;
2096 sctp_key = nla_data(nla);
1139e241
JR
2097 sctp_key->sctp_src = output->tp.src;
2098 sctp_key->sctp_dst = output->tp.dst;
e6445719
PS
2099 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2100 swkey->ip.proto == IPPROTO_ICMP) {
2101 struct ovs_key_icmp *icmp_key;
2102
2103 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2104 if (!nla)
2105 goto nla_put_failure;
2106 icmp_key = nla_data(nla);
1139e241
JR
2107 icmp_key->icmp_type = ntohs(output->tp.src);
2108 icmp_key->icmp_code = ntohs(output->tp.dst);
e6445719
PS
2109 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2110 swkey->ip.proto == IPPROTO_ICMPV6) {
2111 struct ovs_key_icmpv6 *icmpv6_key;
2112
2113 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2114 sizeof(*icmpv6_key));
2115 if (!nla)
2116 goto nla_put_failure;
2117 icmpv6_key = nla_data(nla);
1139e241
JR
2118 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2119 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
e6445719
PS
2120
2121 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
2122 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
2123 struct ovs_key_nd *nd_key;
2124
2125 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2126 if (!nla)
2127 goto nla_put_failure;
2128 nd_key = nla_data(nla);
2129 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2130 sizeof(nd_key->nd_target));
8c63ff09
JP
2131 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2132 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
e6445719
PS
2133 }
2134 }
2135 }
2136
2137unencap:
018c1dda
EG
2138 if (in_encap)
2139 nla_nest_end(skb, in_encap);
e6445719
PS
2140 if (encap)
2141 nla_nest_end(skb, encap);
2142
2143 return 0;
2144
2145nla_put_failure:
2146 return -EMSGSIZE;
2147}
2148
5b4237bb
JS
2149int ovs_nla_put_key(const struct sw_flow_key *swkey,
2150 const struct sw_flow_key *output, int attr, bool is_mask,
2151 struct sk_buff *skb)
2152{
2153 int err;
2154 struct nlattr *nla;
2155
2156 nla = nla_nest_start(skb, attr);
2157 if (!nla)
2158 return -EMSGSIZE;
2159 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2160 if (err)
2161 return err;
2162 nla_nest_end(skb, nla);
2163
2164 return 0;
2165}
2166
2167/* Called with ovs_mutex or RCU read lock. */
74ed7ab9
JS
2168int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
2169{
2170 if (ovs_identifier_is_ufid(&flow->id))
2171 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2172 flow->id.ufid);
2173
2174 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2175 OVS_FLOW_ATTR_KEY, false, skb);
2176}
2177
2178/* Called with ovs_mutex or RCU read lock. */
2179int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
5b4237bb 2180{
26ad0b83 2181 return ovs_nla_put_key(&flow->key, &flow->key,
5b4237bb
JS
2182 OVS_FLOW_ATTR_KEY, false, skb);
2183}
2184
2185/* Called with ovs_mutex or RCU read lock. */
2186int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2187{
2188 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2189 OVS_FLOW_ATTR_MASK, true, skb);
2190}
2191
e6445719
PS
2192#define MAX_ACTIONS_BUFSIZE (32 * 1024)
2193
67c8d22a 2194static struct sw_flow_actions *nla_alloc_flow_actions(int size)
e6445719
PS
2195{
2196 struct sw_flow_actions *sfa;
2197
67c8d22a 2198 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
e6445719
PS
2199
2200 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2201 if (!sfa)
2202 return ERR_PTR(-ENOMEM);
2203
2204 sfa->actions_len = 0;
2205 return sfa;
2206}
2207
34ae932a
TG
2208static void ovs_nla_free_set_action(const struct nlattr *a)
2209{
2210 const struct nlattr *ovs_key = nla_data(a);
2211 struct ovs_tunnel_info *ovs_tun;
2212
2213 switch (nla_type(ovs_key)) {
2214 case OVS_KEY_ATTR_TUNNEL_INFO:
2215 ovs_tun = nla_data(ovs_key);
2216 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2217 break;
2218 }
2219}
2220
2221void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2222{
2223 const struct nlattr *a;
2224 int rem;
2225
2226 if (!sf_acts)
2227 return;
2228
2229 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
2230 switch (nla_type(a)) {
2231 case OVS_ACTION_ATTR_SET:
2232 ovs_nla_free_set_action(a);
2233 break;
7f8a436e
JS
2234 case OVS_ACTION_ATTR_CT:
2235 ovs_ct_free_action(a);
2236 break;
34ae932a
TG
2237 }
2238 }
2239
2240 kfree(sf_acts);
2241}
2242
2243static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2244{
2245 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2246}
2247
e6445719
PS
2248/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2249 * The caller must hold rcu_read_lock for this to be sensible. */
34ae932a 2250void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
e6445719 2251{
34ae932a 2252 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
e6445719
PS
2253}
2254
2255static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
05da5898 2256 int attr_len, bool log)
e6445719
PS
2257{
2258
2259 struct sw_flow_actions *acts;
2260 int new_acts_size;
5f1ee110 2261 size_t req_size = NLA_ALIGN(attr_len);
e6445719
PS
2262 int next_offset = offsetof(struct sw_flow_actions, actions) +
2263 (*sfa)->actions_len;
2264
2265 if (req_size <= (ksize(*sfa) - next_offset))
2266 goto out;
2267
5f1ee110 2268 new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
e6445719
PS
2269
2270 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
67c8d22a 2271 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) {
2272 OVS_NLERR(log, "Flow action size exceeds max %u",
2273 MAX_ACTIONS_BUFSIZE);
e6445719 2274 return ERR_PTR(-EMSGSIZE);
67c8d22a 2275 }
e6445719
PS
2276 new_acts_size = MAX_ACTIONS_BUFSIZE;
2277 }
2278
67c8d22a 2279 acts = nla_alloc_flow_actions(new_acts_size);
e6445719
PS
2280 if (IS_ERR(acts))
2281 return (void *)acts;
2282
2283 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2284 acts->actions_len = (*sfa)->actions_len;
8e2fed1c 2285 acts->orig_len = (*sfa)->orig_len;
e6445719
PS
2286 kfree(*sfa);
2287 *sfa = acts;
2288
2289out:
2290 (*sfa)->actions_len += req_size;
2291 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2292}
2293
f0b128c1 2294static struct nlattr *__add_action(struct sw_flow_actions **sfa,
05da5898 2295 int attrtype, void *data, int len, bool log)
e6445719
PS
2296{
2297 struct nlattr *a;
2298
05da5898 2299 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
e6445719 2300 if (IS_ERR(a))
f0b128c1 2301 return a;
e6445719
PS
2302
2303 a->nla_type = attrtype;
2304 a->nla_len = nla_attr_size(len);
2305
2306 if (data)
2307 memcpy(nla_data(a), data, len);
2308 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2309
f0b128c1
JG
2310 return a;
2311}
2312
7f8a436e
JS
2313int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2314 int len, bool log)
f0b128c1
JG
2315{
2316 struct nlattr *a;
2317
05da5898 2318 a = __add_action(sfa, attrtype, data, len, log);
f0b128c1 2319
f35423c1 2320 return PTR_ERR_OR_ZERO(a);
e6445719
PS
2321}
2322
2323static inline int add_nested_action_start(struct sw_flow_actions **sfa,
05da5898 2324 int attrtype, bool log)
e6445719
PS
2325{
2326 int used = (*sfa)->actions_len;
2327 int err;
2328
7f8a436e 2329 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
e6445719
PS
2330 if (err)
2331 return err;
2332
2333 return used;
2334}
2335
2336static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2337 int st_offset)
2338{
2339 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2340 st_offset);
2341
2342 a->nla_len = sfa->actions_len - st_offset;
2343}
2344
7f8a436e 2345static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0 2346 const struct sw_flow_key *key,
798c1661 2347 struct sw_flow_actions **sfa,
05da5898 2348 __be16 eth_type, __be16 vlan_tci, bool log);
25cd9ba0 2349
7f8a436e 2350static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
798c1661 2351 const struct sw_flow_key *key,
25cd9ba0 2352 struct sw_flow_actions **sfa,
798c1661 2353 __be16 eth_type, __be16 vlan_tci,
2354 bool log, bool last)
e6445719
PS
2355{
2356 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2357 const struct nlattr *probability, *actions;
2358 const struct nlattr *a;
798c1661 2359 int rem, start, err;
2360 struct sample_arg arg;
e6445719
PS
2361
2362 memset(attrs, 0, sizeof(attrs));
2363 nla_for_each_nested(a, attr, rem) {
2364 int type = nla_type(a);
2365 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2366 return -EINVAL;
2367 attrs[type] = a;
2368 }
2369 if (rem)
2370 return -EINVAL;
2371
2372 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2373 if (!probability || nla_len(probability) != sizeof(u32))
2374 return -EINVAL;
2375
2376 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2377 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2378 return -EINVAL;
2379
2380 /* validation done, copy sample action. */
05da5898 2381 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
e6445719
PS
2382 if (start < 0)
2383 return start;
798c1661 2384
2385 /* When both skb and flow may be changed, put the sample
2386 * into a deferred fifo. On the other hand, if only skb
2387 * may be modified, the actions can be executed in place.
2388 *
2389 * Do this analysis at the flow installation time.
2390 * Set 'clone_action->exec' to true if the actions can be
2391 * executed without being deferred.
2392 *
2393 * If the sample is the last action, it can always be excuted
2394 * rather than deferred.
2395 */
2396 arg.exec = last || !actions_may_change_flow(actions);
2397 arg.probability = nla_get_u32(probability);
2398
2399 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2400 log);
e6445719
PS
2401 if (err)
2402 return err;
e6445719 2403
798c1661 2404 err = __ovs_nla_copy_actions(net, actions, key, sfa,
05da5898 2405 eth_type, vlan_tci, log);
798c1661 2406
e6445719
PS
2407 if (err)
2408 return err;
2409
e6445719
PS
2410 add_nested_action_end(*sfa, start);
2411
2412 return 0;
2413}
2414
e6445719
PS
2415void ovs_match_init(struct sw_flow_match *match,
2416 struct sw_flow_key *key,
2279994d 2417 bool reset_key,
e6445719
PS
2418 struct sw_flow_mask *mask)
2419{
2420 memset(match, 0, sizeof(*match));
2421 match->key = key;
2422 match->mask = mask;
2423
2279994d 2424 if (reset_key)
2425 memset(key, 0, sizeof(*key));
e6445719
PS
2426
2427 if (mask) {
2428 memset(&mask->key, 0, sizeof(mask->key));
2429 mask->range.start = mask->range.end = 0;
2430 }
2431}
2432
d91641d9
TG
2433static int validate_geneve_opts(struct sw_flow_key *key)
2434{
2435 struct geneve_opt *option;
2436 int opts_len = key->tun_opts_len;
2437 bool crit_opt = false;
2438
2439 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2440 while (opts_len > 0) {
2441 int len;
2442
2443 if (opts_len < sizeof(*option))
2444 return -EINVAL;
2445
2446 len = sizeof(*option) + option->length * 4;
2447 if (len > opts_len)
2448 return -EINVAL;
2449
2450 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2451
2452 option = (struct geneve_opt *)((u8 *)option + len);
2453 opts_len -= len;
2454 };
2455
2456 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2457
2458 return 0;
2459}
2460
e6445719 2461static int validate_and_copy_set_tun(const struct nlattr *attr,
05da5898 2462 struct sw_flow_actions **sfa, bool log)
e6445719
PS
2463{
2464 struct sw_flow_match match;
2465 struct sw_flow_key key;
34ae932a 2466 struct metadata_dst *tun_dst;
1d8fff90 2467 struct ip_tunnel_info *tun_info;
34ae932a 2468 struct ovs_tunnel_info *ovs_tun;
f0b128c1 2469 struct nlattr *a;
13101602 2470 int err = 0, start, opts_type;
e6445719 2471
2279994d 2472 ovs_match_init(&match, &key, true, NULL);
6b26ba3a 2473 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
1dd144cf
TG
2474 if (opts_type < 0)
2475 return opts_type;
e6445719 2476
f5796684 2477 if (key.tun_opts_len) {
1dd144cf
TG
2478 switch (opts_type) {
2479 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2480 err = validate_geneve_opts(&key);
2481 if (err < 0)
2482 return err;
2483 break;
2484 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2485 break;
2486 }
f5796684
JG
2487 };
2488
05da5898 2489 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
e6445719
PS
2490 if (start < 0)
2491 return start;
2492
3fcece12
JK
2493 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2494 GFP_KERNEL);
2495
34ae932a
TG
2496 if (!tun_dst)
2497 return -ENOMEM;
2498
d71785ff
PA
2499 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2500 if (err) {
2501 dst_release((struct dst_entry *)tun_dst);
2502 return err;
2503 }
2504
f0b128c1 2505 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
34ae932a
TG
2506 sizeof(*ovs_tun), log);
2507 if (IS_ERR(a)) {
2508 dst_release((struct dst_entry *)tun_dst);
f0b128c1 2509 return PTR_ERR(a);
34ae932a
TG
2510 }
2511
2512 ovs_tun = nla_data(a);
2513 ovs_tun->tun_dst = tun_dst;
f0b128c1 2514
34ae932a
TG
2515 tun_info = &tun_dst->u.tun_info;
2516 tun_info->mode = IP_TUNNEL_INFO_TX;
00a93bab
JB
2517 if (key.tun_proto == AF_INET6)
2518 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
1d8fff90 2519 tun_info->key = key.tun_key;
f0b128c1 2520
4c222798
PS
2521 /* We need to store the options in the action itself since
2522 * everything else will go away after flow setup. We can append
2523 * it to tun_info and then point there.
2524 */
2525 ip_tunnel_info_opts_set(tun_info,
2526 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2527 key.tun_opts_len);
e6445719
PS
2528 add_nested_action_end(*sfa, start);
2529
2530 return err;
2531}
2532
b2d0f5d5
YY
2533static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2534 bool is_push_nsh, bool log)
2535{
2536 struct sw_flow_match match;
2537 struct sw_flow_key key;
2538 int ret = 0;
2539
2540 ovs_match_init(&match, &key, true, NULL);
2541 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2542 is_push_nsh, log);
2543 return !ret;
2544}
2545
83d2b9ba
JR
2546/* Return false if there are any non-masked bits set.
2547 * Mask follows data immediately, before any netlink padding.
2548 */
2549static bool validate_masked(u8 *data, int len)
2550{
2551 u8 *mask = data + len;
2552
2553 while (len--)
2554 if (*data++ & ~*mask++)
2555 return false;
2556
2557 return true;
2558}
2559
e6445719
PS
2560static int validate_set(const struct nlattr *a,
2561 const struct sw_flow_key *flow_key,
0a6410fb
JB
2562 struct sw_flow_actions **sfa, bool *skip_copy,
2563 u8 mac_proto, __be16 eth_type, bool masked, bool log)
e6445719
PS
2564{
2565 const struct nlattr *ovs_key = nla_data(a);
2566 int key_type = nla_type(ovs_key);
83d2b9ba 2567 size_t key_len;
e6445719
PS
2568
2569 /* There can be only one key in a action */
2570 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2571 return -EINVAL;
2572
83d2b9ba
JR
2573 key_len = nla_len(ovs_key);
2574 if (masked)
2575 key_len /= 2;
2576
e6445719 2577 if (key_type > OVS_KEY_ATTR_MAX ||
982b5270 2578 !check_attr_len(key_len, ovs_key_lens[key_type].len))
e6445719
PS
2579 return -EINVAL;
2580
83d2b9ba
JR
2581 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2582 return -EINVAL;
2583
e6445719
PS
2584 switch (key_type) {
2585 const struct ovs_key_ipv4 *ipv4_key;
2586 const struct ovs_key_ipv6 *ipv6_key;
2587 int err;
2588
2589 case OVS_KEY_ATTR_PRIORITY:
2590 case OVS_KEY_ATTR_SKB_MARK:
182e3042 2591 case OVS_KEY_ATTR_CT_MARK:
33db4125 2592 case OVS_KEY_ATTR_CT_LABELS:
e6445719
PS
2593 break;
2594
0a6410fb
JB
2595 case OVS_KEY_ATTR_ETHERNET:
2596 if (mac_proto != MAC_PROTO_ETHERNET)
2597 return -EINVAL;
87e159c5 2598 break;
0a6410fb 2599
e6445719 2600 case OVS_KEY_ATTR_TUNNEL:
83d2b9ba
JR
2601 if (masked)
2602 return -EINVAL; /* Masked tunnel set not supported. */
2603
2604 *skip_copy = true;
05da5898 2605 err = validate_and_copy_set_tun(a, sfa, log);
e6445719
PS
2606 if (err)
2607 return err;
2608 break;
2609
2610 case OVS_KEY_ATTR_IPV4:
25cd9ba0 2611 if (eth_type != htons(ETH_P_IP))
e6445719
PS
2612 return -EINVAL;
2613
e6445719 2614 ipv4_key = nla_data(ovs_key);
e6445719 2615
83d2b9ba
JR
2616 if (masked) {
2617 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
e6445719 2618
83d2b9ba
JR
2619 /* Non-writeable fields. */
2620 if (mask->ipv4_proto || mask->ipv4_frag)
2621 return -EINVAL;
2622 } else {
2623 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2624 return -EINVAL;
2625
2626 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2627 return -EINVAL;
2628 }
e6445719
PS
2629 break;
2630
2631 case OVS_KEY_ATTR_IPV6:
25cd9ba0 2632 if (eth_type != htons(ETH_P_IPV6))
e6445719
PS
2633 return -EINVAL;
2634
e6445719 2635 ipv6_key = nla_data(ovs_key);
e6445719 2636
83d2b9ba
JR
2637 if (masked) {
2638 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2639
2640 /* Non-writeable fields. */
2641 if (mask->ipv6_proto || mask->ipv6_frag)
2642 return -EINVAL;
2643
2644 /* Invalid bits in the flow label mask? */
2645 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2646 return -EINVAL;
2647 } else {
2648 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2649 return -EINVAL;
e6445719 2650
83d2b9ba
JR
2651 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2652 return -EINVAL;
2653 }
e6445719
PS
2654 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2655 return -EINVAL;
2656
2657 break;
2658
2659 case OVS_KEY_ATTR_TCP:
83d2b9ba
JR
2660 if ((eth_type != htons(ETH_P_IP) &&
2661 eth_type != htons(ETH_P_IPV6)) ||
2662 flow_key->ip.proto != IPPROTO_TCP)
e6445719
PS
2663 return -EINVAL;
2664
83d2b9ba 2665 break;
e6445719
PS
2666
2667 case OVS_KEY_ATTR_UDP:
83d2b9ba
JR
2668 if ((eth_type != htons(ETH_P_IP) &&
2669 eth_type != htons(ETH_P_IPV6)) ||
2670 flow_key->ip.proto != IPPROTO_UDP)
e6445719
PS
2671 return -EINVAL;
2672
83d2b9ba 2673 break;
25cd9ba0
SH
2674
2675 case OVS_KEY_ATTR_MPLS:
2676 if (!eth_p_mpls(eth_type))
2677 return -EINVAL;
2678 break;
e6445719
PS
2679
2680 case OVS_KEY_ATTR_SCTP:
83d2b9ba
JR
2681 if ((eth_type != htons(ETH_P_IP) &&
2682 eth_type != htons(ETH_P_IPV6)) ||
2683 flow_key->ip.proto != IPPROTO_SCTP)
e6445719
PS
2684 return -EINVAL;
2685
83d2b9ba 2686 break;
e6445719 2687
b2d0f5d5
YY
2688 case OVS_KEY_ATTR_NSH:
2689 if (eth_type != htons(ETH_P_NSH))
2690 return -EINVAL;
2691 if (!validate_nsh(nla_data(a), masked, false, log))
2692 return -EINVAL;
2693 break;
2694
e6445719
PS
2695 default:
2696 return -EINVAL;
2697 }
2698
83d2b9ba
JR
2699 /* Convert non-masked non-tunnel set actions to masked set actions. */
2700 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2701 int start, len = key_len * 2;
2702 struct nlattr *at;
2703
2704 *skip_copy = true;
2705
2706 start = add_nested_action_start(sfa,
2707 OVS_ACTION_ATTR_SET_TO_MASKED,
2708 log);
2709 if (start < 0)
2710 return start;
2711
2712 at = __add_action(sfa, key_type, NULL, len, log);
2713 if (IS_ERR(at))
2714 return PTR_ERR(at);
2715
2716 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2717 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2718 /* Clear non-writeable bits from otherwise writeable fields. */
2719 if (key_type == OVS_KEY_ATTR_IPV6) {
2720 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2721
2722 mask->ipv6_label &= htonl(0x000FFFFF);
2723 }
2724 add_nested_action_end(*sfa, start);
2725 }
2726
e6445719
PS
2727 return 0;
2728}
2729
2730static int validate_userspace(const struct nlattr *attr)
2731{
2732 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2733 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2734 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
8f0aad6f 2735 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
e6445719
PS
2736 };
2737 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2738 int error;
2739
fceb6435
JB
2740 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2741 userspace_policy, NULL);
e6445719
PS
2742 if (error)
2743 return error;
2744
2745 if (!a[OVS_USERSPACE_ATTR_PID] ||
2746 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2747 return -EINVAL;
2748
2749 return 0;
2750}
2751
2752static int copy_action(const struct nlattr *from,
05da5898 2753 struct sw_flow_actions **sfa, bool log)
e6445719
PS
2754{
2755 int totlen = NLA_ALIGN(from->nla_len);
2756 struct nlattr *to;
2757
05da5898 2758 to = reserve_sfa_size(sfa, from->nla_len, log);
e6445719
PS
2759 if (IS_ERR(to))
2760 return PTR_ERR(to);
2761
2762 memcpy(to, from, totlen);
2763 return 0;
2764}
2765
7f8a436e 2766static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0 2767 const struct sw_flow_key *key,
798c1661 2768 struct sw_flow_actions **sfa,
05da5898 2769 __be16 eth_type, __be16 vlan_tci, bool log)
e6445719 2770{
0a6410fb 2771 u8 mac_proto = ovs_key_mac_proto(key);
e6445719
PS
2772 const struct nlattr *a;
2773 int rem, err;
2774
e6445719
PS
2775 nla_for_each_nested(a, attr, rem) {
2776 /* Expected argument lengths, (u32)-1 for variable length. */
2777 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2778 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
971427f3 2779 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
e6445719 2780 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
25cd9ba0
SH
2781 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2782 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
e6445719
PS
2783 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2784 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2785 [OVS_ACTION_ATTR_SET] = (u32)-1,
83d2b9ba 2786 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
971427f3 2787 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
7f8a436e
JS
2788 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2789 [OVS_ACTION_ATTR_CT] = (u32)-1,
b8226962 2790 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
f2a4d086 2791 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
91820da6
JB
2792 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2793 [OVS_ACTION_ATTR_POP_ETH] = 0,
b2d0f5d5
YY
2794 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
2795 [OVS_ACTION_ATTR_POP_NSH] = 0,
cd8a6c33 2796 [OVS_ACTION_ATTR_METER] = sizeof(u32),
e6445719
PS
2797 };
2798 const struct ovs_action_push_vlan *vlan;
2799 int type = nla_type(a);
2800 bool skip_copy;
2801
2802 if (type > OVS_ACTION_ATTR_MAX ||
2803 (action_lens[type] != nla_len(a) &&
2804 action_lens[type] != (u32)-1))
2805 return -EINVAL;
2806
2807 skip_copy = false;
2808 switch (type) {
2809 case OVS_ACTION_ATTR_UNSPEC:
2810 return -EINVAL;
2811
2812 case OVS_ACTION_ATTR_USERSPACE:
2813 err = validate_userspace(a);
2814 if (err)
2815 return err;
2816 break;
2817
2818 case OVS_ACTION_ATTR_OUTPUT:
2819 if (nla_get_u32(a) >= DP_MAX_PORTS)
2820 return -EINVAL;
2821 break;
2822
f2a4d086
WT
2823 case OVS_ACTION_ATTR_TRUNC: {
2824 const struct ovs_action_trunc *trunc = nla_data(a);
2825
2826 if (trunc->max_len < ETH_HLEN)
2827 return -EINVAL;
2828 break;
2829 }
2830
971427f3
AZ
2831 case OVS_ACTION_ATTR_HASH: {
2832 const struct ovs_action_hash *act_hash = nla_data(a);
2833
2834 switch (act_hash->hash_alg) {
2835 case OVS_HASH_ALG_L4:
2836 break;
2837 default:
2838 return -EINVAL;
2839 }
2840
2841 break;
2842 }
e6445719
PS
2843
2844 case OVS_ACTION_ATTR_POP_VLAN:
0a6410fb
JB
2845 if (mac_proto != MAC_PROTO_ETHERNET)
2846 return -EINVAL;
25cd9ba0 2847 vlan_tci = htons(0);
e6445719
PS
2848 break;
2849
2850 case OVS_ACTION_ATTR_PUSH_VLAN:
0a6410fb
JB
2851 if (mac_proto != MAC_PROTO_ETHERNET)
2852 return -EINVAL;
e6445719 2853 vlan = nla_data(a);
018c1dda 2854 if (!eth_type_vlan(vlan->vlan_tpid))
e6445719
PS
2855 return -EINVAL;
2856 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2857 return -EINVAL;
25cd9ba0 2858 vlan_tci = vlan->vlan_tci;
e6445719
PS
2859 break;
2860
971427f3
AZ
2861 case OVS_ACTION_ATTR_RECIRC:
2862 break;
2863
25cd9ba0
SH
2864 case OVS_ACTION_ATTR_PUSH_MPLS: {
2865 const struct ovs_action_push_mpls *mpls = nla_data(a);
2866
25cd9ba0
SH
2867 if (!eth_p_mpls(mpls->mpls_ethertype))
2868 return -EINVAL;
2869 /* Prohibit push MPLS other than to a white list
2870 * for packets that have a known tag order.
2871 */
2872 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2873 (eth_type != htons(ETH_P_IP) &&
2874 eth_type != htons(ETH_P_IPV6) &&
2875 eth_type != htons(ETH_P_ARP) &&
2876 eth_type != htons(ETH_P_RARP) &&
2877 !eth_p_mpls(eth_type)))
2878 return -EINVAL;
2879 eth_type = mpls->mpls_ethertype;
2880 break;
2881 }
2882
2883 case OVS_ACTION_ATTR_POP_MPLS:
2884 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2885 !eth_p_mpls(eth_type))
2886 return -EINVAL;
2887
2888 /* Disallow subsequent L2.5+ set and mpls_pop actions
2889 * as there is no check here to ensure that the new
2890 * eth_type is valid and thus set actions could
2891 * write off the end of the packet or otherwise
2892 * corrupt it.
2893 *
2894 * Support for these actions is planned using packet
2895 * recirculation.
2896 */
2897 eth_type = htons(0);
2898 break;
2899
e6445719 2900 case OVS_ACTION_ATTR_SET:
25cd9ba0 2901 err = validate_set(a, key, sfa,
0a6410fb
JB
2902 &skip_copy, mac_proto, eth_type,
2903 false, log);
83d2b9ba
JR
2904 if (err)
2905 return err;
2906 break;
2907
2908 case OVS_ACTION_ATTR_SET_MASKED:
2909 err = validate_set(a, key, sfa,
0a6410fb
JB
2910 &skip_copy, mac_proto, eth_type,
2911 true, log);
e6445719
PS
2912 if (err)
2913 return err;
2914 break;
2915
798c1661 2916 case OVS_ACTION_ATTR_SAMPLE: {
2917 bool last = nla_is_last(a, rem);
2918
2919 err = validate_and_copy_sample(net, a, key, sfa,
2920 eth_type, vlan_tci,
2921 log, last);
e6445719
PS
2922 if (err)
2923 return err;
2924 skip_copy = true;
2925 break;
798c1661 2926 }
e6445719 2927
7f8a436e
JS
2928 case OVS_ACTION_ATTR_CT:
2929 err = ovs_ct_copy_action(net, a, key, sfa, log);
2930 if (err)
2931 return err;
2932 skip_copy = true;
2933 break;
2934
b8226962
EG
2935 case OVS_ACTION_ATTR_CT_CLEAR:
2936 break;
2937
91820da6
JB
2938 case OVS_ACTION_ATTR_PUSH_ETH:
2939 /* Disallow pushing an Ethernet header if one
2940 * is already present */
2941 if (mac_proto != MAC_PROTO_NONE)
2942 return -EINVAL;
ef92f8d7 2943 mac_proto = MAC_PROTO_ETHERNET;
91820da6
JB
2944 break;
2945
2946 case OVS_ACTION_ATTR_POP_ETH:
2947 if (mac_proto != MAC_PROTO_ETHERNET)
2948 return -EINVAL;
2949 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2950 return -EINVAL;
ef92f8d7 2951 mac_proto = MAC_PROTO_NONE;
91820da6
JB
2952 break;
2953
b2d0f5d5
YY
2954 case OVS_ACTION_ATTR_PUSH_NSH:
2955 if (mac_proto != MAC_PROTO_ETHERNET) {
2956 u8 next_proto;
2957
2958 next_proto = tun_p_from_eth_p(eth_type);
2959 if (!next_proto)
2960 return -EINVAL;
2961 }
2962 mac_proto = MAC_PROTO_NONE;
2963 if (!validate_nsh(nla_data(a), false, true, true))
2964 return -EINVAL;
2965 break;
2966
2967 case OVS_ACTION_ATTR_POP_NSH: {
2968 __be16 inner_proto;
2969
2970 if (eth_type != htons(ETH_P_NSH))
2971 return -EINVAL;
2972 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
2973 if (!inner_proto)
2974 return -EINVAL;
2975 if (key->nsh.base.np == TUN_P_ETHERNET)
2976 mac_proto = MAC_PROTO_ETHERNET;
2977 else
2978 mac_proto = MAC_PROTO_NONE;
2979 break;
2980 }
2981
cd8a6c33
AZ
2982 case OVS_ACTION_ATTR_METER:
2983 /* Non-existent meters are simply ignored. */
2984 break;
2985
e6445719 2986 default:
05da5898 2987 OVS_NLERR(log, "Unknown Action type %d", type);
e6445719
PS
2988 return -EINVAL;
2989 }
2990 if (!skip_copy) {
05da5898 2991 err = copy_action(a, sfa, log);
e6445719
PS
2992 if (err)
2993 return err;
2994 }
2995 }
2996
2997 if (rem > 0)
2998 return -EINVAL;
2999
3000 return 0;
3001}
3002
83d2b9ba 3003/* 'key' must be the masked key. */
7f8a436e 3004int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
25cd9ba0 3005 const struct sw_flow_key *key,
05da5898 3006 struct sw_flow_actions **sfa, bool log)
25cd9ba0 3007{
2fdb957d
PS
3008 int err;
3009
67c8d22a 3010 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
2fdb957d
PS
3011 if (IS_ERR(*sfa))
3012 return PTR_ERR(*sfa);
3013
8e2fed1c 3014 (*sfa)->orig_len = nla_len(attr);
798c1661 3015 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
018c1dda 3016 key->eth.vlan.tci, log);
2fdb957d 3017 if (err)
34ae932a 3018 ovs_nla_free_flow_actions(*sfa);
2fdb957d
PS
3019
3020 return err;
25cd9ba0
SH
3021}
3022
798c1661 3023static int sample_action_to_attr(const struct nlattr *attr,
3024 struct sk_buff *skb)
e6445719 3025{
798c1661 3026 struct nlattr *start, *ac_start = NULL, *sample_arg;
3027 int err = 0, rem = nla_len(attr);
3028 const struct sample_arg *arg;
3029 struct nlattr *actions;
e6445719
PS
3030
3031 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
3032 if (!start)
3033 return -EMSGSIZE;
3034
798c1661 3035 sample_arg = nla_data(attr);
3036 arg = nla_data(sample_arg);
3037 actions = nla_next(sample_arg, &rem);
e6445719 3038
798c1661 3039 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3040 err = -EMSGSIZE;
3041 goto out;
3042 }
3043
3044 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
3045 if (!ac_start) {
3046 err = -EMSGSIZE;
3047 goto out;
3048 }
3049
3050 err = ovs_nla_put_actions(actions, rem, skb);
3051
3052out:
3053 if (err) {
3054 nla_nest_cancel(skb, ac_start);
3055 nla_nest_cancel(skb, start);
3056 } else {
3057 nla_nest_end(skb, ac_start);
3058 nla_nest_end(skb, start);
e6445719
PS
3059 }
3060
e6445719
PS
3061 return err;
3062}
3063
3064static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3065{
3066 const struct nlattr *ovs_key = nla_data(a);
3067 int key_type = nla_type(ovs_key);
3068 struct nlattr *start;
3069 int err;
3070
3071 switch (key_type) {
f0b128c1 3072 case OVS_KEY_ATTR_TUNNEL_INFO: {
34ae932a
TG
3073 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3074 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
f0b128c1 3075
e6445719
PS
3076 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3077 if (!start)
3078 return -EMSGSIZE;
3079
e905eabc
SH
3080 err = ip_tun_to_nlattr(skb, &tun_info->key,
3081 ip_tunnel_info_opts(tun_info),
3082 tun_info->options_len,
3083 ip_tunnel_info_af(tun_info));
e6445719
PS
3084 if (err)
3085 return err;
3086 nla_nest_end(skb, start);
3087 break;
f0b128c1 3088 }
e6445719
PS
3089 default:
3090 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3091 return -EMSGSIZE;
3092 break;
3093 }
3094
3095 return 0;
3096}
3097
83d2b9ba
JR
3098static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3099 struct sk_buff *skb)
3100{
3101 const struct nlattr *ovs_key = nla_data(a);
f4f8e738 3102 struct nlattr *nla;
83d2b9ba
JR
3103 size_t key_len = nla_len(ovs_key) / 2;
3104
3105 /* Revert the conversion we did from a non-masked set action to
3106 * masked set action.
3107 */
f4f8e738
JS
3108 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
3109 if (!nla)
83d2b9ba
JR
3110 return -EMSGSIZE;
3111
f4f8e738
JS
3112 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3113 return -EMSGSIZE;
3114
3115 nla_nest_end(skb, nla);
83d2b9ba
JR
3116 return 0;
3117}
3118
e6445719
PS
3119int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3120{
3121 const struct nlattr *a;
3122 int rem, err;
3123
3124 nla_for_each_attr(a, attr, len, rem) {
3125 int type = nla_type(a);
3126
3127 switch (type) {
3128 case OVS_ACTION_ATTR_SET:
3129 err = set_action_to_attr(a, skb);
3130 if (err)
3131 return err;
3132 break;
3133
83d2b9ba
JR
3134 case OVS_ACTION_ATTR_SET_TO_MASKED:
3135 err = masked_set_action_to_set_action_attr(a, skb);
3136 if (err)
3137 return err;
3138 break;
3139
e6445719
PS
3140 case OVS_ACTION_ATTR_SAMPLE:
3141 err = sample_action_to_attr(a, skb);
3142 if (err)
3143 return err;
3144 break;
7f8a436e
JS
3145
3146 case OVS_ACTION_ATTR_CT:
3147 err = ovs_ct_action_to_attr(nla_data(a), skb);
3148 if (err)
3149 return err;
3150 break;
3151
e6445719
PS
3152 default:
3153 if (nla_put(skb, type, nla_len(a), nla_data(a)))
3154 return -EMSGSIZE;
3155 break;
3156 }
3157 }
3158
3159 return 0;
3160}