5c32cd00918ca98fc71b5612fb0a22dbc486727e
[sliver-openvswitch.git] / datapath / flow_netlink.c
1 /*
2  * Copyright (c) 2007-2013 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 "flow.h"
22 #include "datapath.h"
23 #include <linux/uaccess.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <net/llc_pdu.h>
29 #include <linux/kernel.h>
30 #include <linux/jhash.h>
31 #include <linux/jiffies.h>
32 #include <linux/llc.h>
33 #include <linux/module.h>
34 #include <linux/in.h>
35 #include <linux/rcupdate.h>
36 #include <linux/if_arp.h>
37 #include <linux/ip.h>
38 #include <linux/ipv6.h>
39 #include <linux/sctp.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/icmp.h>
43 #include <linux/icmpv6.h>
44 #include <linux/rculist.h>
45 #include <net/ip.h>
46 #include <net/ip_tunnels.h>
47 #include <net/ipv6.h>
48 #include <net/ndisc.h>
49
50 #include "flow_netlink.h"
51
52 static void update_range__(struct sw_flow_match *match,
53                            size_t offset, size_t size, bool is_mask)
54 {
55         struct sw_flow_key_range *range = NULL;
56         size_t start = rounddown(offset, sizeof(long));
57         size_t end = roundup(offset + size, sizeof(long));
58
59         if (!is_mask)
60                 range = &match->range;
61         else if (match->mask)
62                 range = &match->mask->range;
63
64         if (!range)
65                 return;
66
67         if (range->start == range->end) {
68                 range->start = start;
69                 range->end = end;
70                 return;
71         }
72
73         if (range->start > start)
74                 range->start = start;
75
76         if (range->end < end)
77                 range->end = end;
78 }
79
80 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
81         do { \
82                 update_range__(match, offsetof(struct sw_flow_key, field),  \
83                                      sizeof((match)->key->field), is_mask); \
84                 if (is_mask) {                                              \
85                         if ((match)->mask)                                  \
86                                 (match)->mask->key.field = value;           \
87                 } else {                                                    \
88                         (match)->key->field = value;                        \
89                 }                                                           \
90         } while (0)
91
92 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
93         do { \
94                 update_range__(match, offsetof(struct sw_flow_key, field),  \
95                                 len, is_mask);                              \
96                 if (is_mask) {                                              \
97                         if ((match)->mask)                                  \
98                                 memcpy(&(match)->mask->key.field, value_p, len);\
99                 } else {                                                    \
100                         memcpy(&(match)->key->field, value_p, len);         \
101                 }                                                           \
102         } while (0)
103
104 static u16 range_n_bytes(const struct sw_flow_key_range *range)
105 {
106         return range->end - range->start;
107 }
108
109 static bool match_validate(const struct sw_flow_match *match,
110                            u64 key_attrs, u64 mask_attrs)
111 {
112         u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
113         u64 mask_allowed = key_attrs;  /* At most allow all key attributes */
114
115         /* The following mask attributes allowed only if they
116          * pass the validation tests. */
117         mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
118                         | (1ULL << OVS_KEY_ATTR_IPV6)
119                         | (1ULL << OVS_KEY_ATTR_TCP)
120                         | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
121                         | (1ULL << OVS_KEY_ATTR_UDP)
122                         | (1ULL << OVS_KEY_ATTR_SCTP)
123                         | (1ULL << OVS_KEY_ATTR_ICMP)
124                         | (1ULL << OVS_KEY_ATTR_ICMPV6)
125                         | (1ULL << OVS_KEY_ATTR_ARP)
126                         | (1ULL << OVS_KEY_ATTR_ND));
127
128         /* Always allowed mask fields. */
129         mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
130                        | (1ULL << OVS_KEY_ATTR_IN_PORT)
131                        | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
132
133         /* Check key attributes. */
134         if (match->key->eth.type == htons(ETH_P_ARP)
135                         || match->key->eth.type == htons(ETH_P_RARP)) {
136                 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
137                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
138                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
139         }
140
141         if (match->key->eth.type == htons(ETH_P_IP)) {
142                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
143                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
145
146                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
147                         if (match->key->ip.proto == IPPROTO_UDP) {
148                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
149                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
150                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
151                         }
152
153                         if (match->key->ip.proto == IPPROTO_SCTP) {
154                                 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
155                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
156                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
157                         }
158
159                         if (match->key->ip.proto == IPPROTO_TCP) {
160                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
161                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
162                                 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
163                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
164                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
165                                 }
166                         }
167
168                         if (match->key->ip.proto == IPPROTO_ICMP) {
169                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
170                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
171                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
172                         }
173                 }
174         }
175
176         if (match->key->eth.type == htons(ETH_P_IPV6)) {
177                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
178                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
179                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
180
181                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
182                         if (match->key->ip.proto == IPPROTO_UDP) {
183                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
184                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
185                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
186                         }
187
188                         if (match->key->ip.proto == IPPROTO_SCTP) {
189                                 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
190                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
191                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
192                         }
193
194                         if (match->key->ip.proto == IPPROTO_TCP) {
195                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
196                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
197                                 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
198                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
199                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
200                                 }
201                         }
202
203                         if (match->key->ip.proto == IPPROTO_ICMPV6) {
204                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
205                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
206                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
207
208                                 if (match->key->tp.src ==
209                                                 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
210                                     match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
211                                         key_expected |= 1ULL << OVS_KEY_ATTR_ND;
212                                         if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
213                                                 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
214                                 }
215                         }
216                 }
217         }
218
219         if ((key_attrs & key_expected) != key_expected) {
220                 /* Key attributes check failed. */
221                 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
222                                 (unsigned long long)key_attrs, (unsigned long long)key_expected);
223                 return false;
224         }
225
226         if ((mask_attrs & mask_allowed) != mask_attrs) {
227                 /* Mask attributes check failed. */
228                 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
229                                 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
230                 return false;
231         }
232
233         return true;
234 }
235
236 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
237 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
238         [OVS_KEY_ATTR_ENCAP] = -1,
239         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
240         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
241         [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
242         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
243         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
244         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
245         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
246         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
247         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
248         [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
249         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
250         [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
251         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
252         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
253         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
254         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
255         [OVS_KEY_ATTR_TUNNEL] = -1,
256 };
257
258 static bool is_all_zero(const u8 *fp, size_t size)
259 {
260         int i;
261
262         if (!fp)
263                 return false;
264
265         for (i = 0; i < size; i++)
266                 if (fp[i])
267                         return false;
268
269         return true;
270 }
271
272 static int __parse_flow_nlattrs(const struct nlattr *attr,
273                                 const struct nlattr *a[],
274                                 u64 *attrsp, bool nz)
275 {
276         const struct nlattr *nla;
277         u64 attrs;
278         int rem;
279
280         attrs = *attrsp;
281         nla_for_each_nested(nla, attr, rem) {
282                 u16 type = nla_type(nla);
283                 int expected_len;
284
285                 if (type > OVS_KEY_ATTR_MAX) {
286                         OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
287                                   type, OVS_KEY_ATTR_MAX);
288                         return -EINVAL;
289                 }
290
291                 if (attrs & (1ULL << type)) {
292                         OVS_NLERR("Duplicate key attribute (type %d).\n", type);
293                         return -EINVAL;
294                 }
295
296                 expected_len = ovs_key_lens[type];
297                 if (nla_len(nla) != expected_len && expected_len != -1) {
298                         OVS_NLERR("Key attribute has unexpected length (type=%d"
299                                   ", length=%d, expected=%d).\n", type,
300                                   nla_len(nla), expected_len);
301                         return -EINVAL;
302                 }
303
304                 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
305                         attrs |= 1ULL << type;
306                         a[type] = nla;
307                 }
308         }
309         if (rem) {
310                 OVS_NLERR("Message has %d unknown bytes.\n", rem);
311                 return -EINVAL;
312         }
313
314         *attrsp = attrs;
315         return 0;
316 }
317
318 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
319                                    const struct nlattr *a[], u64 *attrsp)
320 {
321         return __parse_flow_nlattrs(attr, a, attrsp, true);
322 }
323
324 static int parse_flow_nlattrs(const struct nlattr *attr,
325                               const struct nlattr *a[], u64 *attrsp)
326 {
327         return __parse_flow_nlattrs(attr, a, attrsp, false);
328 }
329
330 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
331                                 struct sw_flow_match *match, bool is_mask)
332 {
333         struct nlattr *a;
334         int rem;
335         bool ttl = false;
336         __be16 tun_flags = 0;
337
338         nla_for_each_nested(a, attr, rem) {
339                 int type = nla_type(a);
340                 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
341                         [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
342                         [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
343                         [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
344                         [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
345                         [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
346                         [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
347                         [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
348                 };
349
350                 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
351                         OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
352                         type, OVS_TUNNEL_KEY_ATTR_MAX);
353                         return -EINVAL;
354                 }
355
356                 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
357                         OVS_NLERR("IPv4 tunnel attribute type has unexpected "
358                                   " length (type=%d, length=%d, expected=%d).\n",
359                                   type, nla_len(a), ovs_tunnel_key_lens[type]);
360                         return -EINVAL;
361                 }
362
363                 switch (type) {
364                 case OVS_TUNNEL_KEY_ATTR_ID:
365                         SW_FLOW_KEY_PUT(match, tun_key.tun_id,
366                                         nla_get_be64(a), is_mask);
367                         tun_flags |= TUNNEL_KEY;
368                         break;
369                 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
370                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
371                                         nla_get_be32(a), is_mask);
372                         break;
373                 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
374                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
375                                         nla_get_be32(a), is_mask);
376                         break;
377                 case OVS_TUNNEL_KEY_ATTR_TOS:
378                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
379                                         nla_get_u8(a), is_mask);
380                         break;
381                 case OVS_TUNNEL_KEY_ATTR_TTL:
382                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
383                                         nla_get_u8(a), is_mask);
384                         ttl = true;
385                         break;
386                 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
387                         tun_flags |= TUNNEL_DONT_FRAGMENT;
388                         break;
389                 case OVS_TUNNEL_KEY_ATTR_CSUM:
390                         tun_flags |= TUNNEL_CSUM;
391                         break;
392                 default:
393                         return -EINVAL;
394                 }
395         }
396
397         SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
398
399         if (rem > 0) {
400                 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
401                 return -EINVAL;
402         }
403
404         if (!is_mask) {
405                 if (!match->key->tun_key.ipv4_dst) {
406                         OVS_NLERR("IPv4 tunnel destination address is zero.\n");
407                         return -EINVAL;
408                 }
409
410                 if (!ttl) {
411                         OVS_NLERR("IPv4 tunnel TTL not specified.\n");
412                         return -EINVAL;
413                 }
414         }
415
416         return 0;
417 }
418
419 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
420                               const struct ovs_key_ipv4_tunnel *tun_key,
421                               const struct ovs_key_ipv4_tunnel *output)
422 {
423         struct nlattr *nla;
424
425         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
426         if (!nla)
427                 return -EMSGSIZE;
428
429         if (output->tun_flags & TUNNEL_KEY &&
430             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
431                 return -EMSGSIZE;
432         if (output->ipv4_src &&
433                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
434                 return -EMSGSIZE;
435         if (output->ipv4_dst &&
436                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
437                 return -EMSGSIZE;
438         if (output->ipv4_tos &&
439                 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
440                 return -EMSGSIZE;
441         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
442                 return -EMSGSIZE;
443         if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
444                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
445                 return -EMSGSIZE;
446         if ((output->tun_flags & TUNNEL_CSUM) &&
447                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
448                 return -EMSGSIZE;
449
450         nla_nest_end(skb, nla);
451         return 0;
452 }
453
454
455 static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
456                                  const struct nlattr **a, bool is_mask)
457 {
458         if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
459                 SW_FLOW_KEY_PUT(match, phy.priority,
460                           nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
461                 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
462         }
463
464         if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
465                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
466
467                 if (is_mask)
468                         in_port = 0xffffffff; /* Always exact match in_port. */
469                 else if (in_port >= DP_MAX_PORTS)
470                         return -EINVAL;
471
472                 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
473                 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
474         } else if (!is_mask) {
475                 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
476         }
477
478         if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
479                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
480
481                 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
482                 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
483         }
484         if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
485                 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
486                                          is_mask))
487                         return -EINVAL;
488                 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
489         }
490         return 0;
491 }
492
493 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
494                                 const struct nlattr **a, bool is_mask)
495 {
496         int err;
497         u64 orig_attrs = attrs;
498
499         err = metadata_from_nlattrs(match, &attrs, a, is_mask);
500         if (err)
501                 return err;
502
503         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
504                 const struct ovs_key_ethernet *eth_key;
505
506                 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
507                 SW_FLOW_KEY_MEMCPY(match, eth.src,
508                                 eth_key->eth_src, ETH_ALEN, is_mask);
509                 SW_FLOW_KEY_MEMCPY(match, eth.dst,
510                                 eth_key->eth_dst, ETH_ALEN, is_mask);
511                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
512         }
513
514         if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
515                 __be16 tci;
516
517                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
518                 if (!(tci & htons(VLAN_TAG_PRESENT))) {
519                         if (is_mask)
520                                 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
521                         else
522                                 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
523
524                         return -EINVAL;
525                 }
526
527                 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
528                 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
529         } else if (!is_mask)
530                 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
531
532         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
533                 __be16 eth_type;
534
535                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
536                 if (is_mask) {
537                         /* Always exact match EtherType. */
538                         eth_type = htons(0xffff);
539                 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
540                         OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
541                                         ntohs(eth_type), ETH_P_802_3_MIN);
542                         return -EINVAL;
543                 }
544
545                 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
546                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
547         } else if (!is_mask) {
548                 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
549         }
550
551         if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
552                 const struct ovs_key_ipv4 *ipv4_key;
553
554                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
555                 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
556                         OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
557                                 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
558                         return -EINVAL;
559                 }
560                 SW_FLOW_KEY_PUT(match, ip.proto,
561                                 ipv4_key->ipv4_proto, is_mask);
562                 SW_FLOW_KEY_PUT(match, ip.tos,
563                                 ipv4_key->ipv4_tos, is_mask);
564                 SW_FLOW_KEY_PUT(match, ip.ttl,
565                                 ipv4_key->ipv4_ttl, is_mask);
566                 SW_FLOW_KEY_PUT(match, ip.frag,
567                                 ipv4_key->ipv4_frag, is_mask);
568                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
569                                 ipv4_key->ipv4_src, is_mask);
570                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
571                                 ipv4_key->ipv4_dst, is_mask);
572                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
573         }
574
575         if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
576                 const struct ovs_key_ipv6 *ipv6_key;
577
578                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
579                 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
580                         OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
581                                 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
582                         return -EINVAL;
583                 }
584                 SW_FLOW_KEY_PUT(match, ipv6.label,
585                                 ipv6_key->ipv6_label, is_mask);
586                 SW_FLOW_KEY_PUT(match, ip.proto,
587                                 ipv6_key->ipv6_proto, is_mask);
588                 SW_FLOW_KEY_PUT(match, ip.tos,
589                                 ipv6_key->ipv6_tclass, is_mask);
590                 SW_FLOW_KEY_PUT(match, ip.ttl,
591                                 ipv6_key->ipv6_hlimit, is_mask);
592                 SW_FLOW_KEY_PUT(match, ip.frag,
593                                 ipv6_key->ipv6_frag, is_mask);
594                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
595                                 ipv6_key->ipv6_src,
596                                 sizeof(match->key->ipv6.addr.src),
597                                 is_mask);
598                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
599                                 ipv6_key->ipv6_dst,
600                                 sizeof(match->key->ipv6.addr.dst),
601                                 is_mask);
602
603                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
604         }
605
606         if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
607                 const struct ovs_key_arp *arp_key;
608
609                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
610                 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
611                         OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
612                                   arp_key->arp_op);
613                         return -EINVAL;
614                 }
615
616                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
617                                 arp_key->arp_sip, is_mask);
618                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
619                         arp_key->arp_tip, is_mask);
620                 SW_FLOW_KEY_PUT(match, ip.proto,
621                                 ntohs(arp_key->arp_op), is_mask);
622                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
623                                 arp_key->arp_sha, ETH_ALEN, is_mask);
624                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
625                                 arp_key->arp_tha, ETH_ALEN, is_mask);
626
627                 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
628         }
629
630         if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
631                 const struct ovs_key_tcp *tcp_key;
632
633                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
634                 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
635                 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
636                 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
637         }
638
639         if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
640                 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
641                         SW_FLOW_KEY_PUT(match, tp.flags,
642                                         nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
643                                         is_mask);
644                 } else {
645                         SW_FLOW_KEY_PUT(match, tp.flags,
646                                         nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
647                                         is_mask);
648                 }
649                 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
650         }
651
652         if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
653                 const struct ovs_key_udp *udp_key;
654
655                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
656                 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
657                 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
658                 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
659         }
660
661         if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
662                 const struct ovs_key_sctp *sctp_key;
663
664                 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
665                 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
666                 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
667                 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
668         }
669
670         if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
671                 const struct ovs_key_icmp *icmp_key;
672
673                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
674                 SW_FLOW_KEY_PUT(match, tp.src,
675                                 htons(icmp_key->icmp_type), is_mask);
676                 SW_FLOW_KEY_PUT(match, tp.dst,
677                                 htons(icmp_key->icmp_code), is_mask);
678                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
679         }
680
681         if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
682                 const struct ovs_key_icmpv6 *icmpv6_key;
683
684                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
685                 SW_FLOW_KEY_PUT(match, tp.src,
686                                 htons(icmpv6_key->icmpv6_type), is_mask);
687                 SW_FLOW_KEY_PUT(match, tp.dst,
688                                 htons(icmpv6_key->icmpv6_code), is_mask);
689                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
690         }
691
692         if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
693                 const struct ovs_key_nd *nd_key;
694
695                 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
696                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
697                         nd_key->nd_target,
698                         sizeof(match->key->ipv6.nd.target),
699                         is_mask);
700                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
701                         nd_key->nd_sll, ETH_ALEN, is_mask);
702                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
703                                 nd_key->nd_tll, ETH_ALEN, is_mask);
704                 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
705         }
706
707         if (attrs != 0)
708                 return -EINVAL;
709
710         return 0;
711 }
712
713 static void sw_flow_mask_set(struct sw_flow_mask *mask,
714                              struct sw_flow_key_range *range, u8 val)
715 {
716         u8 *m = (u8 *)&mask->key + range->start;
717
718         mask->range = *range;
719         memset(m, val, range_n_bytes(range));
720 }
721
722 /**
723  * ovs_nla_get_match - parses Netlink attributes into a flow key and
724  * mask. In case the 'mask' is NULL, the flow is treated as exact match
725  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
726  * does not include any don't care bit.
727  * @match: receives the extracted flow match information.
728  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
729  * sequence. The fields should of the packet that triggered the creation
730  * of this flow.
731  * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
732  * attribute specifies the mask field of the wildcarded flow.
733  */
734 int ovs_nla_get_match(struct sw_flow_match *match,
735                       const struct nlattr *key,
736                       const struct nlattr *mask)
737 {
738         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
739         const struct nlattr *encap;
740         u64 key_attrs = 0;
741         u64 mask_attrs = 0;
742         bool encap_valid = false;
743         int err;
744
745         err = parse_flow_nlattrs(key, a, &key_attrs);
746         if (err)
747                 return err;
748
749         if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
750             (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
751             (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
752                 __be16 tci;
753
754                 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
755                       (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
756                         OVS_NLERR("Invalid Vlan frame.\n");
757                         return -EINVAL;
758                 }
759
760                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
761                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
762                 encap = a[OVS_KEY_ATTR_ENCAP];
763                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
764                 encap_valid = true;
765
766                 if (tci & htons(VLAN_TAG_PRESENT)) {
767                         err = parse_flow_nlattrs(encap, a, &key_attrs);
768                         if (err)
769                                 return err;
770                 } else if (!tci) {
771                         /* Corner case for truncated 802.1Q header. */
772                         if (nla_len(encap)) {
773                                 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
774                                 return -EINVAL;
775                         }
776                 } else {
777                         OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
778                         return  -EINVAL;
779                 }
780         }
781
782         err = ovs_key_from_nlattrs(match, key_attrs, a, false);
783         if (err)
784                 return err;
785
786         if (mask) {
787                 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
788                 if (err)
789                         return err;
790
791                 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP)  {
792                         __be16 eth_type = 0;
793                         __be16 tci = 0;
794
795                         if (!encap_valid) {
796                                 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
797                                 return  -EINVAL;
798                         }
799
800                         mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
801                         if (a[OVS_KEY_ATTR_ETHERTYPE])
802                                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
803
804                         if (eth_type == htons(0xffff)) {
805                                 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
806                                 encap = a[OVS_KEY_ATTR_ENCAP];
807                                 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
808                         } else {
809                                 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
810                                                 ntohs(eth_type));
811                                 return -EINVAL;
812                         }
813
814                         if (a[OVS_KEY_ATTR_VLAN])
815                                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
816
817                         if (!(tci & htons(VLAN_TAG_PRESENT))) {
818                                 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
819                                 return -EINVAL;
820                         }
821                 }
822
823                 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
824                 if (err)
825                         return err;
826         } else {
827                 /* Populate exact match flow's key mask. */
828                 if (match->mask)
829                         sw_flow_mask_set(match->mask, &match->range, 0xff);
830         }
831
832         if (!match_validate(match, key_attrs, mask_attrs))
833                 return -EINVAL;
834
835         return 0;
836 }
837
838 /**
839  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
840  * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
841  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
842  * sequence.
843  *
844  * This parses a series of Netlink attributes that form a flow key, which must
845  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
846  * get the metadata, that is, the parts of the flow key that cannot be
847  * extracted from the packet itself.
848  */
849
850 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
851                               const struct nlattr *attr)
852 {
853         struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
854         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
855         u64 attrs = 0;
856         int err;
857         struct sw_flow_match match;
858
859         flow->key.phy.in_port = DP_MAX_PORTS;
860         flow->key.phy.priority = 0;
861         flow->key.phy.skb_mark = 0;
862         memset(tun_key, 0, sizeof(flow->key.tun_key));
863
864         err = parse_flow_nlattrs(attr, a, &attrs);
865         if (err)
866                 return -EINVAL;
867
868         memset(&match, 0, sizeof(match));
869         match.key = &flow->key;
870
871         err = metadata_from_nlattrs(&match, &attrs, a, false);
872         if (err)
873                 return err;
874
875         return 0;
876 }
877
878 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
879                      const struct sw_flow_key *output, struct sk_buff *skb)
880 {
881         struct ovs_key_ethernet *eth_key;
882         struct nlattr *nla, *encap;
883         bool is_mask = (swkey != output);
884
885         if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
886                 goto nla_put_failure;
887
888         if ((swkey->tun_key.ipv4_dst || is_mask) &&
889             ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
890                 goto nla_put_failure;
891
892         if (swkey->phy.in_port == DP_MAX_PORTS) {
893                 if (is_mask && (output->phy.in_port == 0xffff))
894                         if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
895                                 goto nla_put_failure;
896         } else {
897                 u16 upper_u16;
898                 upper_u16 = !is_mask ? 0 : 0xffff;
899
900                 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
901                                 (upper_u16 << 16) | output->phy.in_port))
902                         goto nla_put_failure;
903         }
904
905         if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
906                 goto nla_put_failure;
907
908         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
909         if (!nla)
910                 goto nla_put_failure;
911
912         eth_key = nla_data(nla);
913         ether_addr_copy(eth_key->eth_src, output->eth.src);
914         ether_addr_copy(eth_key->eth_dst, output->eth.dst);
915
916         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
917                 __be16 eth_type;
918                 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
919                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
920                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
921                         goto nla_put_failure;
922                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
923                 if (!swkey->eth.tci)
924                         goto unencap;
925         } else
926                 encap = NULL;
927
928         if (swkey->eth.type == htons(ETH_P_802_2)) {
929                 /*
930                  * Ethertype 802.2 is represented in the netlink with omitted
931                  * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
932                  * 0xffff in the mask attribute.  Ethertype can also
933                  * be wildcarded.
934                  */
935                 if (is_mask && output->eth.type)
936                         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
937                                                 output->eth.type))
938                                 goto nla_put_failure;
939                 goto unencap;
940         }
941
942         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
943                 goto nla_put_failure;
944
945         if (swkey->eth.type == htons(ETH_P_IP)) {
946                 struct ovs_key_ipv4 *ipv4_key;
947
948                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
949                 if (!nla)
950                         goto nla_put_failure;
951                 ipv4_key = nla_data(nla);
952                 ipv4_key->ipv4_src = output->ipv4.addr.src;
953                 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
954                 ipv4_key->ipv4_proto = output->ip.proto;
955                 ipv4_key->ipv4_tos = output->ip.tos;
956                 ipv4_key->ipv4_ttl = output->ip.ttl;
957                 ipv4_key->ipv4_frag = output->ip.frag;
958         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
959                 struct ovs_key_ipv6 *ipv6_key;
960
961                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
962                 if (!nla)
963                         goto nla_put_failure;
964                 ipv6_key = nla_data(nla);
965                 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
966                                 sizeof(ipv6_key->ipv6_src));
967                 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
968                                 sizeof(ipv6_key->ipv6_dst));
969                 ipv6_key->ipv6_label = output->ipv6.label;
970                 ipv6_key->ipv6_proto = output->ip.proto;
971                 ipv6_key->ipv6_tclass = output->ip.tos;
972                 ipv6_key->ipv6_hlimit = output->ip.ttl;
973                 ipv6_key->ipv6_frag = output->ip.frag;
974         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
975                    swkey->eth.type == htons(ETH_P_RARP)) {
976                 struct ovs_key_arp *arp_key;
977
978                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
979                 if (!nla)
980                         goto nla_put_failure;
981                 arp_key = nla_data(nla);
982                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
983                 arp_key->arp_sip = output->ipv4.addr.src;
984                 arp_key->arp_tip = output->ipv4.addr.dst;
985                 arp_key->arp_op = htons(output->ip.proto);
986                 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
987                 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
988         }
989
990         if ((swkey->eth.type == htons(ETH_P_IP) ||
991              swkey->eth.type == htons(ETH_P_IPV6)) &&
992              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
993
994                 if (swkey->ip.proto == IPPROTO_TCP) {
995                         struct ovs_key_tcp *tcp_key;
996
997                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
998                         if (!nla)
999                                 goto nla_put_failure;
1000                         tcp_key = nla_data(nla);
1001                         tcp_key->tcp_src = output->tp.src;
1002                         tcp_key->tcp_dst = output->tp.dst;
1003                         if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1004                                          output->tp.flags))
1005                                 goto nla_put_failure;
1006                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1007                         struct ovs_key_udp *udp_key;
1008
1009                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1010                         if (!nla)
1011                                 goto nla_put_failure;
1012                         udp_key = nla_data(nla);
1013                         udp_key->udp_src = output->tp.src;
1014                         udp_key->udp_dst = output->tp.dst;
1015                 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1016                         struct ovs_key_sctp *sctp_key;
1017
1018                         nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1019                         if (!nla)
1020                                 goto nla_put_failure;
1021                         sctp_key = nla_data(nla);
1022                         sctp_key->sctp_src = output->tp.src;
1023                         sctp_key->sctp_dst = output->tp.dst;
1024                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1025                            swkey->ip.proto == IPPROTO_ICMP) {
1026                         struct ovs_key_icmp *icmp_key;
1027
1028                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1029                         if (!nla)
1030                                 goto nla_put_failure;
1031                         icmp_key = nla_data(nla);
1032                         icmp_key->icmp_type = ntohs(output->tp.src);
1033                         icmp_key->icmp_code = ntohs(output->tp.dst);
1034                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1035                            swkey->ip.proto == IPPROTO_ICMPV6) {
1036                         struct ovs_key_icmpv6 *icmpv6_key;
1037
1038                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1039                                                 sizeof(*icmpv6_key));
1040                         if (!nla)
1041                                 goto nla_put_failure;
1042                         icmpv6_key = nla_data(nla);
1043                         icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1044                         icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1045
1046                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1047                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1048                                 struct ovs_key_nd *nd_key;
1049
1050                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1051                                 if (!nla)
1052                                         goto nla_put_failure;
1053                                 nd_key = nla_data(nla);
1054                                 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1055                                                         sizeof(nd_key->nd_target));
1056                                 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1057                                 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1058                         }
1059                 }
1060         }
1061
1062 unencap:
1063         if (encap)
1064                 nla_nest_end(skb, encap);
1065
1066         return 0;
1067
1068 nla_put_failure:
1069         return -EMSGSIZE;
1070 }
1071
1072 #define MAX_ACTIONS_BUFSIZE     (32 * 1024)
1073
1074 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1075 {
1076         struct sw_flow_actions *sfa;
1077
1078         if (size > MAX_ACTIONS_BUFSIZE)
1079                 return ERR_PTR(-EINVAL);
1080
1081         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1082         if (!sfa)
1083                 return ERR_PTR(-ENOMEM);
1084
1085         sfa->actions_len = 0;
1086         return sfa;
1087 }
1088
1089 /* RCU callback used by ovs_nla_free_flow_actions. */
1090 static void rcu_free_acts_callback(struct rcu_head *rcu)
1091 {
1092         struct sw_flow_actions *sf_acts = container_of(rcu,
1093                         struct sw_flow_actions, rcu);
1094         kfree(sf_acts);
1095 }
1096
1097 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1098  * The caller must hold rcu_read_lock for this to be sensible. */
1099 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1100 {
1101         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1102 }
1103
1104 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1105                                        int attr_len)
1106 {
1107
1108         struct sw_flow_actions *acts;
1109         int new_acts_size;
1110         int req_size = NLA_ALIGN(attr_len);
1111         int next_offset = offsetof(struct sw_flow_actions, actions) +
1112                                         (*sfa)->actions_len;
1113
1114         if (req_size <= (ksize(*sfa) - next_offset))
1115                 goto out;
1116
1117         new_acts_size = ksize(*sfa) * 2;
1118
1119         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1120                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1121                         return ERR_PTR(-EMSGSIZE);
1122                 new_acts_size = MAX_ACTIONS_BUFSIZE;
1123         }
1124
1125         acts = ovs_nla_alloc_flow_actions(new_acts_size);
1126         if (IS_ERR(acts))
1127                 return (void *)acts;
1128
1129         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1130         acts->actions_len = (*sfa)->actions_len;
1131         kfree(*sfa);
1132         *sfa = acts;
1133
1134 out:
1135         (*sfa)->actions_len += req_size;
1136         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1137 }
1138
1139 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1140 {
1141         struct nlattr *a;
1142
1143         a = reserve_sfa_size(sfa, nla_attr_size(len));
1144         if (IS_ERR(a))
1145                 return PTR_ERR(a);
1146
1147         a->nla_type = attrtype;
1148         a->nla_len = nla_attr_size(len);
1149
1150         if (data)
1151                 memcpy(nla_data(a), data, len);
1152         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1153
1154         return 0;
1155 }
1156
1157 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1158                                           int attrtype)
1159 {
1160         int used = (*sfa)->actions_len;
1161         int err;
1162
1163         err = add_action(sfa, attrtype, NULL, 0);
1164         if (err)
1165                 return err;
1166
1167         return used;
1168 }
1169
1170 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1171                                          int st_offset)
1172 {
1173         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1174                                                                st_offset);
1175
1176         a->nla_len = sfa->actions_len - st_offset;
1177 }
1178
1179 static int validate_and_copy_sample(const struct nlattr *attr,
1180                                     const struct sw_flow_key *key, int depth,
1181                                     struct sw_flow_actions **sfa)
1182 {
1183         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1184         const struct nlattr *probability, *actions;
1185         const struct nlattr *a;
1186         int rem, start, err, st_acts;
1187
1188         memset(attrs, 0, sizeof(attrs));
1189         nla_for_each_nested(a, attr, rem) {
1190                 int type = nla_type(a);
1191                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1192                         return -EINVAL;
1193                 attrs[type] = a;
1194         }
1195         if (rem)
1196                 return -EINVAL;
1197
1198         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1199         if (!probability || nla_len(probability) != sizeof(u32))
1200                 return -EINVAL;
1201
1202         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1203         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1204                 return -EINVAL;
1205
1206         /* validation done, copy sample action. */
1207         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1208         if (start < 0)
1209                 return start;
1210         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1211                          nla_data(probability), sizeof(u32));
1212         if (err)
1213                 return err;
1214         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1215         if (st_acts < 0)
1216                 return st_acts;
1217
1218         err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1219         if (err)
1220                 return err;
1221
1222         add_nested_action_end(*sfa, st_acts);
1223         add_nested_action_end(*sfa, start);
1224
1225         return 0;
1226 }
1227
1228 static int validate_tp_port(const struct sw_flow_key *flow_key)
1229 {
1230         if ((flow_key->eth.type == htons(ETH_P_IP) ||
1231              flow_key->eth.type == htons(ETH_P_IPV6)) &&
1232             (flow_key->tp.src || flow_key->tp.dst))
1233                 return 0;
1234
1235         return -EINVAL;
1236 }
1237
1238 void ovs_match_init(struct sw_flow_match *match,
1239                     struct sw_flow_key *key,
1240                     struct sw_flow_mask *mask)
1241 {
1242         memset(match, 0, sizeof(*match));
1243         match->key = key;
1244         match->mask = mask;
1245
1246         memset(key, 0, sizeof(*key));
1247
1248         if (mask) {
1249                 memset(&mask->key, 0, sizeof(mask->key));
1250                 mask->range.start = mask->range.end = 0;
1251         }
1252 }
1253
1254 static int validate_and_copy_set_tun(const struct nlattr *attr,
1255                                      struct sw_flow_actions **sfa)
1256 {
1257         struct sw_flow_match match;
1258         struct sw_flow_key key;
1259         int err, start;
1260
1261         ovs_match_init(&match, &key, NULL);
1262         err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1263         if (err)
1264                 return err;
1265
1266         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1267         if (start < 0)
1268                 return start;
1269
1270         err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1271                         sizeof(match.key->tun_key));
1272         add_nested_action_end(*sfa, start);
1273
1274         return err;
1275 }
1276
1277 static int validate_set(const struct nlattr *a,
1278                         const struct sw_flow_key *flow_key,
1279                         struct sw_flow_actions **sfa,
1280                         bool *set_tun)
1281 {
1282         const struct nlattr *ovs_key = nla_data(a);
1283         int key_type = nla_type(ovs_key);
1284
1285         /* There can be only one key in a action */
1286         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1287                 return -EINVAL;
1288
1289         if (key_type > OVS_KEY_ATTR_MAX ||
1290             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1291              ovs_key_lens[key_type] != -1))
1292                 return -EINVAL;
1293
1294         switch (key_type) {
1295         const struct ovs_key_ipv4 *ipv4_key;
1296         const struct ovs_key_ipv6 *ipv6_key;
1297         int err;
1298
1299         case OVS_KEY_ATTR_PRIORITY:
1300         case OVS_KEY_ATTR_SKB_MARK:
1301         case OVS_KEY_ATTR_ETHERNET:
1302                 break;
1303
1304         case OVS_KEY_ATTR_TUNNEL:
1305                 *set_tun = true;
1306                 err = validate_and_copy_set_tun(a, sfa);
1307                 if (err)
1308                         return err;
1309                 break;
1310
1311         case OVS_KEY_ATTR_IPV4:
1312                 if (flow_key->eth.type != htons(ETH_P_IP))
1313                         return -EINVAL;
1314
1315                 if (!flow_key->ip.proto)
1316                         return -EINVAL;
1317
1318                 ipv4_key = nla_data(ovs_key);
1319                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1320                         return -EINVAL;
1321
1322                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1323                         return -EINVAL;
1324
1325                 break;
1326
1327         case OVS_KEY_ATTR_IPV6:
1328                 if (flow_key->eth.type != htons(ETH_P_IPV6))
1329                         return -EINVAL;
1330
1331                 if (!flow_key->ip.proto)
1332                         return -EINVAL;
1333
1334                 ipv6_key = nla_data(ovs_key);
1335                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1336                         return -EINVAL;
1337
1338                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1339                         return -EINVAL;
1340
1341                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1342                         return -EINVAL;
1343
1344                 break;
1345
1346         case OVS_KEY_ATTR_TCP:
1347                 if (flow_key->ip.proto != IPPROTO_TCP)
1348                         return -EINVAL;
1349
1350                 return validate_tp_port(flow_key);
1351
1352         case OVS_KEY_ATTR_UDP:
1353                 if (flow_key->ip.proto != IPPROTO_UDP)
1354                         return -EINVAL;
1355
1356                 return validate_tp_port(flow_key);
1357
1358         case OVS_KEY_ATTR_SCTP:
1359                 if (flow_key->ip.proto != IPPROTO_SCTP)
1360                         return -EINVAL;
1361
1362                 return validate_tp_port(flow_key);
1363
1364         default:
1365                 return -EINVAL;
1366         }
1367
1368         return 0;
1369 }
1370
1371 static int validate_userspace(const struct nlattr *attr)
1372 {
1373         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1374                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1375                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1376         };
1377         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1378         int error;
1379
1380         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1381                                  attr, userspace_policy);
1382         if (error)
1383                 return error;
1384
1385         if (!a[OVS_USERSPACE_ATTR_PID] ||
1386             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1387                 return -EINVAL;
1388
1389         return 0;
1390 }
1391
1392 static int copy_action(const struct nlattr *from,
1393                        struct sw_flow_actions **sfa)
1394 {
1395         int totlen = NLA_ALIGN(from->nla_len);
1396         struct nlattr *to;
1397
1398         to = reserve_sfa_size(sfa, from->nla_len);
1399         if (IS_ERR(to))
1400                 return PTR_ERR(to);
1401
1402         memcpy(to, from, totlen);
1403         return 0;
1404 }
1405
1406 int ovs_nla_copy_actions(const struct nlattr *attr,
1407                          const struct sw_flow_key *key,
1408                          int depth,
1409                          struct sw_flow_actions **sfa)
1410 {
1411         const struct nlattr *a;
1412         int rem, err;
1413
1414         if (depth >= SAMPLE_ACTION_DEPTH)
1415                 return -EOVERFLOW;
1416
1417         nla_for_each_nested(a, attr, rem) {
1418                 /* Expected argument lengths, (u32)-1 for variable length. */
1419                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1420                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1421                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1422                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1423                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
1424                         [OVS_ACTION_ATTR_SET] = (u32)-1,
1425                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1426                 };
1427                 const struct ovs_action_push_vlan *vlan;
1428                 int type = nla_type(a);
1429                 bool skip_copy;
1430
1431                 if (type > OVS_ACTION_ATTR_MAX ||
1432                     (action_lens[type] != nla_len(a) &&
1433                      action_lens[type] != (u32)-1))
1434                         return -EINVAL;
1435
1436                 skip_copy = false;
1437                 switch (type) {
1438                 case OVS_ACTION_ATTR_UNSPEC:
1439                         return -EINVAL;
1440
1441                 case OVS_ACTION_ATTR_USERSPACE:
1442                         err = validate_userspace(a);
1443                         if (err)
1444                                 return err;
1445                         break;
1446
1447                 case OVS_ACTION_ATTR_OUTPUT:
1448                         if (nla_get_u32(a) >= DP_MAX_PORTS)
1449                                 return -EINVAL;
1450                         break;
1451
1452
1453                 case OVS_ACTION_ATTR_POP_VLAN:
1454                         break;
1455
1456                 case OVS_ACTION_ATTR_PUSH_VLAN:
1457                         vlan = nla_data(a);
1458                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1459                                 return -EINVAL;
1460                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1461                                 return -EINVAL;
1462                         break;
1463
1464                 case OVS_ACTION_ATTR_SET:
1465                         err = validate_set(a, key, sfa, &skip_copy);
1466                         if (err)
1467                                 return err;
1468                         break;
1469
1470                 case OVS_ACTION_ATTR_SAMPLE:
1471                         err = validate_and_copy_sample(a, key, depth, sfa);
1472                         if (err)
1473                                 return err;
1474                         skip_copy = true;
1475                         break;
1476
1477                 default:
1478                         return -EINVAL;
1479                 }
1480                 if (!skip_copy) {
1481                         err = copy_action(a, sfa);
1482                         if (err)
1483                                 return err;
1484                 }
1485         }
1486
1487         if (rem > 0)
1488                 return -EINVAL;
1489
1490         return 0;
1491 }
1492
1493 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1494 {
1495         const struct nlattr *a;
1496         struct nlattr *start;
1497         int err = 0, rem;
1498
1499         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1500         if (!start)
1501                 return -EMSGSIZE;
1502
1503         nla_for_each_nested(a, attr, rem) {
1504                 int type = nla_type(a);
1505                 struct nlattr *st_sample;
1506
1507                 switch (type) {
1508                 case OVS_SAMPLE_ATTR_PROBABILITY:
1509                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1510                                     sizeof(u32), nla_data(a)))
1511                                 return -EMSGSIZE;
1512                         break;
1513                 case OVS_SAMPLE_ATTR_ACTIONS:
1514                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1515                         if (!st_sample)
1516                                 return -EMSGSIZE;
1517                         err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1518                         if (err)
1519                                 return err;
1520                         nla_nest_end(skb, st_sample);
1521                         break;
1522                 }
1523         }
1524
1525         nla_nest_end(skb, start);
1526         return err;
1527 }
1528
1529 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1530 {
1531         const struct nlattr *ovs_key = nla_data(a);
1532         int key_type = nla_type(ovs_key);
1533         struct nlattr *start;
1534         int err;
1535
1536         switch (key_type) {
1537         case OVS_KEY_ATTR_IPV4_TUNNEL:
1538                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1539                 if (!start)
1540                         return -EMSGSIZE;
1541
1542                 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1543                                              nla_data(ovs_key));
1544                 if (err)
1545                         return err;
1546                 nla_nest_end(skb, start);
1547                 break;
1548         default:
1549                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1550                         return -EMSGSIZE;
1551                 break;
1552         }
1553
1554         return 0;
1555 }
1556
1557 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1558 {
1559         const struct nlattr *a;
1560         int rem, err;
1561
1562         nla_for_each_attr(a, attr, len, rem) {
1563                 int type = nla_type(a);
1564
1565                 switch (type) {
1566                 case OVS_ACTION_ATTR_SET:
1567                         err = set_action_to_attr(a, skb);
1568                         if (err)
1569                                 return err;
1570                         break;
1571
1572                 case OVS_ACTION_ATTR_SAMPLE:
1573                         err = sample_action_to_attr(a, skb);
1574                         if (err)
1575                                 return err;
1576                         break;
1577                 default:
1578                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1579                                 return -EMSGSIZE;
1580                         break;
1581                 }
1582         }
1583
1584         return 0;
1585 }