2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Functions for executing flow actions. */
11 #include <linux/skbuff.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>
21 #include <net/checksum.h>
25 #include "openvswitch/datapath-protocol.h"
28 static struct sk_buff *make_writable(struct sk_buff *skb, unsigned min_headroom, gfp_t gfp)
30 if (skb_cloned(skb)) {
32 unsigned headroom = max(min_headroom, skb_headroom(skb));
34 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), gfp);
36 set_skb_csum_bits(skb, nskb);
41 unsigned int hdr_len = (skb_transport_offset(skb)
42 + sizeof(struct tcphdr));
43 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
50 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
52 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
55 /* Verify we were given a vlan packet */
56 if (vh->h_vlan_proto != htons(ETH_P_8021Q) || skb->len < VLAN_ETH_HLEN)
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));
63 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
65 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
67 skb->protocol = eh->h_proto;
68 skb->mac_header += VLAN_HLEN;
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,
78 __be16 mask = a->dl_tci.mask;
79 __be16 tci = a->dl_tci.tci;
81 skb = make_writable(skb, VLAN_HLEN, gfp);
83 return ERR_PTR(-ENOMEM);
85 if (skb->protocol == htons(ETH_P_8021Q)) {
86 /* Modify vlan id, but maintain other TCI values */
87 struct vlan_ethhdr *vh;
90 if (skb->len < VLAN_ETH_HLEN)
93 vh = vlan_eth_hdr(skb);
94 old_tci = vh->h_vlan_TCI;
96 vh->h_vlan_TCI = (vh->h_vlan_TCI & ~mask) | tci;
98 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE) {
99 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
101 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
107 /* Add vlan header */
109 /* Set up checksumming pointers for checksum-deferred packets
110 * on Xen. Otherwise, dev_queue_xmit() will try to do this
111 * when we send the packet out on the wire, and it will fail at
112 * that point because skb_checksum_setup() will not look inside
113 * an 802.1Q header. */
114 err = vswitch_skb_checksum_setup(skb);
120 /* GSO is not implemented for packets with an 802.1Q header, so
121 * we have to do segmentation before we add that header.
123 * GSO does work with hardware-accelerated VLAN tagging, but we
124 * can't use hardware-accelerated VLAN tagging since it
125 * requires the device to have a VLAN group configured (with
126 * e.g. vconfig(8)) and we don't do that.
128 * Having to do this here may be a performance loss, since we
129 * can't take advantage of TSO hardware support, although it
130 * does not make a measurable network performance difference
131 * for 1G Ethernet. Fixing that would require patching the
132 * kernel (either to add GSO support to the VLAN protocol or to
133 * support hardware-accelerated VLAN tagging without VLAN
134 * groups configured). */
135 if (skb_is_gso(skb)) {
136 struct sk_buff *segs;
138 segs = skb_gso_segment(skb, 0);
140 if (unlikely(IS_ERR(segs)))
141 return ERR_CAST(segs);
144 struct sk_buff *nskb = segs->next;
149 /* GSO can change the checksum type so update.*/
150 compute_ip_summed(segs, true);
152 segs = __vlan_put_tag(segs, ntohs(tci));
155 err = execute_actions(dp, segs,
162 while ((segs = nskb)) {
171 } while (segs->next);
174 compute_ip_summed(skb, true);
177 /* The hardware-accelerated version of vlan_put_tag() works
178 * only for a device that has a VLAN group configured (with
179 * e.g. vconfig(8)), so call the software-only version
180 * __vlan_put_tag() directly instead.
182 skb = __vlan_put_tag(skb, ntohs(tci));
184 return ERR_PTR(-ENOMEM);
186 /* GSO doesn't fix up the hardware computed checksum so this
187 * will only be hit in the non-GSO case. */
188 if (OVS_CB(skb)->ip_summed == OVS_CSUM_COMPLETE)
189 skb->csum = csum_add(skb->csum, csum_partial(skb->data
190 + ETH_HLEN, VLAN_HLEN, 0));
196 static struct sk_buff *strip_vlan(struct sk_buff *skb, gfp_t gfp)
198 skb = make_writable(skb, 0, gfp);
204 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
205 const struct odp_action_dl_addr *a,
208 skb = make_writable(skb, 0, gfp);
210 struct ethhdr *eh = eth_hdr(skb);
211 if (a->type == ODPAT_SET_DL_SRC)
212 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
214 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
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)
226 __be32 diff[] = { ~from, to };
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),
234 } else if (pseudohdr)
235 *sum = ~csum_fold(csum_partial((char *)diff, sizeof(diff),
239 static bool is_ip(struct sk_buff *skb, const struct odp_flow_key *key)
241 return (key->dl_type == htons(ETH_P_IP) &&
242 skb->transport_header > skb->network_header);
245 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct odp_flow_key *key)
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;
258 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
259 const struct odp_flow_key *key,
260 const struct odp_action_nw_addr *a,
267 if (unlikely(!is_ip(skb, key)))
270 skb = make_writable(skb, 0, gfp);
275 nwaddr = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
277 check = get_l4_checksum(skb, key);
279 update_csum(check, skb, *nwaddr, a->nw_addr, 1);
280 update_csum(&nh->check, skb, *nwaddr, a->nw_addr, 0);
282 *nwaddr = a->nw_addr;
287 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
288 const struct odp_flow_key *key,
289 const struct odp_action_nw_tos *a,
292 if (unlikely(!is_ip(skb, key)))
295 skb = make_writable(skb, 0, gfp);
297 struct iphdr *nh = ip_hdr(skb);
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),
311 static struct sk_buff *set_tp_port(struct sk_buff *skb,
312 const struct odp_flow_key *key,
313 const struct odp_action_tp_port *a, gfp_t gfp)
319 if (unlikely(!is_ip(skb, key)))
322 skb = make_writable(skb, 0, gfp);
326 /* Must follow make_writable() since that can move the skb data. */
327 check = get_l4_checksum(skb, key);
328 if (unlikely(!check))
332 * Update port and checksum.
334 * This is OK because source and destination port numbers are at the
335 * same offsets in both UDP and TCP headers, and get_l4_checksum() only
336 * supports those protocols.
339 port = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
340 update_csum(check, skb, *port, a->tp_port, 0);
347 * is_spoofed_arp - check for invalid ARP packet
349 * @skb: skbuff containing an Ethernet packet, with network header pointing
350 * just past the Ethernet and optional 802.1Q header.
351 * @key: flow key extracted from @skb by flow_extract()
353 * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
354 * or truncated header fields or one whose inner and outer Ethernet address
357 static bool is_spoofed_arp(struct sk_buff *skb, const struct odp_flow_key *key)
359 struct arp_eth_header *arp;
361 if (key->dl_type != htons(ETH_P_ARP))
364 if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
367 arp = (struct arp_eth_header *)skb_network_header(skb);
368 return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
369 arp->ar_pro != htons(ETH_P_IP) ||
370 arp->ar_hln != ETH_ALEN ||
372 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
375 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
382 p = rcu_dereference(dp->ports[out_port]);
386 vport_send(p->vport, skb);
393 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg,
396 skb = skb_clone(skb, gfp);
399 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
402 /* Send a copy of this packet up to the sFlow agent, along with extra
403 * information about what happened to it. */
404 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
405 const union odp_action *a, int n_actions,
406 gfp_t gfp, struct dp_port *dp_port)
408 struct odp_sflow_sample_header *hdr;
409 unsigned int actlen = n_actions * sizeof(union odp_action);
410 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
411 struct sk_buff *nskb;
413 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, gfp);
417 memcpy(__skb_push(nskb, actlen), a, actlen);
418 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
419 hdr->n_actions = n_actions;
420 hdr->sample_pool = atomic_read(&dp_port->sflow_pool);
421 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
424 /* Execute a list of actions against 'skb'. */
425 int execute_actions(struct datapath *dp, struct sk_buff *skb,
426 const struct odp_flow_key *key,
427 const union odp_action *a, int n_actions,
430 /* Every output action needs a separate clone of 'skb', but the common
431 * case is just a single output action, so that doing a clone and
432 * then freeing the original skbuff is wasteful. So the following code
433 * is slightly obscure just to avoid that. */
435 u32 priority = skb->priority;
438 if (dp->sflow_probability) {
439 struct dp_port *p = OVS_CB(skb)->dp_port;
441 atomic_inc(&p->sflow_pool);
442 if (dp->sflow_probability == UINT_MAX ||
443 net_random() < dp->sflow_probability)
444 sflow_sample(dp, skb, a, n_actions, gfp, p);
448 OVS_CB(skb)->tun_id = 0;
450 for (; n_actions > 0; a++, n_actions--) {
451 if (prev_port != -1) {
452 do_output(dp, skb_clone(skb, gfp), prev_port);
458 prev_port = a->output.port;
461 case ODPAT_CONTROLLER:
462 err = output_control(dp, skb, a->controller.arg, gfp);
469 case ODPAT_SET_TUNNEL:
470 OVS_CB(skb)->tun_id = a->tunnel.tun_id;
473 case ODPAT_SET_DL_TCI:
474 skb = modify_vlan_tci(dp, skb, key, a, n_actions, gfp);
479 case ODPAT_STRIP_VLAN:
480 skb = strip_vlan(skb, gfp);
483 case ODPAT_SET_DL_SRC:
484 case ODPAT_SET_DL_DST:
485 skb = set_dl_addr(skb, &a->dl_addr, gfp);
488 case ODPAT_SET_NW_SRC:
489 case ODPAT_SET_NW_DST:
490 skb = set_nw_addr(skb, key, &a->nw_addr, gfp);
493 case ODPAT_SET_NW_TOS:
494 skb = set_nw_tos(skb, key, &a->nw_tos, gfp);
497 case ODPAT_SET_TP_SRC:
498 case ODPAT_SET_TP_DST:
499 skb = set_tp_port(skb, key, &a->tp_port, gfp);
502 case ODPAT_SET_PRIORITY:
503 skb->priority = a->priority.priority;
506 case ODPAT_POP_PRIORITY:
507 skb->priority = priority;
510 case ODPAT_DROP_SPOOFED_ARP:
511 if (unlikely(is_spoofed_arp(skb, key)))
520 do_output(dp, skb, prev_port);