]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2007-2012 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> |
064af421 BP |
25 | #include <linux/tcp.h> |
26 | #include <linux/udp.h> | |
27 | #include <linux/in6.h> | |
401eeb92 | 28 | #include <linux/if_arp.h> |
064af421 BP |
29 | #include <linux/if_vlan.h> |
30 | #include <net/ip.h> | |
31 | #include <net/checksum.h> | |
530180fd | 32 | #include <net/dsfield.h> |
f2459fe7 | 33 | |
dd8d6b8c | 34 | #include "checksum.h" |
f2459fe7 | 35 | #include "datapath.h" |
6ce39213 | 36 | #include "vlan.h" |
f2459fe7 | 37 | #include "vport.h" |
064af421 | 38 | |
6ff686f2 PS |
39 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
40 | const struct nlattr *attr, int len, bool keep_skb); | |
871dfe07 | 41 | |
10db8b20 | 42 | static int make_writable(struct sk_buff *skb, int write_len) |
064af421 | 43 | { |
10db8b20 JG |
44 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
45 | return 0; | |
0cd8a05e | 46 | |
10db8b20 | 47 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
064af421 BP |
48 | } |
49 | ||
d9065a90 PS |
50 | /* remove VLAN header from packet and update csum accrodingly. */ |
51 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) | |
064af421 | 52 | { |
ceb176fd | 53 | struct vlan_hdr *vhdr; |
10db8b20 | 54 | int err; |
064af421 | 55 | |
10db8b20 JG |
56 | err = make_writable(skb, VLAN_ETH_HLEN); |
57 | if (unlikely(err)) | |
58 | return err; | |
6ce39213 | 59 | |
dd8d6b8c | 60 | if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) |
635c9298 JG |
61 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
62 | + ETH_HLEN, VLAN_HLEN, 0)); | |
63 | ||
ceb176fd PS |
64 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
65 | *current_tci = vhdr->h_vlan_TCI; | |
d9065a90 | 66 | |
6ce39213 | 67 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
ceb176fd | 68 | __skb_pull(skb, VLAN_HLEN); |
064af421 | 69 | |
ceb176fd | 70 | vlan_set_encap_proto(skb, vhdr); |
064af421 | 71 | skb->mac_header += VLAN_HLEN; |
ceb176fd | 72 | skb_reset_mac_len(skb); |
064af421 | 73 | |
10db8b20 | 74 | return 0; |
064af421 BP |
75 | } |
76 | ||
d9065a90 | 77 | static int pop_vlan(struct sk_buff *skb) |
064af421 | 78 | { |
d9065a90 PS |
79 | __be16 tci; |
80 | int err; | |
10db8b20 | 81 | |
d9065a90 PS |
82 | if (likely(vlan_tx_tag_present(skb))) { |
83 | vlan_set_tci(skb, 0); | |
84 | } else { | |
85 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 86 | skb->len < VLAN_ETH_HLEN)) |
10db8b20 | 87 | return 0; |
064af421 | 88 | |
d9065a90 PS |
89 | err = __pop_vlan_tci(skb, &tci); |
90 | if (err) | |
10db8b20 | 91 | return err; |
064af421 | 92 | } |
d9065a90 PS |
93 | /* move next vlan tag to hw accel tag */ |
94 | if (likely(skb->protocol != htons(ETH_P_8021Q) || | |
fb516ed8 | 95 | skb->len < VLAN_ETH_HLEN)) |
d9065a90 PS |
96 | return 0; |
97 | ||
98 | err = __pop_vlan_tci(skb, &tci); | |
99 | if (unlikely(err)) | |
100 | return err; | |
064af421 | 101 | |
10db8b20 | 102 | __vlan_hwaccel_put_tag(skb, ntohs(tci)); |
d9065a90 PS |
103 | return 0; |
104 | } | |
105 | ||
fea393b1 | 106 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
d9065a90 PS |
107 | { |
108 | if (unlikely(vlan_tx_tag_present(skb))) { | |
109 | u16 current_tag; | |
110 | ||
111 | /* push down current VLAN tag */ | |
112 | current_tag = vlan_tx_tag_get(skb); | |
113 | ||
114 | if (!__vlan_put_tag(skb, current_tag)) | |
115 | return -ENOMEM; | |
10db8b20 | 116 | |
d9065a90 PS |
117 | if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) |
118 | skb->csum = csum_add(skb->csum, csum_partial(skb->data | |
119 | + ETH_HLEN, VLAN_HLEN, 0)); | |
120 | ||
121 | } | |
8ddc056d | 122 | __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
10db8b20 | 123 | return 0; |
064af421 BP |
124 | } |
125 | ||
4edb9ae9 PS |
126 | static int set_eth_addr(struct sk_buff *skb, |
127 | const struct ovs_key_ethernet *eth_key) | |
ca78c6b6 | 128 | { |
4edb9ae9 PS |
129 | int err; |
130 | err = make_writable(skb, ETH_HLEN); | |
131 | if (unlikely(err)) | |
132 | return err; | |
133 | ||
39640c1b PS |
134 | memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN); |
135 | memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN); | |
4edb9ae9 PS |
136 | |
137 | return 0; | |
ca78c6b6 BP |
138 | } |
139 | ||
4edb9ae9 PS |
140 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
141 | __be32 *addr, __be32 new_addr) | |
ca78c6b6 BP |
142 | { |
143 | int transport_len = skb->len - skb_transport_offset(skb); | |
4edb9ae9 PS |
144 | |
145 | if (nh->protocol == IPPROTO_TCP) { | |
ca78c6b6 | 146 | if (likely(transport_len >= sizeof(struct tcphdr))) |
4edb9ae9 PS |
147 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
148 | *addr, new_addr, 1); | |
149 | } else if (nh->protocol == IPPROTO_UDP) { | |
55ce87bc JG |
150 | if (likely(transport_len >= sizeof(struct udphdr))) { |
151 | struct udphdr *uh = udp_hdr(skb); | |
152 | ||
153 | if (uh->check || | |
154 | get_ip_summed(skb) == OVS_CSUM_PARTIAL) { | |
155 | inet_proto_csum_replace4(&uh->check, skb, | |
156 | *addr, new_addr, 1); | |
157 | if (!uh->check) | |
158 | uh->check = CSUM_MANGLED_0; | |
159 | } | |
160 | } | |
ca78c6b6 | 161 | } |
4edb9ae9 PS |
162 | |
163 | csum_replace4(&nh->check, *addr, new_addr); | |
164 | skb_clear_rxhash(skb); | |
165 | *addr = new_addr; | |
ca78c6b6 BP |
166 | } |
167 | ||
a61680c6 JP |
168 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
169 | { | |
170 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); | |
171 | nh->ttl = new_ttl; | |
172 | } | |
173 | ||
4edb9ae9 | 174 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
064af421 | 175 | { |
ca78c6b6 | 176 | struct iphdr *nh; |
10db8b20 | 177 | int err; |
ca78c6b6 | 178 | |
10db8b20 JG |
179 | err = make_writable(skb, skb_network_offset(skb) + |
180 | sizeof(struct iphdr)); | |
181 | if (unlikely(err)) | |
182 | return err; | |
ca78c6b6 BP |
183 | |
184 | nh = ip_hdr(skb); | |
ca78c6b6 | 185 | |
4edb9ae9 PS |
186 | if (ipv4_key->ipv4_src != nh->saddr) |
187 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); | |
ca78c6b6 | 188 | |
4edb9ae9 PS |
189 | if (ipv4_key->ipv4_dst != nh->daddr) |
190 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); | |
a4a26436 | 191 | |
4edb9ae9 | 192 | if (ipv4_key->ipv4_tos != nh->tos) |
530180fd | 193 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
ca78c6b6 | 194 | |
a61680c6 JP |
195 | if (ipv4_key->ipv4_ttl != nh->ttl) |
196 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); | |
197 | ||
10db8b20 | 198 | return 0; |
064af421 BP |
199 | } |
200 | ||
4edb9ae9 PS |
201 | /* Must follow make_writable() since that can move the skb data. */ |
202 | static void set_tp_port(struct sk_buff *skb, __be16 *port, | |
203 | __be16 new_port, __sum16 *check) | |
959a2ecd | 204 | { |
4edb9ae9 PS |
205 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
206 | *port = new_port; | |
207 | skb_clear_rxhash(skb); | |
208 | } | |
10db8b20 | 209 | |
55ce87bc JG |
210 | static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port) |
211 | { | |
212 | struct udphdr *uh = udp_hdr(skb); | |
213 | ||
214 | if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) { | |
215 | set_tp_port(skb, port, new_port, &uh->check); | |
216 | ||
217 | if (!uh->check) | |
218 | uh->check = CSUM_MANGLED_0; | |
219 | } else { | |
220 | *port = new_port; | |
221 | skb_clear_rxhash(skb); | |
222 | } | |
223 | } | |
224 | ||
225 | static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key) | |
4edb9ae9 PS |
226 | { |
227 | struct udphdr *uh; | |
228 | int err; | |
10db8b20 | 229 | |
4edb9ae9 PS |
230 | err = make_writable(skb, skb_transport_offset(skb) + |
231 | sizeof(struct udphdr)); | |
10db8b20 JG |
232 | if (unlikely(err)) |
233 | return err; | |
234 | ||
4edb9ae9 PS |
235 | uh = udp_hdr(skb); |
236 | if (udp_port_key->udp_src != uh->source) | |
55ce87bc | 237 | set_udp_port(skb, &uh->source, udp_port_key->udp_src); |
4edb9ae9 PS |
238 | |
239 | if (udp_port_key->udp_dst != uh->dest) | |
55ce87bc | 240 | set_udp_port(skb, &uh->dest, udp_port_key->udp_dst); |
10db8b20 JG |
241 | |
242 | return 0; | |
959a2ecd JP |
243 | } |
244 | ||
55ce87bc | 245 | static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key) |
064af421 | 246 | { |
4edb9ae9 | 247 | struct tcphdr *th; |
10db8b20 | 248 | int err; |
064af421 | 249 | |
10db8b20 JG |
250 | err = make_writable(skb, skb_transport_offset(skb) + |
251 | sizeof(struct tcphdr)); | |
252 | if (unlikely(err)) | |
253 | return err; | |
ca78c6b6 | 254 | |
4edb9ae9 PS |
255 | th = tcp_hdr(skb); |
256 | if (tcp_port_key->tcp_src != th->source) | |
257 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); | |
064af421 | 258 | |
4edb9ae9 PS |
259 | if (tcp_port_key->tcp_dst != th->dest) |
260 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); | |
ca78c6b6 | 261 | |
10db8b20 | 262 | return 0; |
064af421 BP |
263 | } |
264 | ||
f15c8639 | 265 | static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
064af421 | 266 | { |
f15c8639 | 267 | struct vport *vport; |
064af421 | 268 | |
f15c8639 JG |
269 | if (unlikely(!skb)) |
270 | return -ENOMEM; | |
064af421 | 271 | |
95b1d73a | 272 | vport = ovs_vport_rcu(dp, out_port); |
f15c8639 JG |
273 | if (unlikely(!vport)) { |
274 | kfree_skb(skb); | |
275 | return -ENODEV; | |
276 | } | |
064af421 | 277 | |
850b6b3b | 278 | ovs_vport_send(vport, skb); |
f15c8639 | 279 | return 0; |
064af421 BP |
280 | } |
281 | ||
98403001 BP |
282 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
283 | const struct nlattr *attr) | |
064af421 | 284 | { |
856081f6 | 285 | struct dp_upcall_info upcall; |
98403001 BP |
286 | const struct nlattr *a; |
287 | int rem; | |
856081f6 | 288 | |
df2c07f4 | 289 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
a4af2475 | 290 | upcall.key = &OVS_CB(skb)->flow->key; |
98403001 BP |
291 | upcall.userdata = NULL; |
292 | upcall.pid = 0; | |
293 | ||
294 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
295 | a = nla_next(a, &rem)) { | |
296 | switch (nla_type(a)) { | |
297 | case OVS_USERSPACE_ATTR_USERDATA: | |
298 | upcall.userdata = a; | |
299 | break; | |
300 | ||
301 | case OVS_USERSPACE_ATTR_PID: | |
302 | upcall.pid = nla_get_u32(a); | |
303 | break; | |
304 | } | |
305 | } | |
306 | ||
850b6b3b | 307 | return ovs_dp_upcall(dp, skb, &upcall); |
064af421 BP |
308 | } |
309 | ||
6ff686f2 PS |
310 | static int sample(struct datapath *dp, struct sk_buff *skb, |
311 | const struct nlattr *attr) | |
312 | { | |
313 | const struct nlattr *acts_list = NULL; | |
314 | const struct nlattr *a; | |
315 | int rem; | |
316 | ||
317 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; | |
318 | a = nla_next(a, &rem)) { | |
319 | switch (nla_type(a)) { | |
320 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
321 | if (net_random() >= nla_get_u32(a)) | |
322 | return 0; | |
323 | break; | |
324 | ||
325 | case OVS_SAMPLE_ATTR_ACTIONS: | |
326 | acts_list = a; | |
327 | break; | |
328 | } | |
329 | } | |
330 | ||
331 | return do_execute_actions(dp, skb, nla_data(acts_list), | |
332 | nla_len(acts_list), true); | |
333 | } | |
334 | ||
4edb9ae9 PS |
335 | static int execute_set_action(struct sk_buff *skb, |
336 | const struct nlattr *nested_attr) | |
337 | { | |
15c39847 | 338 | int err = 0; |
4edb9ae9 PS |
339 | |
340 | switch (nla_type(nested_attr)) { | |
abff858b PS |
341 | case OVS_KEY_ATTR_PRIORITY: |
342 | skb->priority = nla_get_u32(nested_attr); | |
343 | break; | |
344 | ||
4edb9ae9 PS |
345 | case OVS_KEY_ATTR_TUN_ID: |
346 | OVS_CB(skb)->tun_id = nla_get_be64(nested_attr); | |
4edb9ae9 PS |
347 | break; |
348 | ||
349 | case OVS_KEY_ATTR_ETHERNET: | |
350 | err = set_eth_addr(skb, nla_data(nested_attr)); | |
351 | break; | |
352 | ||
353 | case OVS_KEY_ATTR_IPV4: | |
354 | err = set_ipv4(skb, nla_data(nested_attr)); | |
355 | break; | |
356 | ||
357 | case OVS_KEY_ATTR_TCP: | |
55ce87bc | 358 | err = set_tcp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
359 | break; |
360 | ||
361 | case OVS_KEY_ATTR_UDP: | |
55ce87bc | 362 | err = set_udp(skb, nla_data(nested_attr)); |
4edb9ae9 PS |
363 | break; |
364 | } | |
15c39847 | 365 | |
4edb9ae9 PS |
366 | return err; |
367 | } | |
368 | ||
064af421 | 369 | /* Execute a list of actions against 'skb'. */ |
871dfe07 | 370 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
6ff686f2 | 371 | const struct nlattr *attr, int len, bool keep_skb) |
064af421 BP |
372 | { |
373 | /* Every output action needs a separate clone of 'skb', but the common | |
374 | * case is just a single output action, so that doing a clone and | |
375 | * then freeing the original skbuff is wasteful. So the following code | |
376 | * is slightly obscure just to avoid that. */ | |
377 | int prev_port = -1; | |
cdee00fd | 378 | const struct nlattr *a; |
10db8b20 | 379 | int rem; |
72b06300 | 380 | |
6ff686f2 | 381 | for (a = attr, rem = len; rem > 0; |
a4af2475 | 382 | a = nla_next(a, &rem)) { |
10db8b20 JG |
383 | int err = 0; |
384 | ||
064af421 | 385 | if (prev_port != -1) { |
7956695a | 386 | do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port); |
064af421 BP |
387 | prev_port = -1; |
388 | } | |
389 | ||
cdee00fd | 390 | switch (nla_type(a)) { |
df2c07f4 | 391 | case OVS_ACTION_ATTR_OUTPUT: |
cdee00fd | 392 | prev_port = nla_get_u32(a); |
064af421 BP |
393 | break; |
394 | ||
df2c07f4 | 395 | case OVS_ACTION_ATTR_USERSPACE: |
98403001 | 396 | output_userspace(dp, skb, a); |
064af421 BP |
397 | break; |
398 | ||
fea393b1 BP |
399 | case OVS_ACTION_ATTR_PUSH_VLAN: |
400 | err = push_vlan(skb, nla_data(a)); | |
4edb9ae9 | 401 | if (unlikely(err)) /* skb already freed. */ |
d9065a90 | 402 | return err; |
064af421 BP |
403 | break; |
404 | ||
fea393b1 | 405 | case OVS_ACTION_ATTR_POP_VLAN: |
d9065a90 | 406 | err = pop_vlan(skb); |
064af421 BP |
407 | break; |
408 | ||
4edb9ae9 PS |
409 | case OVS_ACTION_ATTR_SET: |
410 | err = execute_set_action(skb, nla_data(a)); | |
064af421 | 411 | break; |
c1c9c9c4 | 412 | |
6ff686f2 PS |
413 | case OVS_ACTION_ATTR_SAMPLE: |
414 | err = sample(dp, skb, a); | |
415 | break; | |
6ff686f2 | 416 | } |
15c39847 | 417 | |
10db8b20 JG |
418 | if (unlikely(err)) { |
419 | kfree_skb(skb); | |
420 | return err; | |
421 | } | |
064af421 | 422 | } |
6c222e55 | 423 | |
6ff686f2 PS |
424 | if (prev_port != -1) { |
425 | if (keep_skb) | |
426 | skb = skb_clone(skb, GFP_ATOMIC); | |
427 | ||
064af421 | 428 | do_output(dp, skb, prev_port); |
6ff686f2 | 429 | } else if (!keep_skb) |
5b95ab0e | 430 | consume_skb(skb); |
10db8b20 | 431 | |
a5225dd6 | 432 | return 0; |
064af421 | 433 | } |
871dfe07 | 434 | |
e9141eec PS |
435 | /* We limit the number of times that we pass into execute_actions() |
436 | * to avoid blowing out the stack in the event that we have a loop. */ | |
437 | #define MAX_LOOPS 5 | |
438 | ||
439 | struct loop_counter { | |
440 | u8 count; /* Count. */ | |
441 | bool looping; /* Loop detected? */ | |
442 | }; | |
443 | ||
444 | static DEFINE_PER_CPU(struct loop_counter, loop_counters); | |
445 | ||
446 | static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions) | |
447 | { | |
448 | if (net_ratelimit()) | |
449 | pr_warn("%s: flow looped %d times, dropping\n", | |
850b6b3b | 450 | ovs_dp_name(dp), MAX_LOOPS); |
e9141eec PS |
451 | actions->actions_len = 0; |
452 | return -ELOOP; | |
453 | } | |
454 | ||
871dfe07 | 455 | /* Execute a list of actions against 'skb'. */ |
850b6b3b | 456 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb) |
871dfe07 | 457 | { |
a4af2475 BP |
458 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
459 | struct loop_counter *loop; | |
460 | int error; | |
461 | ||
462 | /* Check whether we've looped too much. */ | |
e9141eec | 463 | loop = &__get_cpu_var(loop_counters); |
a4af2475 BP |
464 | if (unlikely(++loop->count > MAX_LOOPS)) |
465 | loop->looping = true; | |
466 | if (unlikely(loop->looping)) { | |
467 | error = loop_suppress(dp, acts); | |
468 | kfree_skb(skb); | |
469 | goto out_loop; | |
470 | } | |
871dfe07 BP |
471 | |
472 | OVS_CB(skb)->tun_id = 0; | |
6ff686f2 PS |
473 | error = do_execute_actions(dp, skb, acts->actions, |
474 | acts->actions_len, false); | |
a4af2475 BP |
475 | |
476 | /* Check whether sub-actions looped too much. */ | |
477 | if (unlikely(loop->looping)) | |
478 | error = loop_suppress(dp, acts); | |
479 | ||
480 | out_loop: | |
481 | /* Decrement loop counter. */ | |
482 | if (!--loop->count) | |
483 | loop->looping = false; | |
871dfe07 | 484 | |
a4af2475 | 485 | return error; |
871dfe07 | 486 | } |