]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: Allow matching on conntrack label
[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 "vlan.h"
45 #include "vport.h"
46
47 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
48 struct sw_flow_key *key,
49 const struct nlattr *attr, int len);
50
51 struct deferred_action {
52 struct sk_buff *skb;
53 const struct nlattr *actions;
54
55 /* Store pkt_key clone when creating deferred action. */
56 struct sw_flow_key pkt_key;
57 };
58
59 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
60 struct ovs_frag_data {
61 unsigned long dst;
62 struct vport *vport;
63 struct ovs_skb_cb cb;
64 __be16 inner_protocol;
65 __u16 vlan_tci;
66 __be16 vlan_proto;
67 unsigned int l2_len;
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 struct action_fifo {
75 int head;
76 int tail;
77 /* Deferred action fifo queue storage. */
78 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
79 };
80
81 static struct action_fifo __percpu *action_fifos;
82 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
83 * looping by the network stack
84 */
85 static DEFINE_PER_CPU(int, exec_actions_level);
86
87 static void action_fifo_init(struct action_fifo *fifo)
88 {
89 fifo->head = 0;
90 fifo->tail = 0;
91 }
92
93 static bool action_fifo_is_empty(const struct action_fifo *fifo)
94 {
95 return (fifo->head == fifo->tail);
96 }
97
98 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
99 {
100 if (action_fifo_is_empty(fifo))
101 return NULL;
102
103 return &fifo->fifo[fifo->tail++];
104 }
105
106 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
107 {
108 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
109 return NULL;
110
111 return &fifo->fifo[fifo->head++];
112 }
113
114 /* Return queue entry if fifo is not full */
115 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
116 const struct sw_flow_key *key,
117 const struct nlattr *attr)
118 {
119 struct action_fifo *fifo;
120 struct deferred_action *da;
121
122 fifo = this_cpu_ptr(action_fifos);
123 da = action_fifo_put(fifo);
124 if (da) {
125 da->skb = skb;
126 da->actions = attr;
127 da->pkt_key = *key;
128 }
129
130 return da;
131 }
132
133 static void invalidate_flow_key(struct sw_flow_key *key)
134 {
135 key->eth.type = htons(0);
136 }
137
138 static bool is_flow_key_valid(const struct sw_flow_key *key)
139 {
140 return !!key->eth.type;
141 }
142
143 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
144 const struct ovs_action_push_mpls *mpls)
145 {
146 __be32 *new_mpls_lse;
147 struct ethhdr *hdr;
148
149 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
150 if (skb_encapsulation(skb))
151 return -ENOTSUPP;
152
153 if (skb_cow_head(skb, MPLS_HLEN) < 0)
154 return -ENOMEM;
155
156 skb_push(skb, MPLS_HLEN);
157 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
158 skb->mac_len);
159 skb_reset_mac_header(skb);
160
161 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
162 *new_mpls_lse = mpls->mpls_lse;
163
164 if (skb->ip_summed == CHECKSUM_COMPLETE)
165 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
166 MPLS_HLEN, 0));
167
168 hdr = eth_hdr(skb);
169 hdr->h_proto = mpls->mpls_ethertype;
170 if (!ovs_skb_get_inner_protocol(skb))
171 ovs_skb_set_inner_protocol(skb, skb->protocol);
172 skb->protocol = mpls->mpls_ethertype;
173
174 invalidate_flow_key(key);
175 return 0;
176 }
177
178 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
179 const __be16 ethertype)
180 {
181 struct ethhdr *hdr;
182 int err;
183
184 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
185 if (unlikely(err))
186 return err;
187
188 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
189
190 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
191 skb->mac_len);
192
193 __skb_pull(skb, MPLS_HLEN);
194 skb_reset_mac_header(skb);
195
196 /* skb_mpls_header() is used to locate the ethertype
197 * field correctly in the presence of VLAN tags.
198 */
199 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
200 hdr->h_proto = ethertype;
201 if (eth_p_mpls(skb->protocol))
202 skb->protocol = ethertype;
203
204 invalidate_flow_key(key);
205 return 0;
206 }
207
208 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
209 const __be32 *mpls_lse, const __be32 *mask)
210 {
211 __be32 *stack;
212 __be32 lse;
213 int err;
214
215 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
216 if (unlikely(err))
217 return err;
218
219 stack = (__be32 *)skb_mpls_header(skb);
220 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
221 if (skb->ip_summed == CHECKSUM_COMPLETE) {
222 __be32 diff[] = { ~(*stack), lse };
223
224 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
225 ~skb->csum);
226 }
227
228 *stack = lse;
229 flow_key->mpls.top_lse = lse;
230 return 0;
231 }
232
233 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
234 {
235 int err;
236
237 err = skb_vlan_pop(skb);
238 if (skb_vlan_tag_present(skb))
239 invalidate_flow_key(key);
240 else
241 key->eth.tci = 0;
242 return err;
243 }
244
245 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
246 const struct ovs_action_push_vlan *vlan)
247 {
248 if (skb_vlan_tag_present(skb))
249 invalidate_flow_key(key);
250 else
251 key->eth.tci = vlan->vlan_tci;
252 return skb_vlan_push(skb, vlan->vlan_tpid,
253 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
254 }
255
256 /* 'src' is already properly masked. */
257 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
258 {
259 u16 *dst = (u16 *)dst_;
260 const u16 *src = (const u16 *)src_;
261 const u16 *mask = (const u16 *)mask_;
262
263 OVS_SET_MASKED(dst[0], src[0], mask[0]);
264 OVS_SET_MASKED(dst[1], src[1], mask[1]);
265 OVS_SET_MASKED(dst[2], src[2], mask[2]);
266 }
267
268 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
269 const struct ovs_key_ethernet *key,
270 const struct ovs_key_ethernet *mask)
271 {
272 int err;
273
274 err = skb_ensure_writable(skb, ETH_HLEN);
275 if (unlikely(err))
276 return err;
277
278 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
279
280 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
281 mask->eth_src);
282 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
283 mask->eth_dst);
284
285 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
286
287 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
288 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
289 return 0;
290 }
291
292 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
293 __be32 addr, __be32 new_addr)
294 {
295 int transport_len = skb->len - skb_transport_offset(skb);
296
297 if (nh->frag_off & htons(IP_OFFSET))
298 return;
299
300 if (nh->protocol == IPPROTO_TCP) {
301 if (likely(transport_len >= sizeof(struct tcphdr)))
302 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
303 addr, new_addr, 1);
304 } else if (nh->protocol == IPPROTO_UDP) {
305 if (likely(transport_len >= sizeof(struct udphdr))) {
306 struct udphdr *uh = udp_hdr(skb);
307
308 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
309 inet_proto_csum_replace4(&uh->check, skb,
310 addr, new_addr, 1);
311 if (!uh->check)
312 uh->check = CSUM_MANGLED_0;
313 }
314 }
315 }
316
317 }
318
319 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
320 __be32 *addr, __be32 new_addr)
321 {
322 update_ip_l4_checksum(skb, nh, *addr, new_addr);
323 csum_replace4(&nh->check, *addr, new_addr);
324 skb_clear_hash(skb);
325 *addr = new_addr;
326 }
327
328 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
329 __be32 addr[4], const __be32 new_addr[4])
330 {
331 int transport_len = skb->len - skb_transport_offset(skb);
332
333 if (l4_proto == NEXTHDR_TCP) {
334 if (likely(transport_len >= sizeof(struct tcphdr)))
335 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
336 addr, new_addr, 1);
337 } else if (l4_proto == NEXTHDR_UDP) {
338 if (likely(transport_len >= sizeof(struct udphdr))) {
339 struct udphdr *uh = udp_hdr(skb);
340
341 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
342 inet_proto_csum_replace16(&uh->check, skb,
343 addr, new_addr, 1);
344 if (!uh->check)
345 uh->check = CSUM_MANGLED_0;
346 }
347 }
348 } else if (l4_proto == NEXTHDR_ICMP) {
349 if (likely(transport_len >= sizeof(struct icmp6hdr)))
350 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
351 skb, addr, new_addr, 1);
352 }
353 }
354
355 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
356 const __be32 mask[4], __be32 masked[4])
357 {
358 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
359 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
360 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
361 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
362 }
363
364 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
365 __be32 addr[4], const __be32 new_addr[4],
366 bool recalculate_csum)
367 {
368 if (likely(recalculate_csum))
369 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
370
371 skb_clear_hash(skb);
372 memcpy(addr, new_addr, sizeof(__be32[4]));
373 }
374
375 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
376 {
377 /* Bits 21-24 are always unmasked, so this retains their values. */
378 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
379 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
380 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
381 }
382
383 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
384 u8 mask)
385 {
386 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
387
388 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
389 nh->ttl = new_ttl;
390 }
391
392 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
393 const struct ovs_key_ipv4 *key,
394 const struct ovs_key_ipv4 *mask)
395 {
396 struct iphdr *nh;
397 __be32 new_addr;
398 int err;
399
400 err = skb_ensure_writable(skb, skb_network_offset(skb) +
401 sizeof(struct iphdr));
402 if (unlikely(err))
403 return err;
404
405 nh = ip_hdr(skb);
406
407 /* Setting an IP addresses is typically only a side effect of
408 * matching on them in the current userspace implementation, so it
409 * makes sense to check if the value actually changed.
410 */
411 if (mask->ipv4_src) {
412 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
413
414 if (unlikely(new_addr != nh->saddr)) {
415 set_ip_addr(skb, nh, &nh->saddr, new_addr);
416 flow_key->ipv4.addr.src = new_addr;
417 }
418 }
419 if (mask->ipv4_dst) {
420 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
421
422 if (unlikely(new_addr != nh->daddr)) {
423 set_ip_addr(skb, nh, &nh->daddr, new_addr);
424 flow_key->ipv4.addr.dst = new_addr;
425 }
426 }
427 if (mask->ipv4_tos) {
428 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
429 flow_key->ip.tos = nh->tos;
430 }
431 if (mask->ipv4_ttl) {
432 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
433 flow_key->ip.ttl = nh->ttl;
434 }
435
436 return 0;
437 }
438
439 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
440 {
441 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
442 }
443
444 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
445 const struct ovs_key_ipv6 *key,
446 const struct ovs_key_ipv6 *mask)
447 {
448 struct ipv6hdr *nh;
449 int err;
450
451 err = skb_ensure_writable(skb, skb_network_offset(skb) +
452 sizeof(struct ipv6hdr));
453 if (unlikely(err))
454 return err;
455
456 nh = ipv6_hdr(skb);
457
458 /* Setting an IP addresses is typically only a side effect of
459 * matching on them in the current userspace implementation, so it
460 * makes sense to check if the value actually changed.
461 */
462 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
463 __be32 *saddr = (__be32 *)&nh->saddr;
464 __be32 masked[4];
465
466 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
467
468 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
469 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
470 true);
471 memcpy(&flow_key->ipv6.addr.src, masked,
472 sizeof(flow_key->ipv6.addr.src));
473 }
474 }
475 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
476 unsigned int offset = 0;
477 int flags = IP6_FH_F_SKIP_RH;
478 bool recalc_csum = true;
479 __be32 *daddr = (__be32 *)&nh->daddr;
480 __be32 masked[4];
481
482 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
483
484 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
485 if (ipv6_ext_hdr(nh->nexthdr))
486 recalc_csum = (ipv6_find_hdr(skb, &offset,
487 NEXTHDR_ROUTING,
488 NULL, &flags)
489 != NEXTHDR_ROUTING);
490
491 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
492 recalc_csum);
493 memcpy(&flow_key->ipv6.addr.dst, masked,
494 sizeof(flow_key->ipv6.addr.dst));
495 }
496 }
497 if (mask->ipv6_tclass) {
498 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
499 flow_key->ip.tos = ipv6_get_dsfield(nh);
500 }
501 if (mask->ipv6_label) {
502 set_ipv6_fl(nh, ntohl(key->ipv6_label),
503 ntohl(mask->ipv6_label));
504 flow_key->ipv6.label =
505 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
506 }
507 if (mask->ipv6_hlimit) {
508 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
509 mask->ipv6_hlimit);
510 flow_key->ip.ttl = nh->hop_limit;
511 }
512 return 0;
513 }
514
515 /* Must follow skb_ensure_writable() since that can move the skb data. */
516 static void set_tp_port(struct sk_buff *skb, __be16 *port,
517 __be16 new_port, __sum16 *check)
518 {
519 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
520 *port = new_port;
521 }
522
523 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
524 const struct ovs_key_udp *key,
525 const struct ovs_key_udp *mask)
526 {
527 struct udphdr *uh;
528 __be16 src, dst;
529 int err;
530
531 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
532 sizeof(struct udphdr));
533 if (unlikely(err))
534 return err;
535
536 uh = udp_hdr(skb);
537 /* Either of the masks is non-zero, so do not bother checking them. */
538 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
539 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
540
541 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
542 if (likely(src != uh->source)) {
543 set_tp_port(skb, &uh->source, src, &uh->check);
544 flow_key->tp.src = src;
545 }
546 if (likely(dst != uh->dest)) {
547 set_tp_port(skb, &uh->dest, dst, &uh->check);
548 flow_key->tp.dst = dst;
549 }
550
551 if (unlikely(!uh->check))
552 uh->check = CSUM_MANGLED_0;
553 } else {
554 uh->source = src;
555 uh->dest = dst;
556 flow_key->tp.src = src;
557 flow_key->tp.dst = dst;
558 }
559
560 skb_clear_hash(skb);
561
562 return 0;
563 }
564
565 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
566 const struct ovs_key_tcp *key,
567 const struct ovs_key_tcp *mask)
568 {
569 struct tcphdr *th;
570 __be16 src, dst;
571 int err;
572
573 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
574 sizeof(struct tcphdr));
575 if (unlikely(err))
576 return err;
577
578 th = tcp_hdr(skb);
579 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
580 if (likely(src != th->source)) {
581 set_tp_port(skb, &th->source, src, &th->check);
582 flow_key->tp.src = src;
583 }
584 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
585 if (likely(dst != th->dest)) {
586 set_tp_port(skb, &th->dest, dst, &th->check);
587 flow_key->tp.dst = dst;
588 }
589 skb_clear_hash(skb);
590
591 return 0;
592 }
593
594 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
595 const struct ovs_key_sctp *key,
596 const struct ovs_key_sctp *mask)
597 {
598 unsigned int sctphoff = skb_transport_offset(skb);
599 struct sctphdr *sh;
600 __le32 old_correct_csum, new_csum, old_csum;
601 int err;
602
603 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
604 if (unlikely(err))
605 return err;
606
607 sh = sctp_hdr(skb);
608 old_csum = sh->checksum;
609 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
610
611 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
612 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
613
614 new_csum = sctp_compute_cksum(skb, sctphoff);
615
616 /* Carry any checksum errors through. */
617 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
618
619 skb_clear_hash(skb);
620 flow_key->tp.src = sh->source;
621 flow_key->tp.dst = sh->dest;
622
623 return 0;
624 }
625
626 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0)
627 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
628 {
629 struct ovs_frag_data *data = get_pcpu_ptr(ovs_frag_data_storage);
630 struct vport *vport = data->vport;
631
632 if (skb_cow_head(skb, data->l2_len) < 0) {
633 kfree_skb(skb);
634 return -ENOMEM;
635 }
636
637 __skb_dst_copy(skb, data->dst);
638 *OVS_CB(skb) = data->cb;
639 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
640 skb->vlan_tci = data->vlan_tci;
641 skb->vlan_proto = data->vlan_proto;
642
643 /* Reconstruct the MAC header. */
644 skb_push(skb, data->l2_len);
645 memcpy(skb->data, &data->l2_data, data->l2_len);
646 ovs_skb_postpush_rcsum(skb, skb->data, data->l2_len);
647 skb_reset_mac_header(skb);
648
649 ovs_vport_send(vport, skb);
650 return 0;
651 }
652
653 static unsigned int
654 ovs_dst_get_mtu(const struct dst_entry *dst)
655 {
656 return dst->dev->mtu;
657 }
658
659 static struct dst_ops ovs_dst_ops = {
660 .family = AF_UNSPEC,
661 .mtu = ovs_dst_get_mtu,
662 };
663
664 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
665 * ovs_vport_output(), which is called once per fragmented packet.
666 */
667 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
668 {
669 unsigned int hlen = skb_network_offset(skb);
670 struct ovs_frag_data *data;
671
672 data = get_pcpu_ptr(ovs_frag_data_storage);
673 data->dst = (unsigned long) skb_dst(skb);
674 data->vport = vport;
675 data->cb = *OVS_CB(skb);
676 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
677 data->vlan_tci = skb->vlan_tci;
678 data->vlan_proto = skb->vlan_proto;
679 data->l2_len = hlen;
680 memcpy(&data->l2_data, skb->data, hlen);
681
682 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
683 skb_pull(skb, hlen);
684 }
685
686 static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
687 __be16 ethertype)
688 {
689 if (skb_network_offset(skb) > MAX_L2_LEN) {
690 OVS_NLERR(1, "L2 header too long to fragment");
691 return;
692 }
693
694 if (ethertype == htons(ETH_P_IP)) {
695 struct dst_entry ovs_dst;
696 unsigned long orig_dst;
697
698 prepare_frag(vport, skb);
699 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
700 DST_OBSOLETE_NONE, DST_NOCOUNT);
701 ovs_dst.dev = vport->dev;
702
703 orig_dst = (unsigned long) skb_dst(skb);
704 skb_dst_set_noref(skb, &ovs_dst);
705 IPCB(skb)->frag_max_size = mru;
706
707 ip_do_fragment(skb->sk, skb, ovs_vport_output);
708 refdst_drop(orig_dst);
709 } else if (ethertype == htons(ETH_P_IPV6)) {
710 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
711 unsigned long orig_dst;
712 struct rt6_info ovs_rt;
713
714 if (!v6ops) {
715 kfree_skb(skb);
716 return;
717 }
718
719 prepare_frag(vport, skb);
720 memset(&ovs_rt, 0, sizeof(ovs_rt));
721 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
722 DST_OBSOLETE_NONE, DST_NOCOUNT);
723 ovs_rt.dst.dev = vport->dev;
724
725 orig_dst = (unsigned long) skb_dst(skb);
726 skb_dst_set_noref(skb, &ovs_rt.dst);
727 IP6CB(skb)->frag_max_size = mru;
728
729 v6ops->fragment(skb->sk, skb, ovs_vport_output);
730 refdst_drop(orig_dst);
731 } else {
732 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
733 ovs_vport_name(vport), ntohs(ethertype), mru,
734 vport->dev->mtu);
735 kfree_skb(skb);
736 }
737 }
738 #else /* <= 3.9 */
739 static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
740 __be16 ethertype)
741 {
742 WARN_ONCE(1, "Fragment unavailable ->%s: eth=%04x, MRU=%d, MTU=%d.",
743 ovs_vport_name(vport), ntohs(ethertype), mru,
744 vport->dev->mtu);
745 kfree_skb(skb);
746 }
747 #endif
748
749 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
750 struct sw_flow_key *key)
751 {
752 struct vport *vport = ovs_vport_rcu(dp, out_port);
753
754 if (likely(vport)) {
755 u16 mru = OVS_CB(skb)->mru;
756
757 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
758 ovs_vport_send(vport, skb);
759 } else if (mru <= vport->dev->mtu) {
760 __be16 ethertype = key->eth.type;
761
762 if (!is_flow_key_valid(key)) {
763 if (eth_p_mpls(skb->protocol))
764 ethertype = ovs_skb_get_inner_protocol(skb);
765 else
766 ethertype = vlan_get_protocol(skb);
767 }
768
769 ovs_fragment(vport, skb, mru, ethertype);
770 } else {
771 OVS_NLERR(true, "Cannot fragment IP frames");
772 kfree_skb(skb);
773 }
774 } else {
775 kfree_skb(skb);
776 }
777 }
778 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
779 struct sw_flow_key *key, const struct nlattr *attr,
780 const struct nlattr *actions, int actions_len)
781 {
782 struct ip_tunnel_info info;
783 struct dp_upcall_info upcall;
784 const struct nlattr *a;
785 int rem;
786
787 memset(&upcall, 0, sizeof(upcall));
788 upcall.cmd = OVS_PACKET_CMD_ACTION;
789 upcall.mru = OVS_CB(skb)->mru;
790
791 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
792 a = nla_next(a, &rem)) {
793 switch (nla_type(a)) {
794 case OVS_USERSPACE_ATTR_USERDATA:
795 upcall.userdata = a;
796 break;
797
798 case OVS_USERSPACE_ATTR_PID:
799 upcall.portid = nla_get_u32(a);
800 break;
801
802 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
803 /* Get out tunnel info. */
804 struct vport *vport;
805
806 vport = ovs_vport_rcu(dp, nla_get_u32(a));
807 if (vport) {
808 int err;
809
810 upcall.egress_tun_info = &info;
811 err = ovs_vport_get_egress_tun_info(vport, skb,
812 &upcall);
813 if (err)
814 upcall.egress_tun_info = NULL;
815 }
816
817 break;
818 }
819
820 case OVS_USERSPACE_ATTR_ACTIONS: {
821 /* Include actions. */
822 upcall.actions = actions;
823 upcall.actions_len = actions_len;
824 break;
825 }
826
827 } /* End of switch. */
828 }
829
830 return ovs_dp_upcall(dp, skb, key, &upcall);
831 }
832
833 static int sample(struct datapath *dp, struct sk_buff *skb,
834 struct sw_flow_key *key, const struct nlattr *attr,
835 const struct nlattr *actions, int actions_len)
836 {
837 const struct nlattr *acts_list = NULL;
838 const struct nlattr *a;
839 int rem;
840
841 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
842 a = nla_next(a, &rem)) {
843 u32 probability;
844
845 switch (nla_type(a)) {
846 case OVS_SAMPLE_ATTR_PROBABILITY:
847 probability = nla_get_u32(a);
848 if (!probability || prandom_u32() > probability)
849 return 0;
850 break;
851
852 case OVS_SAMPLE_ATTR_ACTIONS:
853 acts_list = a;
854 break;
855 }
856 }
857
858 rem = nla_len(acts_list);
859 a = nla_data(acts_list);
860
861 /* Actions list is empty, do nothing */
862 if (unlikely(!rem))
863 return 0;
864
865 /* The only known usage of sample action is having a single user-space
866 * action. Treat this usage as a special case.
867 * The output_userspace() should clone the skb to be sent to the
868 * user space. This skb will be consumed by its caller.
869 */
870 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
871 nla_is_last(a, rem)))
872 return output_userspace(dp, skb, key, a, actions, actions_len);
873
874 skb = skb_clone(skb, GFP_ATOMIC);
875 if (!skb)
876 /* Skip the sample action when out of memory. */
877 return 0;
878
879 if (!add_deferred_actions(skb, key, a)) {
880 if (net_ratelimit())
881 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
882 ovs_dp_name(dp));
883
884 kfree_skb(skb);
885 }
886 return 0;
887 }
888
889 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
890 const struct nlattr *attr)
891 {
892 struct ovs_action_hash *hash_act = nla_data(attr);
893 u32 hash = 0;
894
895 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
896 hash = skb_get_hash(skb);
897 hash = jhash_1word(hash, hash_act->hash_basis);
898 if (!hash)
899 hash = 0x1;
900
901 key->ovs_flow_hash = hash;
902 }
903
904 static int execute_set_action(struct sk_buff *skb,
905 struct sw_flow_key *flow_key,
906 const struct nlattr *a)
907 {
908 /* Only tunnel set execution is supported without a mask. */
909 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
910 struct ovs_tunnel_info *tun = nla_data(a);
911
912 ovs_skb_dst_drop(skb);
913 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
914 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
915 return 0;
916 }
917
918 return -EINVAL;
919 }
920
921 /* Mask is at the midpoint of the data. */
922 #define get_mask(a, type) ((const type)nla_data(a) + 1)
923
924 static int execute_masked_set_action(struct sk_buff *skb,
925 struct sw_flow_key *flow_key,
926 const struct nlattr *a)
927 {
928 int err = 0;
929
930 switch (nla_type(a)) {
931 case OVS_KEY_ATTR_PRIORITY:
932 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
933 *get_mask(a, u32 *));
934 flow_key->phy.priority = skb->priority;
935 break;
936
937 case OVS_KEY_ATTR_SKB_MARK:
938 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
939 flow_key->phy.skb_mark = skb->mark;
940 break;
941
942 case OVS_KEY_ATTR_TUNNEL_INFO:
943 /* Masked data not supported for tunnel. */
944 err = -EINVAL;
945 break;
946
947 case OVS_KEY_ATTR_ETHERNET:
948 err = set_eth_addr(skb, flow_key, nla_data(a),
949 get_mask(a, struct ovs_key_ethernet *));
950 break;
951
952 case OVS_KEY_ATTR_IPV4:
953 err = set_ipv4(skb, flow_key, nla_data(a),
954 get_mask(a, struct ovs_key_ipv4 *));
955 break;
956
957 case OVS_KEY_ATTR_IPV6:
958 err = set_ipv6(skb, flow_key, nla_data(a),
959 get_mask(a, struct ovs_key_ipv6 *));
960 break;
961
962 case OVS_KEY_ATTR_TCP:
963 err = set_tcp(skb, flow_key, nla_data(a),
964 get_mask(a, struct ovs_key_tcp *));
965 break;
966
967 case OVS_KEY_ATTR_UDP:
968 err = set_udp(skb, flow_key, nla_data(a),
969 get_mask(a, struct ovs_key_udp *));
970 break;
971
972 case OVS_KEY_ATTR_SCTP:
973 err = set_sctp(skb, flow_key, nla_data(a),
974 get_mask(a, struct ovs_key_sctp *));
975 break;
976
977 case OVS_KEY_ATTR_MPLS:
978 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
979 __be32 *));
980 break;
981
982 case OVS_KEY_ATTR_CT_STATE:
983 case OVS_KEY_ATTR_CT_ZONE:
984 case OVS_KEY_ATTR_CT_MARK:
985 case OVS_KEY_ATTR_CT_LABEL:
986 err = -EINVAL;
987 break;
988 }
989
990 return err;
991 }
992
993 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
994 struct sw_flow_key *key,
995 const struct nlattr *a, int rem)
996 {
997 struct deferred_action *da;
998
999 if (!is_flow_key_valid(key)) {
1000 int err;
1001
1002 err = ovs_flow_key_update(skb, key);
1003 if (err)
1004 return err;
1005 }
1006 BUG_ON(!is_flow_key_valid(key));
1007
1008 if (!nla_is_last(a, rem)) {
1009 /* Recirc action is the not the last action
1010 * of the action list, need to clone the skb.
1011 */
1012 skb = skb_clone(skb, GFP_ATOMIC);
1013
1014 /* Skip the recirc action when out of memory, but
1015 * continue on with the rest of the action list.
1016 */
1017 if (!skb)
1018 return 0;
1019 }
1020
1021 da = add_deferred_actions(skb, key, NULL);
1022 if (da) {
1023 da->pkt_key.recirc_id = nla_get_u32(a);
1024 } else {
1025 kfree_skb(skb);
1026
1027 if (net_ratelimit())
1028 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1029 ovs_dp_name(dp));
1030 }
1031
1032 return 0;
1033 }
1034
1035 /* Execute a list of actions against 'skb'. */
1036 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1037 struct sw_flow_key *key,
1038 const struct nlattr *attr, int len)
1039 {
1040 /* Every output action needs a separate clone of 'skb', but the common
1041 * case is just a single output action, so that doing a clone and
1042 * then freeing the original skbuff is wasteful. So the following code
1043 * is slightly obscure just to avoid that.
1044 */
1045 int prev_port = -1;
1046 const struct nlattr *a;
1047 int rem;
1048
1049 for (a = attr, rem = len; rem > 0;
1050 a = nla_next(a, &rem)) {
1051 int err = 0;
1052
1053 if (unlikely(prev_port != -1)) {
1054 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1055
1056 if (out_skb)
1057 do_output(dp, out_skb, prev_port, key);
1058
1059 prev_port = -1;
1060 }
1061
1062 switch (nla_type(a)) {
1063 case OVS_ACTION_ATTR_OUTPUT:
1064 prev_port = nla_get_u32(a);
1065 break;
1066
1067 case OVS_ACTION_ATTR_USERSPACE:
1068 output_userspace(dp, skb, key, a, attr, len);
1069 break;
1070
1071 case OVS_ACTION_ATTR_HASH:
1072 execute_hash(skb, key, a);
1073 break;
1074
1075 case OVS_ACTION_ATTR_PUSH_MPLS:
1076 err = push_mpls(skb, key, nla_data(a));
1077 break;
1078
1079 case OVS_ACTION_ATTR_POP_MPLS:
1080 err = pop_mpls(skb, key, nla_get_be16(a));
1081 break;
1082
1083 case OVS_ACTION_ATTR_PUSH_VLAN:
1084 err = push_vlan(skb, key, nla_data(a));
1085 break;
1086
1087 case OVS_ACTION_ATTR_POP_VLAN:
1088 err = pop_vlan(skb, key);
1089 break;
1090
1091 case OVS_ACTION_ATTR_RECIRC:
1092 err = execute_recirc(dp, skb, key, a, rem);
1093 if (nla_is_last(a, rem)) {
1094 /* If this is the last action, the skb has
1095 * been consumed or freed.
1096 * Return immediately.
1097 */
1098 return err;
1099 }
1100 break;
1101
1102 case OVS_ACTION_ATTR_SET:
1103 err = execute_set_action(skb, key, nla_data(a));
1104 break;
1105
1106 case OVS_ACTION_ATTR_SET_MASKED:
1107 case OVS_ACTION_ATTR_SET_TO_MASKED:
1108 err = execute_masked_set_action(skb, key, nla_data(a));
1109 break;
1110
1111 case OVS_ACTION_ATTR_SAMPLE:
1112 err = sample(dp, skb, key, a, attr, len);
1113 break;
1114
1115 case OVS_ACTION_ATTR_CT:
1116 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1117 nla_data(a));
1118
1119 /* Hide stolen IP fragments from user space. */
1120 if (err == -EINPROGRESS)
1121 return 0;
1122 break;
1123 }
1124
1125 if (unlikely(err)) {
1126 kfree_skb(skb);
1127 return err;
1128 }
1129 }
1130
1131 if (prev_port != -1)
1132 do_output(dp, skb, prev_port, key);
1133 else
1134 consume_skb(skb);
1135
1136 return 0;
1137 }
1138
1139 static void process_deferred_actions(struct datapath *dp)
1140 {
1141 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1142
1143 /* Do not touch the FIFO in case there is no deferred actions. */
1144 if (action_fifo_is_empty(fifo))
1145 return;
1146
1147 /* Finishing executing all deferred actions. */
1148 do {
1149 struct deferred_action *da = action_fifo_get(fifo);
1150 struct sk_buff *skb = da->skb;
1151 struct sw_flow_key *key = &da->pkt_key;
1152 const struct nlattr *actions = da->actions;
1153
1154 if (actions)
1155 do_execute_actions(dp, skb, key, actions,
1156 nla_len(actions));
1157 else
1158 ovs_dp_process_packet(skb, key);
1159 } while (!action_fifo_is_empty(fifo));
1160
1161 /* Reset FIFO for the next packet. */
1162 action_fifo_init(fifo);
1163 }
1164
1165 /* Execute a list of actions against 'skb'. */
1166 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1167 const struct sw_flow_actions *acts,
1168 struct sw_flow_key *key)
1169 {
1170 int level = this_cpu_read(exec_actions_level);
1171 int err;
1172
1173 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
1174 if (net_ratelimit())
1175 pr_warn("%s: packet loop detected, dropping.\n",
1176 ovs_dp_name(dp));
1177
1178 kfree_skb(skb);
1179 return -ELOOP;
1180 }
1181
1182 this_cpu_inc(exec_actions_level);
1183 err = do_execute_actions(dp, skb, key,
1184 acts->actions, acts->actions_len);
1185
1186 if (!level)
1187 process_deferred_actions(dp);
1188
1189 this_cpu_dec(exec_actions_level);
1190
1191 /* This return status currently does not reflect the errors
1192 * encounted during deferred actions execution. Probably needs to
1193 * be fixed in the future.
1194 */
1195 return err;
1196 }
1197
1198 int action_fifos_init(void)
1199 {
1200 action_fifos = alloc_percpu(struct action_fifo);
1201 if (!action_fifos)
1202 return -ENOMEM;
1203
1204 return 0;
1205 }
1206
1207 void action_fifos_exit(void)
1208 {
1209 free_percpu(action_fifos);
1210 }