2 * Copyright (c) 2007-2012 Nicira, Inc.
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>
32 #include <net/checksum.h>
33 #include <net/dsfield.h>
38 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
39 const struct nlattr
*attr
, int len
, bool keep_skb
);
41 static int make_writable(struct sk_buff
*skb
, int write_len
)
43 if (!pskb_may_pull(skb
, write_len
))
46 if (!skb_cloned(skb
) || skb_clone_writable(skb
, write_len
))
49 return pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
52 /* remove VLAN header from packet and update csum accordingly. */
53 static int __pop_vlan_tci(struct sk_buff
*skb
, __be16
*current_tci
)
55 struct vlan_hdr
*vhdr
;
58 err
= make_writable(skb
, VLAN_ETH_HLEN
);
62 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
63 skb
->csum
= csum_sub(skb
->csum
, csum_partial(skb
->data
64 + (2 * ETH_ALEN
), VLAN_HLEN
, 0));
66 vhdr
= (struct vlan_hdr
*)(skb
->data
+ ETH_HLEN
);
67 *current_tci
= vhdr
->h_vlan_TCI
;
69 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
70 __skb_pull(skb
, VLAN_HLEN
);
72 vlan_set_encap_proto(skb
, vhdr
);
73 skb
->mac_header
+= VLAN_HLEN
;
74 if (skb_network_offset(skb
) < ETH_HLEN
)
75 skb_set_network_header(skb
, ETH_HLEN
);
76 skb_reset_mac_len(skb
);
81 static int pop_vlan(struct sk_buff
*skb
)
86 if (likely(vlan_tx_tag_present(skb
))) {
89 if (unlikely(skb
->protocol
!= htons(ETH_P_8021Q
) ||
90 skb
->len
< VLAN_ETH_HLEN
))
93 err
= __pop_vlan_tci(skb
, &tci
);
97 /* move next vlan tag to hw accel tag */
98 if (likely(skb
->protocol
!= htons(ETH_P_8021Q
) ||
99 skb
->len
< VLAN_ETH_HLEN
))
102 err
= __pop_vlan_tci(skb
, &tci
);
106 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), ntohs(tci
));
110 static int push_vlan(struct sk_buff
*skb
, const struct ovs_action_push_vlan
*vlan
)
112 if (unlikely(vlan_tx_tag_present(skb
))) {
115 /* push down current VLAN tag */
116 current_tag
= vlan_tx_tag_get(skb
);
118 if (!__vlan_put_tag(skb
, skb
->vlan_proto
, current_tag
))
121 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
122 skb
->csum
= csum_add(skb
->csum
, csum_partial(skb
->data
123 + (2 * ETH_ALEN
), VLAN_HLEN
, 0));
126 __vlan_hwaccel_put_tag(skb
, vlan
->vlan_tpid
, ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
130 static int set_eth_addr(struct sk_buff
*skb
,
131 const struct ovs_key_ethernet
*eth_key
)
134 err
= make_writable(skb
, ETH_HLEN
);
138 memcpy(eth_hdr(skb
)->h_source
, eth_key
->eth_src
, ETH_ALEN
);
139 memcpy(eth_hdr(skb
)->h_dest
, eth_key
->eth_dst
, ETH_ALEN
);
144 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
145 __be32
*addr
, __be32 new_addr
)
147 int transport_len
= skb
->len
- skb_transport_offset(skb
);
149 if (nh
->protocol
== IPPROTO_TCP
) {
150 if (likely(transport_len
>= sizeof(struct tcphdr
)))
151 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
153 } else if (nh
->protocol
== IPPROTO_UDP
) {
154 if (likely(transport_len
>= sizeof(struct udphdr
))) {
155 struct udphdr
*uh
= udp_hdr(skb
);
157 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
158 inet_proto_csum_replace4(&uh
->check
, skb
,
161 uh
->check
= CSUM_MANGLED_0
;
166 csum_replace4(&nh
->check
, *addr
, new_addr
);
171 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
172 __be32 addr
[4], const __be32 new_addr
[4])
174 int transport_len
= skb
->len
- skb_transport_offset(skb
);
176 if (l4_proto
== IPPROTO_TCP
) {
177 if (likely(transport_len
>= sizeof(struct tcphdr
)))
178 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
180 } else if (l4_proto
== IPPROTO_UDP
) {
181 if (likely(transport_len
>= sizeof(struct udphdr
))) {
182 struct udphdr
*uh
= udp_hdr(skb
);
184 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
185 inet_proto_csum_replace16(&uh
->check
, skb
,
188 uh
->check
= CSUM_MANGLED_0
;
194 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
195 __be32 addr
[4], const __be32 new_addr
[4],
196 bool recalculate_csum
)
198 if (recalculate_csum
)
199 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
202 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
205 static void set_ipv6_tc(struct ipv6hdr
*nh
, u8 tc
)
207 nh
->priority
= tc
>> 4;
208 nh
->flow_lbl
[0] = (nh
->flow_lbl
[0] & 0x0F) | ((tc
& 0x0F) << 4);
211 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
)
213 nh
->flow_lbl
[0] = (nh
->flow_lbl
[0] & 0xF0) | (fl
& 0x000F0000) >> 16;
214 nh
->flow_lbl
[1] = (fl
& 0x0000FF00) >> 8;
215 nh
->flow_lbl
[2] = fl
& 0x000000FF;
218 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
)
220 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
224 static int set_ipv4(struct sk_buff
*skb
, const struct ovs_key_ipv4
*ipv4_key
)
229 err
= make_writable(skb
, skb_network_offset(skb
) +
230 sizeof(struct iphdr
));
236 if (ipv4_key
->ipv4_src
!= nh
->saddr
)
237 set_ip_addr(skb
, nh
, &nh
->saddr
, ipv4_key
->ipv4_src
);
239 if (ipv4_key
->ipv4_dst
!= nh
->daddr
)
240 set_ip_addr(skb
, nh
, &nh
->daddr
, ipv4_key
->ipv4_dst
);
242 if (ipv4_key
->ipv4_tos
!= nh
->tos
)
243 ipv4_change_dsfield(nh
, 0, ipv4_key
->ipv4_tos
);
245 if (ipv4_key
->ipv4_ttl
!= nh
->ttl
)
246 set_ip_ttl(skb
, nh
, ipv4_key
->ipv4_ttl
);
251 static int set_ipv6(struct sk_buff
*skb
, const struct ovs_key_ipv6
*ipv6_key
)
258 err
= make_writable(skb
, skb_network_offset(skb
) +
259 sizeof(struct ipv6hdr
));
264 saddr
= (__be32
*)&nh
->saddr
;
265 daddr
= (__be32
*)&nh
->daddr
;
267 if (memcmp(ipv6_key
->ipv6_src
, saddr
, sizeof(ipv6_key
->ipv6_src
)))
268 set_ipv6_addr(skb
, ipv6_key
->ipv6_proto
, saddr
,
269 ipv6_key
->ipv6_src
, true);
271 if (memcmp(ipv6_key
->ipv6_dst
, daddr
, sizeof(ipv6_key
->ipv6_dst
))) {
272 unsigned int offset
= 0;
273 int flags
= IP6_FH_F_SKIP_RH
;
274 bool recalc_csum
= true;
276 if (ipv6_ext_hdr(nh
->nexthdr
))
277 recalc_csum
= ipv6_find_hdr(skb
, &offset
,
278 NEXTHDR_ROUTING
, NULL
,
279 &flags
) != NEXTHDR_ROUTING
;
281 set_ipv6_addr(skb
, ipv6_key
->ipv6_proto
, daddr
,
282 ipv6_key
->ipv6_dst
, recalc_csum
);
285 set_ipv6_tc(nh
, ipv6_key
->ipv6_tclass
);
286 set_ipv6_fl(nh
, ntohl(ipv6_key
->ipv6_label
));
287 nh
->hop_limit
= ipv6_key
->ipv6_hlimit
;
292 /* Must follow make_writable() since that can move the skb data. */
293 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
294 __be16 new_port
, __sum16
*check
)
296 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, 0);
301 static void set_udp_port(struct sk_buff
*skb
, __be16
*port
, __be16 new_port
)
303 struct udphdr
*uh
= udp_hdr(skb
);
305 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
306 set_tp_port(skb
, port
, new_port
, &uh
->check
);
309 uh
->check
= CSUM_MANGLED_0
;
316 static int set_udp(struct sk_buff
*skb
, const struct ovs_key_udp
*udp_port_key
)
321 err
= make_writable(skb
, skb_transport_offset(skb
) +
322 sizeof(struct udphdr
));
327 if (udp_port_key
->udp_src
!= uh
->source
)
328 set_udp_port(skb
, &uh
->source
, udp_port_key
->udp_src
);
330 if (udp_port_key
->udp_dst
!= uh
->dest
)
331 set_udp_port(skb
, &uh
->dest
, udp_port_key
->udp_dst
);
336 static int set_tcp(struct sk_buff
*skb
, const struct ovs_key_tcp
*tcp_port_key
)
341 err
= make_writable(skb
, skb_transport_offset(skb
) +
342 sizeof(struct tcphdr
));
347 if (tcp_port_key
->tcp_src
!= th
->source
)
348 set_tp_port(skb
, &th
->source
, tcp_port_key
->tcp_src
, &th
->check
);
350 if (tcp_port_key
->tcp_dst
!= th
->dest
)
351 set_tp_port(skb
, &th
->dest
, tcp_port_key
->tcp_dst
, &th
->check
);
356 static int do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
)
363 vport
= ovs_vport_rcu(dp
, out_port
);
364 if (unlikely(!vport
)) {
369 ovs_vport_send(vport
, skb
);
373 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
374 const struct nlattr
*attr
)
376 struct dp_upcall_info upcall
;
377 const struct nlattr
*a
;
380 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
381 upcall
.key
= &OVS_CB(skb
)->flow
->key
;
382 upcall
.userdata
= NULL
;
385 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
386 a
= nla_next(a
, &rem
)) {
387 switch (nla_type(a
)) {
388 case OVS_USERSPACE_ATTR_USERDATA
:
392 case OVS_USERSPACE_ATTR_PID
:
393 upcall
.portid
= nla_get_u32(a
);
398 return ovs_dp_upcall(dp
, skb
, &upcall
);
401 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
402 const struct nlattr
*attr
)
404 const struct nlattr
*acts_list
= NULL
;
405 const struct nlattr
*a
;
408 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
409 a
= nla_next(a
, &rem
)) {
410 switch (nla_type(a
)) {
411 case OVS_SAMPLE_ATTR_PROBABILITY
:
412 if (net_random() >= nla_get_u32(a
))
416 case OVS_SAMPLE_ATTR_ACTIONS
:
422 return do_execute_actions(dp
, skb
, nla_data(acts_list
),
423 nla_len(acts_list
), true);
426 static int execute_set_action(struct sk_buff
*skb
,
427 const struct nlattr
*nested_attr
)
431 switch (nla_type(nested_attr
)) {
432 case OVS_KEY_ATTR_PRIORITY
:
433 skb
->priority
= nla_get_u32(nested_attr
);
436 case OVS_KEY_ATTR_SKB_MARK
:
437 skb
->mark
= nla_get_u32(nested_attr
);
440 case OVS_KEY_ATTR_ETHERNET
:
441 err
= set_eth_addr(skb
, nla_data(nested_attr
));
444 case OVS_KEY_ATTR_IPV4
:
445 err
= set_ipv4(skb
, nla_data(nested_attr
));
448 case OVS_KEY_ATTR_IPV6
:
449 err
= set_ipv6(skb
, nla_data(nested_attr
));
452 case OVS_KEY_ATTR_TCP
:
453 err
= set_tcp(skb
, nla_data(nested_attr
));
456 case OVS_KEY_ATTR_UDP
:
457 err
= set_udp(skb
, nla_data(nested_attr
));
464 /* Execute a list of actions against 'skb'. */
465 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
466 const struct nlattr
*attr
, int len
, bool keep_skb
)
468 /* Every output action needs a separate clone of 'skb', but the common
469 * case is just a single output action, so that doing a clone and
470 * then freeing the original skbuff is wasteful. So the following code
471 * is slightly obscure just to avoid that. */
473 const struct nlattr
*a
;
476 for (a
= attr
, rem
= len
; rem
> 0;
477 a
= nla_next(a
, &rem
)) {
480 if (prev_port
!= -1) {
481 do_output(dp
, skb_clone(skb
, GFP_ATOMIC
), prev_port
);
485 switch (nla_type(a
)) {
486 case OVS_ACTION_ATTR_OUTPUT
:
487 prev_port
= nla_get_u32(a
);
490 case OVS_ACTION_ATTR_USERSPACE
:
491 output_userspace(dp
, skb
, a
);
494 case OVS_ACTION_ATTR_PUSH_VLAN
:
495 err
= push_vlan(skb
, nla_data(a
));
496 if (unlikely(err
)) /* skb already freed. */
500 case OVS_ACTION_ATTR_POP_VLAN
:
504 case OVS_ACTION_ATTR_SET
:
505 err
= execute_set_action(skb
, nla_data(a
));
508 case OVS_ACTION_ATTR_SAMPLE
:
509 err
= sample(dp
, skb
, a
);
519 if (prev_port
!= -1) {
521 skb
= skb_clone(skb
, GFP_ATOMIC
);
523 do_output(dp
, skb
, prev_port
);
524 } else if (!keep_skb
)
530 /* Execute a list of actions against 'skb'. */
531 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
)
533 struct sw_flow_actions
*acts
= rcu_dereference(OVS_CB(skb
)->flow
->sf_acts
);
535 return do_execute_actions(dp
, skb
, acts
->actions
,
536 acts
->actions_len
, false);