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