2 * Copyright (c) 2007-2012 Nicira Networks.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/skbuff.h>
24 #include <linux/openvswitch.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/in6.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_vlan.h>
31 #include <net/checksum.h>
32 #include <net/dsfield.h>
37 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
38 const struct nlattr *attr, int len, bool keep_skb);
40 static int make_writable(struct sk_buff *skb, int write_len)
42 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
45 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
48 /* remove VLAN header from packet and update csum accrodingly. */
49 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
51 struct vlan_hdr *vhdr;
54 err = make_writable(skb, VLAN_ETH_HLEN);
58 if (skb->ip_summed == CHECKSUM_COMPLETE)
59 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
60 + ETH_HLEN, VLAN_HLEN, 0));
62 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
63 *current_tci = vhdr->h_vlan_TCI;
65 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
66 __skb_pull(skb, VLAN_HLEN);
68 vlan_set_encap_proto(skb, vhdr);
69 skb->mac_header += VLAN_HLEN;
70 skb_reset_mac_len(skb);
75 static int pop_vlan(struct sk_buff *skb)
80 if (likely(vlan_tx_tag_present(skb))) {
83 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
84 skb->len < VLAN_ETH_HLEN))
87 err = __pop_vlan_tci(skb, &tci);
91 /* move next vlan tag to hw accel tag */
92 if (likely(skb->protocol != htons(ETH_P_8021Q) ||
93 skb->len < VLAN_ETH_HLEN))
96 err = __pop_vlan_tci(skb, &tci);
100 __vlan_hwaccel_put_tag(skb, ntohs(tci));
104 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
106 if (unlikely(vlan_tx_tag_present(skb))) {
109 /* push down current VLAN tag */
110 current_tag = vlan_tx_tag_get(skb);
112 if (!__vlan_put_tag(skb, current_tag))
115 if (skb->ip_summed == CHECKSUM_COMPLETE)
116 skb->csum = csum_add(skb->csum, csum_partial(skb->data
117 + ETH_HLEN, VLAN_HLEN, 0));
120 __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
124 static int set_eth_addr(struct sk_buff *skb,
125 const struct ovs_key_ethernet *eth_key)
128 err = make_writable(skb, ETH_HLEN);
132 memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN);
133 memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN);
138 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
139 __be32 *addr, __be32 new_addr)
141 int transport_len = skb->len - skb_transport_offset(skb);
143 if (nh->protocol == IPPROTO_TCP) {
144 if (likely(transport_len >= sizeof(struct tcphdr)))
145 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
147 } else if (nh->protocol == IPPROTO_UDP) {
148 if (likely(transport_len >= sizeof(struct udphdr))) {
149 struct udphdr *uh = udp_hdr(skb);
151 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
152 inet_proto_csum_replace4(&uh->check, skb,
155 uh->check = CSUM_MANGLED_0;
160 csum_replace4(&nh->check, *addr, new_addr);
165 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
167 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
171 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
176 err = make_writable(skb, skb_network_offset(skb) +
177 sizeof(struct iphdr));
183 if (ipv4_key->ipv4_src != nh->saddr)
184 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
186 if (ipv4_key->ipv4_dst != nh->daddr)
187 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
189 if (ipv4_key->ipv4_tos != nh->tos)
190 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
192 if (ipv4_key->ipv4_ttl != nh->ttl)
193 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
198 /* Must follow make_writable() since that can move the skb data. */
199 static void set_tp_port(struct sk_buff *skb, __be16 *port,
200 __be16 new_port, __sum16 *check)
202 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
207 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
209 struct udphdr *uh = udp_hdr(skb);
211 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
212 set_tp_port(skb, port, new_port, &uh->check);
215 uh->check = CSUM_MANGLED_0;
222 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
227 err = make_writable(skb, skb_transport_offset(skb) +
228 sizeof(struct udphdr));
233 if (udp_port_key->udp_src != uh->source)
234 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
236 if (udp_port_key->udp_dst != uh->dest)
237 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
242 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
247 err = make_writable(skb, skb_transport_offset(skb) +
248 sizeof(struct tcphdr));
253 if (tcp_port_key->tcp_src != th->source)
254 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
256 if (tcp_port_key->tcp_dst != th->dest)
257 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
262 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
269 vport = rcu_dereference(dp->ports[out_port]);
270 if (unlikely(!vport)) {
275 ovs_vport_send(vport, skb);
279 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
280 const struct nlattr *attr)
282 struct dp_upcall_info upcall;
283 const struct nlattr *a;
286 upcall.cmd = OVS_PACKET_CMD_ACTION;
287 upcall.key = &OVS_CB(skb)->flow->key;
288 upcall.userdata = NULL;
291 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
292 a = nla_next(a, &rem)) {
293 switch (nla_type(a)) {
294 case OVS_USERSPACE_ATTR_USERDATA:
298 case OVS_USERSPACE_ATTR_PID:
299 upcall.pid = nla_get_u32(a);
304 return ovs_dp_upcall(dp, skb, &upcall);
307 static int sample(struct datapath *dp, struct sk_buff *skb,
308 const struct nlattr *attr)
310 const struct nlattr *acts_list = NULL;
311 const struct nlattr *a;
314 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
315 a = nla_next(a, &rem)) {
316 switch (nla_type(a)) {
317 case OVS_SAMPLE_ATTR_PROBABILITY:
318 if (net_random() >= nla_get_u32(a))
322 case OVS_SAMPLE_ATTR_ACTIONS:
328 return do_execute_actions(dp, skb, nla_data(acts_list),
329 nla_len(acts_list), true);
332 static int execute_set_action(struct sk_buff *skb,
333 const struct nlattr *nested_attr)
337 switch (nla_type(nested_attr)) {
338 case OVS_KEY_ATTR_PRIORITY:
339 skb->priority = nla_get_u32(nested_attr);
342 case OVS_KEY_ATTR_ETHERNET:
343 err = set_eth_addr(skb, nla_data(nested_attr));
346 case OVS_KEY_ATTR_IPV4:
347 err = set_ipv4(skb, nla_data(nested_attr));
350 case OVS_KEY_ATTR_TCP:
351 err = set_tcp(skb, nla_data(nested_attr));
354 case OVS_KEY_ATTR_UDP:
355 err = set_udp(skb, nla_data(nested_attr));
362 /* Execute a list of actions against 'skb'. */
363 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
364 const struct nlattr *attr, int len, bool keep_skb)
366 /* Every output action needs a separate clone of 'skb', but the common
367 * case is just a single output action, so that doing a clone and
368 * then freeing the original skbuff is wasteful. So the following code
369 * is slightly obscure just to avoid that. */
371 const struct nlattr *a;
374 for (a = attr, rem = len; rem > 0;
375 a = nla_next(a, &rem)) {
378 if (prev_port != -1) {
379 do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
383 switch (nla_type(a)) {
384 case OVS_ACTION_ATTR_OUTPUT:
385 prev_port = nla_get_u32(a);
388 case OVS_ACTION_ATTR_USERSPACE:
389 output_userspace(dp, skb, a);
392 case OVS_ACTION_ATTR_PUSH_VLAN:
393 err = push_vlan(skb, nla_data(a));
394 if (unlikely(err)) /* skb already freed. */
398 case OVS_ACTION_ATTR_POP_VLAN:
402 case OVS_ACTION_ATTR_SET:
403 err = execute_set_action(skb, nla_data(a));
406 case OVS_ACTION_ATTR_SAMPLE:
407 err = sample(dp, skb, a);
417 if (prev_port != -1) {
419 skb = skb_clone(skb, GFP_ATOMIC);
421 do_output(dp, skb, prev_port);
422 } else if (!keep_skb)
428 /* Execute a list of actions against 'skb'. */
429 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb)
431 struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
433 return do_execute_actions(dp, skb, acts->actions,
434 acts->actions_len, false);