datapath: Don't write into IPV4_TUNNEL data when using TUN_ID.
[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 we're only using the TUN_ID action, store the value in a
350                  * temporary instance of struct ovs_key_ipv4_tunnel on the stack.
351                  * If both IPV4_TUNNEL and TUN_ID are being used together we
352                  * can't write into the IPV4_TUNNEL action, so make a copy and
353                  * write into that version.
354                  */
355                 if (!OVS_CB(skb)->tun_key)
356                         memset(tun_key, 0, sizeof(*tun_key));
357                 else if (OVS_CB(skb)->tun_key != tun_key)
358                         memcpy(tun_key, OVS_CB(skb)->tun_key, sizeof(*tun_key));
359                 OVS_CB(skb)->tun_key = tun_key;
360
361                 OVS_CB(skb)->tun_key->tun_id = nla_get_be64(nested_attr);
362                 break;
363
364         case OVS_KEY_ATTR_IPV4_TUNNEL:
365                 OVS_CB(skb)->tun_key = nla_data(nested_attr);
366                 break;
367
368         case OVS_KEY_ATTR_ETHERNET:
369                 err = set_eth_addr(skb, nla_data(nested_attr));
370                 break;
371
372         case OVS_KEY_ATTR_IPV4:
373                 err = set_ipv4(skb, nla_data(nested_attr));
374                 break;
375
376         case OVS_KEY_ATTR_TCP:
377                 err = set_tcp(skb, nla_data(nested_attr));
378                 break;
379
380         case OVS_KEY_ATTR_UDP:
381                 err = set_udp(skb, nla_data(nested_attr));
382                 break;
383         }
384
385         return err;
386 }
387
388 /* Execute a list of actions against 'skb'. */
389 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
390                         const struct nlattr *attr, int len,
391                         struct ovs_key_ipv4_tunnel *tun_key, bool keep_skb)
392 {
393         /* Every output action needs a separate clone of 'skb', but the common
394          * case is just a single output action, so that doing a clone and
395          * then freeing the original skbuff is wasteful.  So the following code
396          * is slightly obscure just to avoid that. */
397         int prev_port = -1;
398         const struct nlattr *a;
399         int rem;
400
401         for (a = attr, rem = len; rem > 0;
402              a = nla_next(a, &rem)) {
403                 int err = 0;
404
405                 if (prev_port != -1) {
406                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
407                         prev_port = -1;
408                 }
409
410                 switch (nla_type(a)) {
411                 case OVS_ACTION_ATTR_OUTPUT:
412                         prev_port = nla_get_u32(a);
413                         break;
414
415                 case OVS_ACTION_ATTR_USERSPACE:
416                         output_userspace(dp, skb, a);
417                         break;
418
419                 case OVS_ACTION_ATTR_PUSH_VLAN:
420                         err = push_vlan(skb, nla_data(a));
421                         if (unlikely(err)) /* skb already freed. */
422                                 return err;
423                         break;
424
425                 case OVS_ACTION_ATTR_POP_VLAN:
426                         err = pop_vlan(skb);
427                         break;
428
429                 case OVS_ACTION_ATTR_SET:
430                         err = execute_set_action(skb, nla_data(a), tun_key);
431                         break;
432
433                 case OVS_ACTION_ATTR_SAMPLE:
434                         err = sample(dp, skb, a, tun_key);
435                         break;
436                 }
437
438                 if (unlikely(err)) {
439                         kfree_skb(skb);
440                         return err;
441                 }
442         }
443
444         if (prev_port != -1) {
445                 if (keep_skb)
446                         skb = skb_clone(skb, GFP_ATOMIC);
447
448                 do_output(dp, skb, prev_port);
449         } else if (!keep_skb)
450                 consume_skb(skb);
451
452         return 0;
453 }
454
455 /* We limit the number of times that we pass into execute_actions()
456  * to avoid blowing out the stack in the event that we have a loop. */
457 #define MAX_LOOPS 5
458
459 struct loop_counter {
460         u8 count;               /* Count. */
461         bool looping;           /* Loop detected? */
462 };
463
464 static DEFINE_PER_CPU(struct loop_counter, loop_counters);
465
466 static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
467 {
468         if (net_ratelimit())
469                 pr_warn("%s: flow looped %d times, dropping\n",
470                                 ovs_dp_name(dp), MAX_LOOPS);
471         actions->actions_len = 0;
472         return -ELOOP;
473 }
474
475 /* Execute a list of actions against 'skb'. */
476 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
477 {
478         struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
479         struct loop_counter *loop;
480         int error;
481         struct ovs_key_ipv4_tunnel tun_key;
482
483         /* Check whether we've looped too much. */
484         loop = &__get_cpu_var(loop_counters);
485         if (unlikely(++loop->count > MAX_LOOPS))
486                 loop->looping = true;
487         if (unlikely(loop->looping)) {
488                 error = loop_suppress(dp, acts);
489                 kfree_skb(skb);
490                 goto out_loop;
491         }
492
493         OVS_CB(skb)->tun_key = NULL;
494         error = do_execute_actions(dp, skb, acts->actions,
495                                          acts->actions_len, &tun_key, false);
496
497         /* Check whether sub-actions looped too much. */
498         if (unlikely(loop->looping))
499                 error = loop_suppress(dp, acts);
500
501 out_loop:
502         /* Decrement loop counter. */
503         if (!--loop->count)
504                 loop->looping = false;
505
506         return error;
507 }