]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/actions.c
datapath: add ipv6 'set' action
[mirror_ovs.git] / datapath / actions.c
1 /*
2 * Copyright (c) 2007-2012 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/in6.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
30 #include <net/ip.h>
31 #include <net/ipv6.h>
32 #include <net/checksum.h>
33 #include <net/dsfield.h>
34
35 #include "checksum.h"
36 #include "datapath.h"
37 #include "vlan.h"
38 #include "vport.h"
39
40 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
41 const struct nlattr *attr, int len,
42 struct ovs_key_ipv4_tunnel *tun_key, bool keep_skb);
43
44 static int make_writable(struct sk_buff *skb, int write_len)
45 {
46 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
47 return 0;
48
49 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
50 }
51
52 /* remove VLAN header from packet and update csum accordingly. */
53 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
54 {
55 struct vlan_hdr *vhdr;
56 int err;
57
58 err = make_writable(skb, VLAN_ETH_HLEN);
59 if (unlikely(err))
60 return err;
61
62 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
63 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
64 + ETH_HLEN, VLAN_HLEN, 0));
65
66 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
67 *current_tci = vhdr->h_vlan_TCI;
68
69 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
70 __skb_pull(skb, VLAN_HLEN);
71
72 vlan_set_encap_proto(skb, vhdr);
73 skb->mac_header += VLAN_HLEN;
74 skb_reset_mac_len(skb);
75
76 return 0;
77 }
78
79 static int pop_vlan(struct sk_buff *skb)
80 {
81 __be16 tci;
82 int err;
83
84 if (likely(vlan_tx_tag_present(skb))) {
85 vlan_set_tci(skb, 0);
86 } else {
87 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
88 skb->len < VLAN_ETH_HLEN))
89 return 0;
90
91 err = __pop_vlan_tci(skb, &tci);
92 if (err)
93 return err;
94 }
95 /* move next vlan tag to hw accel tag */
96 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
97 skb->len < VLAN_ETH_HLEN))
98 return 0;
99
100 err = __pop_vlan_tci(skb, &tci);
101 if (unlikely(err))
102 return err;
103
104 __vlan_hwaccel_put_tag(skb, ntohs(tci));
105 return 0;
106 }
107
108 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
109 {
110 if (unlikely(vlan_tx_tag_present(skb))) {
111 u16 current_tag;
112
113 /* push down current VLAN tag */
114 current_tag = vlan_tx_tag_get(skb);
115
116 if (!__vlan_put_tag(skb, current_tag))
117 return -ENOMEM;
118
119 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
120 skb->csum = csum_add(skb->csum, csum_partial(skb->data
121 + ETH_HLEN, VLAN_HLEN, 0));
122
123 }
124 __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
125 return 0;
126 }
127
128 static int set_eth_addr(struct sk_buff *skb,
129 const struct ovs_key_ethernet *eth_key)
130 {
131 int err;
132 err = make_writable(skb, ETH_HLEN);
133 if (unlikely(err))
134 return err;
135
136 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
137 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
138
139 return 0;
140 }
141
142 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
143 __be32 *addr, __be32 new_addr)
144 {
145 int transport_len = skb->len - skb_transport_offset(skb);
146
147 if (nh->protocol == IPPROTO_TCP) {
148 if (likely(transport_len >= sizeof(struct tcphdr)))
149 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
150 *addr, new_addr, 1);
151 } else if (nh->protocol == IPPROTO_UDP) {
152 if (likely(transport_len >= sizeof(struct udphdr))) {
153 struct udphdr *uh = udp_hdr(skb);
154
155 if (uh->check ||
156 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
157 inet_proto_csum_replace4(&uh->check, skb,
158 *addr, new_addr, 1);
159 if (!uh->check)
160 uh->check = CSUM_MANGLED_0;
161 }
162 }
163 }
164
165 csum_replace4(&nh->check, *addr, new_addr);
166 skb_clear_rxhash(skb);
167 *addr = new_addr;
168 }
169
170 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
171 __be32 addr[4], const __be32 new_addr[4])
172 {
173 int transport_len = skb->len - skb_transport_offset(skb);
174
175 if (l4_proto == IPPROTO_TCP) {
176 if (likely(transport_len >= sizeof(struct tcphdr)))
177 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
178 addr, new_addr, 1);
179 } else if (l4_proto == IPPROTO_UDP) {
180 if (likely(transport_len >= sizeof(struct udphdr))) {
181 struct udphdr *uh = udp_hdr(skb);
182
183 if (uh->check ||
184 get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
185 inet_proto_csum_replace16(&uh->check, skb,
186 addr, new_addr, 1);
187 if (!uh->check)
188 uh->check = CSUM_MANGLED_0;
189 }
190 }
191 }
192 }
193
194 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
195 __be32 addr[4], const __be32 new_addr[4],
196 bool recalculate_csum)
197 {
198 if (recalculate_csum)
199 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
200
201 skb_clear_rxhash(skb);
202 memcpy(addr, new_addr, sizeof(__be32[4]));
203 }
204
205 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
206 {
207 nh->priority = tc >> 4;
208 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
209 }
210
211 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
212 {
213 nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
214 nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
215 nh->flow_lbl[2] = fl & 0x000000FF;
216 }
217
218 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
219 {
220 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
221 nh->ttl = new_ttl;
222 }
223
224 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
225 {
226 struct iphdr *nh;
227 int err;
228
229 err = make_writable(skb, skb_network_offset(skb) +
230 sizeof(struct iphdr));
231 if (unlikely(err))
232 return err;
233
234 nh = ip_hdr(skb);
235
236 if (ipv4_key->ipv4_src != nh->saddr)
237 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
238
239 if (ipv4_key->ipv4_dst != nh->daddr)
240 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
241
242 if (ipv4_key->ipv4_tos != nh->tos)
243 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
244
245 if (ipv4_key->ipv4_ttl != nh->ttl)
246 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
247
248 return 0;
249 }
250
251 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
252 {
253 struct ipv6hdr *nh;
254 int err;
255 __be32 *saddr;
256 __be32 *daddr;
257
258 err = make_writable(skb, skb_network_offset(skb) +
259 sizeof(struct ipv6hdr));
260 if (unlikely(err))
261 return err;
262
263 nh = ipv6_hdr(skb);
264 saddr = (__be32 *)&nh->saddr;
265 daddr = (__be32 *)&nh->daddr;
266
267 if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
268 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
269 ipv6_key->ipv6_src, true);
270
271 if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
272 unsigned int offset = 0;
273 int flags = OVS_IP6T_FH_F_SKIP_RH;
274 bool recalc_csum = true;
275
276 if (ipv6_ext_hdr(nh->nexthdr))
277 recalc_csum = ipv6_find_hdr(skb, &offset,
278 NEXTHDR_ROUTING, NULL,
279 &flags) != NEXTHDR_ROUTING;
280
281 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
282 ipv6_key->ipv6_dst, recalc_csum);
283 }
284
285 set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
286 set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
287 nh->hop_limit = ipv6_key->ipv6_hlimit;
288
289 return 0;
290 }
291
292 /* Must follow make_writable() since that can move the skb data. */
293 static void set_tp_port(struct sk_buff *skb, __be16 *port,
294 __be16 new_port, __sum16 *check)
295 {
296 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
297 *port = new_port;
298 skb_clear_rxhash(skb);
299 }
300
301 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
302 {
303 struct udphdr *uh = udp_hdr(skb);
304
305 if (uh->check && get_ip_summed(skb) != OVS_CSUM_PARTIAL) {
306 set_tp_port(skb, port, new_port, &uh->check);
307
308 if (!uh->check)
309 uh->check = CSUM_MANGLED_0;
310 } else {
311 *port = new_port;
312 skb_clear_rxhash(skb);
313 }
314 }
315
316 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
317 {
318 struct udphdr *uh;
319 int err;
320
321 err = make_writable(skb, skb_transport_offset(skb) +
322 sizeof(struct udphdr));
323 if (unlikely(err))
324 return err;
325
326 uh = udp_hdr(skb);
327 if (udp_port_key->udp_src != uh->source)
328 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
329
330 if (udp_port_key->udp_dst != uh->dest)
331 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
332
333 return 0;
334 }
335
336 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
337 {
338 struct tcphdr *th;
339 int err;
340
341 err = make_writable(skb, skb_transport_offset(skb) +
342 sizeof(struct tcphdr));
343 if (unlikely(err))
344 return err;
345
346 th = tcp_hdr(skb);
347 if (tcp_port_key->tcp_src != th->source)
348 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
349
350 if (tcp_port_key->tcp_dst != th->dest)
351 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
352
353 return 0;
354 }
355
356 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
357 {
358 struct vport *vport;
359
360 if (unlikely(!skb))
361 return -ENOMEM;
362
363 vport = ovs_vport_rcu(dp, out_port);
364 if (unlikely(!vport)) {
365 kfree_skb(skb);
366 return -ENODEV;
367 }
368
369 ovs_vport_send(vport, skb);
370 return 0;
371 }
372
373 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
374 const struct nlattr *attr)
375 {
376 struct dp_upcall_info upcall;
377 const struct nlattr *a;
378 int rem;
379
380 upcall.cmd = OVS_PACKET_CMD_ACTION;
381 upcall.key = &OVS_CB(skb)->flow->key;
382 upcall.userdata = NULL;
383 upcall.pid = 0;
384
385 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
386 a = nla_next(a, &rem)) {
387 switch (nla_type(a)) {
388 case OVS_USERSPACE_ATTR_USERDATA:
389 upcall.userdata = a;
390 break;
391
392 case OVS_USERSPACE_ATTR_PID:
393 upcall.pid = nla_get_u32(a);
394 break;
395 }
396 }
397
398 return ovs_dp_upcall(dp, skb, &upcall);
399 }
400
401 static int sample(struct datapath *dp, struct sk_buff *skb,
402 const struct nlattr *attr,
403 struct ovs_key_ipv4_tunnel *tun_key)
404 {
405 const struct nlattr *acts_list = NULL;
406 const struct nlattr *a;
407 int rem;
408
409 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
410 a = nla_next(a, &rem)) {
411 switch (nla_type(a)) {
412 case OVS_SAMPLE_ATTR_PROBABILITY:
413 if (net_random() >= nla_get_u32(a))
414 return 0;
415 break;
416
417 case OVS_SAMPLE_ATTR_ACTIONS:
418 acts_list = a;
419 break;
420 }
421 }
422
423 return do_execute_actions(dp, skb, nla_data(acts_list),
424 nla_len(acts_list), tun_key, true);
425 }
426
427 static int execute_set_action(struct sk_buff *skb,
428 const struct nlattr *nested_attr,
429 struct ovs_key_ipv4_tunnel *tun_key)
430 {
431 int err = 0;
432
433 switch (nla_type(nested_attr)) {
434 case OVS_KEY_ATTR_PRIORITY:
435 skb->priority = nla_get_u32(nested_attr);
436 break;
437
438 case OVS_KEY_ATTR_TUN_ID:
439 /* If we're only using the TUN_ID action, store the value in a
440 * temporary instance of struct ovs_key_ipv4_tunnel on the stack.
441 * If both IPV4_TUNNEL and TUN_ID are being used together we
442 * can't write into the IPV4_TUNNEL action, so make a copy and
443 * write into that version.
444 */
445 if (!OVS_CB(skb)->tun_key)
446 memset(tun_key, 0, sizeof(*tun_key));
447 else if (OVS_CB(skb)->tun_key != tun_key)
448 memcpy(tun_key, OVS_CB(skb)->tun_key, sizeof(*tun_key));
449 OVS_CB(skb)->tun_key = tun_key;
450
451 OVS_CB(skb)->tun_key->tun_id = nla_get_be64(nested_attr);
452 break;
453
454 case OVS_KEY_ATTR_IPV4_TUNNEL:
455 OVS_CB(skb)->tun_key = nla_data(nested_attr);
456 break;
457
458 case OVS_KEY_ATTR_ETHERNET:
459 err = set_eth_addr(skb, nla_data(nested_attr));
460 break;
461
462 case OVS_KEY_ATTR_IPV4:
463 err = set_ipv4(skb, nla_data(nested_attr));
464 break;
465
466 case OVS_KEY_ATTR_IPV6:
467 err = set_ipv6(skb, nla_data(nested_attr));
468 break;
469
470 case OVS_KEY_ATTR_TCP:
471 err = set_tcp(skb, nla_data(nested_attr));
472 break;
473
474 case OVS_KEY_ATTR_UDP:
475 err = set_udp(skb, nla_data(nested_attr));
476 break;
477 }
478
479 return err;
480 }
481
482 /* Execute a list of actions against 'skb'. */
483 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
484 const struct nlattr *attr, int len,
485 struct ovs_key_ipv4_tunnel *tun_key, bool keep_skb)
486 {
487 /* Every output action needs a separate clone of 'skb', but the common
488 * case is just a single output action, so that doing a clone and
489 * then freeing the original skbuff is wasteful. So the following code
490 * is slightly obscure just to avoid that. */
491 int prev_port = -1;
492 const struct nlattr *a;
493 int rem;
494
495 for (a = attr, rem = len; rem > 0;
496 a = nla_next(a, &rem)) {
497 int err = 0;
498
499 if (prev_port != -1) {
500 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
501 prev_port = -1;
502 }
503
504 switch (nla_type(a)) {
505 case OVS_ACTION_ATTR_OUTPUT:
506 prev_port = nla_get_u32(a);
507 break;
508
509 case OVS_ACTION_ATTR_USERSPACE:
510 output_userspace(dp, skb, a);
511 break;
512
513 case OVS_ACTION_ATTR_PUSH_VLAN:
514 err = push_vlan(skb, nla_data(a));
515 if (unlikely(err)) /* skb already freed. */
516 return err;
517 break;
518
519 case OVS_ACTION_ATTR_POP_VLAN:
520 err = pop_vlan(skb);
521 break;
522
523 case OVS_ACTION_ATTR_SET:
524 err = execute_set_action(skb, nla_data(a), tun_key);
525 break;
526
527 case OVS_ACTION_ATTR_SAMPLE:
528 err = sample(dp, skb, a, tun_key);
529 break;
530 }
531
532 if (unlikely(err)) {
533 kfree_skb(skb);
534 return err;
535 }
536 }
537
538 if (prev_port != -1) {
539 if (keep_skb)
540 skb = skb_clone(skb, GFP_ATOMIC);
541
542 do_output(dp, skb, prev_port);
543 } else if (!keep_skb)
544 consume_skb(skb);
545
546 return 0;
547 }
548
549 /* We limit the number of times that we pass into execute_actions()
550 * to avoid blowing out the stack in the event that we have a loop. */
551 #define MAX_LOOPS 5
552
553 struct loop_counter {
554 u8 count; /* Count. */
555 bool looping; /* Loop detected? */
556 };
557
558 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
559
560 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
561 {
562 if (net_ratelimit())
563 pr_warn("%s: flow looped %d times, dropping\n",
564 ovs_dp_name(dp), MAX_LOOPS);
565 actions->actions_len = 0;
566 return -ELOOP;
567 }
568
569 /* Execute a list of actions against 'skb'. */
570 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
571 {
572 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
573 struct loop_counter *loop;
574 int error;
575 struct ovs_key_ipv4_tunnel tun_key;
576
577 /* Check whether we've looped too much. */
578 loop = &__get_cpu_var(loop_counters);
579 if (unlikely(++loop->count > MAX_LOOPS))
580 loop->looping = true;
581 if (unlikely(loop->looping)) {
582 error = loop_suppress(dp, acts);
583 kfree_skb(skb);
584 goto out_loop;
585 }
586
587 OVS_CB(skb)->tun_key = NULL;
588 error = do_execute_actions(dp, skb, acts->actions,
589 acts->actions_len, &tun_key, false);
590
591 /* Check whether sub-actions looped too much. */
592 if (unlikely(loop->looping))
593 error = loop_suppress(dp, acts);
594
595 out_loop:
596 /* Decrement loop counter. */
597 if (!--loop->count)
598 loop->looping = false;
599
600 return error;
601 }