datapath: Fix L4 checksum handling when dealing with IP fragments
[cascardo/ovs.git] / datapath / actions.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/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/sctp.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/in6.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_vlan.h>
31 #include <net/ip.h>
32 #include <net/ipv6.h>
33 #include <net/checksum.h>
34 #include <net/dsfield.h>
35 #include <net/mpls.h>
36 #include <net/sctp/checksum.h>
37
38 #include "datapath.h"
39 #include "gso.h"
40 #include "vlan.h"
41 #include "vport.h"
42
43 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
44                               struct sw_flow_key *key,
45                               const struct nlattr *attr, int len);
46
47 struct deferred_action {
48         struct sk_buff *skb;
49         const struct nlattr *actions;
50
51         /* Store pkt_key clone when creating deferred action. */
52         struct sw_flow_key pkt_key;
53 };
54
55 #define DEFERRED_ACTION_FIFO_SIZE 10
56 struct action_fifo {
57         int head;
58         int tail;
59         /* Deferred action fifo queue storage. */
60         struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
61 };
62
63 static struct action_fifo __percpu *action_fifos;
64 #define EXEC_ACTIONS_LEVEL_LIMIT 4   /* limit used to detect packet
65                                       * looping by the network stack
66                                       */
67 static DEFINE_PER_CPU(int, exec_actions_level);
68
69 static void action_fifo_init(struct action_fifo *fifo)
70 {
71         fifo->head = 0;
72         fifo->tail = 0;
73 }
74
75 static bool action_fifo_is_empty(const struct action_fifo *fifo)
76 {
77         return (fifo->head == fifo->tail);
78 }
79
80 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
81 {
82         if (action_fifo_is_empty(fifo))
83                 return NULL;
84
85         return &fifo->fifo[fifo->tail++];
86 }
87
88 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
89 {
90         if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
91                 return NULL;
92
93         return &fifo->fifo[fifo->head++];
94 }
95
96 /* Return queue entry if fifo is not full */
97 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
98                                                     const struct sw_flow_key *key,
99                                                     const struct nlattr *attr)
100 {
101         struct action_fifo *fifo;
102         struct deferred_action *da;
103
104         fifo = this_cpu_ptr(action_fifos);
105         da = action_fifo_put(fifo);
106         if (da) {
107                 da->skb = skb;
108                 da->actions = attr;
109                 da->pkt_key = *key;
110         }
111
112         return da;
113 }
114
115 static void invalidate_flow_key(struct sw_flow_key *key)
116 {
117         key->eth.type = htons(0);
118 }
119
120 static bool is_flow_key_valid(const struct sw_flow_key *key)
121 {
122         return !!key->eth.type;
123 }
124
125 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
126                      const struct ovs_action_push_mpls *mpls)
127 {
128         __be32 *new_mpls_lse;
129         struct ethhdr *hdr;
130
131         /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
132         if (skb_encapsulation(skb))
133                 return -ENOTSUPP;
134
135         if (skb_cow_head(skb, MPLS_HLEN) < 0)
136                 return -ENOMEM;
137
138         skb_push(skb, MPLS_HLEN);
139         memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
140                 skb->mac_len);
141         skb_reset_mac_header(skb);
142
143         new_mpls_lse = (__be32 *)skb_mpls_header(skb);
144         *new_mpls_lse = mpls->mpls_lse;
145
146         if (skb->ip_summed == CHECKSUM_COMPLETE)
147                 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
148                                                              MPLS_HLEN, 0));
149
150         hdr = eth_hdr(skb);
151         hdr->h_proto = mpls->mpls_ethertype;
152         if (!ovs_skb_get_inner_protocol(skb))
153                 ovs_skb_set_inner_protocol(skb, skb->protocol);
154         skb->protocol = mpls->mpls_ethertype;
155
156         invalidate_flow_key(key);
157         return 0;
158 }
159
160 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
161                     const __be16 ethertype)
162 {
163         struct ethhdr *hdr;
164         int err;
165
166         err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
167         if (unlikely(err))
168                 return err;
169
170         if (skb->ip_summed == CHECKSUM_COMPLETE)
171                 skb->csum = csum_sub(skb->csum,
172                                      csum_partial(skb_mpls_header(skb),
173                                                   MPLS_HLEN, 0));
174
175         memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
176                 skb->mac_len);
177
178         __skb_pull(skb, MPLS_HLEN);
179         skb_reset_mac_header(skb);
180
181         /* skb_mpls_header() is used to locate the ethertype
182          * field correctly in the presence of VLAN tags.
183          */
184         hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
185         hdr->h_proto = ethertype;
186         if (eth_p_mpls(skb->protocol))
187                 skb->protocol = ethertype;
188
189         invalidate_flow_key(key);
190         return 0;
191 }
192
193 /* 'KEY' must not have any bits set outside of the 'MASK' */
194 #define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
195 #define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
196
197 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
198                     const __be32 *mpls_lse, const __be32 *mask)
199 {
200         __be32 *stack;
201         __be32 lse;
202         int err;
203
204         err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
205         if (unlikely(err))
206                 return err;
207
208         stack = (__be32 *)skb_mpls_header(skb);
209         lse = MASKED(*stack, *mpls_lse, *mask);
210         if (skb->ip_summed == CHECKSUM_COMPLETE) {
211                 __be32 diff[] = { ~(*stack), lse };
212
213                 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
214                                           ~skb->csum);
215         }
216
217         *stack = lse;
218         flow_key->mpls.top_lse = lse;
219         return 0;
220 }
221
222 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
223 {
224         int err;
225
226         err = skb_vlan_pop(skb);
227         if (skb_vlan_tag_present(skb))
228                 invalidate_flow_key(key);
229         else
230                 key->eth.tci = 0;
231
232         return err;
233 }
234
235 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
236                      const struct ovs_action_push_vlan *vlan)
237 {
238         if (skb_vlan_tag_present(skb))
239                 invalidate_flow_key(key);
240         else
241                 key->eth.tci = vlan->vlan_tci;
242
243         return skb_vlan_push(skb, vlan->vlan_tpid,
244                              ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
245 }
246
247 /* 'src' is already properly masked. */
248 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
249 {
250         u16 *dst = (u16 *)dst_;
251         const u16 *src = (const u16 *)src_;
252         const u16 *mask = (const u16 *)mask_;
253
254         SET_MASKED(dst[0], src[0], mask[0]);
255         SET_MASKED(dst[1], src[1], mask[1]);
256         SET_MASKED(dst[2], src[2], mask[2]);
257 }
258
259 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
260                         const struct ovs_key_ethernet *key,
261                         const struct ovs_key_ethernet *mask)
262 {
263         int err;
264
265         err = skb_ensure_writable(skb, ETH_HLEN);
266         if (unlikely(err))
267                 return err;
268
269         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
270
271         ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
272                                mask->eth_src);
273         ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
274                                mask->eth_dst);
275
276         ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
277
278         ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
279         ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
280         return 0;
281 }
282
283 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
284                                   __be32 addr, __be32 new_addr)
285 {
286         int transport_len = skb->len - skb_transport_offset(skb);
287
288         if (nh->frag_off & htons(IP_OFFSET))
289                 return;
290
291         if (nh->protocol == IPPROTO_TCP) {
292                 if (likely(transport_len >= sizeof(struct tcphdr)))
293                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
294                                                  addr, new_addr, 1);
295         } else if (nh->protocol == IPPROTO_UDP) {
296                 if (likely(transport_len >= sizeof(struct udphdr))) {
297                         struct udphdr *uh = udp_hdr(skb);
298
299                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
300                                 inet_proto_csum_replace4(&uh->check, skb,
301                                                          addr, new_addr, 1);
302                                 if (!uh->check)
303                                         uh->check = CSUM_MANGLED_0;
304                         }
305                 }
306         }
307
308 }
309
310 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
311                         __be32 *addr, __be32 new_addr)
312 {
313         update_ip_l4_checksum(skb, nh, *addr, new_addr);
314         csum_replace4(&nh->check, *addr, new_addr);
315         skb_clear_hash(skb);
316         *addr = new_addr;
317 }
318
319 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
320                                  __be32 addr[4], const __be32 new_addr[4])
321 {
322         int transport_len = skb->len - skb_transport_offset(skb);
323
324         if (l4_proto == NEXTHDR_TCP) {
325                 if (likely(transport_len >= sizeof(struct tcphdr)))
326                         inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
327                                                   addr, new_addr, 1);
328         } else if (l4_proto == NEXTHDR_UDP) {
329                 if (likely(transport_len >= sizeof(struct udphdr))) {
330                         struct udphdr *uh = udp_hdr(skb);
331
332                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
333                                 inet_proto_csum_replace16(&uh->check, skb,
334                                                           addr, new_addr, 1);
335                                 if (!uh->check)
336                                         uh->check = CSUM_MANGLED_0;
337                         }
338                 }
339         } else if (l4_proto == NEXTHDR_ICMP) {
340                 if (likely(transport_len >= sizeof(struct icmp6hdr)))
341                         inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
342                                                   skb, addr, new_addr, 1);
343         }
344 }
345
346 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
347                            const __be32 mask[4], __be32 masked[4])
348 {
349         masked[0] = MASKED(old[0], addr[0], mask[0]);
350         masked[1] = MASKED(old[1], addr[1], mask[1]);
351         masked[2] = MASKED(old[2], addr[2], mask[2]);
352         masked[3] = MASKED(old[3], addr[3], mask[3]);
353 }
354
355 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
356                           __be32 addr[4], const __be32 new_addr[4],
357                           bool recalculate_csum)
358 {
359         if (likely(recalculate_csum))
360                 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
361
362         skb_clear_hash(skb);
363         memcpy(addr, new_addr, sizeof(__be32[4]));
364 }
365
366 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
367 {
368         /* Bits 21-24 are always unmasked, so this retains their values. */
369         SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
370         SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
371         SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
372 }
373
374 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
375                        u8 mask)
376 {
377         new_ttl = MASKED(nh->ttl, new_ttl, mask);
378
379         csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
380         nh->ttl = new_ttl;
381 }
382
383 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
384                     const struct ovs_key_ipv4 *key,
385                     const struct ovs_key_ipv4 *mask)
386 {
387         struct iphdr *nh;
388         __be32 new_addr;
389         int err;
390
391         err = skb_ensure_writable(skb, skb_network_offset(skb) +
392                                   sizeof(struct iphdr));
393         if (unlikely(err))
394                 return err;
395
396         nh = ip_hdr(skb);
397
398         /* Setting an IP addresses is typically only a side effect of
399          * matching on them in the current userspace implementation, so it
400          * makes sense to check if the value actually changed.
401          */
402         if (mask->ipv4_src) {
403                 new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
404
405                 if (unlikely(new_addr != nh->saddr)) {
406                         set_ip_addr(skb, nh, &nh->saddr, new_addr);
407                         flow_key->ipv4.addr.src = new_addr;
408                 }
409         }
410         if (mask->ipv4_dst) {
411                 new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
412
413                 if (unlikely(new_addr != nh->daddr)) {
414                         set_ip_addr(skb, nh, &nh->daddr, new_addr);
415                         flow_key->ipv4.addr.dst = new_addr;
416                 }
417         }
418         if (mask->ipv4_tos) {
419                 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
420                 flow_key->ip.tos = nh->tos;
421         }
422         if (mask->ipv4_ttl) {
423                 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
424                 flow_key->ip.ttl = nh->ttl;
425         }
426
427         return 0;
428 }
429
430 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
431 {
432         return !!(addr[0] | addr[1] | addr[2] | addr[3]);
433 }
434
435 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
436                     const struct ovs_key_ipv6 *key,
437                     const struct ovs_key_ipv6 *mask)
438 {
439         struct ipv6hdr *nh;
440         int err;
441
442         err = skb_ensure_writable(skb, skb_network_offset(skb) +
443                                   sizeof(struct ipv6hdr));
444         if (unlikely(err))
445                 return err;
446
447         nh = ipv6_hdr(skb);
448
449         /* Setting an IP addresses is typically only a side effect of
450          * matching on them in the current userspace implementation, so it
451          * makes sense to check if the value actually changed.
452          */
453         if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
454                 __be32 *saddr = (__be32 *)&nh->saddr;
455                 __be32 masked[4];
456
457                 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
458
459                 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
460                         set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
461                                       true);
462                         memcpy(&flow_key->ipv6.addr.src, masked,
463                                sizeof(flow_key->ipv6.addr.src));
464                 }
465         }
466         if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
467                 unsigned int offset = 0;
468                 int flags = IP6_FH_F_SKIP_RH;
469                 bool recalc_csum = true;
470                 __be32 *daddr = (__be32 *)&nh->daddr;
471                 __be32 masked[4];
472
473                 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
474
475                 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
476                         if (ipv6_ext_hdr(nh->nexthdr))
477                                 recalc_csum = (ipv6_find_hdr(skb, &offset,
478                                                              NEXTHDR_ROUTING,
479                                                              NULL, &flags)
480                                                != NEXTHDR_ROUTING);
481
482                         set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
483                                       recalc_csum);
484                         memcpy(&flow_key->ipv6.addr.dst, masked,
485                                sizeof(flow_key->ipv6.addr.dst));
486                 }
487         }
488         if (mask->ipv6_tclass) {
489                 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
490                 flow_key->ip.tos = ipv6_get_dsfield(nh);
491         }
492         if (mask->ipv6_label) {
493                 set_ipv6_fl(nh, ntohl(key->ipv6_label),
494                             ntohl(mask->ipv6_label));
495                 flow_key->ipv6.label =
496                     *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
497         }
498         if (mask->ipv6_hlimit) {
499                 SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
500                 flow_key->ip.ttl = nh->hop_limit;
501         }
502         return 0;
503 }
504
505 /* Must follow skb_ensure_writable() since that can move the skb data. */
506 static void set_tp_port(struct sk_buff *skb, __be16 *port,
507                         __be16 new_port, __sum16 *check)
508 {
509         inet_proto_csum_replace2(check, skb, *port, new_port, 0);
510         *port = new_port;
511 }
512
513 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
514                    const struct ovs_key_udp *key,
515                    const struct ovs_key_udp *mask)
516 {
517         struct udphdr *uh;
518         __be16 src, dst;
519         int err;
520
521         err = skb_ensure_writable(skb, skb_transport_offset(skb) +
522                                   sizeof(struct udphdr));
523         if (unlikely(err))
524                 return err;
525
526         uh = udp_hdr(skb);
527         /* Either of the masks is non-zero, so do not bother checking them. */
528         src = MASKED(uh->source, key->udp_src, mask->udp_src);
529         dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
530
531         if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
532                 if (likely(src != uh->source)) {
533                         set_tp_port(skb, &uh->source, src, &uh->check);
534                         flow_key->tp.src = src;
535                 }
536                 if (likely(dst != uh->dest)) {
537                         set_tp_port(skb, &uh->dest, dst, &uh->check);
538                         flow_key->tp.dst = dst;
539                 }
540
541                 if (unlikely(!uh->check))
542                         uh->check = CSUM_MANGLED_0;
543         } else {
544                 uh->source = src;
545                 uh->dest = dst;
546                 flow_key->tp.src = src;
547                 flow_key->tp.dst = dst;
548         }
549
550         skb_clear_hash(skb);
551
552         return 0;
553 }
554
555 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
556                    const struct ovs_key_tcp *key,
557                    const struct ovs_key_tcp *mask)
558 {
559         struct tcphdr *th;
560         __be16 src, dst;
561         int err;
562
563         err = skb_ensure_writable(skb, skb_transport_offset(skb) +
564                                   sizeof(struct tcphdr));
565         if (unlikely(err))
566                 return err;
567
568         th = tcp_hdr(skb);
569
570         src = MASKED(th->source, key->tcp_src, mask->tcp_src);
571         if (likely(src != th->source)) {
572                 set_tp_port(skb, &th->source, src, &th->check);
573                 flow_key->tp.src = src;
574         }
575         dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
576         if (likely(dst != th->dest)) {
577                 set_tp_port(skb, &th->dest, dst, &th->check);
578                 flow_key->tp.dst = dst;
579         }
580         skb_clear_hash(skb);
581
582         return 0;
583 }
584
585 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
586                     const struct ovs_key_sctp *key,
587                     const struct ovs_key_sctp *mask)
588 {
589         unsigned int sctphoff = skb_transport_offset(skb);
590         struct sctphdr *sh;
591         __le32 old_correct_csum, new_csum, old_csum;
592         int err;
593
594         err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
595         if (unlikely(err))
596                 return err;
597
598         sh = sctp_hdr(skb);
599
600         old_csum = sh->checksum;
601         old_correct_csum = sctp_compute_cksum(skb, sctphoff);
602
603         sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
604         sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
605
606         new_csum = sctp_compute_cksum(skb, sctphoff);
607
608         /* Carry any checksum errors through. */
609         sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
610
611         skb_clear_hash(skb);
612         flow_key->tp.src = sh->source;
613         flow_key->tp.dst = sh->dest;
614
615         return 0;
616 }
617
618 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
619 {
620         struct vport *vport = ovs_vport_rcu(dp, out_port);
621
622         if (likely(vport))
623                 ovs_vport_send(vport, skb);
624         else
625                 kfree_skb(skb);
626 }
627
628 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
629                             struct sw_flow_key *key, const struct nlattr *attr)
630 {
631         struct ovs_tunnel_info info;
632         struct dp_upcall_info upcall;
633         const struct nlattr *a;
634         int rem;
635
636         upcall.cmd = OVS_PACKET_CMD_ACTION;
637         upcall.userdata = NULL;
638         upcall.portid = 0;
639         upcall.egress_tun_info = NULL;
640
641         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
642                  a = nla_next(a, &rem)) {
643                 switch (nla_type(a)) {
644                 case OVS_USERSPACE_ATTR_USERDATA:
645                         upcall.userdata = a;
646                         break;
647
648                 case OVS_USERSPACE_ATTR_PID:
649                         upcall.portid = nla_get_u32(a);
650                         break;
651
652                 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
653                         /* Get out tunnel info. */
654                         struct vport *vport;
655
656                         vport = ovs_vport_rcu(dp, nla_get_u32(a));
657                         if (vport) {
658                                 int err;
659
660                                 err = ovs_vport_get_egress_tun_info(vport, skb,
661                                                                     &info);
662                                 if (!err)
663                                         upcall.egress_tun_info = &info;
664                         }
665                         break;
666                 }
667
668                 } /* End of switch. */
669         }
670
671         return ovs_dp_upcall(dp, skb, key, &upcall);
672 }
673
674 static int sample(struct datapath *dp, struct sk_buff *skb,
675                   struct sw_flow_key *key, const struct nlattr *attr)
676 {
677         const struct nlattr *acts_list = NULL;
678         const struct nlattr *a;
679         int rem;
680
681         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
682                  a = nla_next(a, &rem)) {
683                 switch (nla_type(a)) {
684                 case OVS_SAMPLE_ATTR_PROBABILITY:
685                         if (prandom_u32() >= nla_get_u32(a))
686                                 return 0;
687                         break;
688
689                 case OVS_SAMPLE_ATTR_ACTIONS:
690                         acts_list = a;
691                         break;
692                 }
693         }
694
695         rem = nla_len(acts_list);
696         a = nla_data(acts_list);
697
698         /* Actions list is empty, do nothing */
699         if (unlikely(!rem))
700                 return 0;
701
702         /* The only known usage of sample action is having a single user-space
703          * action. Treat this usage as a special case.
704          * The output_userspace() should clone the skb to be sent to the
705          * user space. This skb will be consumed by its caller.
706          */
707         if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
708                    nla_is_last(a, rem)))
709                 return output_userspace(dp, skb, key, a);
710
711         skb = skb_clone(skb, GFP_ATOMIC);
712         if (!skb)
713                 /* Skip the sample action when out of memory. */
714                 return 0;
715
716         if (!add_deferred_actions(skb, key, a)) {
717                 if (net_ratelimit())
718                         pr_warn("%s: deferred actions limit reached, dropping sample action\n",
719                                 ovs_dp_name(dp));
720
721                 kfree_skb(skb);
722         }
723         return 0;
724 }
725
726 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
727                          const struct nlattr *attr)
728 {
729         struct ovs_action_hash *hash_act = nla_data(attr);
730         u32 hash = 0;
731
732         /* OVS_HASH_ALG_L4 is the only possible hash algorithm.  */
733         hash = skb_get_hash(skb);
734         hash = jhash_1word(hash, hash_act->hash_basis);
735         if (!hash)
736                 hash = 0x1;
737
738         key->ovs_flow_hash = hash;
739 }
740
741 static int execute_set_action(struct sk_buff *skb,
742                               struct sw_flow_key *flow_key,
743                               const struct nlattr *a)
744 {
745         /* Only tunnel set execution is supported without a mask. */
746         if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
747                 OVS_CB(skb)->egress_tun_info = nla_data(a);
748                 return 0;
749         }
750
751         return -EINVAL;
752
753 }
754
755 /* Mask is at the midpoint of the data. */
756 #define get_mask(a, type) ((const type)nla_data(a) + 1)
757
758 static int execute_masked_set_action(struct sk_buff *skb,
759                                      struct sw_flow_key *flow_key,
760                                      const struct nlattr *a)
761 {
762         int err = 0;
763
764         switch (nla_type(a)) {
765         case OVS_KEY_ATTR_PRIORITY:
766                 SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
767                 flow_key->phy.priority = skb->priority;
768                 break;
769
770         case OVS_KEY_ATTR_SKB_MARK:
771                 SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
772                 flow_key->phy.skb_mark = skb->mark;
773                 break;
774
775         case OVS_KEY_ATTR_TUNNEL_INFO:
776                 /* Masked data not supported for tunnel. */
777                 err = -EINVAL;
778                 break;
779
780         case OVS_KEY_ATTR_ETHERNET:
781                 err = set_eth_addr(skb, flow_key, nla_data(a),
782                                    get_mask(a, struct ovs_key_ethernet *));
783                 break;
784
785         case OVS_KEY_ATTR_IPV4:
786                 err = set_ipv4(skb, flow_key, nla_data(a),
787                                get_mask(a, struct ovs_key_ipv4 *));
788                 break;
789
790         case OVS_KEY_ATTR_IPV6:
791                 err = set_ipv6(skb, flow_key, nla_data(a),
792                                get_mask(a, struct ovs_key_ipv6 *));
793                 break;
794
795         case OVS_KEY_ATTR_TCP:
796                 err = set_tcp(skb, flow_key, nla_data(a),
797                               get_mask(a, struct ovs_key_tcp *));
798                 break;
799
800         case OVS_KEY_ATTR_UDP:
801                 err = set_udp(skb, flow_key, nla_data(a),
802                               get_mask(a, struct ovs_key_udp *));
803                 break;
804
805         case OVS_KEY_ATTR_SCTP:
806                 err = set_sctp(skb, flow_key, nla_data(a),
807                                get_mask(a, struct ovs_key_sctp *));
808                 break;
809
810         case OVS_KEY_ATTR_MPLS:
811                 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
812                                                                     __be32 *));
813                 break;
814         }
815
816         return err;
817 }
818
819 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
820                           struct sw_flow_key *key,
821                           const struct nlattr *a, int rem)
822 {
823         struct deferred_action *da;
824
825         if (!is_flow_key_valid(key)) {
826                 int err;
827
828                 err = ovs_flow_key_update(skb, key);
829                 if (err)
830                         return err;
831         }
832         BUG_ON(!is_flow_key_valid(key));
833
834         if (!nla_is_last(a, rem)) {
835                 /* Recirc action is the not the last action
836                  * of the action list, need to clone the skb.
837                  */
838                 skb = skb_clone(skb, GFP_ATOMIC);
839
840                 /* Skip the recirc action when out of memory, but
841                  * continue on with the rest of the action list.
842                  */
843                 if (!skb)
844                         return 0;
845         }
846
847         da = add_deferred_actions(skb, key, NULL);
848         if (da) {
849                 da->pkt_key.recirc_id = nla_get_u32(a);
850         } else {
851                 kfree_skb(skb);
852
853                 if (net_ratelimit())
854                         pr_warn("%s: deferred action limit reached, drop recirc action\n",
855                                 ovs_dp_name(dp));
856         }
857
858         return 0;
859 }
860
861 /* Execute a list of actions against 'skb'. */
862 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
863                               struct sw_flow_key *key,
864                               const struct nlattr *attr, int len)
865 {
866         /* Every output action needs a separate clone of 'skb', but the common
867          * case is just a single output action, so that doing a clone and
868          * then freeing the original skbuff is wasteful.  So the following code
869          * is slightly obscure just to avoid that.
870          */
871         int prev_port = -1;
872         const struct nlattr *a;
873         int rem;
874
875         for (a = attr, rem = len; rem > 0;
876              a = nla_next(a, &rem)) {
877                 int err = 0;
878
879                 if (unlikely(prev_port != -1)) {
880                         struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
881
882                         if (out_skb)
883                                 do_output(dp, out_skb, prev_port);
884
885                         prev_port = -1;
886                 }
887
888                 switch (nla_type(a)) {
889                 case OVS_ACTION_ATTR_OUTPUT:
890                         prev_port = nla_get_u32(a);
891                         break;
892
893                 case OVS_ACTION_ATTR_USERSPACE:
894                         output_userspace(dp, skb, key, a);
895                         break;
896
897                 case OVS_ACTION_ATTR_HASH:
898                         execute_hash(skb, key, a);
899                         break;
900
901                 case OVS_ACTION_ATTR_PUSH_MPLS:
902                         err = push_mpls(skb, key, nla_data(a));
903                         break;
904
905                 case OVS_ACTION_ATTR_POP_MPLS:
906                         err = pop_mpls(skb, key, nla_get_be16(a));
907                         break;
908
909                 case OVS_ACTION_ATTR_PUSH_VLAN:
910                         err = push_vlan(skb, key, nla_data(a));
911                         break;
912
913                 case OVS_ACTION_ATTR_POP_VLAN:
914                         err = pop_vlan(skb, key);
915                         break;
916
917                 case OVS_ACTION_ATTR_RECIRC:
918                         err = execute_recirc(dp, skb, key, a, rem);
919                         if (nla_is_last(a, rem)) {
920                                 /* If this is the last action, the skb has
921                                  * been consumed or freed.
922                                  * Return immediately.
923                                  */
924                                 return err;
925                         }
926                         break;
927
928                 case OVS_ACTION_ATTR_SET:
929                         err = execute_set_action(skb, key, nla_data(a));
930                         break;
931
932                 case OVS_ACTION_ATTR_SET_MASKED:
933                 case OVS_ACTION_ATTR_SET_TO_MASKED:
934                         err = execute_masked_set_action(skb, key, nla_data(a));
935                         break;
936
937                 case OVS_ACTION_ATTR_SAMPLE:
938                         err = sample(dp, skb, key, a);
939                         break;
940                 }
941
942                 if (unlikely(err)) {
943                         kfree_skb(skb);
944                         return err;
945                 }
946         }
947
948         if (prev_port != -1)
949                 do_output(dp, skb, prev_port);
950         else
951                 consume_skb(skb);
952
953         return 0;
954 }
955
956 static void process_deferred_actions(struct datapath *dp)
957 {
958         struct action_fifo *fifo = this_cpu_ptr(action_fifos);
959
960         /* Do not touch the FIFO in case there is no deferred actions. */
961         if (action_fifo_is_empty(fifo))
962                 return;
963
964         /* Finishing executing all deferred actions. */
965         do {
966                 struct deferred_action *da = action_fifo_get(fifo);
967                 struct sk_buff *skb = da->skb;
968                 struct sw_flow_key *key = &da->pkt_key;
969                 const struct nlattr *actions = da->actions;
970
971                 if (actions)
972                         do_execute_actions(dp, skb, key, actions,
973                                            nla_len(actions));
974                 else
975                         ovs_dp_process_packet(skb, key);
976         } while (!action_fifo_is_empty(fifo));
977
978         /* Reset FIFO for the next packet.  */
979         action_fifo_init(fifo);
980 }
981
982 /* Execute a list of actions against 'skb'. */
983 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
984                         const struct sw_flow_actions *acts,
985                         struct sw_flow_key *key)
986 {
987         int level = this_cpu_read(exec_actions_level);
988         int err;
989
990         if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
991                 if (net_ratelimit())
992                         pr_warn("%s: packet loop detected, dropping.\n",
993                                 ovs_dp_name(dp));
994
995                 kfree_skb(skb);
996                 return -ELOOP;
997         }
998
999         this_cpu_inc(exec_actions_level);
1000         err = do_execute_actions(dp, skb, key,
1001                                  acts->actions, acts->actions_len);
1002
1003         if (!level)
1004                 process_deferred_actions(dp);
1005
1006         this_cpu_dec(exec_actions_level);
1007
1008         /* This return status currently does not reflect the errors
1009          * encounted during deferred actions execution. Probably needs to
1010          * be fixed in the future.
1011          */
1012         return err;
1013 }
1014
1015 int action_fifos_init(void)
1016 {
1017         action_fifos = alloc_percpu(struct action_fifo);
1018         if (!action_fifos)
1019                 return -ENOMEM;
1020
1021         return 0;
1022 }
1023
1024 void action_fifos_exit(void)
1025 {
1026         free_percpu(action_fifos);
1027 }