wdp-xflow: Remove wx structure from global list when closing.
[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/xflow.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 xflow_key *key, const union xflow_action *a,
75                                        int n_actions, gfp_t gfp)
76 {
77         __be16 mask = a->dl_tci.mask;
78         __be16 tci = a->dl_tci.tci;
79
80         skb = make_writable(skb, VLAN_HLEN, gfp);
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 = (vh->h_vlan_TCI & ~mask) | tci;
96
97                 if (OVS_CB(skb)->ip_summed == 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                                                               gfp);
158                                 }
159
160                                 if (unlikely(err)) {
161                                         while ((segs = nskb)) {
162                                                 nskb = segs->next;
163                                                 segs->next = NULL;
164                                                 kfree_skb(segs);
165                                         }
166                                         return ERR_PTR(err);
167                                 }
168
169                                 segs = nskb;
170                         } while (segs->next);
171
172                         skb = segs;
173                         compute_ip_summed(skb, true);
174                 }
175
176                 /* The hardware-accelerated version of vlan_put_tag() works
177                  * only for a device that has a VLAN group configured (with
178                  * e.g. vconfig(8)), so call the software-only version
179                  * __vlan_put_tag() directly instead.
180                  */
181                 skb = __vlan_put_tag(skb, ntohs(tci));
182                 if (!skb)
183                         return ERR_PTR(-ENOMEM);
184
185                 /* GSO doesn't fix up the hardware computed checksum so this
186                  * will only be hit in the non-GSO case. */
187                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
188                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
189                                                 + ETH_HLEN, VLAN_HLEN, 0));
190         }
191
192         return skb;
193 }
194
195 static struct sk_buff *strip_vlan(struct sk_buff *skb, gfp_t gfp)
196 {
197         skb = make_writable(skb, 0, gfp);
198         if (skb)
199                 vlan_pull_tag(skb);
200
201         return skb;
202 }
203
204 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
205                                    const struct xflow_action_dl_addr *a,
206                                    gfp_t gfp)
207 {
208         skb = make_writable(skb, 0, gfp);
209         if (skb) {
210                 struct ethhdr *eh = eth_hdr(skb);
211                 if (a->type == XFLOWAT_SET_DL_SRC)
212                         memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
213                 else
214                         memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
215         }
216         return skb;
217 }
218
219 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
220  * covered by the sum has been changed from 'from' to 'to'.  If set,
221  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
222  * Based on nf_proto_csum_replace4. */
223 static void update_csum(__sum16 *sum, struct sk_buff *skb,
224                         __be32 from, __be32 to, int pseudohdr)
225 {
226         __be32 diff[] = { ~from, to };
227
228         if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
229                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
230                                 ~csum_unfold(*sum)));
231                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
232                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
233                                                 ~skb->csum);
234         } else if (pseudohdr)
235                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
236                                 csum_unfold(*sum)));
237 }
238
239 static bool is_ip(struct sk_buff *skb, const struct xflow_key *key)
240 {
241         return (key->dl_type == htons(ETH_P_IP) &&
242                 skb->transport_header > skb->network_header);
243 }
244
245 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct xflow_key *key)
246 {
247         int transport_len = skb->len - skb_transport_offset(skb);
248         if (key->nw_proto == IPPROTO_TCP) {
249                 if (likely(transport_len >= sizeof(struct tcphdr)))
250                         return &tcp_hdr(skb)->check;
251         } else if (key->nw_proto == IPPROTO_UDP) {
252                 if (likely(transport_len >= sizeof(struct udphdr)))
253                         return &udp_hdr(skb)->check;
254         }
255         return NULL;
256 }
257
258 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
259                                    const struct xflow_key *key,
260                                    const struct xflow_action_nw_addr *a,
261                                    gfp_t gfp)
262 {
263         struct iphdr *nh;
264         __sum16 *check;
265         __be32 *nwaddr;
266
267         if (unlikely(!is_ip(skb, key)))
268                 return skb;
269
270         skb = make_writable(skb, 0, gfp);
271         if (unlikely(!skb))
272                 return NULL;
273
274         nh = ip_hdr(skb);
275         nwaddr = a->type == XFLOWAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
276
277         check = get_l4_checksum(skb, key);
278         if (likely(check))
279                 update_csum(check, skb, *nwaddr, a->nw_addr, 1);
280         update_csum(&nh->check, skb, *nwaddr, a->nw_addr, 0);
281
282         *nwaddr = a->nw_addr;
283
284         return skb;
285 }
286
287 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
288                                    const struct xflow_key *key,
289                                    const struct xflow_action_nw_tos *a,
290                                    gfp_t gfp)
291 {
292         if (unlikely(!is_ip(skb, key)))
293                 return skb;
294
295         skb = make_writable(skb, 0, gfp);
296         if (skb) {
297                 struct iphdr *nh = ip_hdr(skb);
298                 u8 *f = &nh->tos;
299                 u8 old = *f;
300                 u8 new;
301
302                 /* Set the DSCP bits and preserve the ECN bits. */
303                 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
304                 update_csum(&nh->check, skb, htons((u16)old),
305                             htons((u16)new), 0);
306                 *f = new;
307         }
308         return skb;
309 }
310
311 static struct sk_buff *set_tp_port(struct sk_buff *skb,
312                                    const struct xflow_key *key,
313                                    const struct xflow_action_tp_port *a,
314                                    gfp_t gfp)
315 {
316         struct udphdr *th;
317         __sum16 *check;
318         __be16 *port;
319
320         if (unlikely(!is_ip(skb, key)))
321                 return skb;
322
323         skb = make_writable(skb, 0, gfp);
324         if (unlikely(!skb))
325                 return NULL;
326
327         /* Must follow make_writable() since that can move the skb data. */
328         check = get_l4_checksum(skb, key);
329         if (unlikely(!check))
330                 return skb;
331
332         /*
333          * Update port and checksum.
334          *
335          * This is OK because source and destination port numbers are at the
336          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
337          * supports those protocols.
338          */
339         th = udp_hdr(skb);
340         port = a->type == XFLOWAT_SET_TP_SRC ? &th->source : &th->dest;
341         update_csum(check, skb, *port, a->tp_port, 0);
342         *port = a->tp_port;
343
344         return skb;
345 }
346
347 /**
348  * is_spoofed_arp - check for invalid ARP packet
349  *
350  * @skb: skbuff containing an Ethernet packet, with network header pointing
351  * just past the Ethernet and optional 802.1Q header.
352  * @key: flow key extracted from @skb by flow_extract()
353  *
354  * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
355  * or truncated header fields or one whose inner and outer Ethernet address
356  * differ.
357  */
358 static bool is_spoofed_arp(struct sk_buff *skb, const struct xflow_key *key)
359 {
360         struct arp_eth_header *arp;
361
362         if (key->dl_type != htons(ETH_P_ARP))
363                 return false;
364
365         if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
366                 return true;
367
368         arp = (struct arp_eth_header *)skb_network_header(skb);
369         return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
370                 arp->ar_pro != htons(ETH_P_IP) ||
371                 arp->ar_hln != ETH_ALEN ||
372                 arp->ar_pln != 4 ||
373                 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
374 }
375
376 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
377 {
378         struct dp_port *p;
379
380         if (!skb)
381                 goto error;
382
383         p = rcu_dereference(dp->ports[out_port]);
384         if (!p)
385                 goto error;
386
387         vport_send(p->vport, skb);
388         return;
389
390 error:
391         kfree_skb(skb);
392 }
393
394 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
395  * none.  */
396 static int output_group(struct datapath *dp, __u16 group,
397                         struct sk_buff *skb, gfp_t gfp)
398 {
399         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
400         int prev_port = -1;
401         int i;
402
403         if (!g)
404                 return -1;
405         for (i = 0; i < g->n_ports; i++) {
406                 struct dp_port *p = rcu_dereference(dp->ports[g->ports[i]]);
407                 if (!p || OVS_CB(skb)->dp_port == p)
408                         continue;
409                 if (prev_port != -1) {
410                         struct sk_buff *clone = skb_clone(skb, gfp);
411                         if (!clone)
412                                 return -1;
413                         do_output(dp, clone, prev_port);
414                 }
415                 prev_port = p->port_no;
416         }
417         return prev_port;
418 }
419
420 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg,
421                           gfp_t gfp)
422 {
423         skb = skb_clone(skb, gfp);
424         if (!skb)
425                 return -ENOMEM;
426         return dp_output_control(dp, skb, _XFLOWL_ACTION_NR, arg);
427 }
428
429 /* Send a copy of this packet up to the sFlow agent, along with extra
430  * information about what happened to it. */
431 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
432                          const union xflow_action *a, int n_actions,
433                          gfp_t gfp, struct dp_port *dp_port)
434 {
435         struct xflow_sflow_sample_header *hdr;
436         unsigned int actlen = n_actions * sizeof(union xflow_action);
437         unsigned int hdrlen = sizeof(struct xflow_sflow_sample_header);
438         struct sk_buff *nskb;
439
440         nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
441         if (!nskb)
442                 return;
443
444         memcpy(__skb_push(nskb, actlen), a, actlen);
445         hdr = (struct xflow_sflow_sample_header*)__skb_push(nskb, hdrlen);
446         hdr->n_actions = n_actions;
447         hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
448         dp_output_control(dp, nskb, _XFLOWL_SFLOW_NR, 0);
449 }
450
451 /* Execute a list of actions against 'skb'. */
452 int execute_actions(struct datapath *dp, struct sk_buff *skb,
453                     const struct xflow_key *key,
454                     const union xflow_action *a, int n_actions,
455                     gfp_t gfp)
456 {
457         /* Every output action needs a separate clone of 'skb', but the common
458          * case is just a single output action, so that doing a clone and
459          * then freeing the original skbuff is wasteful.  So the following code
460          * is slightly obscure just to avoid that. */
461         int prev_port = -1;
462         u32 priority = skb->priority;
463         int err;
464
465         if (dp->sflow_probability) {
466                 struct dp_port *p = OVS_CB(skb)->dp_port;
467                 if (p) {
468                         atomic_inc(&p->sflow_pool);
469                         if (dp->sflow_probability == UINT_MAX ||
470                             net_random() < dp->sflow_probability)
471                                 sflow_sample(dp, skb, a, n_actions, gfp, p);
472                 }
473         }
474
475         OVS_CB(skb)->tun_id = 0;
476
477         for (; n_actions > 0; a++, n_actions--) {
478                 if (prev_port != -1) {
479                         do_output(dp, skb_clone(skb, gfp), prev_port);
480                         prev_port = -1;
481                 }
482
483                 switch (a->type) {
484                 case XFLOWAT_OUTPUT:
485                         prev_port = a->output.port;
486                         break;
487
488                 case XFLOWAT_OUTPUT_GROUP:
489                         prev_port = output_group(dp, a->output_group.group,
490                                                  skb, gfp);
491                         break;
492
493                 case XFLOWAT_CONTROLLER:
494                         err = output_control(dp, skb, a->controller.arg, gfp);
495                         if (err) {
496                                 kfree_skb(skb);
497                                 return err;
498                         }
499                         break;
500
501                 case XFLOWAT_SET_TUNNEL:
502                         OVS_CB(skb)->tun_id = a->tunnel.tun_id;
503                         break;
504
505                 case XFLOWAT_SET_DL_TCI:
506                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
507                         if (IS_ERR(skb))
508                                 return PTR_ERR(skb);
509                         break;
510
511                 case XFLOWAT_STRIP_VLAN:
512                         skb = strip_vlan(skb, gfp);
513                         break;
514
515                 case XFLOWAT_SET_DL_SRC:
516                 case XFLOWAT_SET_DL_DST:
517                         skb = set_dl_addr(skb, &a->dl_addr, gfp);
518                         break;
519
520                 case XFLOWAT_SET_NW_SRC:
521                 case XFLOWAT_SET_NW_DST:
522                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
523                         break;
524
525                 case XFLOWAT_SET_NW_TOS:
526                         skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
527                         break;
528
529                 case XFLOWAT_SET_TP_SRC:
530                 case XFLOWAT_SET_TP_DST:
531                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
532                         break;
533
534                 case XFLOWAT_SET_PRIORITY:
535                         skb->priority = a->priority.priority;
536                         break;
537
538                 case XFLOWAT_POP_PRIORITY:
539                         skb->priority = priority;
540                         break;
541
542                 case XFLOWAT_DROP_SPOOFED_ARP:
543                         if (unlikely(is_spoofed_arp(skb, key)))
544                                 goto exit;
545                         break;
546                 }
547                 if (!skb)
548                         return -ENOMEM;
549         }
550 exit:
551         if (prev_port != -1)
552                 do_output(dp, skb, prev_port);
553         else
554                 kfree_skb(skb);
555         return 0;
556 }