]> Git Repo - linux.git/blob - net/openvswitch/actions.c
net: bcmgenet: Fix return value check for fixed_phy_register()
[linux.git] / net / openvswitch / actions.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2017 Nicira, Inc.
4  */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/skbuff.h>
9 #include <linux/in.h>
10 #include <linux/ip.h>
11 #include <linux/openvswitch.h>
12 #include <linux/sctp.h>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/in6.h>
16 #include <linux/if_arp.h>
17 #include <linux/if_vlan.h>
18
19 #include <net/dst.h>
20 #include <net/gso.h>
21 #include <net/ip.h>
22 #include <net/ipv6.h>
23 #include <net/ip6_fib.h>
24 #include <net/checksum.h>
25 #include <net/dsfield.h>
26 #include <net/mpls.h>
27 #include <net/sctp/checksum.h>
28
29 #include "datapath.h"
30 #include "flow.h"
31 #include "conntrack.h"
32 #include "vport.h"
33 #include "flow_netlink.h"
34 #include "openvswitch_trace.h"
35
36 struct deferred_action {
37         struct sk_buff *skb;
38         const struct nlattr *actions;
39         int actions_len;
40
41         /* Store pkt_key clone when creating deferred action. */
42         struct sw_flow_key pkt_key;
43 };
44
45 #define MAX_L2_LEN      (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
46 struct ovs_frag_data {
47         unsigned long dst;
48         struct vport *vport;
49         struct ovs_skb_cb cb;
50         __be16 inner_protocol;
51         u16 network_offset;     /* valid only for MPLS */
52         u16 vlan_tci;
53         __be16 vlan_proto;
54         unsigned int l2_len;
55         u8 mac_proto;
56         u8 l2_data[MAX_L2_LEN];
57 };
58
59 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
60
61 #define DEFERRED_ACTION_FIFO_SIZE 10
62 #define OVS_RECURSION_LIMIT 5
63 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
64 struct action_fifo {
65         int head;
66         int tail;
67         /* Deferred action fifo queue storage. */
68         struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
69 };
70
71 struct action_flow_keys {
72         struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
73 };
74
75 static struct action_fifo __percpu *action_fifos;
76 static struct action_flow_keys __percpu *flow_keys;
77 static DEFINE_PER_CPU(int, exec_actions_level);
78
79 /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys'
80  * space. Return NULL if out of key spaces.
81  */
82 static struct sw_flow_key *clone_key(const struct sw_flow_key *key_)
83 {
84         struct action_flow_keys *keys = this_cpu_ptr(flow_keys);
85         int level = this_cpu_read(exec_actions_level);
86         struct sw_flow_key *key = NULL;
87
88         if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
89                 key = &keys->key[level - 1];
90                 *key = *key_;
91         }
92
93         return key;
94 }
95
96 static void action_fifo_init(struct action_fifo *fifo)
97 {
98         fifo->head = 0;
99         fifo->tail = 0;
100 }
101
102 static bool action_fifo_is_empty(const struct action_fifo *fifo)
103 {
104         return (fifo->head == fifo->tail);
105 }
106
107 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
108 {
109         if (action_fifo_is_empty(fifo))
110                 return NULL;
111
112         return &fifo->fifo[fifo->tail++];
113 }
114
115 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
116 {
117         if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
118                 return NULL;
119
120         return &fifo->fifo[fifo->head++];
121 }
122
123 /* Return true if fifo is not full */
124 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
125                                     const struct sw_flow_key *key,
126                                     const struct nlattr *actions,
127                                     const int actions_len)
128 {
129         struct action_fifo *fifo;
130         struct deferred_action *da;
131
132         fifo = this_cpu_ptr(action_fifos);
133         da = action_fifo_put(fifo);
134         if (da) {
135                 da->skb = skb;
136                 da->actions = actions;
137                 da->actions_len = actions_len;
138                 da->pkt_key = *key;
139         }
140
141         return da;
142 }
143
144 static void invalidate_flow_key(struct sw_flow_key *key)
145 {
146         key->mac_proto |= SW_FLOW_KEY_INVALID;
147 }
148
149 static bool is_flow_key_valid(const struct sw_flow_key *key)
150 {
151         return !(key->mac_proto & SW_FLOW_KEY_INVALID);
152 }
153
154 static int clone_execute(struct datapath *dp, struct sk_buff *skb,
155                          struct sw_flow_key *key,
156                          u32 recirc_id,
157                          const struct nlattr *actions, int len,
158                          bool last, bool clone_flow_key);
159
160 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
161                               struct sw_flow_key *key,
162                               const struct nlattr *attr, int len);
163
164 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
165                      __be32 mpls_lse, __be16 mpls_ethertype, __u16 mac_len)
166 {
167         int err;
168
169         err = skb_mpls_push(skb, mpls_lse, mpls_ethertype, mac_len, !!mac_len);
170         if (err)
171                 return err;
172
173         if (!mac_len)
174                 key->mac_proto = MAC_PROTO_NONE;
175
176         invalidate_flow_key(key);
177         return 0;
178 }
179
180 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
181                     const __be16 ethertype)
182 {
183         int err;
184
185         err = skb_mpls_pop(skb, ethertype, skb->mac_len,
186                            ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET);
187         if (err)
188                 return err;
189
190         if (ethertype == htons(ETH_P_TEB))
191                 key->mac_proto = MAC_PROTO_ETHERNET;
192
193         invalidate_flow_key(key);
194         return 0;
195 }
196
197 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
198                     const __be32 *mpls_lse, const __be32 *mask)
199 {
200         struct mpls_shim_hdr *stack;
201         __be32 lse;
202         int err;
203
204         if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
205                 return -ENOMEM;
206
207         stack = mpls_hdr(skb);
208         lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask);
209         err = skb_mpls_update_lse(skb, lse);
210         if (err)
211                 return err;
212
213         flow_key->mpls.lse[0] = lse;
214         return 0;
215 }
216
217 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
218 {
219         int err;
220
221         err = skb_vlan_pop(skb);
222         if (skb_vlan_tag_present(skb)) {
223                 invalidate_flow_key(key);
224         } else {
225                 key->eth.vlan.tci = 0;
226                 key->eth.vlan.tpid = 0;
227         }
228         return err;
229 }
230
231 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
232                      const struct ovs_action_push_vlan *vlan)
233 {
234         if (skb_vlan_tag_present(skb)) {
235                 invalidate_flow_key(key);
236         } else {
237                 key->eth.vlan.tci = vlan->vlan_tci;
238                 key->eth.vlan.tpid = vlan->vlan_tpid;
239         }
240         return skb_vlan_push(skb, vlan->vlan_tpid,
241                              ntohs(vlan->vlan_tci) & ~VLAN_CFI_MASK);
242 }
243
244 /* 'src' is already properly masked. */
245 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
246 {
247         u16 *dst = (u16 *)dst_;
248         const u16 *src = (const u16 *)src_;
249         const u16 *mask = (const u16 *)mask_;
250
251         OVS_SET_MASKED(dst[0], src[0], mask[0]);
252         OVS_SET_MASKED(dst[1], src[1], mask[1]);
253         OVS_SET_MASKED(dst[2], src[2], mask[2]);
254 }
255
256 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
257                         const struct ovs_key_ethernet *key,
258                         const struct ovs_key_ethernet *mask)
259 {
260         int err;
261
262         err = skb_ensure_writable(skb, ETH_HLEN);
263         if (unlikely(err))
264                 return err;
265
266         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
267
268         ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
269                                mask->eth_src);
270         ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
271                                mask->eth_dst);
272
273         skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
274
275         ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
276         ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
277         return 0;
278 }
279
280 /* pop_eth does not support VLAN packets as this action is never called
281  * for them.
282  */
283 static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
284 {
285         int err;
286
287         err = skb_eth_pop(skb);
288         if (err)
289                 return err;
290
291         /* safe right before invalidate_flow_key */
292         key->mac_proto = MAC_PROTO_NONE;
293         invalidate_flow_key(key);
294         return 0;
295 }
296
297 static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
298                     const struct ovs_action_push_eth *ethh)
299 {
300         int err;
301
302         err = skb_eth_push(skb, ethh->addresses.eth_dst,
303                            ethh->addresses.eth_src);
304         if (err)
305                 return err;
306
307         /* safe right before invalidate_flow_key */
308         key->mac_proto = MAC_PROTO_ETHERNET;
309         invalidate_flow_key(key);
310         return 0;
311 }
312
313 static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
314                     const struct nshhdr *nh)
315 {
316         int err;
317
318         err = nsh_push(skb, nh);
319         if (err)
320                 return err;
321
322         /* safe right before invalidate_flow_key */
323         key->mac_proto = MAC_PROTO_NONE;
324         invalidate_flow_key(key);
325         return 0;
326 }
327
328 static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key)
329 {
330         int err;
331
332         err = nsh_pop(skb);
333         if (err)
334                 return err;
335
336         /* safe right before invalidate_flow_key */
337         if (skb->protocol == htons(ETH_P_TEB))
338                 key->mac_proto = MAC_PROTO_ETHERNET;
339         else
340                 key->mac_proto = MAC_PROTO_NONE;
341         invalidate_flow_key(key);
342         return 0;
343 }
344
345 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
346                                   __be32 addr, __be32 new_addr)
347 {
348         int transport_len = skb->len - skb_transport_offset(skb);
349
350         if (nh->frag_off & htons(IP_OFFSET))
351                 return;
352
353         if (nh->protocol == IPPROTO_TCP) {
354                 if (likely(transport_len >= sizeof(struct tcphdr)))
355                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
356                                                  addr, new_addr, true);
357         } else if (nh->protocol == IPPROTO_UDP) {
358                 if (likely(transport_len >= sizeof(struct udphdr))) {
359                         struct udphdr *uh = udp_hdr(skb);
360
361                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
362                                 inet_proto_csum_replace4(&uh->check, skb,
363                                                          addr, new_addr, true);
364                                 if (!uh->check)
365                                         uh->check = CSUM_MANGLED_0;
366                         }
367                 }
368         }
369 }
370
371 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
372                         __be32 *addr, __be32 new_addr)
373 {
374         update_ip_l4_checksum(skb, nh, *addr, new_addr);
375         csum_replace4(&nh->check, *addr, new_addr);
376         skb_clear_hash(skb);
377         ovs_ct_clear(skb, NULL);
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 == NEXTHDR_TCP) {
387                 if (likely(transport_len >= sizeof(struct tcphdr)))
388                         inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
389                                                   addr, new_addr, true);
390         } else if (l4_proto == NEXTHDR_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, true);
397                                 if (!uh->check)
398                                         uh->check = CSUM_MANGLED_0;
399                         }
400                 }
401         } else if (l4_proto == NEXTHDR_ICMP) {
402                 if (likely(transport_len >= sizeof(struct icmp6hdr)))
403                         inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
404                                                   skb, addr, new_addr, true);
405         }
406 }
407
408 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
409                            const __be32 mask[4], __be32 masked[4])
410 {
411         masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
412         masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
413         masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
414         masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
415 }
416
417 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
418                           __be32 addr[4], const __be32 new_addr[4],
419                           bool recalculate_csum)
420 {
421         if (recalculate_csum)
422                 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
423
424         skb_clear_hash(skb);
425         ovs_ct_clear(skb, NULL);
426         memcpy(addr, new_addr, sizeof(__be32[4]));
427 }
428
429 static void set_ipv6_dsfield(struct sk_buff *skb, struct ipv6hdr *nh, u8 ipv6_tclass, u8 mask)
430 {
431         u8 old_ipv6_tclass = ipv6_get_dsfield(nh);
432
433         ipv6_tclass = OVS_MASKED(old_ipv6_tclass, ipv6_tclass, mask);
434
435         if (skb->ip_summed == CHECKSUM_COMPLETE)
436                 csum_replace(&skb->csum, (__force __wsum)(old_ipv6_tclass << 12),
437                              (__force __wsum)(ipv6_tclass << 12));
438
439         ipv6_change_dsfield(nh, ~mask, ipv6_tclass);
440 }
441
442 static void set_ipv6_fl(struct sk_buff *skb, struct ipv6hdr *nh, u32 fl, u32 mask)
443 {
444         u32 ofl;
445
446         ofl = nh->flow_lbl[0] << 16 |  nh->flow_lbl[1] << 8 |  nh->flow_lbl[2];
447         fl = OVS_MASKED(ofl, fl, mask);
448
449         /* Bits 21-24 are always unmasked, so this retains their values. */
450         nh->flow_lbl[0] = (u8)(fl >> 16);
451         nh->flow_lbl[1] = (u8)(fl >> 8);
452         nh->flow_lbl[2] = (u8)fl;
453
454         if (skb->ip_summed == CHECKSUM_COMPLETE)
455                 csum_replace(&skb->csum, (__force __wsum)htonl(ofl), (__force __wsum)htonl(fl));
456 }
457
458 static void set_ipv6_ttl(struct sk_buff *skb, struct ipv6hdr *nh, u8 new_ttl, u8 mask)
459 {
460         new_ttl = OVS_MASKED(nh->hop_limit, new_ttl, mask);
461
462         if (skb->ip_summed == CHECKSUM_COMPLETE)
463                 csum_replace(&skb->csum, (__force __wsum)(nh->hop_limit << 8),
464                              (__force __wsum)(new_ttl << 8));
465         nh->hop_limit = new_ttl;
466 }
467
468 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
469                        u8 mask)
470 {
471         new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
472
473         csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
474         nh->ttl = new_ttl;
475 }
476
477 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
478                     const struct ovs_key_ipv4 *key,
479                     const struct ovs_key_ipv4 *mask)
480 {
481         struct iphdr *nh;
482         __be32 new_addr;
483         int err;
484
485         err = skb_ensure_writable(skb, skb_network_offset(skb) +
486                                   sizeof(struct iphdr));
487         if (unlikely(err))
488                 return err;
489
490         nh = ip_hdr(skb);
491
492         /* Setting an IP addresses is typically only a side effect of
493          * matching on them in the current userspace implementation, so it
494          * makes sense to check if the value actually changed.
495          */
496         if (mask->ipv4_src) {
497                 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
498
499                 if (unlikely(new_addr != nh->saddr)) {
500                         set_ip_addr(skb, nh, &nh->saddr, new_addr);
501                         flow_key->ipv4.addr.src = new_addr;
502                 }
503         }
504         if (mask->ipv4_dst) {
505                 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
506
507                 if (unlikely(new_addr != nh->daddr)) {
508                         set_ip_addr(skb, nh, &nh->daddr, new_addr);
509                         flow_key->ipv4.addr.dst = new_addr;
510                 }
511         }
512         if (mask->ipv4_tos) {
513                 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
514                 flow_key->ip.tos = nh->tos;
515         }
516         if (mask->ipv4_ttl) {
517                 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
518                 flow_key->ip.ttl = nh->ttl;
519         }
520
521         return 0;
522 }
523
524 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
525 {
526         return !!(addr[0] | addr[1] | addr[2] | addr[3]);
527 }
528
529 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
530                     const struct ovs_key_ipv6 *key,
531                     const struct ovs_key_ipv6 *mask)
532 {
533         struct ipv6hdr *nh;
534         int err;
535
536         err = skb_ensure_writable(skb, skb_network_offset(skb) +
537                                   sizeof(struct ipv6hdr));
538         if (unlikely(err))
539                 return err;
540
541         nh = ipv6_hdr(skb);
542
543         /* Setting an IP addresses is typically only a side effect of
544          * matching on them in the current userspace implementation, so it
545          * makes sense to check if the value actually changed.
546          */
547         if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
548                 __be32 *saddr = (__be32 *)&nh->saddr;
549                 __be32 masked[4];
550
551                 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
552
553                 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
554                         set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
555                                       true);
556                         memcpy(&flow_key->ipv6.addr.src, masked,
557                                sizeof(flow_key->ipv6.addr.src));
558                 }
559         }
560         if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
561                 unsigned int offset = 0;
562                 int flags = IP6_FH_F_SKIP_RH;
563                 bool recalc_csum = true;
564                 __be32 *daddr = (__be32 *)&nh->daddr;
565                 __be32 masked[4];
566
567                 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
568
569                 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
570                         if (ipv6_ext_hdr(nh->nexthdr))
571                                 recalc_csum = (ipv6_find_hdr(skb, &offset,
572                                                              NEXTHDR_ROUTING,
573                                                              NULL, &flags)
574                                                != NEXTHDR_ROUTING);
575
576                         set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
577                                       recalc_csum);
578                         memcpy(&flow_key->ipv6.addr.dst, masked,
579                                sizeof(flow_key->ipv6.addr.dst));
580                 }
581         }
582         if (mask->ipv6_tclass) {
583                 set_ipv6_dsfield(skb, nh, key->ipv6_tclass, mask->ipv6_tclass);
584                 flow_key->ip.tos = ipv6_get_dsfield(nh);
585         }
586         if (mask->ipv6_label) {
587                 set_ipv6_fl(skb, nh, ntohl(key->ipv6_label),
588                             ntohl(mask->ipv6_label));
589                 flow_key->ipv6.label =
590                     *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
591         }
592         if (mask->ipv6_hlimit) {
593                 set_ipv6_ttl(skb, nh, key->ipv6_hlimit, mask->ipv6_hlimit);
594                 flow_key->ip.ttl = nh->hop_limit;
595         }
596         return 0;
597 }
598
599 static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key,
600                    const struct nlattr *a)
601 {
602         struct nshhdr *nh;
603         size_t length;
604         int err;
605         u8 flags;
606         u8 ttl;
607         int i;
608
609         struct ovs_key_nsh key;
610         struct ovs_key_nsh mask;
611
612         err = nsh_key_from_nlattr(a, &key, &mask);
613         if (err)
614                 return err;
615
616         /* Make sure the NSH base header is there */
617         if (!pskb_may_pull(skb, skb_network_offset(skb) + NSH_BASE_HDR_LEN))
618                 return -ENOMEM;
619
620         nh = nsh_hdr(skb);
621         length = nsh_hdr_len(nh);
622
623         /* Make sure the whole NSH header is there */
624         err = skb_ensure_writable(skb, skb_network_offset(skb) +
625                                        length);
626         if (unlikely(err))
627                 return err;
628
629         nh = nsh_hdr(skb);
630         skb_postpull_rcsum(skb, nh, length);
631         flags = nsh_get_flags(nh);
632         flags = OVS_MASKED(flags, key.base.flags, mask.base.flags);
633         flow_key->nsh.base.flags = flags;
634         ttl = nsh_get_ttl(nh);
635         ttl = OVS_MASKED(ttl, key.base.ttl, mask.base.ttl);
636         flow_key->nsh.base.ttl = ttl;
637         nsh_set_flags_and_ttl(nh, flags, ttl);
638         nh->path_hdr = OVS_MASKED(nh->path_hdr, key.base.path_hdr,
639                                   mask.base.path_hdr);
640         flow_key->nsh.base.path_hdr = nh->path_hdr;
641         switch (nh->mdtype) {
642         case NSH_M_TYPE1:
643                 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) {
644                         nh->md1.context[i] =
645                             OVS_MASKED(nh->md1.context[i], key.context[i],
646                                        mask.context[i]);
647                 }
648                 memcpy(flow_key->nsh.context, nh->md1.context,
649                        sizeof(nh->md1.context));
650                 break;
651         case NSH_M_TYPE2:
652                 memset(flow_key->nsh.context, 0,
653                        sizeof(flow_key->nsh.context));
654                 break;
655         default:
656                 return -EINVAL;
657         }
658         skb_postpush_rcsum(skb, nh, length);
659         return 0;
660 }
661
662 /* Must follow skb_ensure_writable() since that can move the skb data. */
663 static void set_tp_port(struct sk_buff *skb, __be16 *port,
664                         __be16 new_port, __sum16 *check)
665 {
666         ovs_ct_clear(skb, NULL);
667         inet_proto_csum_replace2(check, skb, *port, new_port, false);
668         *port = new_port;
669 }
670
671 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
672                    const struct ovs_key_udp *key,
673                    const struct ovs_key_udp *mask)
674 {
675         struct udphdr *uh;
676         __be16 src, dst;
677         int err;
678
679         err = skb_ensure_writable(skb, skb_transport_offset(skb) +
680                                   sizeof(struct udphdr));
681         if (unlikely(err))
682                 return err;
683
684         uh = udp_hdr(skb);
685         /* Either of the masks is non-zero, so do not bother checking them. */
686         src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
687         dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
688
689         if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
690                 if (likely(src != uh->source)) {
691                         set_tp_port(skb, &uh->source, src, &uh->check);
692                         flow_key->tp.src = src;
693                 }
694                 if (likely(dst != uh->dest)) {
695                         set_tp_port(skb, &uh->dest, dst, &uh->check);
696                         flow_key->tp.dst = dst;
697                 }
698
699                 if (unlikely(!uh->check))
700                         uh->check = CSUM_MANGLED_0;
701         } else {
702                 uh->source = src;
703                 uh->dest = dst;
704                 flow_key->tp.src = src;
705                 flow_key->tp.dst = dst;
706                 ovs_ct_clear(skb, NULL);
707         }
708
709         skb_clear_hash(skb);
710
711         return 0;
712 }
713
714 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
715                    const struct ovs_key_tcp *key,
716                    const struct ovs_key_tcp *mask)
717 {
718         struct tcphdr *th;
719         __be16 src, dst;
720         int err;
721
722         err = skb_ensure_writable(skb, skb_transport_offset(skb) +
723                                   sizeof(struct tcphdr));
724         if (unlikely(err))
725                 return err;
726
727         th = tcp_hdr(skb);
728         src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
729         if (likely(src != th->source)) {
730                 set_tp_port(skb, &th->source, src, &th->check);
731                 flow_key->tp.src = src;
732         }
733         dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
734         if (likely(dst != th->dest)) {
735                 set_tp_port(skb, &th->dest, dst, &th->check);
736                 flow_key->tp.dst = dst;
737         }
738         skb_clear_hash(skb);
739
740         return 0;
741 }
742
743 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
744                     const struct ovs_key_sctp *key,
745                     const struct ovs_key_sctp *mask)
746 {
747         unsigned int sctphoff = skb_transport_offset(skb);
748         struct sctphdr *sh;
749         __le32 old_correct_csum, new_csum, old_csum;
750         int err;
751
752         err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
753         if (unlikely(err))
754                 return err;
755
756         sh = sctp_hdr(skb);
757         old_csum = sh->checksum;
758         old_correct_csum = sctp_compute_cksum(skb, sctphoff);
759
760         sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
761         sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
762
763         new_csum = sctp_compute_cksum(skb, sctphoff);
764
765         /* Carry any checksum errors through. */
766         sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
767
768         skb_clear_hash(skb);
769         ovs_ct_clear(skb, NULL);
770
771         flow_key->tp.src = sh->source;
772         flow_key->tp.dst = sh->dest;
773
774         return 0;
775 }
776
777 static int ovs_vport_output(struct net *net, struct sock *sk,
778                             struct sk_buff *skb)
779 {
780         struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
781         struct vport *vport = data->vport;
782
783         if (skb_cow_head(skb, data->l2_len) < 0) {
784                 kfree_skb(skb);
785                 return -ENOMEM;
786         }
787
788         __skb_dst_copy(skb, data->dst);
789         *OVS_CB(skb) = data->cb;
790         skb->inner_protocol = data->inner_protocol;
791         if (data->vlan_tci & VLAN_CFI_MASK)
792                 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci & ~VLAN_CFI_MASK);
793         else
794                 __vlan_hwaccel_clear_tag(skb);
795
796         /* Reconstruct the MAC header.  */
797         skb_push(skb, data->l2_len);
798         memcpy(skb->data, &data->l2_data, data->l2_len);
799         skb_postpush_rcsum(skb, skb->data, data->l2_len);
800         skb_reset_mac_header(skb);
801
802         if (eth_p_mpls(skb->protocol)) {
803                 skb->inner_network_header = skb->network_header;
804                 skb_set_network_header(skb, data->network_offset);
805                 skb_reset_mac_len(skb);
806         }
807
808         ovs_vport_send(vport, skb, data->mac_proto);
809         return 0;
810 }
811
812 static unsigned int
813 ovs_dst_get_mtu(const struct dst_entry *dst)
814 {
815         return dst->dev->mtu;
816 }
817
818 static struct dst_ops ovs_dst_ops = {
819         .family = AF_UNSPEC,
820         .mtu = ovs_dst_get_mtu,
821 };
822
823 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
824  * ovs_vport_output(), which is called once per fragmented packet.
825  */
826 static void prepare_frag(struct vport *vport, struct sk_buff *skb,
827                          u16 orig_network_offset, u8 mac_proto)
828 {
829         unsigned int hlen = skb_network_offset(skb);
830         struct ovs_frag_data *data;
831
832         data = this_cpu_ptr(&ovs_frag_data_storage);
833         data->dst = skb->_skb_refdst;
834         data->vport = vport;
835         data->cb = *OVS_CB(skb);
836         data->inner_protocol = skb->inner_protocol;
837         data->network_offset = orig_network_offset;
838         if (skb_vlan_tag_present(skb))
839                 data->vlan_tci = skb_vlan_tag_get(skb) | VLAN_CFI_MASK;
840         else
841                 data->vlan_tci = 0;
842         data->vlan_proto = skb->vlan_proto;
843         data->mac_proto = mac_proto;
844         data->l2_len = hlen;
845         memcpy(&data->l2_data, skb->data, hlen);
846
847         memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
848         skb_pull(skb, hlen);
849 }
850
851 static void ovs_fragment(struct net *net, struct vport *vport,
852                          struct sk_buff *skb, u16 mru,
853                          struct sw_flow_key *key)
854 {
855         u16 orig_network_offset = 0;
856
857         if (eth_p_mpls(skb->protocol)) {
858                 orig_network_offset = skb_network_offset(skb);
859                 skb->network_header = skb->inner_network_header;
860         }
861
862         if (skb_network_offset(skb) > MAX_L2_LEN) {
863                 OVS_NLERR(1, "L2 header too long to fragment");
864                 goto err;
865         }
866
867         if (key->eth.type == htons(ETH_P_IP)) {
868                 struct rtable ovs_rt = { 0 };
869                 unsigned long orig_dst;
870
871                 prepare_frag(vport, skb, orig_network_offset,
872                              ovs_key_mac_proto(key));
873                 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
874                          DST_OBSOLETE_NONE, DST_NOCOUNT);
875                 ovs_rt.dst.dev = vport->dev;
876
877                 orig_dst = skb->_skb_refdst;
878                 skb_dst_set_noref(skb, &ovs_rt.dst);
879                 IPCB(skb)->frag_max_size = mru;
880
881                 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
882                 refdst_drop(orig_dst);
883         } else if (key->eth.type == htons(ETH_P_IPV6)) {
884                 unsigned long orig_dst;
885                 struct rt6_info ovs_rt;
886
887                 prepare_frag(vport, skb, orig_network_offset,
888                              ovs_key_mac_proto(key));
889                 memset(&ovs_rt, 0, sizeof(ovs_rt));
890                 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
891                          DST_OBSOLETE_NONE, DST_NOCOUNT);
892                 ovs_rt.dst.dev = vport->dev;
893
894                 orig_dst = skb->_skb_refdst;
895                 skb_dst_set_noref(skb, &ovs_rt.dst);
896                 IP6CB(skb)->frag_max_size = mru;
897
898                 ipv6_stub->ipv6_fragment(net, skb->sk, skb, ovs_vport_output);
899                 refdst_drop(orig_dst);
900         } else {
901                 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
902                           ovs_vport_name(vport), ntohs(key->eth.type), mru,
903                           vport->dev->mtu);
904                 goto err;
905         }
906
907         return;
908 err:
909         kfree_skb(skb);
910 }
911
912 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
913                       struct sw_flow_key *key)
914 {
915         struct vport *vport = ovs_vport_rcu(dp, out_port);
916
917         if (likely(vport && netif_carrier_ok(vport->dev))) {
918                 u16 mru = OVS_CB(skb)->mru;
919                 u32 cutlen = OVS_CB(skb)->cutlen;
920
921                 if (unlikely(cutlen > 0)) {
922                         if (skb->len - cutlen > ovs_mac_header_len(key))
923                                 pskb_trim(skb, skb->len - cutlen);
924                         else
925                                 pskb_trim(skb, ovs_mac_header_len(key));
926                 }
927
928                 if (likely(!mru ||
929                            (skb->len <= mru + vport->dev->hard_header_len))) {
930                         ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
931                 } else if (mru <= vport->dev->mtu) {
932                         struct net *net = read_pnet(&dp->net);
933
934                         ovs_fragment(net, vport, skb, mru, key);
935                 } else {
936                         kfree_skb(skb);
937                 }
938         } else {
939                 kfree_skb(skb);
940         }
941 }
942
943 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
944                             struct sw_flow_key *key, const struct nlattr *attr,
945                             const struct nlattr *actions, int actions_len,
946                             uint32_t cutlen)
947 {
948         struct dp_upcall_info upcall;
949         const struct nlattr *a;
950         int rem;
951
952         memset(&upcall, 0, sizeof(upcall));
953         upcall.cmd = OVS_PACKET_CMD_ACTION;
954         upcall.mru = OVS_CB(skb)->mru;
955
956         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
957              a = nla_next(a, &rem)) {
958                 switch (nla_type(a)) {
959                 case OVS_USERSPACE_ATTR_USERDATA:
960                         upcall.userdata = a;
961                         break;
962
963                 case OVS_USERSPACE_ATTR_PID:
964                         if (dp->user_features &
965                             OVS_DP_F_DISPATCH_UPCALL_PER_CPU)
966                                 upcall.portid =
967                                   ovs_dp_get_upcall_portid(dp,
968                                                            smp_processor_id());
969                         else
970                                 upcall.portid = nla_get_u32(a);
971                         break;
972
973                 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
974                         /* Get out tunnel info. */
975                         struct vport *vport;
976
977                         vport = ovs_vport_rcu(dp, nla_get_u32(a));
978                         if (vport) {
979                                 int err;
980
981                                 err = dev_fill_metadata_dst(vport->dev, skb);
982                                 if (!err)
983                                         upcall.egress_tun_info = skb_tunnel_info(skb);
984                         }
985
986                         break;
987                 }
988
989                 case OVS_USERSPACE_ATTR_ACTIONS: {
990                         /* Include actions. */
991                         upcall.actions = actions;
992                         upcall.actions_len = actions_len;
993                         break;
994                 }
995
996                 } /* End of switch. */
997         }
998
999         return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
1000 }
1001
1002 static int dec_ttl_exception_handler(struct datapath *dp, struct sk_buff *skb,
1003                                      struct sw_flow_key *key,
1004                                      const struct nlattr *attr)
1005 {
1006         /* The first attribute is always 'OVS_DEC_TTL_ATTR_ACTION'. */
1007         struct nlattr *actions = nla_data(attr);
1008
1009         if (nla_len(actions))
1010                 return clone_execute(dp, skb, key, 0, nla_data(actions),
1011                                      nla_len(actions), true, false);
1012
1013         consume_skb(skb);
1014         return 0;
1015 }
1016
1017 /* When 'last' is true, sample() should always consume the 'skb'.
1018  * Otherwise, sample() should keep 'skb' intact regardless what
1019  * actions are executed within sample().
1020  */
1021 static int sample(struct datapath *dp, struct sk_buff *skb,
1022                   struct sw_flow_key *key, const struct nlattr *attr,
1023                   bool last)
1024 {
1025         struct nlattr *actions;
1026         struct nlattr *sample_arg;
1027         int rem = nla_len(attr);
1028         const struct sample_arg *arg;
1029         bool clone_flow_key;
1030
1031         /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
1032         sample_arg = nla_data(attr);
1033         arg = nla_data(sample_arg);
1034         actions = nla_next(sample_arg, &rem);
1035
1036         if ((arg->probability != U32_MAX) &&
1037             (!arg->probability || get_random_u32() > arg->probability)) {
1038                 if (last)
1039                         consume_skb(skb);
1040                 return 0;
1041         }
1042
1043         clone_flow_key = !arg->exec;
1044         return clone_execute(dp, skb, key, 0, actions, rem, last,
1045                              clone_flow_key);
1046 }
1047
1048 /* When 'last' is true, clone() should always consume the 'skb'.
1049  * Otherwise, clone() should keep 'skb' intact regardless what
1050  * actions are executed within clone().
1051  */
1052 static int clone(struct datapath *dp, struct sk_buff *skb,
1053                  struct sw_flow_key *key, const struct nlattr *attr,
1054                  bool last)
1055 {
1056         struct nlattr *actions;
1057         struct nlattr *clone_arg;
1058         int rem = nla_len(attr);
1059         bool dont_clone_flow_key;
1060
1061         /* The first action is always 'OVS_CLONE_ATTR_EXEC'. */
1062         clone_arg = nla_data(attr);
1063         dont_clone_flow_key = nla_get_u32(clone_arg);
1064         actions = nla_next(clone_arg, &rem);
1065
1066         return clone_execute(dp, skb, key, 0, actions, rem, last,
1067                              !dont_clone_flow_key);
1068 }
1069
1070 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
1071                          const struct nlattr *attr)
1072 {
1073         struct ovs_action_hash *hash_act = nla_data(attr);
1074         u32 hash = 0;
1075
1076         if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
1077                 /* OVS_HASH_ALG_L4 hasing type. */
1078                 hash = skb_get_hash(skb);
1079         } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) {
1080                 /* OVS_HASH_ALG_SYM_L4 hashing type.  NOTE: this doesn't
1081                  * extend past an encapsulated header.
1082                  */
1083                 hash = __skb_get_hash_symmetric(skb);
1084         }
1085
1086         hash = jhash_1word(hash, hash_act->hash_basis);
1087         if (!hash)
1088                 hash = 0x1;
1089
1090         key->ovs_flow_hash = hash;
1091 }
1092
1093 static int execute_set_action(struct sk_buff *skb,
1094                               struct sw_flow_key *flow_key,
1095                               const struct nlattr *a)
1096 {
1097         /* Only tunnel set execution is supported without a mask. */
1098         if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
1099                 struct ovs_tunnel_info *tun = nla_data(a);
1100
1101                 skb_dst_drop(skb);
1102                 dst_hold((struct dst_entry *)tun->tun_dst);
1103                 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
1104                 return 0;
1105         }
1106
1107         return -EINVAL;
1108 }
1109
1110 /* Mask is at the midpoint of the data. */
1111 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1112
1113 static int execute_masked_set_action(struct sk_buff *skb,
1114                                      struct sw_flow_key *flow_key,
1115                                      const struct nlattr *a)
1116 {
1117         int err = 0;
1118
1119         switch (nla_type(a)) {
1120         case OVS_KEY_ATTR_PRIORITY:
1121                 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
1122                                *get_mask(a, u32 *));
1123                 flow_key->phy.priority = skb->priority;
1124                 break;
1125
1126         case OVS_KEY_ATTR_SKB_MARK:
1127                 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
1128                 flow_key->phy.skb_mark = skb->mark;
1129                 break;
1130
1131         case OVS_KEY_ATTR_TUNNEL_INFO:
1132                 /* Masked data not supported for tunnel. */
1133                 err = -EINVAL;
1134                 break;
1135
1136         case OVS_KEY_ATTR_ETHERNET:
1137                 err = set_eth_addr(skb, flow_key, nla_data(a),
1138                                    get_mask(a, struct ovs_key_ethernet *));
1139                 break;
1140
1141         case OVS_KEY_ATTR_NSH:
1142                 err = set_nsh(skb, flow_key, a);
1143                 break;
1144
1145         case OVS_KEY_ATTR_IPV4:
1146                 err = set_ipv4(skb, flow_key, nla_data(a),
1147                                get_mask(a, struct ovs_key_ipv4 *));
1148                 break;
1149
1150         case OVS_KEY_ATTR_IPV6:
1151                 err = set_ipv6(skb, flow_key, nla_data(a),
1152                                get_mask(a, struct ovs_key_ipv6 *));
1153                 break;
1154
1155         case OVS_KEY_ATTR_TCP:
1156                 err = set_tcp(skb, flow_key, nla_data(a),
1157                               get_mask(a, struct ovs_key_tcp *));
1158                 break;
1159
1160         case OVS_KEY_ATTR_UDP:
1161                 err = set_udp(skb, flow_key, nla_data(a),
1162                               get_mask(a, struct ovs_key_udp *));
1163                 break;
1164
1165         case OVS_KEY_ATTR_SCTP:
1166                 err = set_sctp(skb, flow_key, nla_data(a),
1167                                get_mask(a, struct ovs_key_sctp *));
1168                 break;
1169
1170         case OVS_KEY_ATTR_MPLS:
1171                 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1172                                                                     __be32 *));
1173                 break;
1174
1175         case OVS_KEY_ATTR_CT_STATE:
1176         case OVS_KEY_ATTR_CT_ZONE:
1177         case OVS_KEY_ATTR_CT_MARK:
1178         case OVS_KEY_ATTR_CT_LABELS:
1179         case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
1180         case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
1181                 err = -EINVAL;
1182                 break;
1183         }
1184
1185         return err;
1186 }
1187
1188 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1189                           struct sw_flow_key *key,
1190                           const struct nlattr *a, bool last)
1191 {
1192         u32 recirc_id;
1193
1194         if (!is_flow_key_valid(key)) {
1195                 int err;
1196
1197                 err = ovs_flow_key_update(skb, key);
1198                 if (err)
1199                         return err;
1200         }
1201         BUG_ON(!is_flow_key_valid(key));
1202
1203         recirc_id = nla_get_u32(a);
1204         return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true);
1205 }
1206
1207 static int execute_check_pkt_len(struct datapath *dp, struct sk_buff *skb,
1208                                  struct sw_flow_key *key,
1209                                  const struct nlattr *attr, bool last)
1210 {
1211         struct ovs_skb_cb *ovs_cb = OVS_CB(skb);
1212         const struct nlattr *actions, *cpl_arg;
1213         int len, max_len, rem = nla_len(attr);
1214         const struct check_pkt_len_arg *arg;
1215         bool clone_flow_key;
1216
1217         /* The first netlink attribute in 'attr' is always
1218          * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
1219          */
1220         cpl_arg = nla_data(attr);
1221         arg = nla_data(cpl_arg);
1222
1223         len = ovs_cb->mru ? ovs_cb->mru + skb->mac_len : skb->len;
1224         max_len = arg->pkt_len;
1225
1226         if ((skb_is_gso(skb) && skb_gso_validate_mac_len(skb, max_len)) ||
1227             len <= max_len) {
1228                 /* Second netlink attribute in 'attr' is always
1229                  * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
1230                  */
1231                 actions = nla_next(cpl_arg, &rem);
1232                 clone_flow_key = !arg->exec_for_lesser_equal;
1233         } else {
1234                 /* Third netlink attribute in 'attr' is always
1235                  * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER'.
1236                  */
1237                 actions = nla_next(cpl_arg, &rem);
1238                 actions = nla_next(actions, &rem);
1239                 clone_flow_key = !arg->exec_for_greater;
1240         }
1241
1242         return clone_execute(dp, skb, key, 0, nla_data(actions),
1243                              nla_len(actions), last, clone_flow_key);
1244 }
1245
1246 static int execute_dec_ttl(struct sk_buff *skb, struct sw_flow_key *key)
1247 {
1248         int err;
1249
1250         if (skb->protocol == htons(ETH_P_IPV6)) {
1251                 struct ipv6hdr *nh;
1252
1253                 err = skb_ensure_writable(skb, skb_network_offset(skb) +
1254                                           sizeof(*nh));
1255                 if (unlikely(err))
1256                         return err;
1257
1258                 nh = ipv6_hdr(skb);
1259
1260                 if (nh->hop_limit <= 1)
1261                         return -EHOSTUNREACH;
1262
1263                 key->ip.ttl = --nh->hop_limit;
1264         } else if (skb->protocol == htons(ETH_P_IP)) {
1265                 struct iphdr *nh;
1266                 u8 old_ttl;
1267
1268                 err = skb_ensure_writable(skb, skb_network_offset(skb) +
1269                                           sizeof(*nh));
1270                 if (unlikely(err))
1271                         return err;
1272
1273                 nh = ip_hdr(skb);
1274                 if (nh->ttl <= 1)
1275                         return -EHOSTUNREACH;
1276
1277                 old_ttl = nh->ttl--;
1278                 csum_replace2(&nh->check, htons(old_ttl << 8),
1279                               htons(nh->ttl << 8));
1280                 key->ip.ttl = nh->ttl;
1281         }
1282         return 0;
1283 }
1284
1285 /* Execute a list of actions against 'skb'. */
1286 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1287                               struct sw_flow_key *key,
1288                               const struct nlattr *attr, int len)
1289 {
1290         const struct nlattr *a;
1291         int rem;
1292
1293         for (a = attr, rem = len; rem > 0;
1294              a = nla_next(a, &rem)) {
1295                 int err = 0;
1296
1297                 if (trace_ovs_do_execute_action_enabled())
1298                         trace_ovs_do_execute_action(dp, skb, key, a, rem);
1299
1300                 switch (nla_type(a)) {
1301                 case OVS_ACTION_ATTR_OUTPUT: {
1302                         int port = nla_get_u32(a);
1303                         struct sk_buff *clone;
1304
1305                         /* Every output action needs a separate clone
1306                          * of 'skb', In case the output action is the
1307                          * last action, cloning can be avoided.
1308                          */
1309                         if (nla_is_last(a, rem)) {
1310                                 do_output(dp, skb, port, key);
1311                                 /* 'skb' has been used for output.
1312                                  */
1313                                 return 0;
1314                         }
1315
1316                         clone = skb_clone(skb, GFP_ATOMIC);
1317                         if (clone)
1318                                 do_output(dp, clone, port, key);
1319                         OVS_CB(skb)->cutlen = 0;
1320                         break;
1321                 }
1322
1323                 case OVS_ACTION_ATTR_TRUNC: {
1324                         struct ovs_action_trunc *trunc = nla_data(a);
1325
1326                         if (skb->len > trunc->max_len)
1327                                 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1328                         break;
1329                 }
1330
1331                 case OVS_ACTION_ATTR_USERSPACE:
1332                         output_userspace(dp, skb, key, a, attr,
1333                                                      len, OVS_CB(skb)->cutlen);
1334                         OVS_CB(skb)->cutlen = 0;
1335                         break;
1336
1337                 case OVS_ACTION_ATTR_HASH:
1338                         execute_hash(skb, key, a);
1339                         break;
1340
1341                 case OVS_ACTION_ATTR_PUSH_MPLS: {
1342                         struct ovs_action_push_mpls *mpls = nla_data(a);
1343
1344                         err = push_mpls(skb, key, mpls->mpls_lse,
1345                                         mpls->mpls_ethertype, skb->mac_len);
1346                         break;
1347                 }
1348                 case OVS_ACTION_ATTR_ADD_MPLS: {
1349                         struct ovs_action_add_mpls *mpls = nla_data(a);
1350                         __u16 mac_len = 0;
1351
1352                         if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK)
1353                                 mac_len = skb->mac_len;
1354
1355                         err = push_mpls(skb, key, mpls->mpls_lse,
1356                                         mpls->mpls_ethertype, mac_len);
1357                         break;
1358                 }
1359                 case OVS_ACTION_ATTR_POP_MPLS:
1360                         err = pop_mpls(skb, key, nla_get_be16(a));
1361                         break;
1362
1363                 case OVS_ACTION_ATTR_PUSH_VLAN:
1364                         err = push_vlan(skb, key, nla_data(a));
1365                         break;
1366
1367                 case OVS_ACTION_ATTR_POP_VLAN:
1368                         err = pop_vlan(skb, key);
1369                         break;
1370
1371                 case OVS_ACTION_ATTR_RECIRC: {
1372                         bool last = nla_is_last(a, rem);
1373
1374                         err = execute_recirc(dp, skb, key, a, last);
1375                         if (last) {
1376                                 /* If this is the last action, the skb has
1377                                  * been consumed or freed.
1378                                  * Return immediately.
1379                                  */
1380                                 return err;
1381                         }
1382                         break;
1383                 }
1384
1385                 case OVS_ACTION_ATTR_SET:
1386                         err = execute_set_action(skb, key, nla_data(a));
1387                         break;
1388
1389                 case OVS_ACTION_ATTR_SET_MASKED:
1390                 case OVS_ACTION_ATTR_SET_TO_MASKED:
1391                         err = execute_masked_set_action(skb, key, nla_data(a));
1392                         break;
1393
1394                 case OVS_ACTION_ATTR_SAMPLE: {
1395                         bool last = nla_is_last(a, rem);
1396
1397                         err = sample(dp, skb, key, a, last);
1398                         if (last)
1399                                 return err;
1400
1401                         break;
1402                 }
1403
1404                 case OVS_ACTION_ATTR_CT:
1405                         if (!is_flow_key_valid(key)) {
1406                                 err = ovs_flow_key_update(skb, key);
1407                                 if (err)
1408                                         return err;
1409                         }
1410
1411                         err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1412                                              nla_data(a));
1413
1414                         /* Hide stolen IP fragments from user space. */
1415                         if (err)
1416                                 return err == -EINPROGRESS ? 0 : err;
1417                         break;
1418
1419                 case OVS_ACTION_ATTR_CT_CLEAR:
1420                         err = ovs_ct_clear(skb, key);
1421                         break;
1422
1423                 case OVS_ACTION_ATTR_PUSH_ETH:
1424                         err = push_eth(skb, key, nla_data(a));
1425                         break;
1426
1427                 case OVS_ACTION_ATTR_POP_ETH:
1428                         err = pop_eth(skb, key);
1429                         break;
1430
1431                 case OVS_ACTION_ATTR_PUSH_NSH: {
1432                         u8 buffer[NSH_HDR_MAX_LEN];
1433                         struct nshhdr *nh = (struct nshhdr *)buffer;
1434
1435                         err = nsh_hdr_from_nlattr(nla_data(a), nh,
1436                                                   NSH_HDR_MAX_LEN);
1437                         if (unlikely(err))
1438                                 break;
1439                         err = push_nsh(skb, key, nh);
1440                         break;
1441                 }
1442
1443                 case OVS_ACTION_ATTR_POP_NSH:
1444                         err = pop_nsh(skb, key);
1445                         break;
1446
1447                 case OVS_ACTION_ATTR_METER:
1448                         if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) {
1449                                 consume_skb(skb);
1450                                 return 0;
1451                         }
1452                         break;
1453
1454                 case OVS_ACTION_ATTR_CLONE: {
1455                         bool last = nla_is_last(a, rem);
1456
1457                         err = clone(dp, skb, key, a, last);
1458                         if (last)
1459                                 return err;
1460
1461                         break;
1462                 }
1463
1464                 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
1465                         bool last = nla_is_last(a, rem);
1466
1467                         err = execute_check_pkt_len(dp, skb, key, a, last);
1468                         if (last)
1469                                 return err;
1470
1471                         break;
1472                 }
1473
1474                 case OVS_ACTION_ATTR_DEC_TTL:
1475                         err = execute_dec_ttl(skb, key);
1476                         if (err == -EHOSTUNREACH)
1477                                 return dec_ttl_exception_handler(dp, skb,
1478                                                                  key, a);
1479                         break;
1480                 }
1481
1482                 if (unlikely(err)) {
1483                         kfree_skb(skb);
1484                         return err;
1485                 }
1486         }
1487
1488         consume_skb(skb);
1489         return 0;
1490 }
1491
1492 /* Execute the actions on the clone of the packet. The effect of the
1493  * execution does not affect the original 'skb' nor the original 'key'.
1494  *
1495  * The execution may be deferred in case the actions can not be executed
1496  * immediately.
1497  */
1498 static int clone_execute(struct datapath *dp, struct sk_buff *skb,
1499                          struct sw_flow_key *key, u32 recirc_id,
1500                          const struct nlattr *actions, int len,
1501                          bool last, bool clone_flow_key)
1502 {
1503         struct deferred_action *da;
1504         struct sw_flow_key *clone;
1505
1506         skb = last ? skb : skb_clone(skb, GFP_ATOMIC);
1507         if (!skb) {
1508                 /* Out of memory, skip this action.
1509                  */
1510                 return 0;
1511         }
1512
1513         /* When clone_flow_key is false, the 'key' will not be change
1514          * by the actions, then the 'key' can be used directly.
1515          * Otherwise, try to clone key from the next recursion level of
1516          * 'flow_keys'. If clone is successful, execute the actions
1517          * without deferring.
1518          */
1519         clone = clone_flow_key ? clone_key(key) : key;
1520         if (clone) {
1521                 int err = 0;
1522
1523                 if (actions) { /* Sample action */
1524                         if (clone_flow_key)
1525                                 __this_cpu_inc(exec_actions_level);
1526
1527                         err = do_execute_actions(dp, skb, clone,
1528                                                  actions, len);
1529
1530                         if (clone_flow_key)
1531                                 __this_cpu_dec(exec_actions_level);
1532                 } else { /* Recirc action */
1533                         clone->recirc_id = recirc_id;
1534                         ovs_dp_process_packet(skb, clone);
1535                 }
1536                 return err;
1537         }
1538
1539         /* Out of 'flow_keys' space. Defer actions */
1540         da = add_deferred_actions(skb, key, actions, len);
1541         if (da) {
1542                 if (!actions) { /* Recirc action */
1543                         key = &da->pkt_key;
1544                         key->recirc_id = recirc_id;
1545                 }
1546         } else {
1547                 /* Out of per CPU action FIFO space. Drop the 'skb' and
1548                  * log an error.
1549                  */
1550                 kfree_skb(skb);
1551
1552                 if (net_ratelimit()) {
1553                         if (actions) { /* Sample action */
1554                                 pr_warn("%s: deferred action limit reached, drop sample action\n",
1555                                         ovs_dp_name(dp));
1556                         } else {  /* Recirc action */
1557                                 pr_warn("%s: deferred action limit reached, drop recirc action (recirc_id=%#x)\n",
1558                                         ovs_dp_name(dp), recirc_id);
1559                         }
1560                 }
1561         }
1562         return 0;
1563 }
1564
1565 static void process_deferred_actions(struct datapath *dp)
1566 {
1567         struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1568
1569         /* Do not touch the FIFO in case there is no deferred actions. */
1570         if (action_fifo_is_empty(fifo))
1571                 return;
1572
1573         /* Finishing executing all deferred actions. */
1574         do {
1575                 struct deferred_action *da = action_fifo_get(fifo);
1576                 struct sk_buff *skb = da->skb;
1577                 struct sw_flow_key *key = &da->pkt_key;
1578                 const struct nlattr *actions = da->actions;
1579                 int actions_len = da->actions_len;
1580
1581                 if (actions)
1582                         do_execute_actions(dp, skb, key, actions, actions_len);
1583                 else
1584                         ovs_dp_process_packet(skb, key);
1585         } while (!action_fifo_is_empty(fifo));
1586
1587         /* Reset FIFO for the next packet.  */
1588         action_fifo_init(fifo);
1589 }
1590
1591 /* Execute a list of actions against 'skb'. */
1592 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1593                         const struct sw_flow_actions *acts,
1594                         struct sw_flow_key *key)
1595 {
1596         int err, level;
1597
1598         level = __this_cpu_inc_return(exec_actions_level);
1599         if (unlikely(level > OVS_RECURSION_LIMIT)) {
1600                 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1601                                      ovs_dp_name(dp));
1602                 kfree_skb(skb);
1603                 err = -ENETDOWN;
1604                 goto out;
1605         }
1606
1607         OVS_CB(skb)->acts_origlen = acts->orig_len;
1608         err = do_execute_actions(dp, skb, key,
1609                                  acts->actions, acts->actions_len);
1610
1611         if (level == 1)
1612                 process_deferred_actions(dp);
1613
1614 out:
1615         __this_cpu_dec(exec_actions_level);
1616         return err;
1617 }
1618
1619 int action_fifos_init(void)
1620 {
1621         action_fifos = alloc_percpu(struct action_fifo);
1622         if (!action_fifos)
1623                 return -ENOMEM;
1624
1625         flow_keys = alloc_percpu(struct action_flow_keys);
1626         if (!flow_keys) {
1627                 free_percpu(action_fifos);
1628                 return -ENOMEM;
1629         }
1630
1631         return 0;
1632 }
1633
1634 void action_fifos_exit(void)
1635 {
1636         free_percpu(action_fifos);
1637         free_percpu(flow_keys);
1638 }
This page took 0.12864 seconds and 4 git commands to generate.