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