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