]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
a6059080 | 2 | * Copyright (c) 2007-2014 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> |
10f72e3d | 25 | #include <linux/sctp.h> |
064af421 BP |
26 | #include <linux/tcp.h> |
27 | #include <linux/udp.h> | |
28 | #include <linux/in6.h> | |
401eeb92 | 29 | #include <linux/if_arp.h> |
064af421 BP |
30 | #include <linux/if_vlan.h> |
31 | #include <net/ip.h> | |
bc7a5acd | 32 | #include <net/ipv6.h> |
064af421 | 33 | #include <net/checksum.h> |
530180fd | 34 | #include <net/dsfield.h> |
10f72e3d | 35 | #include <net/sctp/checksum.h> |
f2459fe7 | 36 | |
f2459fe7 | 37 | #include "datapath.h" |
ccf43786 SH |
38 | #include "gso.h" |
39 | #include "mpls.h" | |
6ce39213 | 40 | #include "vlan.h" |
f2459fe7 | 41 | #include "vport.h" |
064af421 | 42 | |
6ff686f2 | 43 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
fbf4f74d | 44 | const struct nlattr *attr, int len); |
871dfe07 | 45 | |
10db8b20 | 46 | static int make_writable(struct sk_buff *skb, int write_len) |
064af421 | 47 | { |
10db8b20 JG |
48 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
49 | return 0; | |
0cd8a05e | 50 | |
10db8b20 | 51 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
064af421 BP |
52 | } |
53 | ||
ccf43786 SH |
54 | /* The end of the mac header. |
55 | * | |
56 | * For non-MPLS skbs this will correspond to the network header. | |
57 | * For MPLS skbs it will be before the network_header as the MPLS | |
58 | * label stack lies between the end of the mac header and the network | |
59 | * header. That is, for MPLS skbs the end of the mac header | |
60 | * is the top of the MPLS label stack. | |
61 | */ | |
62 | static unsigned char *mac_header_end(const struct sk_buff *skb) | |
63 | { | |
64 | return skb_mac_header(skb) + skb->mac_len; | |
65 | } | |
66 | ||
67 | static int push_mpls(struct sk_buff *skb, | |
68 | const struct ovs_action_push_mpls *mpls) | |
69 | { | |
70 | __be32 *new_mpls_lse; | |
71 | struct ethhdr *hdr; | |
72 | ||
73 | if (skb_cow_head(skb, MPLS_HLEN) < 0) | |
74 | return -ENOMEM; | |
75 | ||
76 | skb_push(skb, MPLS_HLEN); | |
77 | memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), | |
78 | skb->mac_len); | |
79 | skb_reset_mac_header(skb); | |
80 | ||
81 | new_mpls_lse = (__be32 *)mac_header_end(skb); | |
82 | *new_mpls_lse = mpls->mpls_lse; | |
83 | ||
84 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
85 | skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse, | |
86 | MPLS_HLEN, 0)); | |
87 | ||
88 | hdr = eth_hdr(skb); | |
89 | hdr->h_proto = mpls->mpls_ethertype; | |
90 | if (!ovs_skb_get_inner_protocol(skb)) | |
91 | ovs_skb_set_inner_protocol(skb, skb->protocol); | |
92 | skb->protocol = mpls->mpls_ethertype; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | static int pop_mpls(struct sk_buff *skb, const __be16 ethertype) | |
97 | { | |
98 | struct ethhdr *hdr; | |
99 | int err; | |
100 | ||
101 | err = make_writable(skb, skb->mac_len + MPLS_HLEN); | |
102 | if (unlikely(err)) | |
103 | return err; | |
104 | ||
105 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
106 | skb->csum = csum_sub(skb->csum, | |
107 | csum_partial(mac_header_end(skb), | |
108 | MPLS_HLEN, 0)); | |
109 | ||
110 | memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), | |
111 | skb->mac_len); | |
112 | ||
113 | __skb_pull(skb, MPLS_HLEN); | |
114 | skb_reset_mac_header(skb); | |
115 | ||
116 | /* mac_header_end() is used to locate the ethertype | |
117 | * field correctly in the presence of VLAN tags. | |
118 | */ | |
119 | hdr = (struct ethhdr *)(mac_header_end(skb) - ETH_HLEN); | |
120 | hdr->h_proto = ethertype; | |
121 | if (eth_p_mpls(skb->protocol)) | |
122 | skb->protocol = ethertype; | |
123 | return 0; | |
124 | } | |
125 | ||
126 | static int set_mpls(struct sk_buff *skb, const __be32 *mpls_lse) | |
127 | { | |
128 | __be32 *stack = (__be32 *)mac_header_end(skb); | |
129 | int err; | |
130 | ||
131 | err = make_writable(skb, skb->mac_len + MPLS_HLEN); | |
132 | if (unlikely(err)) | |
133 | return err; | |
134 | ||
135 | if (skb->ip_summed == CHECKSUM_COMPLETE) { | |
136 | __be32 diff[] = { ~(*stack), *mpls_lse }; | |
137 | skb->csum = ~csum_partial((char *)diff, sizeof(diff), | |
138 | ~skb->csum); | |
139 | } | |
140 | ||
141 | *stack = *mpls_lse; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
b2492cb7 | 146 | /* remove VLAN header from packet and update csum accordingly. */ |
d9065a90 | 147 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) |
064af421 | 148 | { |
ceb176fd | 149 | struct vlan_hdr *vhdr; |
10db8b20 | 150 | int err; |
064af421 | 151 | |
10db8b20 JG |
152 | err = make_writable(skb, VLAN_ETH_HLEN); |
153 | if (unlikely(err)) | |
154 | return err; | |
6ce39213 | 155 | |
237c4f2a | 156 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
635c9298 | 157 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
af9d14a8 | 158 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
635c9298 | 159 | |
ceb176fd PS |
160 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
161 | *current_tci = vhdr->h_vlan_TCI; | |
d9065a90 | 162 | |
6ce39213 | 163 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
ceb176fd | 164 | __skb_pull(skb, VLAN_HLEN); |
064af421 | 165 | |
ceb176fd | 166 | vlan_set_encap_proto(skb, vhdr); |
064af421 | 167 | skb->mac_header += VLAN_HLEN; |
ccf43786 SH |
168 | /* Update mac_len for subsequent MPLS actions */ |
169 | skb->mac_len -= VLAN_HLEN; | |
064af421 | 170 | |
10db8b20 | 171 | return 0; |
064af421 BP |
172 | } |
173 | ||
d9065a90 | 174 | static int pop_vlan(struct sk_buff *skb) |
064af421 | 175 | { |
d9065a90 PS |
176 | __be16 tci; |
177 | int err; | |
10db8b20 | 178 | |
d9065a90 PS |
179 | if (likely(vlan_tx_tag_present(skb))) { |
180 | vlan_set_tci(skb, 0); | |
181 | } else { | |
182 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 183 | skb->len < VLAN_ETH_HLEN)) |
10db8b20 | 184 | return 0; |
064af421 | 185 | |
d9065a90 PS |
186 | err = __pop_vlan_tci(skb, &tci); |
187 | if (err) | |
10db8b20 | 188 | return err; |
064af421 | 189 | } |
d9065a90 PS |
190 | /* move next vlan tag to hw accel tag */ |
191 | if (likely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 192 | skb->len < VLAN_ETH_HLEN)) |
d9065a90 PS |
193 | return 0; |
194 | ||
195 | err = __pop_vlan_tci(skb, &tci); | |
196 | if (unlikely(err)) | |
197 | return err; | |
064af421 | 198 | |
9b764edf | 199 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci)); |
d9065a90 PS |
200 | return 0; |
201 | } | |
202 | ||
fea393b1 | 203 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
d9065a90 PS |
204 | { |
205 | if (unlikely(vlan_tx_tag_present(skb))) { | |
206 | u16 current_tag; | |
207 | ||
208 | /* push down current VLAN tag */ | |
209 | current_tag = vlan_tx_tag_get(skb); | |
210 | ||
9b764edf | 211 | if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag)) |
d9065a90 | 212 | return -ENOMEM; |
10db8b20 | 213 | |
ccf43786 SH |
214 | /* Update mac_len for subsequent MPLS actions */ |
215 | skb->mac_len += VLAN_HLEN; | |
216 | ||
237c4f2a | 217 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
d9065a90 | 218 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
af9d14a8 | 219 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
d9065a90 PS |
220 | |
221 | } | |
9b764edf | 222 | __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
10db8b20 | 223 | return 0; |
064af421 BP |
224 | } |
225 | ||
4edb9ae9 PS |
226 | static int set_eth_addr(struct sk_buff *skb, |
227 | const struct ovs_key_ethernet *eth_key) | |
ca78c6b6 | 228 | { |
4edb9ae9 PS |
229 | int err; |
230 | err = make_writable(skb, ETH_HLEN); | |
231 | if (unlikely(err)) | |
232 | return err; | |
233 | ||
237c4f2a | 234 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
3cfede14 | 235 | |
982a47ec JP |
236 | ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src); |
237 | ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst); | |
4edb9ae9 | 238 | |
237c4f2a | 239 | ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
3cfede14 | 240 | |
4edb9ae9 | 241 | return 0; |
ca78c6b6 BP |
242 | } |
243 | ||
4edb9ae9 PS |
244 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
245 | __be32 *addr, __be32 new_addr) | |
ca78c6b6 BP |
246 | { |
247 | int transport_len = skb->len - skb_transport_offset(skb); | |
4edb9ae9 PS |
248 | |
249 | if (nh->protocol == IPPROTO_TCP) { | |
ca78c6b6 | 250 | if (likely(transport_len >= sizeof(struct tcphdr))) |
4edb9ae9 PS |
251 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
252 | *addr, new_addr, 1); | |
253 | } else if (nh->protocol == IPPROTO_UDP) { | |
55ce87bc JG |
254 | if (likely(transport_len >= sizeof(struct udphdr))) { |
255 | struct udphdr *uh = udp_hdr(skb); | |
256 | ||
237c4f2a | 257 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
55ce87bc JG |
258 | inet_proto_csum_replace4(&uh->check, skb, |
259 | *addr, new_addr, 1); | |
260 | if (!uh->check) | |
261 | uh->check = CSUM_MANGLED_0; | |
262 | } | |
263 | } | |
ca78c6b6 | 264 | } |
4edb9ae9 PS |
265 | |
266 | csum_replace4(&nh->check, *addr, new_addr); | |
e2f3178f | 267 | skb_clear_hash(skb); |
4edb9ae9 | 268 | *addr = new_addr; |
ca78c6b6 BP |
269 | } |
270 | ||
bc7a5acd AA |
271 | static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, |
272 | __be32 addr[4], const __be32 new_addr[4]) | |
273 | { | |
274 | int transport_len = skb->len - skb_transport_offset(skb); | |
275 | ||
276 | if (l4_proto == IPPROTO_TCP) { | |
277 | if (likely(transport_len >= sizeof(struct tcphdr))) | |
278 | inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, | |
279 | addr, new_addr, 1); | |
280 | } else if (l4_proto == IPPROTO_UDP) { | |
281 | if (likely(transport_len >= sizeof(struct udphdr))) { | |
282 | struct udphdr *uh = udp_hdr(skb); | |
283 | ||
237c4f2a | 284 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
bc7a5acd AA |
285 | inet_proto_csum_replace16(&uh->check, skb, |
286 | addr, new_addr, 1); | |
287 | if (!uh->check) | |
288 | uh->check = CSUM_MANGLED_0; | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, | |
295 | __be32 addr[4], const __be32 new_addr[4], | |
296 | bool recalculate_csum) | |
297 | { | |
298 | if (recalculate_csum) | |
299 | update_ipv6_checksum(skb, l4_proto, addr, new_addr); | |
300 | ||
e2f3178f | 301 | skb_clear_hash(skb); |
bc7a5acd AA |
302 | memcpy(addr, new_addr, sizeof(__be32[4])); |
303 | } | |
304 | ||
305 | static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc) | |
306 | { | |
307 | nh->priority = tc >> 4; | |
308 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4); | |
309 | } | |
310 | ||
311 | static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl) | |
312 | { | |
313 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16; | |
314 | nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8; | |
315 | nh->flow_lbl[2] = fl & 0x000000FF; | |
316 | } | |
317 | ||
a61680c6 JP |
318 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
319 | { | |
320 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); | |
321 | nh->ttl = new_ttl; | |
322 | } | |
323 | ||
4edb9ae9 | 324 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
064af421 | 325 | { |
ca78c6b6 | 326 | struct iphdr *nh; |
10db8b20 | 327 | int err; |
ca78c6b6 | 328 | |
10db8b20 JG |
329 | err = make_writable(skb, skb_network_offset(skb) + |
330 | sizeof(struct iphdr)); | |
331 | if (unlikely(err)) | |
332 | return err; | |
ca78c6b6 BP |
333 | |
334 | nh = ip_hdr(skb); | |
ca78c6b6 | 335 | |
4edb9ae9 PS |
336 | if (ipv4_key->ipv4_src != nh->saddr) |
337 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); | |
ca78c6b6 | 338 | |
4edb9ae9 PS |
339 | if (ipv4_key->ipv4_dst != nh->daddr) |
340 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); | |
a4a26436 | 341 | |
4edb9ae9 | 342 | if (ipv4_key->ipv4_tos != nh->tos) |
530180fd | 343 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
ca78c6b6 | 344 | |
a61680c6 JP |
345 | if (ipv4_key->ipv4_ttl != nh->ttl) |
346 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); | |
347 | ||
10db8b20 | 348 | return 0; |
064af421 BP |
349 | } |
350 | ||
bc7a5acd AA |
351 | static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key) |
352 | { | |
353 | struct ipv6hdr *nh; | |
354 | int err; | |
355 | __be32 *saddr; | |
356 | __be32 *daddr; | |
357 | ||
358 | err = make_writable(skb, skb_network_offset(skb) + | |
359 | sizeof(struct ipv6hdr)); | |
360 | if (unlikely(err)) | |
361 | return err; | |
362 | ||
363 | nh = ipv6_hdr(skb); | |
364 | saddr = (__be32 *)&nh->saddr; | |
365 | daddr = (__be32 *)&nh->daddr; | |
366 | ||
367 | if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) | |
368 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr, | |
369 | ipv6_key->ipv6_src, true); | |
370 | ||
371 | if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) { | |
372 | unsigned int offset = 0; | |
373 | int flags = OVS_IP6T_FH_F_SKIP_RH; | |
374 | bool recalc_csum = true; | |
375 | ||
376 | if (ipv6_ext_hdr(nh->nexthdr)) | |
377 | recalc_csum = ipv6_find_hdr(skb, &offset, | |
378 | NEXTHDR_ROUTING, NULL, | |
379 | &flags) != NEXTHDR_ROUTING; | |
380 | ||
381 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr, | |
382 | ipv6_key->ipv6_dst, recalc_csum); | |
383 | } | |
384 | ||
385 | set_ipv6_tc(nh, ipv6_key->ipv6_tclass); | |
386 | set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label)); | |
387 | nh->hop_limit = ipv6_key->ipv6_hlimit; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
4edb9ae9 PS |
392 | /* Must follow make_writable() since that can move the skb data. */ |
393 | static void set_tp_port(struct sk_buff *skb, __be16 *port, | |
394 | __be16 new_port, __sum16 *check) | |
959a2ecd | 395 | { |
4edb9ae9 PS |
396 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
397 | *port = new_port; | |
e2f3178f | 398 | skb_clear_hash(skb); |
4edb9ae9 | 399 | } |
10db8b20 | 400 | |
55ce87bc JG |
401 | static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port) |
402 | { | |
403 | struct udphdr *uh = udp_hdr(skb); | |
404 | ||
237c4f2a | 405 | if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) { |
55ce87bc JG |
406 | set_tp_port(skb, port, new_port, &uh->check); |
407 | ||
408 | if (!uh->check) | |
409 | uh->check = CSUM_MANGLED_0; | |
410 | } else { | |
411 | *port = new_port; | |
e2f3178f | 412 | skb_clear_hash(skb); |
55ce87bc JG |
413 | } |
414 | } | |
415 | ||
416 | static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key) | |
4edb9ae9 PS |
417 | { |
418 | struct udphdr *uh; | |
419 | int err; | |
10db8b20 | 420 | |
4edb9ae9 PS |
421 | err = make_writable(skb, skb_transport_offset(skb) + |
422 | sizeof(struct udphdr)); | |
10db8b20 JG |
423 | if (unlikely(err)) |
424 | return err; | |
425 | ||
4edb9ae9 PS |
426 | uh = udp_hdr(skb); |
427 | if (udp_port_key->udp_src != uh->source) | |
55ce87bc | 428 | set_udp_port(skb, &uh->source, udp_port_key->udp_src); |
4edb9ae9 PS |
429 | |
430 | if (udp_port_key->udp_dst != uh->dest) | |
55ce87bc | 431 | set_udp_port(skb, &uh->dest, udp_port_key->udp_dst); |
10db8b20 JG |
432 | |
433 | return 0; | |
959a2ecd JP |
434 | } |
435 | ||
55ce87bc | 436 | static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key) |
064af421 | 437 | { |
4edb9ae9 | 438 | struct tcphdr *th; |
10db8b20 | 439 | int err; |
064af421 | 440 | |
10db8b20 JG |
441 | err = make_writable(skb, skb_transport_offset(skb) + |
442 | sizeof(struct tcphdr)); | |
443 | if (unlikely(err)) | |
444 | return err; | |
ca78c6b6 | 445 | |
4edb9ae9 PS |
446 | th = tcp_hdr(skb); |
447 | if (tcp_port_key->tcp_src != th->source) | |
448 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); | |
064af421 | 449 | |
4edb9ae9 PS |
450 | if (tcp_port_key->tcp_dst != th->dest) |
451 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); | |
ca78c6b6 | 452 | |
10db8b20 | 453 | return 0; |
064af421 BP |
454 | } |
455 | ||
10f72e3d JS |
456 | static int set_sctp(struct sk_buff *skb, |
457 | const struct ovs_key_sctp *sctp_port_key) | |
458 | { | |
459 | struct sctphdr *sh; | |
460 | int err; | |
461 | unsigned int sctphoff = skb_transport_offset(skb); | |
462 | ||
463 | err = make_writable(skb, sctphoff + sizeof(struct sctphdr)); | |
464 | if (unlikely(err)) | |
465 | return err; | |
466 | ||
467 | sh = sctp_hdr(skb); | |
468 | if (sctp_port_key->sctp_src != sh->source || | |
469 | sctp_port_key->sctp_dst != sh->dest) { | |
470 | __le32 old_correct_csum, new_csum, old_csum; | |
471 | ||
472 | old_csum = sh->checksum; | |
473 | old_correct_csum = sctp_compute_cksum(skb, sctphoff); | |
474 | ||
475 | sh->source = sctp_port_key->sctp_src; | |
476 | sh->dest = sctp_port_key->sctp_dst; | |
477 | ||
478 | new_csum = sctp_compute_cksum(skb, sctphoff); | |
479 | ||
480 | /* Carry any checksum errors through. */ | |
481 | sh->checksum = old_csum ^ old_correct_csum ^ new_csum; | |
482 | ||
e2f3178f | 483 | skb_clear_hash(skb); |
10f72e3d JS |
484 | } |
485 | ||
486 | return 0; | |
487 | } | |
488 | ||
fe90efd9 | 489 | static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
064af421 | 490 | { |
fe90efd9 | 491 | struct vport *vport = ovs_vport_rcu(dp, out_port); |
064af421 | 492 | |
fe90efd9 AZ |
493 | if (likely(vport)) |
494 | ovs_vport_send(vport, skb); | |
495 | else | |
f15c8639 | 496 | kfree_skb(skb); |
064af421 BP |
497 | } |
498 | ||
98403001 BP |
499 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
500 | const struct nlattr *attr) | |
064af421 | 501 | { |
856081f6 | 502 | struct dp_upcall_info upcall; |
98403001 BP |
503 | const struct nlattr *a; |
504 | int rem; | |
856081f6 | 505 | |
df2c07f4 | 506 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
98403001 | 507 | upcall.userdata = NULL; |
28aea917 | 508 | upcall.portid = 0; |
98403001 BP |
509 | |
510 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
511 | a = nla_next(a, &rem)) { | |
512 | switch (nla_type(a)) { | |
513 | case OVS_USERSPACE_ATTR_USERDATA: | |
514 | upcall.userdata = a; | |
515 | break; | |
516 | ||
517 | case OVS_USERSPACE_ATTR_PID: | |
28aea917 | 518 | upcall.portid = nla_get_u32(a); |
98403001 BP |
519 | break; |
520 | } | |
521 | } | |
522 | ||
850b6b3b | 523 | return ovs_dp_upcall(dp, skb, &upcall); |
064af421 BP |
524 | } |
525 | ||
fbf4f74d SH |
526 | static bool last_action(const struct nlattr *a, int rem) |
527 | { | |
528 | return a->nla_len == rem; | |
529 | } | |
530 | ||
6ff686f2 | 531 | static int sample(struct datapath *dp, struct sk_buff *skb, |
85c9de19 | 532 | const struct nlattr *attr) |
6ff686f2 PS |
533 | { |
534 | const struct nlattr *acts_list = NULL; | |
535 | const struct nlattr *a; | |
fbf4f74d | 536 | struct sk_buff *sample_skb; |
6ff686f2 PS |
537 | int rem; |
538 | ||
539 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
540 | a = nla_next(a, &rem)) { | |
541 | switch (nla_type(a)) { | |
542 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
e2f3178f | 543 | if (prandom_u32() >= nla_get_u32(a)) |
6ff686f2 PS |
544 | return 0; |
545 | break; | |
546 | ||
547 | case OVS_SAMPLE_ATTR_ACTIONS: | |
548 | acts_list = a; | |
549 | break; | |
550 | } | |
551 | } | |
552 | ||
fbf4f74d SH |
553 | rem = nla_len(acts_list); |
554 | a = nla_data(acts_list); | |
555 | ||
556 | /* Actions list is either empty or only contains a single user-space | |
557 | * action, the latter being a special case as it is the only known | |
558 | * usage of the sample action. | |
559 | * In these special cases don't clone the skb as there are no | |
560 | * side-effects in the nested actions. | |
561 | * Otherwise, clone in case the nested actions have side effects. */ | |
562 | if (likely(rem == 0 || | |
563 | (nla_type(a) == OVS_ACTION_ATTR_USERSPACE && | |
564 | last_action(a, rem)))) { | |
565 | sample_skb = skb; | |
566 | skb_get(skb); | |
567 | } else { | |
568 | sample_skb = skb_clone(skb, GFP_ATOMIC); | |
15cd5a8e AZ |
569 | if (!sample_skb) |
570 | /* Skip the sample action when out of memory. */ | |
571 | return 0; | |
fbf4f74d SH |
572 | } |
573 | ||
574 | /* Note that do_execute_actions() never consumes skb. | |
575 | * In the case where skb has been cloned above it is the clone that | |
576 | * is consumed. Otherwise the skb_get(skb) call prevents | |
577 | * consumption by do_execute_actions(). Thus, it is safe to simply | |
578 | * return the error code and let the caller (also | |
579 | * do_execute_actions()) free skb on error. */ | |
580 | return do_execute_actions(dp, sample_skb, a, rem); | |
6ff686f2 PS |
581 | } |
582 | ||
7804df20 AZ |
583 | static void execute_hash(struct sk_buff *skb, const struct nlattr *attr) |
584 | { | |
585 | struct sw_flow_key *key = OVS_CB(skb)->pkt_key; | |
586 | struct ovs_action_hash *hash_act = nla_data(attr); | |
587 | u32 hash = 0; | |
588 | ||
589 | /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */ | |
e2f3178f | 590 | hash = skb_get_hash(skb); |
7804df20 AZ |
591 | hash = jhash_1word(hash, hash_act->hash_basis); |
592 | if (!hash) | |
593 | hash = 0x1; | |
594 | ||
595 | key->ovs_flow_hash = hash; | |
596 | } | |
597 | ||
4edb9ae9 | 598 | static int execute_set_action(struct sk_buff *skb, |
85c9de19 | 599 | const struct nlattr *nested_attr) |
4edb9ae9 | 600 | { |
15c39847 | 601 | int err = 0; |
4edb9ae9 PS |
602 | |
603 | switch (nla_type(nested_attr)) { | |
abff858b PS |
604 | case OVS_KEY_ATTR_PRIORITY: |
605 | skb->priority = nla_get_u32(nested_attr); | |
606 | break; | |
607 | ||
72e8bf28 | 608 | case OVS_KEY_ATTR_SKB_MARK: |
3025a772 | 609 | skb->mark = nla_get_u32(nested_attr); |
72e8bf28 AA |
610 | break; |
611 | ||
f0cd669f | 612 | case OVS_KEY_ATTR_TUNNEL_INFO: |
fb66fbd1 | 613 | OVS_CB(skb)->egress_tun_info = nla_data(nested_attr); |
4edb9ae9 PS |
614 | break; |
615 | ||
616 | case OVS_KEY_ATTR_ETHERNET: | |
617 | err = set_eth_addr(skb, nla_data(nested_attr)); | |
618 | break; | |
619 | ||
620 | case OVS_KEY_ATTR_IPV4: | |
621 | err = set_ipv4(skb, nla_data(nested_attr)); | |
622 | break; | |
623 | ||
bc7a5acd AA |
624 | case OVS_KEY_ATTR_IPV6: |
625 | err = set_ipv6(skb, nla_data(nested_attr)); | |
626 | break; | |
627 | ||
4edb9ae9 | 628 | case OVS_KEY_ATTR_TCP: |
55ce87bc | 629 | err = set_tcp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
630 | break; |
631 | ||
632 | case OVS_KEY_ATTR_UDP: | |
55ce87bc | 633 | err = set_udp(skb, nla_data(nested_attr)); |
4edb9ae9 | 634 | break; |
10f72e3d JS |
635 | |
636 | case OVS_KEY_ATTR_SCTP: | |
637 | err = set_sctp(skb, nla_data(nested_attr)); | |
638 | break; | |
ccf43786 SH |
639 | |
640 | case OVS_KEY_ATTR_MPLS: | |
641 | err = set_mpls(skb, nla_data(nested_attr)); | |
642 | break; | |
4edb9ae9 | 643 | } |
15c39847 | 644 | |
4edb9ae9 PS |
645 | return err; |
646 | } | |
647 | ||
a6059080 | 648 | static int execute_recirc(struct datapath *dp, struct sk_buff *skb, |
fb66fbd1 | 649 | const struct nlattr *a) |
a6059080 AZ |
650 | { |
651 | struct sw_flow_key recirc_key; | |
a6059080 AZ |
652 | int err; |
653 | ||
7f45215a PS |
654 | err = ovs_flow_key_extract_recirc(nla_get_u32(a), OVS_CB(skb)->pkt_key, |
655 | skb, &recirc_key); | |
97a360ce SH |
656 | if (err) { |
657 | kfree_skb(skb); | |
a6059080 | 658 | return err; |
97a360ce | 659 | } |
a6059080 | 660 | |
fb66fbd1 | 661 | ovs_dp_process_packet(skb, true); |
a6059080 AZ |
662 | |
663 | return 0; | |
664 | } | |
665 | ||
064af421 | 666 | /* Execute a list of actions against 'skb'. */ |
871dfe07 | 667 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
fbf4f74d | 668 | const struct nlattr *attr, int len) |
064af421 BP |
669 | { |
670 | /* Every output action needs a separate clone of 'skb', but the common | |
671 | * case is just a single output action, so that doing a clone and | |
672 | * then freeing the original skbuff is wasteful. So the following code | |
673 | * is slightly obscure just to avoid that. */ | |
674 | int prev_port = -1; | |
cdee00fd | 675 | const struct nlattr *a; |
10db8b20 | 676 | int rem; |
72b06300 | 677 | |
6ff686f2 | 678 | for (a = attr, rem = len; rem > 0; |
a4af2475 | 679 | a = nla_next(a, &rem)) { |
10db8b20 JG |
680 | int err = 0; |
681 | ||
fe90efd9 AZ |
682 | if (unlikely(prev_port != -1)) { |
683 | struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC); | |
684 | ||
685 | if (out_skb) | |
686 | do_output(dp, out_skb, prev_port); | |
687 | ||
064af421 BP |
688 | prev_port = -1; |
689 | } | |
690 | ||
cdee00fd | 691 | switch (nla_type(a)) { |
df2c07f4 | 692 | case OVS_ACTION_ATTR_OUTPUT: |
cdee00fd | 693 | prev_port = nla_get_u32(a); |
064af421 BP |
694 | break; |
695 | ||
df2c07f4 | 696 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 697 | output_userspace(dp, skb, a); |
064af421 | 698 | break; |
7804df20 AZ |
699 | |
700 | case OVS_ACTION_ATTR_HASH: | |
701 | execute_hash(skb, a); | |
702 | break; | |
064af421 | 703 | |
ccf43786 SH |
704 | case OVS_ACTION_ATTR_PUSH_MPLS: |
705 | err = push_mpls(skb, nla_data(a)); | |
706 | break; | |
707 | ||
708 | case OVS_ACTION_ATTR_POP_MPLS: | |
709 | err = pop_mpls(skb, nla_get_be16(a)); | |
710 | break; | |
711 | ||
fea393b1 BP |
712 | case OVS_ACTION_ATTR_PUSH_VLAN: |
713 | err = push_vlan(skb, nla_data(a)); | |
4edb9ae9 | 714 | if (unlikely(err)) /* skb already freed. */ |
d9065a90 | 715 | return err; |
064af421 BP |
716 | break; |
717 | ||
fea393b1 | 718 | case OVS_ACTION_ATTR_POP_VLAN: |
d9065a90 | 719 | err = pop_vlan(skb); |
064af421 BP |
720 | break; |
721 | ||
a6059080 AZ |
722 | case OVS_ACTION_ATTR_RECIRC: { |
723 | struct sk_buff *recirc_skb; | |
a6059080 | 724 | |
37dfe526 AZ |
725 | if (last_action(a, rem)) |
726 | return execute_recirc(dp, skb, a); | |
a6059080 | 727 | |
37dfe526 AZ |
728 | /* Recirc action is the not the last action |
729 | * of the action list. */ | |
730 | recirc_skb = skb_clone(skb, GFP_ATOMIC); | |
a6059080 | 731 | |
37dfe526 AZ |
732 | /* Skip the recirc action when out of memory, but |
733 | * continue on with the rest of the action list. */ | |
734 | if (recirc_skb) | |
735 | err = execute_recirc(dp, recirc_skb, a); | |
a6059080 AZ |
736 | |
737 | break; | |
738 | } | |
739 | ||
4edb9ae9 | 740 | case OVS_ACTION_ATTR_SET: |
85c9de19 | 741 | err = execute_set_action(skb, nla_data(a)); |
064af421 | 742 | break; |
c1c9c9c4 | 743 | |
6ff686f2 | 744 | case OVS_ACTION_ATTR_SAMPLE: |
85c9de19 | 745 | err = sample(dp, skb, a); |
6ff686f2 | 746 | break; |
6ff686f2 | 747 | } |
15c39847 | 748 | |
10db8b20 JG |
749 | if (unlikely(err)) { |
750 | kfree_skb(skb); | |
751 | return err; | |
752 | } | |
064af421 | 753 | } |
6c222e55 | 754 | |
fbf4f74d | 755 | if (prev_port != -1) |
064af421 | 756 | do_output(dp, skb, prev_port); |
fbf4f74d | 757 | else |
5b95ab0e | 758 | consume_skb(skb); |
10db8b20 | 759 | |
a5225dd6 | 760 | return 0; |
064af421 | 761 | } |
871dfe07 | 762 | |
e9141eec | 763 | /* We limit the number of times that we pass into execute_actions() |
ca93abce AZ |
764 | * to avoid blowing out the stack in the event that we have a loop. |
765 | * | |
766 | * Each loop adds some (estimated) cost to the kernel stack. | |
767 | * The loop terminates when the max cost is exceeded. | |
768 | * */ | |
769 | #define RECIRC_STACK_COST 1 | |
770 | #define DEFAULT_STACK_COST 4 | |
771 | /* Allow up to 4 regular services, and up to 3 recirculations */ | |
772 | #define MAX_STACK_COST (DEFAULT_STACK_COST * 4 + RECIRC_STACK_COST * 3) | |
e9141eec PS |
773 | |
774 | struct loop_counter { | |
ca93abce | 775 | u8 stack_cost; /* loop stack cost. */ |
e9141eec PS |
776 | bool looping; /* Loop detected? */ |
777 | }; | |
778 | ||
779 | static DEFINE_PER_CPU(struct loop_counter, loop_counters); | |
780 | ||
781 | static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions) | |
782 | { | |
783 | if (net_ratelimit()) | |
ca93abce AZ |
784 | pr_warn("%s: flow loop detected, dropping\n", |
785 | ovs_dp_name(dp)); | |
e9141eec PS |
786 | actions->actions_len = 0; |
787 | return -ELOOP; | |
788 | } | |
789 | ||
871dfe07 | 790 | /* Execute a list of actions against 'skb'. */ |
ca93abce | 791 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, bool recirc) |
871dfe07 | 792 | { |
a4af2475 | 793 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
ca93abce | 794 | const u8 stack_cost = recirc ? RECIRC_STACK_COST : DEFAULT_STACK_COST; |
a4af2475 BP |
795 | struct loop_counter *loop; |
796 | int error; | |
797 | ||
798 | /* Check whether we've looped too much. */ | |
e9141eec | 799 | loop = &__get_cpu_var(loop_counters); |
ca93abce AZ |
800 | loop->stack_cost += stack_cost; |
801 | if (unlikely(loop->stack_cost > MAX_STACK_COST)) | |
a4af2475 BP |
802 | loop->looping = true; |
803 | if (unlikely(loop->looping)) { | |
804 | error = loop_suppress(dp, acts); | |
805 | kfree_skb(skb); | |
806 | goto out_loop; | |
807 | } | |
871dfe07 | 808 | |
fbf4f74d | 809 | error = do_execute_actions(dp, skb, acts->actions, acts->actions_len); |
a4af2475 BP |
810 | |
811 | /* Check whether sub-actions looped too much. */ | |
812 | if (unlikely(loop->looping)) | |
813 | error = loop_suppress(dp, acts); | |
814 | ||
815 | out_loop: | |
ca93abce AZ |
816 | /* Decrement loop stack cost. */ |
817 | loop->stack_cost -= stack_cost; | |
818 | if (!loop->stack_cost) | |
a4af2475 | 819 | loop->looping = false; |
871dfe07 | 820 | |
a4af2475 | 821 | return error; |
871dfe07 | 822 | } |