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