datapath: Replace "struct odp_action" by Netlink attributes.
[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 struct nlattr *a, u32 actions_len)
77 {
78         __be16 tci = nla_get_be16(a);
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                         const struct nlattr *actions_left;
136                         u32 actions_len_left;
137                         struct sk_buff *segs;
138
139                         segs = skb_gso_segment(skb, 0);
140                         kfree_skb(skb);
141                         if (unlikely(IS_ERR(segs)))
142                                 return ERR_CAST(segs);
143
144                         actions_len_left = actions_len;
145                         actions_left = nla_next(a, &actions_len_left);
146
147                         do {
148                                 struct sk_buff *nskb = segs->next;
149                                 int err;
150
151                                 segs->next = NULL;
152
153                                 /* GSO can change the checksum type so update.*/
154                                 compute_ip_summed(segs, true);
155
156                                 segs = __vlan_put_tag(segs, ntohs(tci));
157                                 err = -ENOMEM;
158                                 if (segs) {
159                                         err = execute_actions(
160                                                 dp, segs, key, actions_left,
161                                                 actions_len_left);
162                                 }
163
164                                 if (unlikely(err)) {
165                                         while ((segs = nskb)) {
166                                                 nskb = segs->next;
167                                                 segs->next = NULL;
168                                                 kfree_skb(segs);
169                                         }
170                                         return ERR_PTR(err);
171                                 }
172
173                                 segs = nskb;
174                         } while (segs->next);
175
176                         skb = segs;
177                         compute_ip_summed(skb, true);
178                 }
179
180                 /* The hardware-accelerated version of vlan_put_tag() works
181                  * only for a device that has a VLAN group configured (with
182                  * e.g. vconfig(8)), so call the software-only version
183                  * __vlan_put_tag() directly instead.
184                  */
185                 skb = __vlan_put_tag(skb, ntohs(tci));
186                 if (!skb)
187                         return ERR_PTR(-ENOMEM);
188
189                 /* GSO doesn't fix up the hardware computed checksum so this
190                  * will only be hit in the non-GSO case. */
191                 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
192                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
193                                                 + ETH_HLEN, VLAN_HLEN, 0));
194         }
195
196         return skb;
197 }
198
199 static struct sk_buff *strip_vlan(struct sk_buff *skb)
200 {
201         skb = make_writable(skb, 0);
202         if (skb)
203                 vlan_pull_tag(skb);
204         return skb;
205 }
206
207 static bool is_ip(struct sk_buff *skb, const struct odp_flow_key *key)
208 {
209         return (key->dl_type == htons(ETH_P_IP) &&
210                 skb->transport_header > skb->network_header);
211 }
212
213 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct odp_flow_key *key)
214 {
215         int transport_len = skb->len - skb_transport_offset(skb);
216         if (key->nw_proto == IPPROTO_TCP) {
217                 if (likely(transport_len >= sizeof(struct tcphdr)))
218                         return &tcp_hdr(skb)->check;
219         } else if (key->nw_proto == IPPROTO_UDP) {
220                 if (likely(transport_len >= sizeof(struct udphdr)))
221                         return &udp_hdr(skb)->check;
222         }
223         return NULL;
224 }
225
226 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
227                                    const struct odp_flow_key *key,
228                                    const struct nlattr *a)
229 {
230         __be32 new_nwaddr = nla_get_be32(a);
231         struct iphdr *nh;
232         __sum16 *check;
233         __be32 *nwaddr;
234
235         if (unlikely(!is_ip(skb, key)))
236                 return skb;
237
238         skb = make_writable(skb, 0);
239         if (unlikely(!skb))
240                 return NULL;
241
242         nh = ip_hdr(skb);
243         nwaddr = nla_type(a) == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
244
245         check = get_l4_checksum(skb, key);
246         if (likely(check))
247                 inet_proto_csum_replace4(check, skb, *nwaddr, new_nwaddr, 1);
248         csum_replace4(&nh->check, *nwaddr, new_nwaddr);
249
250         *nwaddr = new_nwaddr;
251
252         return skb;
253 }
254
255 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
256                                   const struct odp_flow_key *key,
257                                   u8 nw_tos)
258 {
259         if (unlikely(!is_ip(skb, key)))
260                 return skb;
261
262         skb = make_writable(skb, 0);
263         if (skb) {
264                 struct iphdr *nh = ip_hdr(skb);
265                 u8 *f = &nh->tos;
266                 u8 old = *f;
267                 u8 new;
268
269                 /* Set the DSCP bits and preserve the ECN bits. */
270                 new = nw_tos | (nh->tos & INET_ECN_MASK);
271                 csum_replace4(&nh->check, (__force __be32)old,
272                                           (__force __be32)new);
273                 *f = new;
274         }
275         return skb;
276 }
277
278 static struct sk_buff *set_tp_port(struct sk_buff *skb,
279                                    const struct odp_flow_key *key,
280                                    const struct nlattr *a)
281 {
282         struct udphdr *th;
283         __sum16 *check;
284         __be16 *port;
285
286         if (unlikely(!is_ip(skb, key)))
287                 return skb;
288
289         skb = make_writable(skb, 0);
290         if (unlikely(!skb))
291                 return NULL;
292
293         /* Must follow make_writable() since that can move the skb data. */
294         check = get_l4_checksum(skb, key);
295         if (unlikely(!check))
296                 return skb;
297
298         /*
299          * Update port and checksum.
300          *
301          * This is OK because source and destination port numbers are at the
302          * same offsets in both UDP and TCP headers, and get_l4_checksum() only
303          * supports those protocols.
304          */
305         th = udp_hdr(skb);
306         port = nla_type(a) == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
307         inet_proto_csum_replace2(check, skb, *port, nla_get_be16(a), 0);
308         *port = nla_get_be16(a);
309
310         return skb;
311 }
312
313 /**
314  * is_spoofed_arp - check for invalid ARP packet
315  *
316  * @skb: skbuff containing an Ethernet packet, with network header pointing
317  * just past the Ethernet and optional 802.1Q header.
318  * @key: flow key extracted from @skb by flow_extract()
319  *
320  * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
321  * or truncated header fields or one whose inner and outer Ethernet address
322  * differ.
323  */
324 static bool is_spoofed_arp(struct sk_buff *skb, const struct odp_flow_key *key)
325 {
326         struct arp_eth_header *arp;
327
328         if (key->dl_type != htons(ETH_P_ARP))
329                 return false;
330
331         if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
332                 return true;
333
334         arp = (struct arp_eth_header *)skb_network_header(skb);
335         return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
336                 arp->ar_pro != htons(ETH_P_IP) ||
337                 arp->ar_hln != ETH_ALEN ||
338                 arp->ar_pln != 4 ||
339                 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
340 }
341
342 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
343 {
344         struct vport *p;
345
346         if (!skb)
347                 goto error;
348
349         p = rcu_dereference(dp->ports[out_port]);
350         if (!p)
351                 goto error;
352
353         vport_send(p, skb);
354         return;
355
356 error:
357         kfree_skb(skb);
358 }
359
360 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg)
361 {
362         skb = skb_clone(skb, GFP_ATOMIC);
363         if (!skb)
364                 return -ENOMEM;
365         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
366 }
367
368 /* Send a copy of this packet up to the sFlow agent, along with extra
369  * information about what happened to it. */
370 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
371                          const struct nlattr *a, u32 actions_len,
372                          struct vport *vport)
373 {
374         struct odp_sflow_sample_header *hdr;
375         unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
376         struct sk_buff *nskb;
377
378         nskb = skb_copy_expand(skb, actions_len + hdrlen, 0, GFP_ATOMIC);
379         if (!nskb)
380                 return;
381
382         memcpy(__skb_push(nskb, actions_len), a, actions_len);
383         hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
384         hdr->actions_len = actions_len;
385         hdr->sample_pool = atomic_read(&vport->sflow_pool);
386         dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
387 }
388
389 /* Execute a list of actions against 'skb'. */
390 int execute_actions(struct datapath *dp, struct sk_buff *skb,
391                     const struct odp_flow_key *key,
392                     const struct nlattr *actions, u32 actions_len)
393 {
394         /* Every output action needs a separate clone of 'skb', but the common
395          * case is just a single output action, so that doing a clone and
396          * then freeing the original skbuff is wasteful.  So the following code
397          * is slightly obscure just to avoid that. */
398         int prev_port = -1;
399         u32 priority = skb->priority;
400         const struct nlattr *a;
401         int rem, err;
402
403         if (dp->sflow_probability) {
404                 struct vport *p = OVS_CB(skb)->vport;
405                 if (p) {
406                         atomic_inc(&p->sflow_pool);
407                         if (dp->sflow_probability == UINT_MAX ||
408                             net_random() < dp->sflow_probability)
409                                 sflow_sample(dp, skb, actions, actions_len, p);
410                 }
411         }
412
413         OVS_CB(skb)->tun_id = 0;
414
415         for (a = actions, rem = actions_len; rem > 0; a = nla_next(a, &rem)) {
416                 if (prev_port != -1) {
417                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
418                         prev_port = -1;
419                 }
420
421                 switch (nla_type(a)) {
422                 case ODPAT_OUTPUT:
423                         prev_port = nla_get_u32(a);
424                         break;
425
426                 case ODPAT_CONTROLLER:
427                         err = output_control(dp, skb, nla_get_u32(a));
428                         if (err) {
429                                 kfree_skb(skb);
430                                 return err;
431                         }
432                         break;
433
434                 case ODPAT_SET_TUNNEL:
435                         OVS_CB(skb)->tun_id = nla_get_be32(a);
436                         break;
437
438                 case ODPAT_SET_DL_TCI:
439                         skb = modify_vlan_tci(dp, skb, key, a, rem);
440                         if (IS_ERR(skb))
441                                 return PTR_ERR(skb);
442                         break;
443
444                 case ODPAT_STRIP_VLAN:
445                         skb = strip_vlan(skb);
446                         break;
447
448                 case ODPAT_SET_DL_SRC:
449                         skb = make_writable(skb, 0);
450                         if (!skb)
451                                 return -ENOMEM;
452                         memcpy(eth_hdr(skb)->h_source, nla_data(a), ETH_ALEN);
453                         break;
454
455                 case ODPAT_SET_DL_DST:
456                         skb = make_writable(skb, 0);
457                         if (!skb)
458                                 return -ENOMEM;
459                         memcpy(eth_hdr(skb)->h_dest, nla_data(a), ETH_ALEN);
460                         break;
461
462                 case ODPAT_SET_NW_SRC:
463                 case ODPAT_SET_NW_DST:
464                         skb = set_nw_addr(skb, key, a);
465                         break;
466
467                 case ODPAT_SET_NW_TOS:
468                         skb = set_nw_tos(skb, key, nla_get_u8(a));
469                         break;
470
471                 case ODPAT_SET_TP_SRC:
472                 case ODPAT_SET_TP_DST:
473                         skb = set_tp_port(skb, key, a);
474                         break;
475
476                 case ODPAT_SET_PRIORITY:
477                         skb->priority = nla_get_u32(a);
478                         break;
479
480                 case ODPAT_POP_PRIORITY:
481                         skb->priority = priority;
482                         break;
483
484                 case ODPAT_DROP_SPOOFED_ARP:
485                         if (unlikely(is_spoofed_arp(skb, key)))
486                                 goto exit;
487                         break;
488                 }
489                 if (!skb)
490                         return -ENOMEM;
491         }
492 exit:
493         if (prev_port != -1)
494                 do_output(dp, skb, prev_port);
495         else
496                 kfree_skb(skb);
497         return 0;
498 }