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