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