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