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