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