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