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