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