datapath: Don't recursively sample packets or reset their "tun_id"s.
[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 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 #include <linux/skbuff.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/if_arp.h>
18 #include <linux/if_vlan.h>
19 #include <net/inet_ecn.h>
20 #include <net/ip.h>
21 #include <net/checksum.h>
22
23 #include "actions.h"
24 #include "checksum.h"
25 #include "datapath.h"
26 #include "openvswitch/datapath-protocol.h"
27 #include "vport.h"
28
29 static int do_execute_actions(struct datapath *, struct sk_buff *,
30                               const struct odp_flow_key *,
31                               const struct nlattr *actions, u32 actions_len);
32
33 static struct sk_buff *make_writable(struct sk_buff *skb, unsigned min_headroom)
34 {
35         if (skb_cloned(skb)) {
36                 struct sk_buff *nskb;
37                 unsigned headroom = max(min_headroom, skb_headroom(skb));
38
39                 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), GFP_ATOMIC);
40                 if (nskb) {
41                         set_skb_csum_bits(skb, nskb);
42                         kfree_skb(skb);
43                         return nskb;
44                 }
45         } else {
46                 unsigned int hdr_len = (skb_transport_offset(skb)
47                                         + sizeof(struct tcphdr));
48                 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
49                         return skb;
50         }
51         kfree_skb(skb);
52         return NULL;
53 }
54
55 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
56 {
57         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
58         struct ethhdr *eh;
59
60         /* Verify we were given a vlan packet */
61         if (vh->h_vlan_proto != htons(ETH_P_8021Q) || skb->len < VLAN_ETH_HLEN)
62                 return skb;
63
64         if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
65                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
66                                         + ETH_HLEN, VLAN_HLEN, 0));
67
68         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
69
70         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
71
72         skb->protocol = eh->h_proto;
73         skb->mac_header += VLAN_HLEN;
74
75         return skb;
76 }
77
78 static struct sk_buff *modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
79                                        const struct odp_flow_key *key,
80                                        const struct nlattr *a, u32 actions_len)
81 {
82         __be16 tci = nla_get_be16(a);
83
84         skb = make_writable(skb, VLAN_HLEN);
85         if (!skb)
86                 return ERR_PTR(-ENOMEM);
87
88         if (skb->protocol == htons(ETH_P_8021Q)) {
89                 /* Modify vlan id, but maintain other TCI values */
90                 struct vlan_ethhdr *vh;
91                 __be16 old_tci;
92
93                 if (skb->len < VLAN_ETH_HLEN)
94                         return skb;
95
96                 vh = vlan_eth_hdr(skb);
97                 old_tci = vh->h_vlan_TCI;
98
99                 vh->h_vlan_TCI = tci;
100
101                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) {
102                         __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
103
104                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
105                                                 ~skb->csum);
106                 }
107         } else {
108                 int err;
109
110                 /* Add vlan header */
111
112                 /* Set up checksumming pointers for checksum-deferred packets
113                  * on Xen.  Otherwise, dev_queue_xmit() will try to do this
114                  * when we send the packet out on the wire, and it will fail at
115                  * that point because skb_checksum_setup() will not look inside
116                  * an 802.1Q header. */
117                 err = vswitch_skb_checksum_setup(skb);
118                 if (unlikely(err)) {
119                         kfree_skb(skb);
120                         return ERR_PTR(err);
121                 }
122
123                 /* GSO is not implemented for packets with an 802.1Q header, so
124                  * we have to do segmentation before we add that header.
125                  *
126                  * GSO does work with hardware-accelerated VLAN tagging, but we
127                  * can't use hardware-accelerated VLAN tagging since it
128                  * requires the device to have a VLAN group configured (with
129                  * e.g. vconfig(8)) and we don't do that.
130                  *
131                  * Having to do this here may be a performance loss, since we
132                  * can't take advantage of TSO hardware support, although it
133                  * does not make a measurable network performance difference
134                  * for 1G Ethernet.  Fixing that would require patching the
135                  * kernel (either to add GSO support to the VLAN protocol or to
136                  * support hardware-accelerated VLAN tagging without VLAN
137                  * groups configured). */
138                 if (skb_is_gso(skb)) {
139                         const struct nlattr *actions_left;
140                         u32 actions_len_left;
141                         struct sk_buff *segs;
142
143                         segs = skb_gso_segment(skb, 0);
144                         kfree_skb(skb);
145                         if (IS_ERR(segs))
146                                 return ERR_CAST(segs);
147
148                         actions_len_left = actions_len;
149                         actions_left = nla_next(a, &actions_len_left);
150
151                         do {
152                                 struct sk_buff *nskb = segs->next;
153                                 int err;
154
155                                 segs->next = NULL;
156
157                                 /* GSO can change the checksum type so update.*/
158                                 compute_ip_summed(segs, true);
159
160                                 segs = __vlan_put_tag(segs, ntohs(tci));
161                                 err = -ENOMEM;
162                                 if (segs) {
163                                         err = do_execute_actions(
164                                                 dp, segs, key, actions_left,
165                                                 actions_len_left);
166                                 }
167
168                                 if (unlikely(err)) {
169                                         while ((segs = nskb)) {
170                                                 nskb = segs->next;
171                                                 segs->next = NULL;
172                                                 kfree_skb(segs);
173                                         }
174                                         return ERR_PTR(err);
175                                 }
176
177                                 segs = nskb;
178                         } while (segs->next);
179
180                         skb = segs;
181                         compute_ip_summed(skb, true);
182                 }
183
184                 /* The hardware-accelerated version of vlan_put_tag() works
185                  * only for a device that has a VLAN group configured (with
186                  * e.g. vconfig(8)), so call the software-only version
187                  * __vlan_put_tag() directly instead.
188                  */
189                 skb = __vlan_put_tag(skb, ntohs(tci));
190                 if (!skb)
191                         return ERR_PTR(-ENOMEM);
192
193                 /* GSO doesn't fix up the hardware computed checksum so this
194                  * will only be hit in the non-GSO case. */
195                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
196                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
197                                                 + ETH_HLEN, VLAN_HLEN, 0));
198         }
199
200         return skb;
201 }
202
203 static struct sk_buff *strip_vlan(struct sk_buff *skb)
204 {
205         skb = make_writable(skb, 0);
206         if (skb)
207                 vlan_pull_tag(skb);
208         return skb;
209 }
210
211 static bool is_ip(struct sk_buff *skb, const struct odp_flow_key *key)
212 {
213         return (key->dl_type == htons(ETH_P_IP) &&
214                 skb->transport_header > skb->network_header);
215 }
216
217 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct odp_flow_key *key)
218 {
219         int transport_len = skb->len - skb_transport_offset(skb);
220         if (key->nw_proto == IPPROTO_TCP) {
221                 if (likely(transport_len >= sizeof(struct tcphdr)))
222                         return &tcp_hdr(skb)->check;
223         } else if (key->nw_proto == IPPROTO_UDP) {
224                 if (likely(transport_len >= sizeof(struct udphdr)))
225                         return &udp_hdr(skb)->check;
226         }
227         return NULL;
228 }
229
230 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
231                                    const struct odp_flow_key *key,
232                                    const struct nlattr *a)
233 {
234         __be32 new_nwaddr = nla_get_be32(a);
235         struct iphdr *nh;
236         __sum16 *check;
237         __be32 *nwaddr;
238
239         if (unlikely(!is_ip(skb, key)))
240                 return skb;
241
242         skb = make_writable(skb, 0);
243         if (unlikely(!skb))
244                 return NULL;
245
246         nh = ip_hdr(skb);
247         nwaddr = nla_type(a) == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
248
249         check = get_l4_checksum(skb, key);
250         if (likely(check))
251                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
252         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
253
254         *nwaddr = new_nwaddr;
255
256         return skb;
257 }
258
259 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
260                                   const struct odp_flow_key *key,
261                                   u8 nw_tos)
262 {
263         if (unlikely(!is_ip(skb, key)))
264                 return skb;
265
266         skb = make_writable(skb, 0);
267         if (skb) {
268                 struct iphdr *nh = ip_hdr(skb);
269                 u8 *f = &nh->tos;
270                 u8 old = *f;
271                 u8 new;
272
273                 /* Set the DSCP bits and preserve the ECN bits. */
274                 new = nw_tos | (nh->tos & INET_ECN_MASK);
275                 csum_replace4(&nh->check, (__force __be32)old,
276                                           (__force __be32)new);
277                 *f = new;
278         }
279         return skb;
280 }
281
282 static struct sk_buff *set_tp_port(struct sk_buff *skb,
283                                    const struct odp_flow_key *key,
284                                    const struct nlattr *a)
285 {
286         struct udphdr *th;
287         __sum16 *check;
288         __be16 *port;
289
290         if (unlikely(!is_ip(skb, key)))
291                 return skb;
292
293         skb = make_writable(skb, 0);
294         if (unlikely(!skb))
295                 return NULL;
296
297         /* Must follow make_writable() since that can move the skb data. */
298         check = get_l4_checksum(skb, key);
299         if (unlikely(!check))
300                 return skb;
301
302         /*
303          * Update port and checksum.
304          *
305          * This is OK because source and destination port numbers are at the
306          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
307          * supports those protocols.
308          */
309         th = udp_hdr(skb);
310         port = nla_type(a) == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
311         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
312         *port = nla_get_be16(a);
313
314         return skb;
315 }
316
317 /**
318  * is_spoofed_arp - check for invalid ARP packet
319  *
320  * @skb: skbuff containing an Ethernet packet, with network header pointing
321  * just past the Ethernet and optional 802.1Q header.
322  * @key: flow key extracted from @skb by flow_extract()
323  *
324  * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
325  * or truncated header fields or one whose inner and outer Ethernet address
326  * differ.
327  */
328 static bool is_spoofed_arp(struct sk_buff *skb, const struct odp_flow_key *key)
329 {
330         struct arp_eth_header *arp;
331
332         if (key->dl_type != htons(ETH_P_ARP))
333                 return false;
334
335         if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
336                 return true;
337
338         arp = (struct arp_eth_header *)skb_network_header(skb);
339         return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
340                 arp->ar_pro != htons(ETH_P_IP) ||
341                 arp->ar_hln != ETH_ALEN ||
342                 arp->ar_pln != 4 ||
343                 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
344 }
345
346 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
347 {
348         struct vport *p;
349
350         if (!skb)
351                 goto error;
352
353         p = rcu_dereference(dp->ports[out_port]);
354         if (!p)
355                 goto error;
356
357         vport_send(p, skb);
358         return;
359
360 error:
361         kfree_skb(skb);
362 }
363
364 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg)
365 {
366         skb = skb_clone(skb, GFP_ATOMIC);
367         if (!skb)
368                 return -ENOMEM;
369         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
370 }
371
372 /* Execute a list of actions against 'skb'. */
373 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
374                               const struct odp_flow_key *key,
375                               const struct nlattr *actions, u32 actions_len)
376 {
377         /* Every output action needs a separate clone of 'skb', but the common
378          * case is just a single output action, so that doing a clone and
379          * then freeing the original skbuff is wasteful.  So the following code
380          * is slightly obscure just to avoid that. */
381         int prev_port = -1;
382         u32 priority = skb->priority;
383         const struct nlattr *a;
384         int rem, err;
385
386         for (a = actions, rem = actions_len; rem > 0; a = nla_next(a, &rem)) {
387                 if (prev_port != -1) {
388                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
389                         prev_port = -1;
390                 }
391
392                 switch (nla_type(a)) {
393                 case ODPAT_OUTPUT:
394                         prev_port = nla_get_u32(a);
395                         break;
396
397                 case ODPAT_CONTROLLER:
398                         err = output_control(dp, skb, nla_get_u64(a));
399                         if (err) {
400                                 kfree_skb(skb);
401                                 return err;
402                         }
403                         break;
404
405                 case ODPAT_SET_TUNNEL:
406                         OVS_CB(skb)->tun_id = nla_get_be64(a);
407                         break;
408
409                 case ODPAT_SET_DL_TCI:
410                         skb = modify_vlan_tci(dp, skb, key, a, rem);
411                         if (IS_ERR(skb))
412                                 return PTR_ERR(skb);
413                         break;
414
415                 case ODPAT_STRIP_VLAN:
416                         skb = strip_vlan(skb);
417                         break;
418
419                 case ODPAT_SET_DL_SRC:
420                         skb = make_writable(skb, 0);
421                         if (!skb)
422                                 return -ENOMEM;
423                         memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
424                         break;
425
426                 case ODPAT_SET_DL_DST:
427                         skb = make_writable(skb, 0);
428                         if (!skb)
429                                 return -ENOMEM;
430                         memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
431                         break;
432
433                 case ODPAT_SET_NW_SRC:
434                 case ODPAT_SET_NW_DST:
435                         skb = set_nw_addr(skb, key, a);
436                         break;
437
438                 case ODPAT_SET_NW_TOS:
439                         skb = set_nw_tos(skb, key, nla_get_u8(a));
440                         break;
441
442                 case ODPAT_SET_TP_SRC:
443                 case ODPAT_SET_TP_DST:
444                         skb = set_tp_port(skb, key, a);
445                         break;
446
447                 case ODPAT_SET_PRIORITY:
448                         skb->priority = nla_get_u32(a);
449                         break;
450
451                 case ODPAT_POP_PRIORITY:
452                         skb->priority = priority;
453                         break;
454
455                 case ODPAT_DROP_SPOOFED_ARP:
456                         if (unlikely(is_spoofed_arp(skb, key)))
457                                 goto exit;
458                         break;
459                 }
460                 if (!skb)
461                         return -ENOMEM;
462         }
463 exit:
464         if (prev_port != -1)
465                 do_output(dp, skb, prev_port);
466         else
467                 kfree_skb(skb);
468         return 0;
469 }
470
471 /* Send a copy of this packet up to the sFlow agent, along with extra
472  * information about what happened to it. */
473 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
474                          const struct nlattr *a, u32 actions_len,
475                          struct vport *vport)
476 {
477         struct odp_sflow_sample_header *hdr;
478         unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
479         struct sk_buff *nskb;
480
481         nskb = skb_copy_expand(skb, actions_len + hdrlen, 0, GFP_ATOMIC);
482         if (!nskb)
483                 return;
484
485         memcpy(__skb_push(nskb, actions_len), a, actions_len);
486         hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
487         hdr->actions_len = actions_len;
488         hdr->sample_pool = atomic_read(&vport->sflow_pool);
489         dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
490 }
491
492 /* Execute a list of actions against 'skb'. */
493 int execute_actions(struct datapath *dp, struct sk_buff *skb,
494                     const struct odp_flow_key *key,
495                     const struct nlattr *actions, u32 actions_len)
496 {
497         if (dp->sflow_probability) {
498                 struct vport *p = OVS_CB(skb)->vport;
499                 if (p) {
500                         atomic_inc(&p->sflow_pool);
501                         if (dp->sflow_probability == UINT_MAX ||
502                             net_random() < dp->sflow_probability)
503                                 sflow_sample(dp, skb, actions, actions_len, p);
504                 }
505         }
506
507         OVS_CB(skb)->tun_id = 0;
508
509         return do_execute_actions(dp, skb, key, actions, actions_len);
510 }