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