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