xref: /linux/net/openvswitch/actions.c (revision d457a0e329b0bfd3a1450e0b1a18cd2b47a25a08)
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 	/* OVS_HASH_ALG_L4 is the only possible hash algorithm.  */
1077 	hash = skb_get_hash(skb);
1078 	hash = jhash_1word(hash, hash_act->hash_basis);
1079 	if (!hash)
1080 		hash = 0x1;
1081 
1082 	key->ovs_flow_hash = hash;
1083 }
1084 
1085 static int execute_set_action(struct sk_buff *skb,
1086 			      struct sw_flow_key *flow_key,
1087 			      const struct nlattr *a)
1088 {
1089 	/* Only tunnel set execution is supported without a mask. */
1090 	if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
1091 		struct ovs_tunnel_info *tun = nla_data(a);
1092 
1093 		skb_dst_drop(skb);
1094 		dst_hold((struct dst_entry *)tun->tun_dst);
1095 		skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
1096 		return 0;
1097 	}
1098 
1099 	return -EINVAL;
1100 }
1101 
1102 /* Mask is at the midpoint of the data. */
1103 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1104 
1105 static int execute_masked_set_action(struct sk_buff *skb,
1106 				     struct sw_flow_key *flow_key,
1107 				     const struct nlattr *a)
1108 {
1109 	int err = 0;
1110 
1111 	switch (nla_type(a)) {
1112 	case OVS_KEY_ATTR_PRIORITY:
1113 		OVS_SET_MASKED(skb->priority, nla_get_u32(a),
1114 			       *get_mask(a, u32 *));
1115 		flow_key->phy.priority = skb->priority;
1116 		break;
1117 
1118 	case OVS_KEY_ATTR_SKB_MARK:
1119 		OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
1120 		flow_key->phy.skb_mark = skb->mark;
1121 		break;
1122 
1123 	case OVS_KEY_ATTR_TUNNEL_INFO:
1124 		/* Masked data not supported for tunnel. */
1125 		err = -EINVAL;
1126 		break;
1127 
1128 	case OVS_KEY_ATTR_ETHERNET:
1129 		err = set_eth_addr(skb, flow_key, nla_data(a),
1130 				   get_mask(a, struct ovs_key_ethernet *));
1131 		break;
1132 
1133 	case OVS_KEY_ATTR_NSH:
1134 		err = set_nsh(skb, flow_key, a);
1135 		break;
1136 
1137 	case OVS_KEY_ATTR_IPV4:
1138 		err = set_ipv4(skb, flow_key, nla_data(a),
1139 			       get_mask(a, struct ovs_key_ipv4 *));
1140 		break;
1141 
1142 	case OVS_KEY_ATTR_IPV6:
1143 		err = set_ipv6(skb, flow_key, nla_data(a),
1144 			       get_mask(a, struct ovs_key_ipv6 *));
1145 		break;
1146 
1147 	case OVS_KEY_ATTR_TCP:
1148 		err = set_tcp(skb, flow_key, nla_data(a),
1149 			      get_mask(a, struct ovs_key_tcp *));
1150 		break;
1151 
1152 	case OVS_KEY_ATTR_UDP:
1153 		err = set_udp(skb, flow_key, nla_data(a),
1154 			      get_mask(a, struct ovs_key_udp *));
1155 		break;
1156 
1157 	case OVS_KEY_ATTR_SCTP:
1158 		err = set_sctp(skb, flow_key, nla_data(a),
1159 			       get_mask(a, struct ovs_key_sctp *));
1160 		break;
1161 
1162 	case OVS_KEY_ATTR_MPLS:
1163 		err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1164 								    __be32 *));
1165 		break;
1166 
1167 	case OVS_KEY_ATTR_CT_STATE:
1168 	case OVS_KEY_ATTR_CT_ZONE:
1169 	case OVS_KEY_ATTR_CT_MARK:
1170 	case OVS_KEY_ATTR_CT_LABELS:
1171 	case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
1172 	case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
1173 		err = -EINVAL;
1174 		break;
1175 	}
1176 
1177 	return err;
1178 }
1179 
1180 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1181 			  struct sw_flow_key *key,
1182 			  const struct nlattr *a, bool last)
1183 {
1184 	u32 recirc_id;
1185 
1186 	if (!is_flow_key_valid(key)) {
1187 		int err;
1188 
1189 		err = ovs_flow_key_update(skb, key);
1190 		if (err)
1191 			return err;
1192 	}
1193 	BUG_ON(!is_flow_key_valid(key));
1194 
1195 	recirc_id = nla_get_u32(a);
1196 	return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true);
1197 }
1198 
1199 static int execute_check_pkt_len(struct datapath *dp, struct sk_buff *skb,
1200 				 struct sw_flow_key *key,
1201 				 const struct nlattr *attr, bool last)
1202 {
1203 	struct ovs_skb_cb *ovs_cb = OVS_CB(skb);
1204 	const struct nlattr *actions, *cpl_arg;
1205 	int len, max_len, rem = nla_len(attr);
1206 	const struct check_pkt_len_arg *arg;
1207 	bool clone_flow_key;
1208 
1209 	/* The first netlink attribute in 'attr' is always
1210 	 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
1211 	 */
1212 	cpl_arg = nla_data(attr);
1213 	arg = nla_data(cpl_arg);
1214 
1215 	len = ovs_cb->mru ? ovs_cb->mru + skb->mac_len : skb->len;
1216 	max_len = arg->pkt_len;
1217 
1218 	if ((skb_is_gso(skb) && skb_gso_validate_mac_len(skb, max_len)) ||
1219 	    len <= max_len) {
1220 		/* Second netlink attribute in 'attr' is always
1221 		 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
1222 		 */
1223 		actions = nla_next(cpl_arg, &rem);
1224 		clone_flow_key = !arg->exec_for_lesser_equal;
1225 	} else {
1226 		/* Third netlink attribute in 'attr' is always
1227 		 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER'.
1228 		 */
1229 		actions = nla_next(cpl_arg, &rem);
1230 		actions = nla_next(actions, &rem);
1231 		clone_flow_key = !arg->exec_for_greater;
1232 	}
1233 
1234 	return clone_execute(dp, skb, key, 0, nla_data(actions),
1235 			     nla_len(actions), last, clone_flow_key);
1236 }
1237 
1238 static int execute_dec_ttl(struct sk_buff *skb, struct sw_flow_key *key)
1239 {
1240 	int err;
1241 
1242 	if (skb->protocol == htons(ETH_P_IPV6)) {
1243 		struct ipv6hdr *nh;
1244 
1245 		err = skb_ensure_writable(skb, skb_network_offset(skb) +
1246 					  sizeof(*nh));
1247 		if (unlikely(err))
1248 			return err;
1249 
1250 		nh = ipv6_hdr(skb);
1251 
1252 		if (nh->hop_limit <= 1)
1253 			return -EHOSTUNREACH;
1254 
1255 		key->ip.ttl = --nh->hop_limit;
1256 	} else if (skb->protocol == htons(ETH_P_IP)) {
1257 		struct iphdr *nh;
1258 		u8 old_ttl;
1259 
1260 		err = skb_ensure_writable(skb, skb_network_offset(skb) +
1261 					  sizeof(*nh));
1262 		if (unlikely(err))
1263 			return err;
1264 
1265 		nh = ip_hdr(skb);
1266 		if (nh->ttl <= 1)
1267 			return -EHOSTUNREACH;
1268 
1269 		old_ttl = nh->ttl--;
1270 		csum_replace2(&nh->check, htons(old_ttl << 8),
1271 			      htons(nh->ttl << 8));
1272 		key->ip.ttl = nh->ttl;
1273 	}
1274 	return 0;
1275 }
1276 
1277 /* Execute a list of actions against 'skb'. */
1278 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1279 			      struct sw_flow_key *key,
1280 			      const struct nlattr *attr, int len)
1281 {
1282 	const struct nlattr *a;
1283 	int rem;
1284 
1285 	for (a = attr, rem = len; rem > 0;
1286 	     a = nla_next(a, &rem)) {
1287 		int err = 0;
1288 
1289 		if (trace_ovs_do_execute_action_enabled())
1290 			trace_ovs_do_execute_action(dp, skb, key, a, rem);
1291 
1292 		switch (nla_type(a)) {
1293 		case OVS_ACTION_ATTR_OUTPUT: {
1294 			int port = nla_get_u32(a);
1295 			struct sk_buff *clone;
1296 
1297 			/* Every output action needs a separate clone
1298 			 * of 'skb', In case the output action is the
1299 			 * last action, cloning can be avoided.
1300 			 */
1301 			if (nla_is_last(a, rem)) {
1302 				do_output(dp, skb, port, key);
1303 				/* 'skb' has been used for output.
1304 				 */
1305 				return 0;
1306 			}
1307 
1308 			clone = skb_clone(skb, GFP_ATOMIC);
1309 			if (clone)
1310 				do_output(dp, clone, port, key);
1311 			OVS_CB(skb)->cutlen = 0;
1312 			break;
1313 		}
1314 
1315 		case OVS_ACTION_ATTR_TRUNC: {
1316 			struct ovs_action_trunc *trunc = nla_data(a);
1317 
1318 			if (skb->len > trunc->max_len)
1319 				OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1320 			break;
1321 		}
1322 
1323 		case OVS_ACTION_ATTR_USERSPACE:
1324 			output_userspace(dp, skb, key, a, attr,
1325 						     len, OVS_CB(skb)->cutlen);
1326 			OVS_CB(skb)->cutlen = 0;
1327 			break;
1328 
1329 		case OVS_ACTION_ATTR_HASH:
1330 			execute_hash(skb, key, a);
1331 			break;
1332 
1333 		case OVS_ACTION_ATTR_PUSH_MPLS: {
1334 			struct ovs_action_push_mpls *mpls = nla_data(a);
1335 
1336 			err = push_mpls(skb, key, mpls->mpls_lse,
1337 					mpls->mpls_ethertype, skb->mac_len);
1338 			break;
1339 		}
1340 		case OVS_ACTION_ATTR_ADD_MPLS: {
1341 			struct ovs_action_add_mpls *mpls = nla_data(a);
1342 			__u16 mac_len = 0;
1343 
1344 			if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK)
1345 				mac_len = skb->mac_len;
1346 
1347 			err = push_mpls(skb, key, mpls->mpls_lse,
1348 					mpls->mpls_ethertype, mac_len);
1349 			break;
1350 		}
1351 		case OVS_ACTION_ATTR_POP_MPLS:
1352 			err = pop_mpls(skb, key, nla_get_be16(a));
1353 			break;
1354 
1355 		case OVS_ACTION_ATTR_PUSH_VLAN:
1356 			err = push_vlan(skb, key, nla_data(a));
1357 			break;
1358 
1359 		case OVS_ACTION_ATTR_POP_VLAN:
1360 			err = pop_vlan(skb, key);
1361 			break;
1362 
1363 		case OVS_ACTION_ATTR_RECIRC: {
1364 			bool last = nla_is_last(a, rem);
1365 
1366 			err = execute_recirc(dp, skb, key, a, last);
1367 			if (last) {
1368 				/* If this is the last action, the skb has
1369 				 * been consumed or freed.
1370 				 * Return immediately.
1371 				 */
1372 				return err;
1373 			}
1374 			break;
1375 		}
1376 
1377 		case OVS_ACTION_ATTR_SET:
1378 			err = execute_set_action(skb, key, nla_data(a));
1379 			break;
1380 
1381 		case OVS_ACTION_ATTR_SET_MASKED:
1382 		case OVS_ACTION_ATTR_SET_TO_MASKED:
1383 			err = execute_masked_set_action(skb, key, nla_data(a));
1384 			break;
1385 
1386 		case OVS_ACTION_ATTR_SAMPLE: {
1387 			bool last = nla_is_last(a, rem);
1388 
1389 			err = sample(dp, skb, key, a, last);
1390 			if (last)
1391 				return err;
1392 
1393 			break;
1394 		}
1395 
1396 		case OVS_ACTION_ATTR_CT:
1397 			if (!is_flow_key_valid(key)) {
1398 				err = ovs_flow_key_update(skb, key);
1399 				if (err)
1400 					return err;
1401 			}
1402 
1403 			err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1404 					     nla_data(a));
1405 
1406 			/* Hide stolen IP fragments from user space. */
1407 			if (err)
1408 				return err == -EINPROGRESS ? 0 : err;
1409 			break;
1410 
1411 		case OVS_ACTION_ATTR_CT_CLEAR:
1412 			err = ovs_ct_clear(skb, key);
1413 			break;
1414 
1415 		case OVS_ACTION_ATTR_PUSH_ETH:
1416 			err = push_eth(skb, key, nla_data(a));
1417 			break;
1418 
1419 		case OVS_ACTION_ATTR_POP_ETH:
1420 			err = pop_eth(skb, key);
1421 			break;
1422 
1423 		case OVS_ACTION_ATTR_PUSH_NSH: {
1424 			u8 buffer[NSH_HDR_MAX_LEN];
1425 			struct nshhdr *nh = (struct nshhdr *)buffer;
1426 
1427 			err = nsh_hdr_from_nlattr(nla_data(a), nh,
1428 						  NSH_HDR_MAX_LEN);
1429 			if (unlikely(err))
1430 				break;
1431 			err = push_nsh(skb, key, nh);
1432 			break;
1433 		}
1434 
1435 		case OVS_ACTION_ATTR_POP_NSH:
1436 			err = pop_nsh(skb, key);
1437 			break;
1438 
1439 		case OVS_ACTION_ATTR_METER:
1440 			if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) {
1441 				consume_skb(skb);
1442 				return 0;
1443 			}
1444 			break;
1445 
1446 		case OVS_ACTION_ATTR_CLONE: {
1447 			bool last = nla_is_last(a, rem);
1448 
1449 			err = clone(dp, skb, key, a, last);
1450 			if (last)
1451 				return err;
1452 
1453 			break;
1454 		}
1455 
1456 		case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
1457 			bool last = nla_is_last(a, rem);
1458 
1459 			err = execute_check_pkt_len(dp, skb, key, a, last);
1460 			if (last)
1461 				return err;
1462 
1463 			break;
1464 		}
1465 
1466 		case OVS_ACTION_ATTR_DEC_TTL:
1467 			err = execute_dec_ttl(skb, key);
1468 			if (err == -EHOSTUNREACH)
1469 				return dec_ttl_exception_handler(dp, skb,
1470 								 key, a);
1471 			break;
1472 		}
1473 
1474 		if (unlikely(err)) {
1475 			kfree_skb(skb);
1476 			return err;
1477 		}
1478 	}
1479 
1480 	consume_skb(skb);
1481 	return 0;
1482 }
1483 
1484 /* Execute the actions on the clone of the packet. The effect of the
1485  * execution does not affect the original 'skb' nor the original 'key'.
1486  *
1487  * The execution may be deferred in case the actions can not be executed
1488  * immediately.
1489  */
1490 static int clone_execute(struct datapath *dp, struct sk_buff *skb,
1491 			 struct sw_flow_key *key, u32 recirc_id,
1492 			 const struct nlattr *actions, int len,
1493 			 bool last, bool clone_flow_key)
1494 {
1495 	struct deferred_action *da;
1496 	struct sw_flow_key *clone;
1497 
1498 	skb = last ? skb : skb_clone(skb, GFP_ATOMIC);
1499 	if (!skb) {
1500 		/* Out of memory, skip this action.
1501 		 */
1502 		return 0;
1503 	}
1504 
1505 	/* When clone_flow_key is false, the 'key' will not be change
1506 	 * by the actions, then the 'key' can be used directly.
1507 	 * Otherwise, try to clone key from the next recursion level of
1508 	 * 'flow_keys'. If clone is successful, execute the actions
1509 	 * without deferring.
1510 	 */
1511 	clone = clone_flow_key ? clone_key(key) : key;
1512 	if (clone) {
1513 		int err = 0;
1514 
1515 		if (actions) { /* Sample action */
1516 			if (clone_flow_key)
1517 				__this_cpu_inc(exec_actions_level);
1518 
1519 			err = do_execute_actions(dp, skb, clone,
1520 						 actions, len);
1521 
1522 			if (clone_flow_key)
1523 				__this_cpu_dec(exec_actions_level);
1524 		} else { /* Recirc action */
1525 			clone->recirc_id = recirc_id;
1526 			ovs_dp_process_packet(skb, clone);
1527 		}
1528 		return err;
1529 	}
1530 
1531 	/* Out of 'flow_keys' space. Defer actions */
1532 	da = add_deferred_actions(skb, key, actions, len);
1533 	if (da) {
1534 		if (!actions) { /* Recirc action */
1535 			key = &da->pkt_key;
1536 			key->recirc_id = recirc_id;
1537 		}
1538 	} else {
1539 		/* Out of per CPU action FIFO space. Drop the 'skb' and
1540 		 * log an error.
1541 		 */
1542 		kfree_skb(skb);
1543 
1544 		if (net_ratelimit()) {
1545 			if (actions) { /* Sample action */
1546 				pr_warn("%s: deferred action limit reached, drop sample action\n",
1547 					ovs_dp_name(dp));
1548 			} else {  /* Recirc action */
1549 				pr_warn("%s: deferred action limit reached, drop recirc action (recirc_id=%#x)\n",
1550 					ovs_dp_name(dp), recirc_id);
1551 			}
1552 		}
1553 	}
1554 	return 0;
1555 }
1556 
1557 static void process_deferred_actions(struct datapath *dp)
1558 {
1559 	struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1560 
1561 	/* Do not touch the FIFO in case there is no deferred actions. */
1562 	if (action_fifo_is_empty(fifo))
1563 		return;
1564 
1565 	/* Finishing executing all deferred actions. */
1566 	do {
1567 		struct deferred_action *da = action_fifo_get(fifo);
1568 		struct sk_buff *skb = da->skb;
1569 		struct sw_flow_key *key = &da->pkt_key;
1570 		const struct nlattr *actions = da->actions;
1571 		int actions_len = da->actions_len;
1572 
1573 		if (actions)
1574 			do_execute_actions(dp, skb, key, actions, actions_len);
1575 		else
1576 			ovs_dp_process_packet(skb, key);
1577 	} while (!action_fifo_is_empty(fifo));
1578 
1579 	/* Reset FIFO for the next packet.  */
1580 	action_fifo_init(fifo);
1581 }
1582 
1583 /* Execute a list of actions against 'skb'. */
1584 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1585 			const struct sw_flow_actions *acts,
1586 			struct sw_flow_key *key)
1587 {
1588 	int err, level;
1589 
1590 	level = __this_cpu_inc_return(exec_actions_level);
1591 	if (unlikely(level > OVS_RECURSION_LIMIT)) {
1592 		net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1593 				     ovs_dp_name(dp));
1594 		kfree_skb(skb);
1595 		err = -ENETDOWN;
1596 		goto out;
1597 	}
1598 
1599 	OVS_CB(skb)->acts_origlen = acts->orig_len;
1600 	err = do_execute_actions(dp, skb, key,
1601 				 acts->actions, acts->actions_len);
1602 
1603 	if (level == 1)
1604 		process_deferred_actions(dp);
1605 
1606 out:
1607 	__this_cpu_dec(exec_actions_level);
1608 	return err;
1609 }
1610 
1611 int action_fifos_init(void)
1612 {
1613 	action_fifos = alloc_percpu(struct action_fifo);
1614 	if (!action_fifos)
1615 		return -ENOMEM;
1616 
1617 	flow_keys = alloc_percpu(struct action_flow_keys);
1618 	if (!flow_keys) {
1619 		free_percpu(action_fifos);
1620 		return -ENOMEM;
1621 	}
1622 
1623 	return 0;
1624 }
1625 
1626 void action_fifos_exit(void)
1627 {
1628 	free_percpu(action_fifos);
1629 	free_percpu(flow_keys);
1630 }
1631