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