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