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