datapath: Validate ToS when flow is added.
[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_vlan.h>
18 #include <net/inet_ecn.h>
19 #include <net/ip.h>
20 #include <net/checksum.h>
21 #include "datapath.h"
22 #include "dp_dev.h"
23 #include "actions.h"
24 #include "openvswitch/datapath-protocol.h"
25
26 static struct sk_buff *
27 make_writable(struct sk_buff *skb, unsigned min_headroom, gfp_t gfp)
28 {
29         if (skb_shared(skb) || skb_cloned(skb)) {
30                 struct sk_buff *nskb;
31                 unsigned headroom = max(min_headroom, skb_headroom(skb));
32
33                 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), gfp);
34                 if (nskb) {
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
36                         /* Before 2.6.24 these fields were not copied when
37                          * doing an skb_copy_expand. */
38                         nskb->ip_summed = skb->ip_summed;
39                         nskb->csum = skb->csum;
40 #endif
41 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
42                         /* These fields are copied in skb_clone but not in
43                          * skb_copy or related functions.  We need to manually
44                          * copy them over here. */
45                         nskb->proto_data_valid = skb->proto_data_valid;
46                         nskb->proto_csum_blank = skb->proto_csum_blank;
47 #endif
48                         kfree_skb(skb);
49                         return nskb;
50                 }
51         } else {
52                 unsigned int hdr_len = (skb_transport_offset(skb)
53                                         + sizeof(struct tcphdr));
54                 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
55                         return skb;
56         }
57         kfree_skb(skb);
58         return NULL;
59 }
60
61
62 static struct sk_buff *
63 vlan_pull_tag(struct sk_buff *skb)
64 {
65         struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
66         struct ethhdr *eh;
67
68         /* Verify we were given a vlan packet */
69         if (vh->h_vlan_proto != htons(ETH_P_8021Q))
70                 return skb;
71
72         if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
73                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
74                                         + ETH_HLEN, VLAN_HLEN, 0));
75
76         memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
77
78         eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
79
80         skb->protocol = eh->h_proto;
81         skb->mac_header += VLAN_HLEN;
82
83         return skb;
84 }
85
86
87 static struct sk_buff *
88 modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
89                 struct odp_flow_key *key, const union odp_action *a,
90                 int n_actions, gfp_t gfp)
91 {
92         u16 tci, mask;
93
94         if (a->type == ODPAT_SET_VLAN_VID) {
95                 tci = ntohs(a->vlan_vid.vlan_vid);
96                 mask = VLAN_VID_MASK;
97                 key->dl_vlan = htons(tci & mask);
98         } else {
99                 tci = a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT;
100                 mask = VLAN_PCP_MASK;
101         }
102
103         skb = make_writable(skb, VLAN_HLEN, gfp);
104         if (!skb)
105                 return ERR_PTR(-ENOMEM);
106
107         if (skb->protocol == htons(ETH_P_8021Q)) {
108                 /* Modify vlan id, but maintain other TCI values */
109                 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
110                 __be16 old_tci = vh->h_vlan_TCI;
111
112                 vh->h_vlan_TCI = htons((ntohs(vh->h_vlan_TCI) & ~mask) | tci);
113
114                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
115                         __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
116
117                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
118                                                 ~skb->csum);
119                 }
120         } else {
121                 /* Add vlan header */
122
123                 /* Set up checksumming pointers for checksum-deferred packets
124                  * on Xen.  Otherwise, dev_queue_xmit() will try to do this
125                  * when we send the packet out on the wire, and it will fail at
126                  * that point because skb_checksum_setup() will not look inside
127                  * an 802.1Q header. */
128                 vswitch_skb_checksum_setup(skb);
129
130                 /* GSO is not implemented for packets with an 802.1Q header, so
131                  * we have to do segmentation before we add that header.
132                  *
133                  * GSO does work with hardware-accelerated VLAN tagging, but we
134                  * can't use hardware-accelerated VLAN tagging since it
135                  * requires the device to have a VLAN group configured (with
136                  * e.g. vconfig(8)) and we don't do that.
137                  *
138                  * Having to do this here may be a performance loss, since we
139                  * can't take advantage of TSO hardware support, although it
140                  * does not make a measurable network performance difference
141                  * for 1G Ethernet.  Fixing that would require patching the
142                  * kernel (either to add GSO support to the VLAN protocol or to
143                  * support hardware-accelerated VLAN tagging without VLAN
144                  * groups configured). */
145                 if (skb_is_gso(skb)) {
146                         struct sk_buff *segs;
147
148                         segs = skb_gso_segment(skb, 0);
149                         kfree_skb(skb);
150                         if (unlikely(IS_ERR(segs)))
151                                 return ERR_CAST(segs);
152
153                         do {
154                                 struct sk_buff *nskb = segs->next;
155                                 int err;
156
157                                 segs->next = NULL;
158
159                                 /* GSO can change the checksum type so update.*/
160                                 compute_ip_summed(segs, true);
161
162                                 segs = __vlan_put_tag(segs, tci);
163                                 err = -ENOMEM;
164                                 if (segs) {
165                                         struct odp_flow_key segkey = *key;
166                                         err = execute_actions(dp, segs,
167                                                               &segkey, a + 1,
168                                                               n_actions - 1,
169                                                               gfp);
170                                 }
171
172                                 if (unlikely(err)) {
173                                         while ((segs = nskb)) {
174                                                 nskb = segs->next;
175                                                 segs->next = NULL;
176                                                 kfree_skb(segs);
177                                         }
178                                         return ERR_PTR(err);
179                                 }
180
181                                 segs = nskb;
182                         } while (segs->next);
183
184                         skb = segs;
185                         compute_ip_summed(skb, true);
186                 }
187
188                 /* The hardware-accelerated version of vlan_put_tag() works
189                  * only for a device that has a VLAN group configured (with
190                  * e.g. vconfig(8)), so call the software-only version
191                  * __vlan_put_tag() directly instead.
192                  */
193                 skb = __vlan_put_tag(skb, tci);
194                 if (!skb)
195                         return ERR_PTR(-ENOMEM);
196
197                 /* GSO doesn't fix up the hardware computed checksum so this
198                  * will only be hit in the non-GSO case. */
199                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
200                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
201                                                 + ETH_HLEN, VLAN_HLEN, 0));
202         }
203
204         return skb;
205 }
206
207 static struct sk_buff *strip_vlan(struct sk_buff *skb,
208                                   struct odp_flow_key *key, gfp_t gfp)
209 {
210         skb = make_writable(skb, 0, gfp);
211         if (skb) {
212                 vlan_pull_tag(skb);
213                 key->dl_vlan = htons(ODP_VLAN_NONE);
214         }
215         return skb;
216 }
217
218 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
219                                    const struct odp_action_dl_addr *a,
220                                    gfp_t gfp)
221 {
222         skb = make_writable(skb, 0, gfp);
223         if (skb) {
224                 struct ethhdr *eh = eth_hdr(skb);
225                 memcpy(a->type == ODPAT_SET_DL_SRC ? eh->h_source : eh->h_dest,
226                        a->dl_addr, ETH_ALEN);
227         }
228         return skb;
229 }
230
231 /* Updates 'sum', which is a field in 'skb''s data, given that a 4-byte field
232  * covered by the sum has been changed from 'from' to 'to'.  If set,
233  * 'pseudohdr' indicates that the field is in the TCP or UDP pseudo-header.
234  * Based on nf_proto_csum_replace4. */
235 static void update_csum(__sum16 *sum, struct sk_buff *skb,
236                         __be32 from, __be32 to, int pseudohdr)
237 {
238         __be32 diff[] = { ~from, to };
239
240         if (OVS_CB(skb)->ip_summed != OVS_CSUM_PARTIAL) {
241                 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff),
242                                 ~csum_unfold(*sum)));
243                 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE && pseudohdr)
244                         skb->csum = ~csum_partial((char *)diff, sizeof(diff),
245                                                 ~skb->csum);
246         } else if (pseudohdr)
247                 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
248                                 csum_unfold(*sum)));
249 }
250
251 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
252                                    struct odp_flow_key *key,
253                                    const struct odp_action_nw_addr *a,
254                                    gfp_t gfp)
255 {
256         if (key->dl_type != htons(ETH_P_IP))
257                 return skb;
258
259         skb = make_writable(skb, 0, gfp);
260         if (skb) {
261                 struct iphdr *nh = ip_hdr(skb);
262                 u32 *f = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
263                 u32 old = *f;
264                 u32 new = a->nw_addr;
265
266                 if (key->nw_proto == IPPROTO_TCP) {
267                         struct tcphdr *th = tcp_hdr(skb);
268                         update_csum(&th->check, skb, old, new, 1);
269                 } else if (key->nw_proto == IPPROTO_UDP) {
270                         struct udphdr *th = udp_hdr(skb);
271                         update_csum(&th->check, skb, old, new, 1);
272                 }
273                 update_csum(&nh->check, skb, old, new, 0);
274                 *f = new;
275         }
276         return skb;
277 }
278
279 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
280                                    struct odp_flow_key *key,
281                                    const struct odp_action_nw_tos *a,
282                                    gfp_t gfp)
283 {
284         if (key->dl_type != htons(ETH_P_IP))
285                 return skb;
286
287         skb = make_writable(skb, 0, gfp);
288         if (skb) {
289                 struct iphdr *nh = ip_hdr(skb);
290                 u8 *f = &nh->tos;
291                 u8 old = *f;
292                 u8 new;
293
294                 /* Set the DSCP bits and preserve the ECN bits. */
295                 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
296                 update_csum(&nh->check, skb, htons((uint16_t)old),
297                                 htons((uint16_t)new), 0);
298                 *f = new;
299         }
300         return skb;
301 }
302
303 static struct sk_buff *
304 set_tp_port(struct sk_buff *skb, struct odp_flow_key *key,
305             const struct odp_action_tp_port *a,
306             gfp_t gfp)
307 {
308         int check_ofs;
309
310         if (key->dl_type != htons(ETH_P_IP))
311                 return skb;
312
313         if (key->nw_proto == IPPROTO_TCP)
314                 check_ofs = offsetof(struct tcphdr, check);
315         else if (key->nw_proto == IPPROTO_UDP)
316                 check_ofs = offsetof(struct udphdr, check);
317         else
318                 return skb;
319
320         skb = make_writable(skb, 0, gfp);
321         if (skb) {
322                 struct udphdr *th = udp_hdr(skb);
323                 u16 *f = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
324                 u16 old = *f;
325                 u16 new = a->tp_port;
326                 update_csum((u16*)(skb_transport_header(skb) + check_ofs), 
327                                 skb, old, new, 0);
328                 *f = new;
329         }
330         return skb;
331 }
332
333 static inline unsigned packet_length(const struct sk_buff *skb)
334 {
335         unsigned length = skb->len - ETH_HLEN;
336         if (skb->protocol == htons(ETH_P_8021Q))
337                 length -= VLAN_HLEN;
338         return length;
339 }
340
341 int dp_xmit_skb(struct sk_buff *skb)
342 {
343         struct datapath *dp = skb->dev->br_port->dp;
344         int len = skb->len;
345
346         if (packet_length(skb) > skb->dev->mtu && !skb_is_gso(skb)) {
347                 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
348                        dp_name(dp), packet_length(skb), skb->dev->mtu);
349                 kfree_skb(skb);
350                 return -E2BIG;
351         }
352
353         forward_ip_summed(skb);
354         dev_queue_xmit(skb);
355
356         return len;
357 }
358
359 static void
360 do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
361 {
362         struct net_bridge_port *p;
363         struct net_device *dev;
364
365         if (!skb)
366                 goto error;
367
368         p = dp->ports[out_port];
369         if (!p)
370                 goto error;
371
372         dev = skb->dev = p->dev;
373         if (is_dp_dev(dev))
374                 dp_dev_recv(dev, skb);
375         else
376                 dp_xmit_skb(skb);
377         return;
378
379 error:
380         kfree_skb(skb);
381 }
382
383 /* Never consumes 'skb'.  Returns a port that 'skb' should be sent to, -1 if
384  * none.  */
385 static int output_group(struct datapath *dp, __u16 group,
386                         struct sk_buff *skb, gfp_t gfp)
387 {
388         struct dp_port_group *g = rcu_dereference(dp->groups[group]);
389         int prev_port = -1;
390         int i;
391
392         if (!g)
393                 return -1;
394         for (i = 0; i < g->n_ports; i++) {
395                 struct net_bridge_port *p = dp->ports[g->ports[i]];
396                 if (!p || skb->dev == p->dev)
397                         continue;
398                 if (prev_port != -1) {
399                         struct sk_buff *clone = skb_clone(skb, gfp);
400                         if (!clone)
401                                 return -1;
402                         do_output(dp, clone, prev_port);
403                 }
404                 prev_port = p->port_no;
405         }
406         return prev_port;
407 }
408
409 static int
410 output_control(struct datapath *dp, struct sk_buff *skb, u32 arg, gfp_t gfp)
411 {
412         skb = skb_clone(skb, gfp);
413         if (!skb)
414                 return -ENOMEM;
415         return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
416 }
417
418 /* Send a copy of this packet up to the sFlow agent, along with extra
419  * information about what happened to it. */
420 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
421                          const union odp_action *a, int n_actions,
422                          gfp_t gfp, struct net_bridge_port *nbp)
423 {
424         struct odp_sflow_sample_header *hdr;
425         unsigned int actlen = n_actions * sizeof(union odp_action);
426         unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
427         struct sk_buff *nskb;
428
429         nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
430         if (!nskb)
431                 return;
432
433         memcpy(__skb_push(nskb, actlen), a, actlen);
434         hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
435         hdr->n_actions = n_actions;
436         hdr->sample_pool = atomic_read(&nbp->sflow_pool);
437         dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
438 }
439
440 /* Execute a list of actions against 'skb'. */
441 int execute_actions(struct datapath *dp, struct sk_buff *skb,
442                     struct odp_flow_key *key,
443                     const union odp_action *a, int n_actions,
444                     gfp_t gfp)
445 {
446         /* Every output action needs a separate clone of 'skb', but the common
447          * case is just a single output action, so that doing a clone and
448          * then freeing the original skbuff is wasteful.  So the following code
449          * is slightly obscure just to avoid that. */
450         int prev_port = -1;
451         int err;
452
453         if (dp->sflow_probability) {
454                 struct net_bridge_port *p = skb->dev->br_port;
455                 if (p) {
456                         atomic_inc(&p->sflow_pool);
457                         if (dp->sflow_probability == UINT_MAX ||
458                             net_random() < dp->sflow_probability)
459                                 sflow_sample(dp, skb, a, n_actions, gfp, p);
460                 }
461         }
462
463         for (; n_actions > 0; a++, n_actions--) {
464                 WARN_ON_ONCE(skb_shared(skb));
465                 if (prev_port != -1) {
466                         do_output(dp, skb_clone(skb, gfp), prev_port);
467                         prev_port = -1;
468                 }
469
470                 switch (a->type) {
471                 case ODPAT_OUTPUT:
472                         prev_port = a->output.port;
473                         break;
474
475                 case ODPAT_OUTPUT_GROUP:
476                         prev_port = output_group(dp, a->output_group.group,
477                                                  skb, gfp);
478                         break;
479
480                 case ODPAT_CONTROLLER:
481                         err = output_control(dp, skb, a->controller.arg, gfp);
482                         if (err) {
483                                 kfree_skb(skb);
484                                 return err;
485                         }
486                         break;
487
488                 case ODPAT_SET_VLAN_VID:
489                 case ODPAT_SET_VLAN_PCP:
490                         skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
491                         if (IS_ERR(skb))
492                                 return PTR_ERR(skb);
493                         break;
494
495                 case ODPAT_STRIP_VLAN:
496                         skb = strip_vlan(skb, key, gfp);
497                         break;
498
499                 case ODPAT_SET_DL_SRC:
500                 case ODPAT_SET_DL_DST:
501                         skb = set_dl_addr(skb, &a->dl_addr, gfp);
502                         break;
503
504                 case ODPAT_SET_NW_SRC:
505                 case ODPAT_SET_NW_DST:
506                         skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
507                         break;
508
509                 case ODPAT_SET_NW_TOS:
510                         skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
511                         break;
512
513                 case ODPAT_SET_TP_SRC:
514                 case ODPAT_SET_TP_DST:
515                         skb = set_tp_port(skb, key, &a->tp_port, gfp);
516                         break;
517                 }
518                 if (!skb)
519                         return -ENOMEM;
520         }
521         if (prev_port != -1)
522                 do_output(dp, skb, prev_port);
523         else
524                 kfree_skb(skb);
525         return 0;
526 }