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>
26 #include "openvswitch/datapath-protocol.h"
29 static struct sk_buff *make_writable(struct sk_buff *skb, unsigned min_headroom)
31 if (skb_cloned(skb)) {
33 unsigned headroom = max(min_headroom, skb_headroom(skb));
35 nskb = skb_copy_expand(skb, headroom, skb_tailroom(skb), GFP_ATOMIC);
37 set_skb_csum_bits(skb, nskb);
42 unsigned int hdr_len = (skb_transport_offset(skb)
43 + sizeof(struct tcphdr));
44 if (pskb_may_pull(skb, min(hdr_len, skb->len)))
51 static struct sk_buff *vlan_pull_tag(struct sk_buff *skb)
53 struct vlan_ethhdr *vh = vlan_eth_hdr(skb);
56 /* Verify we were given a vlan packet */
57 if (vh->h_vlan_proto != htons(ETH_P_8021Q) || skb->len < VLAN_ETH_HLEN)
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));
64 memmove(skb->data + VLAN_HLEN, skb->data, 2 * VLAN_ETH_ALEN);
66 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
68 skb->protocol = eh->h_proto;
69 skb->mac_header += VLAN_HLEN;
74 static struct sk_buff *modify_vlan_tci(struct datapath *dp, struct sk_buff *skb,
75 const struct odp_flow_key *key,
76 const union odp_action *a, int n_actions)
78 __be16 tci = a->dl_tci.tci;
80 skb = make_writable(skb, VLAN_HLEN);
82 return ERR_PTR(-ENOMEM);
84 if (skb->protocol == htons(ETH_P_8021Q)) {
85 /* Modify vlan id, but maintain other TCI values */
86 struct vlan_ethhdr *vh;
89 if (skb->len < VLAN_ETH_HLEN)
92 vh = vlan_eth_hdr(skb);
93 old_tci = vh->h_vlan_TCI;
97 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE) {
98 __be16 diff[] = { ~old_tci, vh->h_vlan_TCI };
100 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
106 /* Add vlan header */
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);
119 /* GSO is not implemented for packets with an 802.1Q header, so
120 * we have to do segmentation before we add that header.
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.
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 struct sk_buff *segs;
137 segs = skb_gso_segment(skb, 0);
139 if (unlikely(IS_ERR(segs)))
140 return ERR_CAST(segs);
143 struct sk_buff *nskb = segs->next;
148 /* GSO can change the checksum type so update.*/
149 compute_ip_summed(segs, true);
151 segs = __vlan_put_tag(segs, ntohs(tci));
154 err = execute_actions(dp, segs,
160 while ((segs = nskb)) {
169 } while (segs->next);
172 compute_ip_summed(skb, true);
175 /* The hardware-accelerated version of vlan_put_tag() works
176 * only for a device that has a VLAN group configured (with
177 * e.g. vconfig(8)), so call the software-only version
178 * __vlan_put_tag() directly instead.
180 skb = __vlan_put_tag(skb, ntohs(tci));
182 return ERR_PTR(-ENOMEM);
184 /* GSO doesn't fix up the hardware computed checksum so this
185 * will only be hit in the non-GSO case. */
186 if (get_ip_summed(skb) == OVS_CSUM_COMPLETE)
187 skb->csum = csum_add(skb->csum, csum_partial(skb->data
188 + ETH_HLEN, VLAN_HLEN, 0));
194 static struct sk_buff *strip_vlan(struct sk_buff *skb)
196 skb = make_writable(skb, 0);
202 static struct sk_buff *set_dl_addr(struct sk_buff *skb,
203 const struct odp_action_dl_addr *a)
205 skb = make_writable(skb, 0);
207 struct ethhdr *eh = eth_hdr(skb);
208 if (a->type == ODPAT_SET_DL_SRC)
209 memcpy(eh->h_source, a->dl_addr, ETH_ALEN);
211 memcpy(eh->h_dest, a->dl_addr, ETH_ALEN);
216 static bool is_ip(struct sk_buff *skb, const struct odp_flow_key *key)
218 return (key->dl_type == htons(ETH_P_IP) &&
219 skb->transport_header > skb->network_header);
222 static __sum16 *get_l4_checksum(struct sk_buff *skb, const struct odp_flow_key *key)
224 int transport_len = skb->len - skb_transport_offset(skb);
225 if (key->nw_proto == IPPROTO_TCP) {
226 if (likely(transport_len >= sizeof(struct tcphdr)))
227 return &tcp_hdr(skb)->check;
228 } else if (key->nw_proto == IPPROTO_UDP) {
229 if (likely(transport_len >= sizeof(struct udphdr)))
230 return &udp_hdr(skb)->check;
235 static struct sk_buff *set_nw_addr(struct sk_buff *skb,
236 const struct odp_flow_key *key,
237 const struct odp_action_nw_addr *a)
243 if (unlikely(!is_ip(skb, key)))
246 skb = make_writable(skb, 0);
251 nwaddr = a->type == ODPAT_SET_NW_SRC ? &nh->saddr : &nh->daddr;
253 check = get_l4_checksum(skb, key);
255 inet_proto_csum_replace4(check, skb, *nwaddr, a->nw_addr, 1);
256 csum_replace4(&nh->check, *nwaddr, a->nw_addr);
258 *nwaddr = a->nw_addr;
263 static struct sk_buff *set_nw_tos(struct sk_buff *skb,
264 const struct odp_flow_key *key,
265 const struct odp_action_nw_tos *a)
267 if (unlikely(!is_ip(skb, key)))
270 skb = make_writable(skb, 0);
272 struct iphdr *nh = ip_hdr(skb);
277 /* Set the DSCP bits and preserve the ECN bits. */
278 new = a->nw_tos | (nh->tos & INET_ECN_MASK);
279 csum_replace4(&nh->check, (__force __be32)old,
280 (__force __be32)new);
286 static struct sk_buff *set_tp_port(struct sk_buff *skb,
287 const struct odp_flow_key *key,
288 const struct odp_action_tp_port *a)
294 if (unlikely(!is_ip(skb, key)))
297 skb = make_writable(skb, 0);
301 /* Must follow make_writable() since that can move the skb data. */
302 check = get_l4_checksum(skb, key);
303 if (unlikely(!check))
307 * Update port and checksum.
309 * This is OK because source and destination port numbers are at the
310 * same offsets in both UDP and TCP headers, and get_l4_checksum() only
311 * supports those protocols.
314 port = a->type == ODPAT_SET_TP_SRC ? &th->source : &th->dest;
315 inet_proto_csum_replace2(check, skb, *port, a->tp_port, 0);
322 * is_spoofed_arp - check for invalid ARP packet
324 * @skb: skbuff containing an Ethernet packet, with network header pointing
325 * just past the Ethernet and optional 802.1Q header.
326 * @key: flow key extracted from @skb by flow_extract()
328 * Returns true if @skb is an invalid Ethernet+IPv4 ARP packet: one with screwy
329 * or truncated header fields or one whose inner and outer Ethernet address
332 static bool is_spoofed_arp(struct sk_buff *skb, const struct odp_flow_key *key)
334 struct arp_eth_header *arp;
336 if (key->dl_type != htons(ETH_P_ARP))
339 if (skb_network_offset(skb) + sizeof(struct arp_eth_header) > skb->len)
342 arp = (struct arp_eth_header *)skb_network_header(skb);
343 return (arp->ar_hrd != htons(ARPHRD_ETHER) ||
344 arp->ar_pro != htons(ETH_P_IP) ||
345 arp->ar_hln != ETH_ALEN ||
347 compare_ether_addr(arp->ar_sha, eth_hdr(skb)->h_source));
350 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
357 p = rcu_dereference(dp->ports[out_port]);
368 static int output_control(struct datapath *dp, struct sk_buff *skb, u32 arg)
370 skb = skb_clone(skb, GFP_ATOMIC);
373 return dp_output_control(dp, skb, _ODPL_ACTION_NR, arg);
376 /* Send a copy of this packet up to the sFlow agent, along with extra
377 * information about what happened to it. */
378 static void sflow_sample(struct datapath *dp, struct sk_buff *skb,
379 const union odp_action *a, int n_actions,
382 struct odp_sflow_sample_header *hdr;
383 unsigned int actlen = n_actions * sizeof(union odp_action);
384 unsigned int hdrlen = sizeof(struct odp_sflow_sample_header);
385 struct sk_buff *nskb;
387 nskb = skb_copy_expand(skb, actlen + hdrlen, 0, GFP_ATOMIC);
391 memcpy(__skb_push(nskb, actlen), a, actlen);
392 hdr = (struct odp_sflow_sample_header*)__skb_push(nskb, hdrlen);
393 hdr->n_actions = n_actions;
394 hdr->sample_pool = atomic_read(&vport->sflow_pool);
395 dp_output_control(dp, nskb, _ODPL_SFLOW_NR, 0);
398 /* Execute a list of actions against 'skb'. */
399 int execute_actions(struct datapath *dp, struct sk_buff *skb,
400 const struct odp_flow_key *key,
401 const union odp_action *a, int n_actions)
403 /* Every output action needs a separate clone of 'skb', but the common
404 * case is just a single output action, so that doing a clone and
405 * then freeing the original skbuff is wasteful. So the following code
406 * is slightly obscure just to avoid that. */
408 u32 priority = skb->priority;
411 if (dp->sflow_probability) {
412 struct vport *p = OVS_CB(skb)->vport;
414 atomic_inc(&p->sflow_pool);
415 if (dp->sflow_probability == UINT_MAX ||
416 net_random() < dp->sflow_probability)
417 sflow_sample(dp, skb, a, n_actions, p);
421 OVS_CB(skb)->tun_id = 0;
423 for (; n_actions > 0; a++, n_actions--) {
424 if (prev_port != -1) {
425 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
431 prev_port = a->output.port;
434 case ODPAT_CONTROLLER:
435 err = output_control(dp, skb, a->controller.arg);
442 case ODPAT_SET_TUNNEL:
443 OVS_CB(skb)->tun_id = a->tunnel.tun_id;
446 case ODPAT_SET_DL_TCI:
447 skb = modify_vlan_tci(dp, skb, key, a, n_actions);
452 case ODPAT_STRIP_VLAN:
453 skb = strip_vlan(skb);
456 case ODPAT_SET_DL_SRC:
457 case ODPAT_SET_DL_DST:
458 skb = set_dl_addr(skb, &a->dl_addr);
461 case ODPAT_SET_NW_SRC:
462 case ODPAT_SET_NW_DST:
463 skb = set_nw_addr(skb, key, &a->nw_addr);
466 case ODPAT_SET_NW_TOS:
467 skb = set_nw_tos(skb, key, &a->nw_tos);
470 case ODPAT_SET_TP_SRC:
471 case ODPAT_SET_TP_DST:
472 skb = set_tp_port(skb, key, &a->tp_port);
475 case ODPAT_SET_PRIORITY:
476 skb->priority = a->priority.priority;
479 case ODPAT_POP_PRIORITY:
480 skb->priority = priority;
483 case ODPAT_DROP_SPOOFED_ARP:
484 if (unlikely(is_spoofed_arp(skb, key)))
493 do_output(dp, skb, prev_port);