]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/openvswitch/actions.c
net: Change pseudohdr argument of inet_proto_csum_replace* to be a bool
[mirror_ubuntu-artful-kernel.git] / net / openvswitch / actions.c
CommitLineData
ccb1352e 1/*
971427f3 2 * Copyright (c) 2007-2014 Nicira, Inc.
ccb1352e
JG
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>
a175a723 25#include <linux/sctp.h>
ccb1352e
JG
26#include <linux/tcp.h>
27#include <linux/udp.h>
28#include <linux/in6.h>
29#include <linux/if_arp.h>
30#include <linux/if_vlan.h>
25cd9ba0 31
ccb1352e 32#include <net/ip.h>
3fdbd1ce 33#include <net/ipv6.h>
ccb1352e
JG
34#include <net/checksum.h>
35#include <net/dsfield.h>
25cd9ba0 36#include <net/mpls.h>
a175a723 37#include <net/sctp/checksum.h>
ccb1352e
JG
38
39#include "datapath.h"
971427f3 40#include "flow.h"
ccb1352e
JG
41#include "vport.h"
42
43static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 44 struct sw_flow_key *key,
651887b0 45 const struct nlattr *attr, int len);
ccb1352e 46
971427f3
AZ
47struct deferred_action {
48 struct sk_buff *skb;
49 const struct nlattr *actions;
50
51 /* Store pkt_key clone when creating deferred action. */
52 struct sw_flow_key pkt_key;
53};
54
55#define DEFERRED_ACTION_FIFO_SIZE 10
56struct action_fifo {
57 int head;
58 int tail;
59 /* Deferred action fifo queue storage. */
60 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
61};
62
63static struct action_fifo __percpu *action_fifos;
64static DEFINE_PER_CPU(int, exec_actions_level);
65
66static void action_fifo_init(struct action_fifo *fifo)
67{
68 fifo->head = 0;
69 fifo->tail = 0;
70}
71
12eb18f7 72static bool action_fifo_is_empty(const struct action_fifo *fifo)
971427f3
AZ
73{
74 return (fifo->head == fifo->tail);
75}
76
77static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
78{
79 if (action_fifo_is_empty(fifo))
80 return NULL;
81
82 return &fifo->fifo[fifo->tail++];
83}
84
85static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
86{
87 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
88 return NULL;
89
90 return &fifo->fifo[fifo->head++];
91}
92
93/* Return true if fifo is not full */
94static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
12eb18f7 95 const struct sw_flow_key *key,
971427f3
AZ
96 const struct nlattr *attr)
97{
98 struct action_fifo *fifo;
99 struct deferred_action *da;
100
101 fifo = this_cpu_ptr(action_fifos);
102 da = action_fifo_put(fifo);
103 if (da) {
104 da->skb = skb;
105 da->actions = attr;
106 da->pkt_key = *key;
107 }
108
109 return da;
110}
111
fff06c36
PS
112static void invalidate_flow_key(struct sw_flow_key *key)
113{
114 key->eth.type = htons(0);
115}
116
117static bool is_flow_key_valid(const struct sw_flow_key *key)
118{
119 return !!key->eth.type;
120}
121
fff06c36 122static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
25cd9ba0
SH
123 const struct ovs_action_push_mpls *mpls)
124{
125 __be32 *new_mpls_lse;
126 struct ethhdr *hdr;
127
128 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
129 if (skb->encapsulation)
130 return -ENOTSUPP;
131
132 if (skb_cow_head(skb, MPLS_HLEN) < 0)
133 return -ENOMEM;
134
135 skb_push(skb, MPLS_HLEN);
136 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
137 skb->mac_len);
138 skb_reset_mac_header(skb);
139
140 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
141 *new_mpls_lse = mpls->mpls_lse;
142
143 if (skb->ip_summed == CHECKSUM_COMPLETE)
144 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
145 MPLS_HLEN, 0));
146
147 hdr = eth_hdr(skb);
148 hdr->h_proto = mpls->mpls_ethertype;
149
cbe7e76d
PS
150 if (!skb->inner_protocol)
151 skb_set_inner_protocol(skb, skb->protocol);
25cd9ba0
SH
152 skb->protocol = mpls->mpls_ethertype;
153
fff06c36 154 invalidate_flow_key(key);
25cd9ba0
SH
155 return 0;
156}
157
fff06c36
PS
158static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
159 const __be16 ethertype)
25cd9ba0
SH
160{
161 struct ethhdr *hdr;
162 int err;
163
e2195121 164 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
25cd9ba0
SH
165 if (unlikely(err))
166 return err;
167
1abcd82c 168 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
25cd9ba0
SH
169
170 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
171 skb->mac_len);
172
173 __skb_pull(skb, MPLS_HLEN);
174 skb_reset_mac_header(skb);
175
176 /* skb_mpls_header() is used to locate the ethertype
177 * field correctly in the presence of VLAN tags.
178 */
179 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
180 hdr->h_proto = ethertype;
181 if (eth_p_mpls(skb->protocol))
182 skb->protocol = ethertype;
fff06c36
PS
183
184 invalidate_flow_key(key);
25cd9ba0
SH
185 return 0;
186}
187
83d2b9ba
JR
188/* 'KEY' must not have any bits set outside of the 'MASK' */
189#define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
190#define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
191
192static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
193 const __be32 *mpls_lse, const __be32 *mask)
25cd9ba0
SH
194{
195 __be32 *stack;
83d2b9ba 196 __be32 lse;
25cd9ba0
SH
197 int err;
198
e2195121 199 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
25cd9ba0
SH
200 if (unlikely(err))
201 return err;
202
203 stack = (__be32 *)skb_mpls_header(skb);
83d2b9ba 204 lse = MASKED(*stack, *mpls_lse, *mask);
25cd9ba0 205 if (skb->ip_summed == CHECKSUM_COMPLETE) {
83d2b9ba
JR
206 __be32 diff[] = { ~(*stack), lse };
207
25cd9ba0
SH
208 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
209 ~skb->csum);
210 }
211
83d2b9ba
JR
212 *stack = lse;
213 flow_key->mpls.top_lse = lse;
25cd9ba0
SH
214 return 0;
215}
216
fff06c36 217static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
ccb1352e 218{
ccb1352e
JG
219 int err;
220
93515d53 221 err = skb_vlan_pop(skb);
df8a39de 222 if (skb_vlan_tag_present(skb))
93515d53
JP
223 invalidate_flow_key(key);
224 else
fff06c36 225 key->eth.tci = 0;
93515d53 226 return err;
ccb1352e
JG
227}
228
fff06c36
PS
229static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
230 const struct ovs_action_push_vlan *vlan)
ccb1352e 231{
df8a39de 232 if (skb_vlan_tag_present(skb))
fff06c36 233 invalidate_flow_key(key);
93515d53 234 else
fff06c36 235 key->eth.tci = vlan->vlan_tci;
93515d53
JP
236 return skb_vlan_push(skb, vlan->vlan_tpid,
237 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
ccb1352e
JG
238}
239
83d2b9ba
JR
240/* 'src' is already properly masked. */
241static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
242{
243 u16 *dst = (u16 *)dst_;
244 const u16 *src = (const u16 *)src_;
245 const u16 *mask = (const u16 *)mask_;
246
247 SET_MASKED(dst[0], src[0], mask[0]);
248 SET_MASKED(dst[1], src[1], mask[1]);
249 SET_MASKED(dst[2], src[2], mask[2]);
250}
251
252static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
253 const struct ovs_key_ethernet *key,
254 const struct ovs_key_ethernet *mask)
ccb1352e
JG
255{
256 int err;
83d2b9ba 257
e2195121 258 err = skb_ensure_writable(skb, ETH_HLEN);
ccb1352e
JG
259 if (unlikely(err))
260 return err;
261
b34df5e8
PS
262 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
263
83d2b9ba
JR
264 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
265 mask->eth_src);
266 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
267 mask->eth_dst);
ccb1352e 268
b34df5e8
PS
269 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
270
83d2b9ba
JR
271 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
272 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
ccb1352e
JG
273 return 0;
274}
275
3576fd79
GG
276static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
277 __be32 addr, __be32 new_addr)
ccb1352e
JG
278{
279 int transport_len = skb->len - skb_transport_offset(skb);
280
3576fd79
GG
281 if (nh->frag_off & htons(IP_OFFSET))
282 return;
283
ccb1352e
JG
284 if (nh->protocol == IPPROTO_TCP) {
285 if (likely(transport_len >= sizeof(struct tcphdr)))
286 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
4b048d6d 287 addr, new_addr, true);
ccb1352e 288 } else if (nh->protocol == IPPROTO_UDP) {
81e5d41d
JG
289 if (likely(transport_len >= sizeof(struct udphdr))) {
290 struct udphdr *uh = udp_hdr(skb);
291
292 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
293 inet_proto_csum_replace4(&uh->check, skb,
4b048d6d 294 addr, new_addr, true);
81e5d41d
JG
295 if (!uh->check)
296 uh->check = CSUM_MANGLED_0;
297 }
298 }
ccb1352e 299 }
3576fd79 300}
ccb1352e 301
3576fd79
GG
302static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
303 __be32 *addr, __be32 new_addr)
304{
305 update_ip_l4_checksum(skb, nh, *addr, new_addr);
ccb1352e 306 csum_replace4(&nh->check, *addr, new_addr);
7539fadc 307 skb_clear_hash(skb);
ccb1352e
JG
308 *addr = new_addr;
309}
310
3fdbd1ce
AA
311static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
312 __be32 addr[4], const __be32 new_addr[4])
313{
314 int transport_len = skb->len - skb_transport_offset(skb);
315
856447d0 316 if (l4_proto == NEXTHDR_TCP) {
3fdbd1ce
AA
317 if (likely(transport_len >= sizeof(struct tcphdr)))
318 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
4b048d6d 319 addr, new_addr, true);
856447d0 320 } else if (l4_proto == NEXTHDR_UDP) {
3fdbd1ce
AA
321 if (likely(transport_len >= sizeof(struct udphdr))) {
322 struct udphdr *uh = udp_hdr(skb);
323
324 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
325 inet_proto_csum_replace16(&uh->check, skb,
4b048d6d 326 addr, new_addr, true);
3fdbd1ce
AA
327 if (!uh->check)
328 uh->check = CSUM_MANGLED_0;
329 }
330 }
856447d0
JG
331 } else if (l4_proto == NEXTHDR_ICMP) {
332 if (likely(transport_len >= sizeof(struct icmp6hdr)))
333 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
4b048d6d 334 skb, addr, new_addr, true);
3fdbd1ce
AA
335 }
336}
337
83d2b9ba
JR
338static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
339 const __be32 mask[4], __be32 masked[4])
340{
341 masked[0] = MASKED(old[0], addr[0], mask[0]);
342 masked[1] = MASKED(old[1], addr[1], mask[1]);
343 masked[2] = MASKED(old[2], addr[2], mask[2]);
344 masked[3] = MASKED(old[3], addr[3], mask[3]);
345}
346
3fdbd1ce
AA
347static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
348 __be32 addr[4], const __be32 new_addr[4],
349 bool recalculate_csum)
350{
351 if (recalculate_csum)
352 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
353
7539fadc 354 skb_clear_hash(skb);
3fdbd1ce
AA
355 memcpy(addr, new_addr, sizeof(__be32[4]));
356}
357
83d2b9ba 358static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
3fdbd1ce 359{
83d2b9ba
JR
360 /* Bits 21-24 are always unmasked, so this retains their values. */
361 SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
362 SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
363 SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
3fdbd1ce
AA
364}
365
83d2b9ba
JR
366static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
367 u8 mask)
3fdbd1ce 368{
83d2b9ba 369 new_ttl = MASKED(nh->ttl, new_ttl, mask);
3fdbd1ce 370
ccb1352e
JG
371 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
372 nh->ttl = new_ttl;
373}
374
83d2b9ba
JR
375static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
376 const struct ovs_key_ipv4 *key,
377 const struct ovs_key_ipv4 *mask)
ccb1352e
JG
378{
379 struct iphdr *nh;
83d2b9ba 380 __be32 new_addr;
ccb1352e
JG
381 int err;
382
e2195121
JP
383 err = skb_ensure_writable(skb, skb_network_offset(skb) +
384 sizeof(struct iphdr));
ccb1352e
JG
385 if (unlikely(err))
386 return err;
387
388 nh = ip_hdr(skb);
389
83d2b9ba
JR
390 /* Setting an IP addresses is typically only a side effect of
391 * matching on them in the current userspace implementation, so it
392 * makes sense to check if the value actually changed.
393 */
394 if (mask->ipv4_src) {
395 new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
ccb1352e 396
83d2b9ba
JR
397 if (unlikely(new_addr != nh->saddr)) {
398 set_ip_addr(skb, nh, &nh->saddr, new_addr);
399 flow_key->ipv4.addr.src = new_addr;
400 }
fff06c36 401 }
83d2b9ba
JR
402 if (mask->ipv4_dst) {
403 new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
ccb1352e 404
83d2b9ba
JR
405 if (unlikely(new_addr != nh->daddr)) {
406 set_ip_addr(skb, nh, &nh->daddr, new_addr);
407 flow_key->ipv4.addr.dst = new_addr;
408 }
fff06c36 409 }
83d2b9ba
JR
410 if (mask->ipv4_tos) {
411 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
412 flow_key->ip.tos = nh->tos;
413 }
414 if (mask->ipv4_ttl) {
415 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
416 flow_key->ip.ttl = nh->ttl;
fff06c36 417 }
ccb1352e
JG
418
419 return 0;
420}
421
83d2b9ba
JR
422static bool is_ipv6_mask_nonzero(const __be32 addr[4])
423{
424 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
425}
426
427static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
428 const struct ovs_key_ipv6 *key,
429 const struct ovs_key_ipv6 *mask)
3fdbd1ce
AA
430{
431 struct ipv6hdr *nh;
432 int err;
3fdbd1ce 433
e2195121
JP
434 err = skb_ensure_writable(skb, skb_network_offset(skb) +
435 sizeof(struct ipv6hdr));
3fdbd1ce
AA
436 if (unlikely(err))
437 return err;
438
439 nh = ipv6_hdr(skb);
3fdbd1ce 440
83d2b9ba
JR
441 /* Setting an IP addresses is typically only a side effect of
442 * matching on them in the current userspace implementation, so it
443 * makes sense to check if the value actually changed.
444 */
445 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
446 __be32 *saddr = (__be32 *)&nh->saddr;
447 __be32 masked[4];
448
449 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
450
451 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
452 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
453 true);
454 memcpy(&flow_key->ipv6.addr.src, masked,
455 sizeof(flow_key->ipv6.addr.src));
456 }
457 }
458 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
3fdbd1ce
AA
459 unsigned int offset = 0;
460 int flags = IP6_FH_F_SKIP_RH;
461 bool recalc_csum = true;
83d2b9ba
JR
462 __be32 *daddr = (__be32 *)&nh->daddr;
463 __be32 masked[4];
464
465 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
466
467 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
468 if (ipv6_ext_hdr(nh->nexthdr))
469 recalc_csum = (ipv6_find_hdr(skb, &offset,
470 NEXTHDR_ROUTING,
471 NULL, &flags)
472 != NEXTHDR_ROUTING);
473
474 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
475 recalc_csum);
476 memcpy(&flow_key->ipv6.addr.dst, masked,
477 sizeof(flow_key->ipv6.addr.dst));
478 }
479 }
480 if (mask->ipv6_tclass) {
481 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
482 flow_key->ip.tos = ipv6_get_dsfield(nh);
483 }
484 if (mask->ipv6_label) {
485 set_ipv6_fl(nh, ntohl(key->ipv6_label),
486 ntohl(mask->ipv6_label));
487 flow_key->ipv6.label =
488 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
489 }
490 if (mask->ipv6_hlimit) {
491 SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
492 flow_key->ip.ttl = nh->hop_limit;
3fdbd1ce 493 }
3fdbd1ce
AA
494 return 0;
495}
496
e2195121 497/* Must follow skb_ensure_writable() since that can move the skb data. */
ccb1352e 498static void set_tp_port(struct sk_buff *skb, __be16 *port,
83d2b9ba 499 __be16 new_port, __sum16 *check)
ccb1352e 500{
4b048d6d 501 inet_proto_csum_replace2(check, skb, *port, new_port, false);
ccb1352e 502 *port = new_port;
81e5d41d
JG
503}
504
83d2b9ba
JR
505static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
506 const struct ovs_key_udp *key,
507 const struct ovs_key_udp *mask)
ccb1352e
JG
508{
509 struct udphdr *uh;
83d2b9ba 510 __be16 src, dst;
ccb1352e
JG
511 int err;
512
e2195121
JP
513 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
514 sizeof(struct udphdr));
ccb1352e
JG
515 if (unlikely(err))
516 return err;
517
518 uh = udp_hdr(skb);
83d2b9ba
JR
519 /* Either of the masks is non-zero, so do not bother checking them. */
520 src = MASKED(uh->source, key->udp_src, mask->udp_src);
521 dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
ccb1352e 522
83d2b9ba
JR
523 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
524 if (likely(src != uh->source)) {
525 set_tp_port(skb, &uh->source, src, &uh->check);
526 flow_key->tp.src = src;
527 }
528 if (likely(dst != uh->dest)) {
529 set_tp_port(skb, &uh->dest, dst, &uh->check);
530 flow_key->tp.dst = dst;
531 }
532
533 if (unlikely(!uh->check))
534 uh->check = CSUM_MANGLED_0;
535 } else {
536 uh->source = src;
537 uh->dest = dst;
538 flow_key->tp.src = src;
539 flow_key->tp.dst = dst;
fff06c36 540 }
ccb1352e 541
83d2b9ba
JR
542 skb_clear_hash(skb);
543
ccb1352e
JG
544 return 0;
545}
546
83d2b9ba
JR
547static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
548 const struct ovs_key_tcp *key,
549 const struct ovs_key_tcp *mask)
ccb1352e
JG
550{
551 struct tcphdr *th;
83d2b9ba 552 __be16 src, dst;
ccb1352e
JG
553 int err;
554
e2195121
JP
555 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
556 sizeof(struct tcphdr));
ccb1352e
JG
557 if (unlikely(err))
558 return err;
559
560 th = tcp_hdr(skb);
83d2b9ba
JR
561 src = MASKED(th->source, key->tcp_src, mask->tcp_src);
562 if (likely(src != th->source)) {
563 set_tp_port(skb, &th->source, src, &th->check);
564 flow_key->tp.src = src;
fff06c36 565 }
83d2b9ba
JR
566 dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
567 if (likely(dst != th->dest)) {
568 set_tp_port(skb, &th->dest, dst, &th->check);
569 flow_key->tp.dst = dst;
fff06c36 570 }
83d2b9ba 571 skb_clear_hash(skb);
ccb1352e
JG
572
573 return 0;
574}
575
83d2b9ba
JR
576static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
577 const struct ovs_key_sctp *key,
578 const struct ovs_key_sctp *mask)
a175a723 579{
83d2b9ba 580 unsigned int sctphoff = skb_transport_offset(skb);
a175a723 581 struct sctphdr *sh;
83d2b9ba 582 __le32 old_correct_csum, new_csum, old_csum;
a175a723 583 int err;
a175a723 584
e2195121 585 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
a175a723
JS
586 if (unlikely(err))
587 return err;
588
589 sh = sctp_hdr(skb);
83d2b9ba
JR
590 old_csum = sh->checksum;
591 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
a175a723 592
83d2b9ba
JR
593 sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
594 sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
a175a723 595
83d2b9ba 596 new_csum = sctp_compute_cksum(skb, sctphoff);
a175a723 597
83d2b9ba
JR
598 /* Carry any checksum errors through. */
599 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
a175a723 600
83d2b9ba
JR
601 skb_clear_hash(skb);
602 flow_key->tp.src = sh->source;
603 flow_key->tp.dst = sh->dest;
a175a723
JS
604
605 return 0;
606}
607
738967b8 608static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
ccb1352e 609{
738967b8 610 struct vport *vport = ovs_vport_rcu(dp, out_port);
ccb1352e 611
738967b8
AZ
612 if (likely(vport))
613 ovs_vport_send(vport, skb);
614 else
ccb1352e 615 kfree_skb(skb);
ccb1352e
JG
616}
617
618static int output_userspace(struct datapath *dp, struct sk_buff *skb,
ccea7445
NM
619 struct sw_flow_key *key, const struct nlattr *attr,
620 const struct nlattr *actions, int actions_len)
ccb1352e 621{
1d8fff90 622 struct ip_tunnel_info info;
ccb1352e
JG
623 struct dp_upcall_info upcall;
624 const struct nlattr *a;
625 int rem;
626
ccea7445 627 memset(&upcall, 0, sizeof(upcall));
ccb1352e 628 upcall.cmd = OVS_PACKET_CMD_ACTION;
ccb1352e
JG
629
630 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
631 a = nla_next(a, &rem)) {
632 switch (nla_type(a)) {
633 case OVS_USERSPACE_ATTR_USERDATA:
634 upcall.userdata = a;
635 break;
636
637 case OVS_USERSPACE_ATTR_PID:
15e47304 638 upcall.portid = nla_get_u32(a);
ccb1352e 639 break;
8f0aad6f
WZ
640
641 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
642 /* Get out tunnel info. */
643 struct vport *vport;
644
645 vport = ovs_vport_rcu(dp, nla_get_u32(a));
646 if (vport) {
647 int err;
648
649 err = ovs_vport_get_egress_tun_info(vport, skb,
650 &info);
651 if (!err)
652 upcall.egress_tun_info = &info;
653 }
654 break;
ccb1352e 655 }
8f0aad6f 656
ccea7445
NM
657 case OVS_USERSPACE_ATTR_ACTIONS: {
658 /* Include actions. */
659 upcall.actions = actions;
660 upcall.actions_len = actions_len;
661 break;
662 }
663
8f0aad6f 664 } /* End of switch. */
ccb1352e
JG
665 }
666
e8eedb85 667 return ovs_dp_upcall(dp, skb, key, &upcall);
ccb1352e
JG
668}
669
670static int sample(struct datapath *dp, struct sk_buff *skb,
ccea7445
NM
671 struct sw_flow_key *key, const struct nlattr *attr,
672 const struct nlattr *actions, int actions_len)
ccb1352e
JG
673{
674 const struct nlattr *acts_list = NULL;
675 const struct nlattr *a;
676 int rem;
677
678 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
679 a = nla_next(a, &rem)) {
e05176a3
WZ
680 u32 probability;
681
ccb1352e
JG
682 switch (nla_type(a)) {
683 case OVS_SAMPLE_ATTR_PROBABILITY:
e05176a3
WZ
684 probability = nla_get_u32(a);
685 if (!probability || prandom_u32() > probability)
ccb1352e
JG
686 return 0;
687 break;
688
689 case OVS_SAMPLE_ATTR_ACTIONS:
690 acts_list = a;
691 break;
692 }
693 }
694
651887b0
SH
695 rem = nla_len(acts_list);
696 a = nla_data(acts_list);
697
32ae87ff
AZ
698 /* Actions list is empty, do nothing */
699 if (unlikely(!rem))
700 return 0;
651887b0 701
32ae87ff
AZ
702 /* The only known usage of sample action is having a single user-space
703 * action. Treat this usage as a special case.
704 * The output_userspace() should clone the skb to be sent to the
705 * user space. This skb will be consumed by its caller.
651887b0 706 */
32ae87ff 707 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
941d8ebc 708 nla_is_last(a, rem)))
ccea7445 709 return output_userspace(dp, skb, key, a, actions, actions_len);
32ae87ff
AZ
710
711 skb = skb_clone(skb, GFP_ATOMIC);
712 if (!skb)
713 /* Skip the sample action when out of memory. */
714 return 0;
715
971427f3
AZ
716 if (!add_deferred_actions(skb, key, a)) {
717 if (net_ratelimit())
718 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
719 ovs_dp_name(dp));
720
721 kfree_skb(skb);
722 }
723 return 0;
724}
725
726static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
727 const struct nlattr *attr)
728{
729 struct ovs_action_hash *hash_act = nla_data(attr);
730 u32 hash = 0;
731
732 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
733 hash = skb_get_hash(skb);
734 hash = jhash_1word(hash, hash_act->hash_basis);
735 if (!hash)
736 hash = 0x1;
737
738 key->ovs_flow_hash = hash;
ccb1352e
JG
739}
740
83d2b9ba
JR
741static int execute_set_action(struct sk_buff *skb,
742 struct sw_flow_key *flow_key,
743 const struct nlattr *a)
744{
745 /* Only tunnel set execution is supported without a mask. */
746 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
34ae932a
TG
747 struct ovs_tunnel_info *tun = nla_data(a);
748
749 skb_dst_drop(skb);
750 dst_hold((struct dst_entry *)tun->tun_dst);
751 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
752
753 /* FIXME: Remove when all vports have been converted */
754 OVS_CB(skb)->egress_tun_info = &tun->tun_dst->u.tun_info;
755
83d2b9ba
JR
756 return 0;
757 }
758
759 return -EINVAL;
760}
761
762/* Mask is at the midpoint of the data. */
763#define get_mask(a, type) ((const type)nla_data(a) + 1)
764
765static int execute_masked_set_action(struct sk_buff *skb,
766 struct sw_flow_key *flow_key,
767 const struct nlattr *a)
ccb1352e
JG
768{
769 int err = 0;
770
83d2b9ba 771 switch (nla_type(a)) {
ccb1352e 772 case OVS_KEY_ATTR_PRIORITY:
83d2b9ba
JR
773 SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
774 flow_key->phy.priority = skb->priority;
ccb1352e
JG
775 break;
776
39c7caeb 777 case OVS_KEY_ATTR_SKB_MARK:
83d2b9ba
JR
778 SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
779 flow_key->phy.skb_mark = skb->mark;
39c7caeb
AA
780 break;
781
f0b128c1 782 case OVS_KEY_ATTR_TUNNEL_INFO:
83d2b9ba
JR
783 /* Masked data not supported for tunnel. */
784 err = -EINVAL;
7d5437c7
PS
785 break;
786
ccb1352e 787 case OVS_KEY_ATTR_ETHERNET:
83d2b9ba
JR
788 err = set_eth_addr(skb, flow_key, nla_data(a),
789 get_mask(a, struct ovs_key_ethernet *));
ccb1352e
JG
790 break;
791
792 case OVS_KEY_ATTR_IPV4:
83d2b9ba
JR
793 err = set_ipv4(skb, flow_key, nla_data(a),
794 get_mask(a, struct ovs_key_ipv4 *));
ccb1352e
JG
795 break;
796
3fdbd1ce 797 case OVS_KEY_ATTR_IPV6:
83d2b9ba
JR
798 err = set_ipv6(skb, flow_key, nla_data(a),
799 get_mask(a, struct ovs_key_ipv6 *));
3fdbd1ce
AA
800 break;
801
ccb1352e 802 case OVS_KEY_ATTR_TCP:
83d2b9ba
JR
803 err = set_tcp(skb, flow_key, nla_data(a),
804 get_mask(a, struct ovs_key_tcp *));
ccb1352e
JG
805 break;
806
807 case OVS_KEY_ATTR_UDP:
83d2b9ba
JR
808 err = set_udp(skb, flow_key, nla_data(a),
809 get_mask(a, struct ovs_key_udp *));
ccb1352e 810 break;
a175a723
JS
811
812 case OVS_KEY_ATTR_SCTP:
83d2b9ba
JR
813 err = set_sctp(skb, flow_key, nla_data(a),
814 get_mask(a, struct ovs_key_sctp *));
a175a723 815 break;
25cd9ba0
SH
816
817 case OVS_KEY_ATTR_MPLS:
83d2b9ba
JR
818 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
819 __be32 *));
25cd9ba0 820 break;
ccb1352e
JG
821 }
822
823 return err;
824}
825
971427f3
AZ
826static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
827 struct sw_flow_key *key,
828 const struct nlattr *a, int rem)
829{
830 struct deferred_action *da;
971427f3 831
fff06c36
PS
832 if (!is_flow_key_valid(key)) {
833 int err;
834
835 err = ovs_flow_key_update(skb, key);
836 if (err)
837 return err;
838 }
839 BUG_ON(!is_flow_key_valid(key));
971427f3 840
941d8ebc 841 if (!nla_is_last(a, rem)) {
971427f3
AZ
842 /* Recirc action is the not the last action
843 * of the action list, need to clone the skb.
844 */
845 skb = skb_clone(skb, GFP_ATOMIC);
846
847 /* Skip the recirc action when out of memory, but
848 * continue on with the rest of the action list.
849 */
850 if (!skb)
851 return 0;
852 }
853
854 da = add_deferred_actions(skb, key, NULL);
855 if (da) {
856 da->pkt_key.recirc_id = nla_get_u32(a);
857 } else {
858 kfree_skb(skb);
859
860 if (net_ratelimit())
861 pr_warn("%s: deferred action limit reached, drop recirc action\n",
862 ovs_dp_name(dp));
863 }
864
865 return 0;
866}
867
ccb1352e
JG
868/* Execute a list of actions against 'skb'. */
869static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
2ff3e4e4 870 struct sw_flow_key *key,
651887b0 871 const struct nlattr *attr, int len)
ccb1352e
JG
872{
873 /* Every output action needs a separate clone of 'skb', but the common
874 * case is just a single output action, so that doing a clone and
875 * then freeing the original skbuff is wasteful. So the following code
fff06c36
PS
876 * is slightly obscure just to avoid that.
877 */
ccb1352e
JG
878 int prev_port = -1;
879 const struct nlattr *a;
880 int rem;
881
882 for (a = attr, rem = len; rem > 0;
883 a = nla_next(a, &rem)) {
884 int err = 0;
885
738967b8
AZ
886 if (unlikely(prev_port != -1)) {
887 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
888
889 if (out_skb)
890 do_output(dp, out_skb, prev_port);
891
ccb1352e
JG
892 prev_port = -1;
893 }
894
895 switch (nla_type(a)) {
896 case OVS_ACTION_ATTR_OUTPUT:
897 prev_port = nla_get_u32(a);
898 break;
899
900 case OVS_ACTION_ATTR_USERSPACE:
ccea7445 901 output_userspace(dp, skb, key, a, attr, len);
ccb1352e
JG
902 break;
903
971427f3
AZ
904 case OVS_ACTION_ATTR_HASH:
905 execute_hash(skb, key, a);
906 break;
907
25cd9ba0 908 case OVS_ACTION_ATTR_PUSH_MPLS:
fff06c36 909 err = push_mpls(skb, key, nla_data(a));
25cd9ba0
SH
910 break;
911
912 case OVS_ACTION_ATTR_POP_MPLS:
fff06c36 913 err = pop_mpls(skb, key, nla_get_be16(a));
25cd9ba0
SH
914 break;
915
ccb1352e 916 case OVS_ACTION_ATTR_PUSH_VLAN:
fff06c36 917 err = push_vlan(skb, key, nla_data(a));
ccb1352e
JG
918 break;
919
920 case OVS_ACTION_ATTR_POP_VLAN:
fff06c36 921 err = pop_vlan(skb, key);
ccb1352e
JG
922 break;
923
971427f3
AZ
924 case OVS_ACTION_ATTR_RECIRC:
925 err = execute_recirc(dp, skb, key, a, rem);
941d8ebc 926 if (nla_is_last(a, rem)) {
971427f3
AZ
927 /* If this is the last action, the skb has
928 * been consumed or freed.
929 * Return immediately.
930 */
931 return err;
932 }
933 break;
934
ccb1352e 935 case OVS_ACTION_ATTR_SET:
fff06c36 936 err = execute_set_action(skb, key, nla_data(a));
ccb1352e
JG
937 break;
938
83d2b9ba
JR
939 case OVS_ACTION_ATTR_SET_MASKED:
940 case OVS_ACTION_ATTR_SET_TO_MASKED:
941 err = execute_masked_set_action(skb, key, nla_data(a));
942 break;
943
ccb1352e 944 case OVS_ACTION_ATTR_SAMPLE:
ccea7445 945 err = sample(dp, skb, key, a, attr, len);
ccb1352e
JG
946 break;
947 }
948
949 if (unlikely(err)) {
950 kfree_skb(skb);
951 return err;
952 }
953 }
954
651887b0 955 if (prev_port != -1)
ccb1352e 956 do_output(dp, skb, prev_port);
651887b0 957 else
ccb1352e
JG
958 consume_skb(skb);
959
960 return 0;
961}
962
971427f3
AZ
963static void process_deferred_actions(struct datapath *dp)
964{
965 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
966
967 /* Do not touch the FIFO in case there is no deferred actions. */
968 if (action_fifo_is_empty(fifo))
969 return;
970
971 /* Finishing executing all deferred actions. */
972 do {
973 struct deferred_action *da = action_fifo_get(fifo);
974 struct sk_buff *skb = da->skb;
975 struct sw_flow_key *key = &da->pkt_key;
976 const struct nlattr *actions = da->actions;
977
978 if (actions)
979 do_execute_actions(dp, skb, key, actions,
980 nla_len(actions));
981 else
982 ovs_dp_process_packet(skb, key);
983 } while (!action_fifo_is_empty(fifo));
984
985 /* Reset FIFO for the next packet. */
986 action_fifo_init(fifo);
987}
988
ccb1352e 989/* Execute a list of actions against 'skb'. */
2ff3e4e4 990int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
12eb18f7
TG
991 const struct sw_flow_actions *acts,
992 struct sw_flow_key *key)
ccb1352e 993{
971427f3 994 int level = this_cpu_read(exec_actions_level);
971427f3
AZ
995 int err;
996
971427f3 997 this_cpu_inc(exec_actions_level);
f0b128c1 998 OVS_CB(skb)->egress_tun_info = NULL;
971427f3
AZ
999 err = do_execute_actions(dp, skb, key,
1000 acts->actions, acts->actions_len);
1001
1002 if (!level)
1003 process_deferred_actions(dp);
1004
1005 this_cpu_dec(exec_actions_level);
1006 return err;
1007}
1008
1009int action_fifos_init(void)
1010{
1011 action_fifos = alloc_percpu(struct action_fifo);
1012 if (!action_fifos)
1013 return -ENOMEM;
ccb1352e 1014
971427f3
AZ
1015 return 0;
1016}
1017
1018void action_fifos_exit(void)
1019{
1020 free_percpu(action_fifos);
ccb1352e 1021}