]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: Simplify do_execute_actions().
[mirror_ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2015 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/in6.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_vlan.h>
32
33 #include <net/dst.h>
34 #include <net/ip.h>
35 #include <net/ipv6.h>
36 #include <net/checksum.h>
37 #include <net/dsfield.h>
38 #include <net/mpls.h>
39 #include <net/sctp/checksum.h>
40
41 #include "datapath.h"
42 #include "conntrack.h"
43 #include "gso.h"
44 #include "vport.h"
45
46 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
47 struct sw_flow_key *key,
48 const struct nlattr *attr, int len);
49
50 struct deferred_action {
51 struct sk_buff *skb;
52 const struct nlattr *actions;
53
54 /* Store pkt_key clone when creating deferred action. */
55 struct sw_flow_key pkt_key;
56 };
57
58 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
59 struct ovs_frag_data {
60 unsigned long dst;
61 struct vport *vport;
62 struct ovs_gso_cb cb;
63 __be16 inner_protocol;
64 __u16 vlan_tci;
65 __be16 vlan_proto;
66 unsigned int l2_len;
67 u8 mac_proto;
68 u8 l2_data[MAX_L2_LEN];
69 };
70
71 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
72
73 #define DEFERRED_ACTION_FIFO_SIZE 10
74 #define OVS_RECURSION_LIMIT 4
75 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
76 struct action_fifo {
77 int head;
78 int tail;
79 /* Deferred action fifo queue storage. */
80 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
81 };
82
83 struct recirc_keys {
84 struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
85 };
86
87 static struct action_fifo __percpu *action_fifos;
88 static struct recirc_keys __percpu *recirc_keys;
89 static DEFINE_PER_CPU(int, exec_actions_level);
90
91 static void action_fifo_init(struct action_fifo *fifo)
92 {
93 fifo->head = 0;
94 fifo->tail = 0;
95 }
96
97 static bool action_fifo_is_empty(const struct action_fifo *fifo)
98 {
99 return (fifo->head == fifo->tail);
100 }
101
102 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
103 {
104 if (action_fifo_is_empty(fifo))
105 return NULL;
106
107 return &fifo->fifo[fifo->tail++];
108 }
109
110 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
111 {
112 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
113 return NULL;
114
115 return &fifo->fifo[fifo->head++];
116 }
117
118 /* Return queue entry if fifo is not full */
119 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
120 const struct sw_flow_key *key,
121 const struct nlattr *attr)
122 {
123 struct action_fifo *fifo;
124 struct deferred_action *da;
125
126 fifo = this_cpu_ptr(action_fifos);
127 da = action_fifo_put(fifo);
128 if (da) {
129 da->skb = skb;
130 da->actions = attr;
131 da->pkt_key = *key;
132 }
133
134 return da;
135 }
136
137 static void invalidate_flow_key(struct sw_flow_key *key)
138 {
139 key->mac_proto |= SW_FLOW_KEY_INVALID;
140 }
141
142 static bool is_flow_key_valid(const struct sw_flow_key *key)
143 {
144 return !(key->mac_proto & SW_FLOW_KEY_INVALID);
145 }
146
147 static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
148 __be16 ethertype)
149 {
150 if (skb->ip_summed == CHECKSUM_COMPLETE) {
151 __be16 diff[] = { ~(hdr->h_proto), ethertype };
152
153 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
154 ~skb->csum);
155 }
156
157 hdr->h_proto = ethertype;
158 }
159
160 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
161 const struct ovs_action_push_mpls *mpls)
162 {
163 __be32 *new_mpls_lse;
164
165 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
166 if (skb->encapsulation)
167 return -ENOTSUPP;
168
169 if (skb_cow_head(skb, MPLS_HLEN) < 0)
170 return -ENOMEM;
171
172 skb_push(skb, MPLS_HLEN);
173 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
174 skb->mac_len);
175 skb_reset_mac_header(skb);
176
177 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
178 *new_mpls_lse = mpls->mpls_lse;
179
180 skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
181
182 if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET)
183 update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
184 if (!ovs_skb_get_inner_protocol(skb))
185 ovs_skb_set_inner_protocol(skb, skb->protocol);
186 skb->protocol = mpls->mpls_ethertype;
187
188 invalidate_flow_key(key);
189 return 0;
190 }
191
192 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
193 const __be16 ethertype)
194 {
195 int err;
196
197 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
198 if (unlikely(err))
199 return err;
200
201 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
202
203 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
204 skb->mac_len);
205
206 __skb_pull(skb, MPLS_HLEN);
207 skb_reset_mac_header(skb);
208
209 if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET) {
210 struct ethhdr *hdr;
211
212 /* skb_mpls_header() is used to locate the ethertype
213 * field correctly in the presence of VLAN tags.
214 */
215 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
216 update_ethertype(skb, hdr, ethertype);
217 }
218 if (eth_p_mpls(skb->protocol))
219 skb->protocol = ethertype;
220
221 invalidate_flow_key(key);
222 return 0;
223 }
224
225 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
226 const __be32 *mpls_lse, const __be32 *mask)
227 {
228 __be32 *stack;
229 __be32 lse;
230 int err;
231
232 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
233 if (unlikely(err))
234 return err;
235
236 stack = (__be32 *)skb_mpls_header(skb);
237 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
238 if (skb->ip_summed == CHECKSUM_COMPLETE) {
239 __be32 diff[] = { ~(*stack), lse };
240
241 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
242 ~skb->csum);
243 }
244
245 *stack = lse;
246 flow_key->mpls.top_lse = lse;
247 return 0;
248 }
249
250 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
251 {
252 int err;
253
254 err = skb_vlan_pop(skb);
255 if (skb_vlan_tag_present(skb)) {
256 invalidate_flow_key(key);
257 } else {
258 key->eth.vlan.tci = 0;
259 key->eth.vlan.tpid = 0;
260 }
261 return err;
262 }
263
264 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
265 const struct ovs_action_push_vlan *vlan)
266 {
267 if (skb_vlan_tag_present(skb)) {
268 invalidate_flow_key(key);
269 } else {
270 key->eth.vlan.tci = vlan->vlan_tci;
271 key->eth.vlan.tpid = vlan->vlan_tpid;
272 }
273 return skb_vlan_push(skb, vlan->vlan_tpid,
274 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
275 }
276
277 /* 'src' is already properly masked. */
278 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
279 {
280 u16 *dst = (u16 *)dst_;
281 const u16 *src = (const u16 *)src_;
282 const u16 *mask = (const u16 *)mask_;
283
284 OVS_SET_MASKED(dst[0], src[0], mask[0]);
285 OVS_SET_MASKED(dst[1], src[1], mask[1]);
286 OVS_SET_MASKED(dst[2], src[2], mask[2]);
287 }
288
289 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
290 const struct ovs_key_ethernet *key,
291 const struct ovs_key_ethernet *mask)
292 {
293 int err;
294
295 err = skb_ensure_writable(skb, ETH_HLEN);
296 if (unlikely(err))
297 return err;
298
299 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
300
301 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
302 mask->eth_src);
303 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
304 mask->eth_dst);
305
306 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
307
308 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
309 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
310 return 0;
311 }
312
313 /* pop_eth does not support VLAN packets as this action is never called
314 * for them.
315 */
316 static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
317 {
318 skb_pull_rcsum(skb, ETH_HLEN);
319 skb_reset_mac_header(skb);
320 skb_reset_mac_len(skb);
321
322 /* safe right before invalidate_flow_key */
323 key->mac_proto = MAC_PROTO_NONE;
324 invalidate_flow_key(key);
325 return 0;
326 }
327
328 static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
329 const struct ovs_action_push_eth *ethh)
330 {
331 struct ethhdr *hdr;
332
333 /* Add the new Ethernet header */
334 if (skb_cow_head(skb, ETH_HLEN) < 0)
335 return -ENOMEM;
336
337 skb_push(skb, ETH_HLEN);
338 skb_reset_mac_header(skb);
339 skb_reset_mac_len(skb);
340
341 hdr = eth_hdr(skb);
342 ether_addr_copy(hdr->h_source, ethh->addresses.eth_src);
343 ether_addr_copy(hdr->h_dest, ethh->addresses.eth_dst);
344 hdr->h_proto = skb->protocol;
345
346 skb_postpush_rcsum(skb, hdr, ETH_HLEN);
347
348 /* safe right before invalidate_flow_key */
349 key->mac_proto = MAC_PROTO_ETHERNET;
350 invalidate_flow_key(key);
351 return 0;
352 }
353
354 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
355 __be32 addr, __be32 new_addr)
356 {
357 int transport_len = skb->len - skb_transport_offset(skb);
358
359 if (nh->frag_off & htons(IP_OFFSET))
360 return;
361
362 if (nh->protocol == IPPROTO_TCP) {
363 if (likely(transport_len >= sizeof(struct tcphdr)))
364 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
365 addr, new_addr, true);
366 } else if (nh->protocol == IPPROTO_UDP) {
367 if (likely(transport_len >= sizeof(struct udphdr))) {
368 struct udphdr *uh = udp_hdr(skb);
369
370 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
371 inet_proto_csum_replace4(&uh->check, skb,
372 addr, new_addr, true);
373 if (!uh->check)
374 uh->check = CSUM_MANGLED_0;
375 }
376 }
377 }
378
379 }
380
381 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
382 __be32 *addr, __be32 new_addr)
383 {
384 update_ip_l4_checksum(skb, nh, *addr, new_addr);
385 csum_replace4(&nh->check, *addr, new_addr);
386 skb_clear_hash(skb);
387 *addr = new_addr;
388 }
389
390 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
391 __be32 addr[4], const __be32 new_addr[4])
392 {
393 int transport_len = skb->len - skb_transport_offset(skb);
394
395 if (l4_proto == NEXTHDR_TCP) {
396 if (likely(transport_len >= sizeof(struct tcphdr)))
397 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
398 addr, new_addr, true);
399 } else if (l4_proto == NEXTHDR_UDP) {
400 if (likely(transport_len >= sizeof(struct udphdr))) {
401 struct udphdr *uh = udp_hdr(skb);
402
403 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
404 inet_proto_csum_replace16(&uh->check, skb,
405 addr, new_addr, true);
406 if (!uh->check)
407 uh->check = CSUM_MANGLED_0;
408 }
409 }
410 } else if (l4_proto == NEXTHDR_ICMP) {
411 if (likely(transport_len >= sizeof(struct icmp6hdr)))
412 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
413 skb, addr, new_addr, true);
414 }
415 }
416
417 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
418 const __be32 mask[4], __be32 masked[4])
419 {
420 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
421 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
422 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
423 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
424 }
425
426 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
427 __be32 addr[4], const __be32 new_addr[4],
428 bool recalculate_csum)
429 {
430 if (likely(recalculate_csum))
431 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
432
433 skb_clear_hash(skb);
434 memcpy(addr, new_addr, sizeof(__be32[4]));
435 }
436
437 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
438 {
439 /* Bits 21-24 are always unmasked, so this retains their values. */
440 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
441 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
442 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
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 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
561 flow_key->ip.tos = ipv6_get_dsfield(nh);
562 }
563 if (mask->ipv6_label) {
564 set_ipv6_fl(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 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
571 mask->ipv6_hlimit);
572 flow_key->ip.ttl = nh->hop_limit;
573 }
574 return 0;
575 }
576
577 /* Must follow skb_ensure_writable() since that can move the skb data. */
578 static void set_tp_port(struct sk_buff *skb, __be16 *port,
579 __be16 new_port, __sum16 *check)
580 {
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 }
621
622 skb_clear_hash(skb);
623
624 return 0;
625 }
626
627 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
628 const struct ovs_key_tcp *key,
629 const struct ovs_key_tcp *mask)
630 {
631 struct tcphdr *th;
632 __be16 src, dst;
633 int err;
634
635 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
636 sizeof(struct tcphdr));
637 if (unlikely(err))
638 return err;
639
640 th = tcp_hdr(skb);
641 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
642 if (likely(src != th->source)) {
643 set_tp_port(skb, &th->source, src, &th->check);
644 flow_key->tp.src = src;
645 }
646 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
647 if (likely(dst != th->dest)) {
648 set_tp_port(skb, &th->dest, dst, &th->check);
649 flow_key->tp.dst = dst;
650 }
651 skb_clear_hash(skb);
652
653 return 0;
654 }
655
656 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
657 const struct ovs_key_sctp *key,
658 const struct ovs_key_sctp *mask)
659 {
660 unsigned int sctphoff = skb_transport_offset(skb);
661 struct sctphdr *sh;
662 __le32 old_correct_csum, new_csum, old_csum;
663 int err;
664
665 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
666 if (unlikely(err))
667 return err;
668
669 sh = sctp_hdr(skb);
670 old_csum = sh->checksum;
671 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
672
673 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
674 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
675
676 new_csum = sctp_compute_cksum(skb, sctphoff);
677
678 /* Carry any checksum errors through. */
679 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
680
681 skb_clear_hash(skb);
682 flow_key->tp.src = sh->source;
683 flow_key->tp.dst = sh->dest;
684
685 return 0;
686 }
687
688 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
689 {
690 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
691 struct vport *vport = data->vport;
692
693 if (skb_cow_head(skb, data->l2_len) < 0) {
694 kfree_skb(skb);
695 return -ENOMEM;
696 }
697
698 __skb_dst_copy(skb, data->dst);
699 *OVS_GSO_CB(skb) = data->cb;
700 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
701 skb->vlan_tci = data->vlan_tci;
702 skb->vlan_proto = data->vlan_proto;
703
704 /* Reconstruct the MAC header. */
705 skb_push(skb, data->l2_len);
706 memcpy(skb->data, &data->l2_data, data->l2_len);
707 skb_postpush_rcsum(skb, skb->data, data->l2_len);
708 skb_reset_mac_header(skb);
709
710 ovs_vport_send(vport, skb, data->mac_proto);
711 return 0;
712 }
713
714 static unsigned int
715 ovs_dst_get_mtu(const struct dst_entry *dst)
716 {
717 return dst->dev->mtu;
718 }
719
720 static struct dst_ops ovs_dst_ops = {
721 .family = AF_UNSPEC,
722 .mtu = ovs_dst_get_mtu,
723 };
724
725 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
726 * ovs_vport_output(), which is called once per fragmented packet.
727 */
728 static void prepare_frag(struct vport *vport, struct sk_buff *skb,
729 u8 mac_proto)
730 {
731 unsigned int hlen = skb_network_offset(skb);
732 struct ovs_frag_data *data;
733
734 data = this_cpu_ptr(&ovs_frag_data_storage);
735 data->dst = (unsigned long) skb_dst(skb);
736 data->vport = vport;
737 data->cb = *OVS_GSO_CB(skb);
738 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
739 data->vlan_tci = skb->vlan_tci;
740 data->vlan_proto = skb->vlan_proto;
741 data->mac_proto = mac_proto;
742 data->l2_len = hlen;
743 memcpy(&data->l2_data, skb->data, hlen);
744
745 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
746 skb_pull(skb, hlen);
747 }
748
749 static void ovs_fragment(struct net *net, struct vport *vport,
750 struct sk_buff *skb, u16 mru,
751 struct sw_flow_key *key)
752 {
753 if (skb_network_offset(skb) > MAX_L2_LEN) {
754 OVS_NLERR(1, "L2 header too long to fragment");
755 goto err;
756 }
757
758 if (key->eth.type == htons(ETH_P_IP)) {
759 struct dst_entry ovs_dst;
760 unsigned long orig_dst;
761
762 prepare_frag(vport, skb, ovs_key_mac_proto(key));
763 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
764 DST_OBSOLETE_NONE, DST_NOCOUNT);
765 ovs_dst.dev = vport->dev;
766
767 orig_dst = (unsigned long) skb_dst(skb);
768 skb_dst_set_noref(skb, &ovs_dst);
769 IPCB(skb)->frag_max_size = mru;
770
771 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
772 refdst_drop(orig_dst);
773 } else if (key->eth.type == htons(ETH_P_IPV6)) {
774 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
775 unsigned long orig_dst;
776 struct rt6_info ovs_rt;
777
778 if (!v6ops) {
779 goto err;
780 }
781
782 prepare_frag(vport, skb,
783 ovs_key_mac_proto(key));
784 memset(&ovs_rt, 0, sizeof(ovs_rt));
785 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
786 DST_OBSOLETE_NONE, DST_NOCOUNT);
787 ovs_rt.dst.dev = vport->dev;
788
789 orig_dst = (unsigned long) skb_dst(skb);
790 skb_dst_set_noref(skb, &ovs_rt.dst);
791 IP6CB(skb)->frag_max_size = mru;
792 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
793 v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
794 #else
795 v6ops->fragment(skb->sk, skb, ovs_vport_output);
796 #endif
797 refdst_drop(orig_dst);
798 } else {
799 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
800 ovs_vport_name(vport), ntohs(key->eth.type), mru,
801 vport->dev->mtu);
802 goto err;
803 }
804
805 return;
806 err:
807 kfree_skb(skb);
808 }
809
810 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
811 struct sw_flow_key *key)
812 {
813 struct vport *vport = ovs_vport_rcu(dp, out_port);
814
815 if (likely(vport)) {
816 u16 mru = OVS_CB(skb)->mru;
817 u32 cutlen = OVS_CB(skb)->cutlen;
818
819 if (unlikely(cutlen > 0)) {
820 if (skb->len - cutlen > ovs_mac_header_len(key))
821 pskb_trim(skb, skb->len - cutlen);
822 else
823 pskb_trim(skb, ovs_mac_header_len(key));
824 }
825
826 if (likely(!mru ||
827 (skb->len <= mru + vport->dev->hard_header_len))) {
828 ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
829 } else if (mru <= vport->dev->mtu) {
830 struct net *net = ovs_dp_get_net(dp);
831
832 ovs_fragment(net, vport, skb, mru, key);
833 } else {
834 OVS_NLERR(true, "Cannot fragment IP frames");
835 kfree_skb(skb);
836 }
837 } else {
838 kfree_skb(skb);
839 }
840 }
841
842 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
843 struct sw_flow_key *key, const struct nlattr *attr,
844 const struct nlattr *actions, int actions_len,
845 uint32_t cutlen)
846 {
847 struct dp_upcall_info upcall;
848 const struct nlattr *a;
849 int rem, err;
850
851 memset(&upcall, 0, sizeof(upcall));
852 upcall.cmd = OVS_PACKET_CMD_ACTION;
853 upcall.mru = OVS_CB(skb)->mru;
854
855 SKB_INIT_FILL_METADATA_DST(skb);
856 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
857 a = nla_next(a, &rem)) {
858 switch (nla_type(a)) {
859 case OVS_USERSPACE_ATTR_USERDATA:
860 upcall.userdata = a;
861 break;
862
863 case OVS_USERSPACE_ATTR_PID:
864 upcall.portid = nla_get_u32(a);
865 break;
866
867 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
868 /* Get out tunnel info. */
869 struct vport *vport;
870
871 vport = ovs_vport_rcu(dp, nla_get_u32(a));
872 if (vport) {
873 err = dev_fill_metadata_dst(vport->dev, skb);
874 if (!err)
875 upcall.egress_tun_info = skb_tunnel_info(skb);
876 }
877
878 break;
879 }
880
881 case OVS_USERSPACE_ATTR_ACTIONS: {
882 /* Include actions. */
883 upcall.actions = actions;
884 upcall.actions_len = actions_len;
885 break;
886 }
887
888 } /* End of switch. */
889 }
890
891 err = ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
892 SKB_RESTORE_FILL_METADATA_DST(skb);
893 return err;
894 }
895
896 static int sample(struct datapath *dp, struct sk_buff *skb,
897 struct sw_flow_key *key, const struct nlattr *attr,
898 const struct nlattr *actions, int actions_len)
899 {
900 const struct nlattr *acts_list = NULL;
901 const struct nlattr *a;
902 int rem;
903 u32 cutlen = 0;
904
905 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
906 a = nla_next(a, &rem)) {
907 u32 probability;
908
909 switch (nla_type(a)) {
910 case OVS_SAMPLE_ATTR_PROBABILITY:
911 probability = nla_get_u32(a);
912 if (!probability || prandom_u32() > probability)
913 return 0;
914 break;
915
916 case OVS_SAMPLE_ATTR_ACTIONS:
917 acts_list = a;
918 break;
919 }
920 }
921
922 rem = nla_len(acts_list);
923 a = nla_data(acts_list);
924
925 /* Actions list is empty, do nothing */
926 if (unlikely(!rem))
927 return 0;
928
929 /* The only known usage of sample action is having a single user-space
930 * action, or having a truncate action followed by a single user-space
931 * action. Treat this usage as a special case.
932 * The output_userspace() should clone the skb to be sent to the
933 * user space. This skb will be consumed by its caller.
934 */
935 if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
936 struct ovs_action_trunc *trunc = nla_data(a);
937
938 if (skb->len > trunc->max_len)
939 cutlen = skb->len - trunc->max_len;
940
941 a = nla_next(a, &rem);
942 }
943
944 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
945 nla_is_last(a, rem)))
946 return output_userspace(dp, skb, key, a, actions,
947 actions_len, cutlen);
948
949 skb = skb_clone(skb, GFP_ATOMIC);
950 if (!skb)
951 /* Skip the sample action when out of memory. */
952 return 0;
953
954 if (!add_deferred_actions(skb, key, a)) {
955 if (net_ratelimit())
956 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
957 ovs_dp_name(dp));
958
959 kfree_skb(skb);
960 }
961 return 0;
962 }
963
964 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
965 const struct nlattr *attr)
966 {
967 struct ovs_action_hash *hash_act = nla_data(attr);
968 u32 hash = 0;
969
970 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
971 hash = skb_get_hash(skb);
972 hash = jhash_1word(hash, hash_act->hash_basis);
973 if (!hash)
974 hash = 0x1;
975
976 key->ovs_flow_hash = hash;
977 }
978
979 static int execute_set_action(struct sk_buff *skb,
980 struct sw_flow_key *flow_key,
981 const struct nlattr *a)
982 {
983 /* Only tunnel set execution is supported without a mask. */
984 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
985 struct ovs_tunnel_info *tun = nla_data(a);
986
987 ovs_skb_dst_drop(skb);
988 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
989 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
990 return 0;
991 }
992
993 return -EINVAL;
994 }
995
996 /* Mask is at the midpoint of the data. */
997 #define get_mask(a, type) ((const type)nla_data(a) + 1)
998
999 static int execute_masked_set_action(struct sk_buff *skb,
1000 struct sw_flow_key *flow_key,
1001 const struct nlattr *a)
1002 {
1003 int err = 0;
1004
1005 switch (nla_type(a)) {
1006 case OVS_KEY_ATTR_PRIORITY:
1007 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
1008 *get_mask(a, u32 *));
1009 flow_key->phy.priority = skb->priority;
1010 break;
1011
1012 case OVS_KEY_ATTR_SKB_MARK:
1013 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
1014 flow_key->phy.skb_mark = skb->mark;
1015 break;
1016
1017 case OVS_KEY_ATTR_TUNNEL_INFO:
1018 /* Masked data not supported for tunnel. */
1019 err = -EINVAL;
1020 break;
1021
1022 case OVS_KEY_ATTR_ETHERNET:
1023 err = set_eth_addr(skb, flow_key, nla_data(a),
1024 get_mask(a, struct ovs_key_ethernet *));
1025 break;
1026
1027 case OVS_KEY_ATTR_IPV4:
1028 err = set_ipv4(skb, flow_key, nla_data(a),
1029 get_mask(a, struct ovs_key_ipv4 *));
1030 break;
1031
1032 case OVS_KEY_ATTR_IPV6:
1033 err = set_ipv6(skb, flow_key, nla_data(a),
1034 get_mask(a, struct ovs_key_ipv6 *));
1035 break;
1036
1037 case OVS_KEY_ATTR_TCP:
1038 err = set_tcp(skb, flow_key, nla_data(a),
1039 get_mask(a, struct ovs_key_tcp *));
1040 break;
1041
1042 case OVS_KEY_ATTR_UDP:
1043 err = set_udp(skb, flow_key, nla_data(a),
1044 get_mask(a, struct ovs_key_udp *));
1045 break;
1046
1047 case OVS_KEY_ATTR_SCTP:
1048 err = set_sctp(skb, flow_key, nla_data(a),
1049 get_mask(a, struct ovs_key_sctp *));
1050 break;
1051
1052 case OVS_KEY_ATTR_MPLS:
1053 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1054 __be32 *));
1055 break;
1056
1057 case OVS_KEY_ATTR_CT_STATE:
1058 case OVS_KEY_ATTR_CT_ZONE:
1059 case OVS_KEY_ATTR_CT_MARK:
1060 case OVS_KEY_ATTR_CT_LABELS:
1061 err = -EINVAL;
1062 break;
1063 }
1064
1065 return err;
1066 }
1067
1068 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1069 struct sw_flow_key *key,
1070 const struct nlattr *a, int rem)
1071 {
1072 struct deferred_action *da;
1073 int level;
1074
1075 if (!is_flow_key_valid(key)) {
1076 int err;
1077
1078 err = ovs_flow_key_update(skb, key);
1079 if (err)
1080 return err;
1081 }
1082 BUG_ON(!is_flow_key_valid(key));
1083
1084 if (!nla_is_last(a, rem)) {
1085 /* Recirc action is the not the last action
1086 * of the action list, need to clone the skb.
1087 */
1088 skb = skb_clone(skb, GFP_ATOMIC);
1089
1090 /* Skip the recirc action when out of memory, but
1091 * continue on with the rest of the action list.
1092 */
1093 if (!skb)
1094 return 0;
1095 }
1096
1097 level = this_cpu_read(exec_actions_level);
1098 if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
1099 struct recirc_keys *rks = this_cpu_ptr(recirc_keys);
1100 struct sw_flow_key *recirc_key = &rks->key[level - 1];
1101
1102 *recirc_key = *key;
1103 recirc_key->recirc_id = nla_get_u32(a);
1104 ovs_dp_process_packet(skb, recirc_key);
1105
1106 return 0;
1107 }
1108
1109 da = add_deferred_actions(skb, key, NULL);
1110 if (da) {
1111 da->pkt_key.recirc_id = nla_get_u32(a);
1112 } else {
1113 kfree_skb(skb);
1114
1115 if (net_ratelimit())
1116 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1117 ovs_dp_name(dp));
1118 }
1119
1120 return 0;
1121 }
1122
1123 /* Execute a list of actions against 'skb'. */
1124 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1125 struct sw_flow_key *key,
1126 const struct nlattr *attr, int len)
1127 {
1128 const struct nlattr *a;
1129 int rem;
1130
1131 for (a = attr, rem = len; rem > 0;
1132 a = nla_next(a, &rem)) {
1133 int err = 0;
1134
1135 switch (nla_type(a)) {
1136 case OVS_ACTION_ATTR_OUTPUT: {
1137 int port = nla_get_u32(a);
1138 struct sk_buff *clone;
1139
1140 /* Every output action needs a separate clone
1141 * of 'skb', In case the output action is the
1142 * last action, cloning can be avoided.
1143 */
1144 if (nla_is_last(a, rem)) {
1145 do_output(dp, skb, port, key);
1146 /* 'skb' has been used for output.
1147 */
1148 return 0;
1149 }
1150
1151 clone = skb_clone(skb, GFP_ATOMIC);
1152 if (clone)
1153 do_output(dp, clone, port, key);
1154 OVS_CB(skb)->cutlen = 0;
1155 break;
1156 }
1157
1158 case OVS_ACTION_ATTR_TRUNC: {
1159 struct ovs_action_trunc *trunc = nla_data(a);
1160
1161 if (skb->len > trunc->max_len)
1162 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1163 break;
1164 }
1165
1166 case OVS_ACTION_ATTR_USERSPACE:
1167 output_userspace(dp, skb, key, a, attr,
1168 len, OVS_CB(skb)->cutlen);
1169 OVS_CB(skb)->cutlen = 0;
1170 break;
1171
1172 case OVS_ACTION_ATTR_HASH:
1173 execute_hash(skb, key, a);
1174 break;
1175
1176 case OVS_ACTION_ATTR_PUSH_MPLS:
1177 err = push_mpls(skb, key, nla_data(a));
1178 break;
1179
1180 case OVS_ACTION_ATTR_POP_MPLS:
1181 err = pop_mpls(skb, key, nla_get_be16(a));
1182 break;
1183
1184 case OVS_ACTION_ATTR_PUSH_VLAN:
1185 err = push_vlan(skb, key, nla_data(a));
1186 break;
1187
1188 case OVS_ACTION_ATTR_POP_VLAN:
1189 err = pop_vlan(skb, key);
1190 break;
1191
1192 case OVS_ACTION_ATTR_RECIRC:
1193 err = execute_recirc(dp, skb, key, a, rem);
1194 if (nla_is_last(a, rem)) {
1195 /* If this is the last action, the skb has
1196 * been consumed or freed.
1197 * Return immediately.
1198 */
1199 return err;
1200 }
1201 break;
1202
1203 case OVS_ACTION_ATTR_SET:
1204 err = execute_set_action(skb, key, nla_data(a));
1205 break;
1206
1207 case OVS_ACTION_ATTR_SET_MASKED:
1208 case OVS_ACTION_ATTR_SET_TO_MASKED:
1209 err = execute_masked_set_action(skb, key, nla_data(a));
1210 break;
1211
1212 case OVS_ACTION_ATTR_SAMPLE:
1213 err = sample(dp, skb, key, a, attr, len);
1214 break;
1215
1216 case OVS_ACTION_ATTR_CT:
1217 if (!is_flow_key_valid(key)) {
1218 err = ovs_flow_key_update(skb, key);
1219 if (err)
1220 return err;
1221 }
1222
1223 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1224 nla_data(a));
1225
1226 /* Hide stolen IP fragments from user space. */
1227 if (err)
1228 return err == -EINPROGRESS ? 0 : err;
1229 break;
1230
1231 case OVS_ACTION_ATTR_PUSH_ETH:
1232 err = push_eth(skb, key, nla_data(a));
1233 break;
1234
1235 case OVS_ACTION_ATTR_POP_ETH:
1236 err = pop_eth(skb, key);
1237 break;
1238 }
1239
1240 if (unlikely(err)) {
1241 kfree_skb(skb);
1242 return err;
1243 }
1244 }
1245
1246 consume_skb(skb);
1247 return 0;
1248 }
1249
1250 static void process_deferred_actions(struct datapath *dp)
1251 {
1252 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1253
1254 /* Do not touch the FIFO in case there is no deferred actions. */
1255 if (action_fifo_is_empty(fifo))
1256 return;
1257
1258 /* Finishing executing all deferred actions. */
1259 do {
1260 struct deferred_action *da = action_fifo_get(fifo);
1261 struct sk_buff *skb = da->skb;
1262 struct sw_flow_key *key = &da->pkt_key;
1263 const struct nlattr *actions = da->actions;
1264
1265 if (actions)
1266 do_execute_actions(dp, skb, key, actions,
1267 nla_len(actions));
1268 else
1269 ovs_dp_process_packet(skb, key);
1270 } while (!action_fifo_is_empty(fifo));
1271
1272 /* Reset FIFO for the next packet. */
1273 action_fifo_init(fifo);
1274 }
1275
1276 /* Execute a list of actions against 'skb'. */
1277 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1278 const struct sw_flow_actions *acts,
1279 struct sw_flow_key *key)
1280 {
1281 int err, level;
1282
1283 level = __this_cpu_inc_return(exec_actions_level);
1284 if (unlikely(level > OVS_RECURSION_LIMIT)) {
1285 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1286 ovs_dp_name(dp));
1287 kfree_skb(skb);
1288 err = -ENETDOWN;
1289 goto out;
1290 }
1291
1292 err = do_execute_actions(dp, skb, key,
1293 acts->actions, acts->actions_len);
1294
1295 if (level == 1)
1296 process_deferred_actions(dp);
1297
1298 out:
1299 __this_cpu_dec(exec_actions_level);
1300 return err;
1301 }
1302
1303 int action_fifos_init(void)
1304 {
1305 action_fifos = alloc_percpu(struct action_fifo);
1306 if (!action_fifos)
1307 return -ENOMEM;
1308
1309 recirc_keys = alloc_percpu(struct recirc_keys);
1310 if (!recirc_keys) {
1311 free_percpu(action_fifos);
1312 return -ENOMEM;
1313 }
1314
1315 return 0;
1316 }
1317
1318 void action_fifos_exit(void)
1319 {
1320 free_percpu(action_fifos);
1321 free_percpu(recirc_keys);
1322 }