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