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