datapath: Remove flow member from struct ovs_skb_cb
[cascardo/ovs.git] / datapath / actions.c
1 /*
2  * Copyright (c) 2007-2014 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/sctp.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/in6.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_vlan.h>
31 #include <net/ip.h>
32 #include <net/ipv6.h>
33 #include <net/checksum.h>
34 #include <net/dsfield.h>
35 #include <net/sctp/checksum.h>
36
37 #include "datapath.h"
38 #include "gso.h"
39 #include "mpls.h"
40 #include "vlan.h"
41 #include "vport.h"
42
43 static void flow_key_clone(struct sk_buff *skb, struct sw_flow_key *new_key)
44 {
45         *new_key = *OVS_CB(skb)->pkt_key;
46         OVS_CB(skb)->pkt_key = new_key;
47 }
48
49 static void flow_key_set_recirc_id(struct sk_buff *skb, u32 recirc_id)
50 {
51         OVS_CB(skb)->pkt_key->recirc_id = recirc_id;
52 }
53
54 static void flow_key_set_priority(struct sk_buff *skb, u32 priority)
55 {
56         OVS_CB(skb)->pkt_key->phy.priority = priority;
57 }
58
59 static void flow_key_set_skb_mark(struct sk_buff *skb, u32 skb_mark)
60 {
61         OVS_CB(skb)->pkt_key->phy.skb_mark = skb_mark;
62 }
63
64 static void flow_key_set_eth_src(struct sk_buff *skb, const u8 addr[])
65 {
66         ether_addr_copy(OVS_CB(skb)->pkt_key->eth.src, addr);
67 }
68
69 static void flow_key_set_eth_dst(struct sk_buff *skb, const u8 addr[])
70 {
71         ether_addr_copy(OVS_CB(skb)->pkt_key->eth.dst, addr);
72 }
73
74 static void flow_key_set_vlan_tci(struct sk_buff *skb, __be16 tci)
75 {
76         OVS_CB(skb)->pkt_key->eth.tci = tci;
77 }
78
79 static void flow_key_set_mpls_top_lse(struct sk_buff *skb, __be32 top_lse)
80 {
81         OVS_CB(skb)->pkt_key->mpls.top_lse = top_lse;
82 }
83
84 static void flow_key_set_ipv4_src(struct sk_buff *skb, __be32 addr)
85 {
86         OVS_CB(skb)->pkt_key->ipv4.addr.src = addr;
87 }
88
89 static void flow_key_set_ipv4_dst(struct sk_buff *skb, __be32 addr)
90 {
91         OVS_CB(skb)->pkt_key->ipv4.addr.src = addr;
92 }
93
94 static void flow_key_set_ip_tos(struct sk_buff *skb, u8 tos)
95 {
96         OVS_CB(skb)->pkt_key->ip.tos = tos;
97 }
98
99 static void flow_key_set_ip_ttl(struct sk_buff *skb, u8 ttl)
100 {
101         OVS_CB(skb)->pkt_key->ip.ttl = ttl;
102 }
103
104 static void flow_key_set_ipv6_src(struct sk_buff *skb,
105                                   const __be32 addr[4])
106 {
107         memcpy(&OVS_CB(skb)->pkt_key->ipv6.addr.src, addr, sizeof(__be32[4]));
108 }
109
110 static void flow_key_set_ipv6_dst(struct sk_buff *skb,
111                                   const __be32 addr[4])
112 {
113         memcpy(&OVS_CB(skb)->pkt_key->ipv6.addr.dst, addr, sizeof(__be32[4]));
114 }
115
116 static void flow_key_set_ipv6_fl(struct sk_buff *skb,
117                                  const struct ipv6hdr *nh)
118 {
119         OVS_CB(skb)->pkt_key->ipv6.label = *(__be32 *)nh &
120                                            htonl(IPV6_FLOWINFO_FLOWLABEL);
121 }
122
123 static void flow_key_set_tp_src(struct sk_buff *skb, __be16 port)
124 {
125         OVS_CB(skb)->pkt_key->tp.src = port;
126 }
127
128 static void flow_key_set_tp_dst(struct sk_buff *skb, __be16 port)
129 {
130         OVS_CB(skb)->pkt_key->tp.dst = port;
131 }
132
133 static void invalidate_skb_flow_key(struct sk_buff *skb)
134 {
135         OVS_CB(skb)->pkt_key->eth.type = htons(0);
136 }
137
138 static bool is_skb_flow_key_valid(struct sk_buff *skb)
139 {
140         return !!OVS_CB(skb)->pkt_key->eth.type;
141 }
142
143 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
144                               const struct nlattr *attr, int len);
145
146 static int make_writable(struct sk_buff *skb, int write_len)
147 {
148         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
149                 return 0;
150
151         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
152 }
153
154 /* The end of the mac header.
155  *
156  * For non-MPLS skbs this will correspond to the network header.
157  * For MPLS skbs it will be before the network_header as the MPLS
158  * label stack lies between the end of the mac header and the network
159  * header. That is, for MPLS skbs the end of the mac header
160  * is the top of the MPLS label stack.
161  */
162 static unsigned char *mac_header_end(const struct sk_buff *skb)
163 {
164         return skb_mac_header(skb) + skb->mac_len;
165 }
166
167 static int push_mpls(struct sk_buff *skb,
168                      const struct ovs_action_push_mpls *mpls)
169 {
170         __be32 *new_mpls_lse;
171         struct ethhdr *hdr;
172
173         if (skb_cow_head(skb, MPLS_HLEN) < 0)
174                 return -ENOMEM;
175
176         skb_push(skb, MPLS_HLEN);
177         memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
178                 skb->mac_len);
179         skb_reset_mac_header(skb);
180
181         new_mpls_lse = (__be32 *)mac_header_end(skb);
182         *new_mpls_lse = mpls->mpls_lse;
183
184         if (skb->ip_summed == CHECKSUM_COMPLETE)
185                 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
186                                                              MPLS_HLEN, 0));
187
188         hdr = eth_hdr(skb);
189         hdr->h_proto = mpls->mpls_ethertype;
190         if (!ovs_skb_get_inner_protocol(skb))
191                 ovs_skb_set_inner_protocol(skb, skb->protocol);
192         skb->protocol = mpls->mpls_ethertype;
193         invalidate_skb_flow_key(skb);
194         return 0;
195 }
196
197 static int pop_mpls(struct sk_buff *skb, const __be16 ethertype)
198 {
199         struct ethhdr *hdr;
200         int err;
201
202         err = make_writable(skb, skb->mac_len + MPLS_HLEN);
203         if (unlikely(err))
204                 return err;
205
206         if (skb->ip_summed == CHECKSUM_COMPLETE)
207                 skb->csum = csum_sub(skb->csum,
208                                      csum_partial(mac_header_end(skb),
209                                                   MPLS_HLEN, 0));
210
211         memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
212                 skb->mac_len);
213
214         __skb_pull(skb, MPLS_HLEN);
215         skb_reset_mac_header(skb);
216
217         /* mac_header_end() is used to locate the ethertype
218          * field correctly in the presence of VLAN tags.
219          */
220         hdr = (struct ethhdr *)(mac_header_end(skb) - ETH_HLEN);
221         hdr->h_proto = ethertype;
222         if (eth_p_mpls(skb->protocol))
223                 skb->protocol = ethertype;
224         invalidate_skb_flow_key(skb);
225         return 0;
226 }
227
228 static int set_mpls(struct sk_buff *skb, const __be32 *mpls_lse)
229 {
230         __be32 *stack = (__be32 *)mac_header_end(skb);
231         int err;
232
233         err = make_writable(skb, skb->mac_len + MPLS_HLEN);
234         if (unlikely(err))
235                 return err;
236
237         if (skb->ip_summed == CHECKSUM_COMPLETE) {
238                 __be32 diff[] = { ~(*stack), *mpls_lse };
239                 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
240                                           ~skb->csum);
241         }
242
243         *stack = *mpls_lse;
244         flow_key_set_mpls_top_lse(skb, *stack);
245         return 0;
246 }
247
248 /* remove VLAN header from packet and update csum accordingly. */
249 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
250 {
251         struct vlan_hdr *vhdr;
252         int err;
253
254         err = make_writable(skb, VLAN_ETH_HLEN);
255         if (unlikely(err))
256                 return err;
257
258         if (skb->ip_summed == CHECKSUM_COMPLETE)
259                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
260                                         + (2 * ETH_ALEN), VLAN_HLEN, 0));
261
262         vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
263         *current_tci = vhdr->h_vlan_TCI;
264
265         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
266         __skb_pull(skb, VLAN_HLEN);
267
268         vlan_set_encap_proto(skb, vhdr);
269         skb->mac_header += VLAN_HLEN;
270         /* Update mac_len for subsequent MPLS actions */
271         skb->mac_len -= VLAN_HLEN;
272
273         return 0;
274 }
275
276 static int pop_vlan(struct sk_buff *skb)
277 {
278         __be16 tci;
279         int err;
280
281         if (likely(vlan_tx_tag_present(skb))) {
282                 vlan_set_tci(skb, 0);
283         } else {
284                 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
285                              skb->len < VLAN_ETH_HLEN))
286                         return 0;
287
288                 err = __pop_vlan_tci(skb, &tci);
289                 if (err)
290                         return err;
291         }
292         /* move next vlan tag to hw accel tag */
293         if (likely(skb->protocol != htons(ETH_P_8021Q) ||
294                    skb->len < VLAN_ETH_HLEN)) {
295                 flow_key_set_vlan_tci(skb, 0);
296                 return 0;
297         }
298
299         invalidate_skb_flow_key(skb);
300         err = __pop_vlan_tci(skb, &tci);
301         if (unlikely(err))
302                 return err;
303
304         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
305         return 0;
306 }
307
308 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
309 {
310         if (unlikely(vlan_tx_tag_present(skb))) {
311                 u16 current_tag;
312
313                 /* push down current VLAN tag */
314                 current_tag = vlan_tx_tag_get(skb);
315
316                 if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
317                         return -ENOMEM;
318
319                 /* Update mac_len for subsequent MPLS actions */
320                 skb->mac_len += VLAN_HLEN;
321
322                 if (skb->ip_summed == CHECKSUM_COMPLETE)
323                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
324                                         + (2 * ETH_ALEN), VLAN_HLEN, 0));
325
326                 invalidate_skb_flow_key(skb);
327         } else {
328                 flow_key_set_vlan_tci(skb,  vlan->vlan_tci);
329         }
330         __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
331         return 0;
332 }
333
334 static int set_eth_addr(struct sk_buff *skb,
335                         const struct ovs_key_ethernet *eth_key)
336 {
337         int err;
338         err = make_writable(skb, ETH_HLEN);
339         if (unlikely(err))
340                 return err;
341
342         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
343
344         ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
345         ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
346
347         ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
348
349         flow_key_set_eth_src(skb, eth_key->eth_src);
350         flow_key_set_eth_dst(skb, eth_key->eth_dst);
351         return 0;
352 }
353
354 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
355                         __be32 *addr, __be32 new_addr)
356 {
357         int transport_len = skb->len - skb_transport_offset(skb);
358
359         if (nh->protocol == IPPROTO_TCP) {
360                 if (likely(transport_len >= sizeof(struct tcphdr)))
361                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
362                                                  *addr, new_addr, 1);
363         } else if (nh->protocol == IPPROTO_UDP) {
364                 if (likely(transport_len >= sizeof(struct udphdr))) {
365                         struct udphdr *uh = udp_hdr(skb);
366
367                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
368                                 inet_proto_csum_replace4(&uh->check, skb,
369                                                          *addr, new_addr, 1);
370                                 if (!uh->check)
371                                         uh->check = CSUM_MANGLED_0;
372                         }
373                 }
374         }
375
376         csum_replace4(&nh->check, *addr, new_addr);
377         skb_clear_hash(skb);
378         *addr = new_addr;
379 }
380
381 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
382                                  __be32 addr[4], const __be32 new_addr[4])
383 {
384         int transport_len = skb->len - skb_transport_offset(skb);
385
386         if (l4_proto == IPPROTO_TCP) {
387                 if (likely(transport_len >= sizeof(struct tcphdr)))
388                         inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
389                                                   addr, new_addr, 1);
390         } else if (l4_proto == IPPROTO_UDP) {
391                 if (likely(transport_len >= sizeof(struct udphdr))) {
392                         struct udphdr *uh = udp_hdr(skb);
393
394                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
395                                 inet_proto_csum_replace16(&uh->check, skb,
396                                                           addr, new_addr, 1);
397                                 if (!uh->check)
398                                         uh->check = CSUM_MANGLED_0;
399                         }
400                 }
401         }
402 }
403
404 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
405                           __be32 addr[4], const __be32 new_addr[4],
406                           bool recalculate_csum)
407 {
408         if (likely(recalculate_csum))
409                 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
410
411         skb_clear_hash(skb);
412         memcpy(addr, new_addr, sizeof(__be32[4]));
413 }
414
415 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
416 {
417         nh->priority = tc >> 4;
418         nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
419 }
420
421 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
422 {
423         nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
424         nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
425         nh->flow_lbl[2] = fl & 0x000000FF;
426 }
427
428 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
429 {
430         csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
431         nh->ttl = new_ttl;
432 }
433
434 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
435 {
436         struct iphdr *nh;
437         int err;
438
439         err = make_writable(skb, skb_network_offset(skb) +
440                                  sizeof(struct iphdr));
441         if (unlikely(err))
442                 return err;
443
444         nh = ip_hdr(skb);
445
446         if (ipv4_key->ipv4_src != nh->saddr) {
447                 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
448                 flow_key_set_ipv4_src(skb, ipv4_key->ipv4_src);
449         }
450
451         if (ipv4_key->ipv4_dst != nh->daddr) {
452                 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
453                 flow_key_set_ipv4_dst(skb, ipv4_key->ipv4_dst);
454         }
455
456         if (ipv4_key->ipv4_tos != nh->tos) {
457                 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
458                 flow_key_set_ip_tos(skb, nh->tos);
459         }
460
461         if (ipv4_key->ipv4_ttl != nh->ttl) {
462                 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
463                 flow_key_set_ip_ttl(skb, ipv4_key->ipv4_ttl);
464         }
465
466         return 0;
467 }
468
469 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
470 {
471         struct ipv6hdr *nh;
472         int err;
473         __be32 *saddr;
474         __be32 *daddr;
475
476         err = make_writable(skb, skb_network_offset(skb) +
477                             sizeof(struct ipv6hdr));
478         if (unlikely(err))
479                 return err;
480
481         nh = ipv6_hdr(skb);
482         saddr = (__be32 *)&nh->saddr;
483         daddr = (__be32 *)&nh->daddr;
484
485         if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
486                 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
487                               ipv6_key->ipv6_src, true);
488                 flow_key_set_ipv6_src(skb, ipv6_key->ipv6_src);
489         }
490
491         if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
492                 unsigned int offset = 0;
493                 int flags = OVS_IP6T_FH_F_SKIP_RH;
494                 bool recalc_csum = true;
495
496                 if (ipv6_ext_hdr(nh->nexthdr))
497                         recalc_csum = ipv6_find_hdr(skb, &offset,
498                                                     NEXTHDR_ROUTING, NULL,
499                                                     &flags) != NEXTHDR_ROUTING;
500
501                 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
502                               ipv6_key->ipv6_dst, recalc_csum);
503                 flow_key_set_ipv6_dst(skb, ipv6_key->ipv6_dst);
504         }
505
506         set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
507         flow_key_set_ip_tos(skb, ipv6_get_dsfield(nh));
508
509         set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
510         flow_key_set_ipv6_fl(skb, nh);
511
512         nh->hop_limit = ipv6_key->ipv6_hlimit;
513         flow_key_set_ip_ttl(skb, ipv6_key->ipv6_hlimit);
514         return 0;
515 }
516
517 /* Must follow make_writable() since that can move the skb data. */
518 static void set_tp_port(struct sk_buff *skb, __be16 *port,
519                          __be16 new_port, __sum16 *check)
520 {
521         inet_proto_csum_replace2(check, skb, *port, new_port, 0);
522         *port = new_port;
523         skb_clear_hash(skb);
524 }
525
526 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
527 {
528         struct udphdr *uh = udp_hdr(skb);
529
530         if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
531                 set_tp_port(skb, port, new_port, &uh->check);
532
533                 if (!uh->check)
534                         uh->check = CSUM_MANGLED_0;
535         } else {
536                 *port = new_port;
537                 skb_clear_hash(skb);
538         }
539 }
540
541 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
542 {
543         struct udphdr *uh;
544         int err;
545
546         err = make_writable(skb, skb_transport_offset(skb) +
547                                  sizeof(struct udphdr));
548         if (unlikely(err))
549                 return err;
550
551         uh = udp_hdr(skb);
552         if (udp_port_key->udp_src != uh->source) {
553                 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
554                 flow_key_set_tp_src(skb, udp_port_key->udp_src);
555         }
556
557         if (udp_port_key->udp_dst != uh->dest) {
558                 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
559                 flow_key_set_tp_dst(skb, udp_port_key->udp_dst);
560         }
561
562         return 0;
563 }
564
565 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
566 {
567         struct tcphdr *th;
568         int err;
569
570         err = make_writable(skb, skb_transport_offset(skb) +
571                                  sizeof(struct tcphdr));
572         if (unlikely(err))
573                 return err;
574
575         th = tcp_hdr(skb);
576         if (tcp_port_key->tcp_src != th->source) {
577                 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
578                 flow_key_set_tp_src(skb, tcp_port_key->tcp_src);
579         }
580
581         if (tcp_port_key->tcp_dst != th->dest) {
582                 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
583                 flow_key_set_tp_dst(skb, tcp_port_key->tcp_dst);
584         }
585
586         return 0;
587 }
588
589 static int set_sctp(struct sk_buff *skb,
590                     const struct ovs_key_sctp *sctp_port_key)
591 {
592         struct sctphdr *sh;
593         int err;
594         unsigned int sctphoff = skb_transport_offset(skb);
595
596         err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
597         if (unlikely(err))
598                 return err;
599
600         sh = sctp_hdr(skb);
601         if (sctp_port_key->sctp_src != sh->source ||
602             sctp_port_key->sctp_dst != sh->dest) {
603                 __le32 old_correct_csum, new_csum, old_csum;
604
605                 old_csum = sh->checksum;
606                 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
607
608                 sh->source = sctp_port_key->sctp_src;
609                 sh->dest = sctp_port_key->sctp_dst;
610
611                 new_csum = sctp_compute_cksum(skb, sctphoff);
612
613                 /* Carry any checksum errors through. */
614                 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
615
616                 skb_clear_hash(skb);
617                 flow_key_set_tp_src(skb, sctp_port_key->sctp_src);
618                 flow_key_set_tp_dst(skb, sctp_port_key->sctp_dst);
619         }
620
621         return 0;
622 }
623
624 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
625 {
626         struct vport *vport = ovs_vport_rcu(dp, out_port);
627
628         if (likely(vport))
629                 ovs_vport_send(vport, skb);
630         else
631                 kfree_skb(skb);
632 }
633
634 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
635                             const struct nlattr *attr)
636 {
637         struct dp_upcall_info upcall;
638         const struct nlattr *a;
639         int rem;
640         struct ovs_tunnel_info info;
641
642         upcall.cmd = OVS_PACKET_CMD_ACTION;
643         upcall.userdata = NULL;
644         upcall.portid = 0;
645         upcall.egress_tun_info = NULL;
646
647         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
648                  a = nla_next(a, &rem)) {
649                 switch (nla_type(a)) {
650                 case OVS_USERSPACE_ATTR_USERDATA:
651                         upcall.userdata = a;
652                         break;
653
654                 case OVS_USERSPACE_ATTR_PID:
655                         upcall.portid = nla_get_u32(a);
656                         break;
657
658                 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
659                         /* Get out tunnel info. */
660                         struct vport *vport;
661
662                         vport = ovs_vport_rcu(dp, nla_get_u32(a));
663                         if (vport) {
664                                 int err;
665
666                                 err = ovs_vport_get_egress_tun_info(vport, skb,
667                                                                     &info);
668                                 if (!err)
669                                         upcall.egress_tun_info = &info;
670                         }
671                         break;
672                 }
673
674                 } /* End of switch. */
675         }
676
677         return ovs_dp_upcall(dp, skb, &upcall);
678 }
679
680 static bool last_action(const struct nlattr *a, int rem)
681 {
682         return a->nla_len == rem;
683 }
684
685 static int sample(struct datapath *dp, struct sk_buff *skb,
686                   const struct nlattr *attr)
687 {
688         struct sw_flow_key sample_key;
689         const struct nlattr *acts_list = NULL;
690         const struct nlattr *a;
691         struct sk_buff *sample_skb;
692         int rem;
693
694         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
695                  a = nla_next(a, &rem)) {
696                 switch (nla_type(a)) {
697                 case OVS_SAMPLE_ATTR_PROBABILITY:
698                         if (prandom_u32() >= nla_get_u32(a))
699                                 return 0;
700                         break;
701
702                 case OVS_SAMPLE_ATTR_ACTIONS:
703                         acts_list = a;
704                         break;
705                 }
706         }
707
708         rem = nla_len(acts_list);
709         a = nla_data(acts_list);
710
711         /* Actions list is either empty or only contains a single user-space
712          * action, the latter being a special case as it is the only known
713          * usage of the sample action.
714          * In these special cases don't clone the skb as there are no
715          * side-effects in the nested actions.
716          * Otherwise, clone in case the nested actions have side effects. */
717         if (likely(rem == 0 ||
718                    (nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
719                     last_action(a, rem)))) {
720                 sample_skb = skb;
721                 skb_get(skb);
722         } else {
723                 sample_skb = skb_clone(skb, GFP_ATOMIC);
724                 if (!sample_skb)
725                         /* Skip the sample action when out of memory. */
726                         return 0;
727
728                 flow_key_clone(skb, &sample_key);
729         }
730
731         /* Note that do_execute_actions() never consumes skb.
732          * In the case where skb has been cloned above it is the clone that
733          * is consumed.  Otherwise the skb_get(skb) call prevents
734          * consumption by do_execute_actions(). Thus, it is safe to simply
735          * return the error code and let the caller (also
736          * do_execute_actions()) free skb on error. */
737         return do_execute_actions(dp, sample_skb, a, rem);
738 }
739
740 static void execute_hash(struct sk_buff *skb, const struct nlattr *attr)
741 {
742         struct sw_flow_key *key = OVS_CB(skb)->pkt_key;
743         struct ovs_action_hash *hash_act = nla_data(attr);
744         u32 hash = 0;
745
746         /* OVS_HASH_ALG_L4 is the only possible hash algorithm.  */
747         hash = skb_get_hash(skb);
748         hash = jhash_1word(hash, hash_act->hash_basis);
749         if (!hash)
750                 hash = 0x1;
751
752         key->ovs_flow_hash = hash;
753 }
754
755 static int execute_set_action(struct sk_buff *skb,
756                                  const struct nlattr *nested_attr)
757 {
758         int err = 0;
759
760         switch (nla_type(nested_attr)) {
761         case OVS_KEY_ATTR_PRIORITY:
762                 skb->priority = nla_get_u32(nested_attr);
763                 flow_key_set_priority(skb, skb->priority);
764                 break;
765
766         case OVS_KEY_ATTR_SKB_MARK:
767                 skb->mark = nla_get_u32(nested_attr);
768                 flow_key_set_skb_mark(skb, skb->mark);
769                 break;
770
771         case OVS_KEY_ATTR_TUNNEL_INFO:
772                 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
773                 break;
774
775         case OVS_KEY_ATTR_ETHERNET:
776                 err = set_eth_addr(skb, nla_data(nested_attr));
777                 break;
778
779         case OVS_KEY_ATTR_IPV4:
780                 err = set_ipv4(skb, nla_data(nested_attr));
781                 break;
782
783         case OVS_KEY_ATTR_IPV6:
784                 err = set_ipv6(skb, nla_data(nested_attr));
785                 break;
786
787         case OVS_KEY_ATTR_TCP:
788                 err = set_tcp(skb, nla_data(nested_attr));
789                 break;
790
791         case OVS_KEY_ATTR_UDP:
792                 err = set_udp(skb, nla_data(nested_attr));
793                 break;
794
795         case OVS_KEY_ATTR_SCTP:
796                 err = set_sctp(skb, nla_data(nested_attr));
797                 break;
798
799         case OVS_KEY_ATTR_MPLS:
800                 err = set_mpls(skb, nla_data(nested_attr));
801                 break;
802         }
803
804         return err;
805 }
806
807
808 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
809                           const struct nlattr *a, int rem)
810 {
811         struct sw_flow_key recirc_key;
812         int err;
813
814         if (!last_action(a, rem)) {
815                 /* Recirc action is the not the last action
816                  * of the action list. */
817                 skb = skb_clone(skb, GFP_ATOMIC);
818
819                 /* Skip the recirc action when out of memory, but
820                  * continue on with the rest of the action list. */
821                 if (!skb)
822                         return 0;
823         }
824
825         if (!is_skb_flow_key_valid(skb)) {
826                 err = ovs_flow_key_update(skb, OVS_CB(skb)->pkt_key);
827                 if (err) {
828                         kfree_skb(skb);
829                         return err;
830                 }
831         }
832         BUG_ON(!is_skb_flow_key_valid(skb));
833
834         if (!last_action(a, rem))
835                 flow_key_clone(skb, &recirc_key);
836
837         flow_key_set_recirc_id(skb, nla_get_u32(a));
838         ovs_dp_process_packet(skb, true);
839         return 0;
840 }
841
842 /* Execute a list of actions against 'skb'. */
843 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
844                         const struct nlattr *attr, int len)
845 {
846         /* Every output action needs a separate clone of 'skb', but the common
847          * case is just a single output action, so that doing a clone and
848          * then freeing the original skbuff is wasteful.  So the following code
849          * is slightly obscure just to avoid that. */
850         int prev_port = -1;
851         const struct nlattr *a;
852         int rem;
853
854         for (a = attr, rem = len; rem > 0;
855              a = nla_next(a, &rem)) {
856                 int err = 0;
857
858                 if (unlikely(prev_port != -1)) {
859                         struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
860
861                         if (out_skb)
862                                 do_output(dp, out_skb, prev_port);
863
864                         prev_port = -1;
865                 }
866
867                 switch (nla_type(a)) {
868                 case OVS_ACTION_ATTR_OUTPUT:
869                         prev_port = nla_get_u32(a);
870                         break;
871
872                 case OVS_ACTION_ATTR_USERSPACE:
873                         output_userspace(dp, skb, a);
874                         break;
875
876                 case OVS_ACTION_ATTR_HASH:
877                         execute_hash(skb, a);
878                         break;
879
880                 case OVS_ACTION_ATTR_PUSH_MPLS:
881                         err = push_mpls(skb, nla_data(a));
882                         break;
883
884                 case OVS_ACTION_ATTR_POP_MPLS:
885                         err = pop_mpls(skb, nla_get_be16(a));
886                         break;
887
888                 case OVS_ACTION_ATTR_PUSH_VLAN:
889                         err = push_vlan(skb, nla_data(a));
890                         if (unlikely(err)) /* skb already freed. */
891                                 return err;
892                         break;
893
894                 case OVS_ACTION_ATTR_POP_VLAN:
895                         err = pop_vlan(skb);
896                         break;
897
898                 case OVS_ACTION_ATTR_RECIRC:
899                         err = execute_recirc(dp, skb, a, rem);
900                         break;
901
902                 case OVS_ACTION_ATTR_SET:
903                         err = execute_set_action(skb, nla_data(a));
904                         break;
905
906                 case OVS_ACTION_ATTR_SAMPLE:
907                         err = sample(dp, skb, a);
908                         break;
909                 }
910
911                 if (unlikely(err)) {
912                         kfree_skb(skb);
913                         return err;
914                 }
915         }
916
917         if (prev_port != -1)
918                 do_output(dp, skb, prev_port);
919         else
920                 consume_skb(skb);
921
922         return 0;
923 }
924
925 /* We limit the number of times that we pass into execute_actions()
926  * to avoid blowing out the stack in the event that we have a loop.
927  *
928  * Each loop adds some (estimated) cost to the kernel stack.
929  * The loop terminates when the max cost is exceeded.
930  * */
931 #define RECIRC_STACK_COST 1
932 #define DEFAULT_STACK_COST 4
933 /* Allow up to 4 regular services, and up to 3 recirculations */
934 #define MAX_STACK_COST (DEFAULT_STACK_COST * 4 + RECIRC_STACK_COST * 3)
935
936 struct loop_counter {
937         u8 stack_cost;          /* loop stack cost. */
938         bool looping;           /* Loop detected? */
939 };
940
941 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
942
943 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
944 {
945         if (net_ratelimit())
946                 pr_warn("%s: flow loop detected, dropping\n",
947                                 ovs_dp_name(dp));
948         actions->actions_len = 0;
949         return -ELOOP;
950 }
951
952 /* Execute a list of actions against 'skb'. */
953 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
954                         struct sw_flow_actions *acts, bool recirc)
955 {
956         const u8 stack_cost = recirc ? RECIRC_STACK_COST : DEFAULT_STACK_COST;
957         struct loop_counter *loop;
958         int error;
959
960         /* Check whether we've looped too much. */
961         loop = &__get_cpu_var(loop_counters);
962         loop->stack_cost += stack_cost;
963         if (unlikely(loop->stack_cost > MAX_STACK_COST))
964                 loop->looping = true;
965         if (unlikely(loop->looping)) {
966                 error = loop_suppress(dp, acts);
967                 kfree_skb(skb);
968                 goto out_loop;
969         }
970
971         error = do_execute_actions(dp, skb, acts->actions, acts->actions_len);
972
973         /* Check whether sub-actions looped too much. */
974         if (unlikely(loop->looping))
975                 error = loop_suppress(dp, acts);
976
977 out_loop:
978         /* Decrement loop stack cost. */
979         loop->stack_cost -= stack_cost;
980         if (!loop->stack_cost)
981                 loop->looping = false;
982
983         return error;
984 }