2 * Copyright (c) 2007-2013 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 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/if_tunnel.h>
24 #include <linux/if_vlan.h>
25 #include <linux/icmp.h>
28 #include <linux/ipv6.h>
29 #include <linux/kernel.h>
30 #include <linux/kmod.h>
31 #include <linux/netdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
38 #include <net/protocol.h>
39 #include <net/route.h>
44 #ifdef OVS_USE_COMPAT_GSO_SEGMENTATION
45 static bool dev_supports_vlan_tx(struct net_device *dev)
47 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
49 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
50 return dev->features & NETIF_F_HW_VLAN_TX;
52 /* Assume that the driver is buggy. */
57 /* Strictly this is not needed and will be optimised out
58 * as this code is guarded by if LINUX_VERSION_CODE < KERNEL_VERSION(3,19,0).
59 * It is here to make things explicit should the compatibility
60 * code be extended in some way prior extending its life-span
63 static bool supports_mpls_gso(void)
65 /* MPLS GSO was introduced in v3.11, however it was not correctly
66 * activated using mpls_features until v3.19. */
67 #ifdef OVS_USE_COMPAT_GSO_SEGMENTATION
74 int rpl_dev_queue_xmit(struct sk_buff *skb)
82 /* Avoid traversing any VLAN tags that are present to determine if
83 * the ethtype is MPLS. Instead compare the mac_len (end of L2) and
84 * skb_network_offset() (beginning of L3) whose inequality will
85 * indicate the presence of an MPLS label stack. */
86 if (skb->mac_len != skb_network_offset(skb) && !supports_mpls_gso())
89 if (skb_vlan_tag_present(skb) && !dev_supports_vlan_tx(skb->dev))
95 features = netif_skb_features(skb);
98 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
99 skb_vlan_tag_get(skb));
105 /* As of v3.11 the kernel provides an mpls_features field in
106 * struct net_device which allows devices to advertise which
107 * features its supports for MPLS. This value defaults to
108 * NETIF_F_SG and as of v3.19.
110 * This compatibility code is intended for kernels older
111 * than v3.19 that do not support MPLS GSO and do not
112 * use mpls_features. Thus this code uses NETIF_F_SG
113 * directly in place of mpls_features.
116 features &= NETIF_F_SG;
118 if (netif_needs_gso(skb, features)) {
119 struct sk_buff *nskb;
121 nskb = skb_gso_segment(skb, features);
123 if (unlikely(skb_cloned(skb) &&
124 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
127 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
141 err = dev_queue_xmit(skb);
149 return dev_queue_xmit(skb);
155 EXPORT_SYMBOL_GPL(rpl_dev_queue_xmit);
156 #endif /* OVS_USE_COMPAT_GSO_SEGMENTATION */
158 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
159 static __be16 __skb_network_protocol(struct sk_buff *skb)
161 __be16 type = skb->protocol;
162 int vlan_depth = ETH_HLEN;
164 while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
167 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
170 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
171 type = vh->h_vlan_encapsulated_proto;
172 vlan_depth += VLAN_HLEN;
175 if (eth_p_mpls(type))
176 type = ovs_skb_get_inner_protocol(skb);
181 static struct sk_buff *tnl_skb_gso_segment(struct sk_buff *skb,
182 netdev_features_t features,
184 sa_family_t sa_family)
186 void *iph = skb_network_header(skb);
187 int pkt_hlen = skb_inner_network_offset(skb); /* inner l2 + tunnel hdr. */
188 int mac_offset = skb_inner_mac_offset(skb);
189 int outer_l3_offset = skb_network_offset(skb);
190 int outer_l4_offset = skb_transport_offset(skb);
191 struct sk_buff *skb1 = skb;
192 struct dst_entry *dst = skb_dst(skb);
193 struct sk_buff *segs;
194 __be16 proto = skb->protocol;
195 char cb[sizeof(skb->cb)];
197 OVS_GSO_CB(skb)->ipv6 = (sa_family == AF_INET6);
198 /* setup whole inner packet to get protocol. */
199 __skb_pull(skb, mac_offset);
200 skb->protocol = __skb_network_protocol(skb);
202 /* setup l3 packet to gso, to get around segmentation bug on older kernel.*/
203 __skb_pull(skb, (pkt_hlen - mac_offset));
204 skb_reset_mac_header(skb);
205 skb_reset_network_header(skb);
206 skb_reset_transport_header(skb);
208 /* From 3.9 kernel skb->cb is used by skb gso. Therefore
209 * make copy of it to restore it back. */
210 memcpy(cb, skb->cb, sizeof(cb));
212 /* We are handling offloads by segmenting l3 packet, so
213 * no need to call OVS compat segmentation function. */
215 #ifdef HAVE___SKB_GSO_SEGMENT
216 #undef __skb_gso_segment
217 segs = __skb_gso_segment(skb, 0, tx_path);
219 #undef skb_gso_segment
220 segs = skb_gso_segment(skb, 0);
223 if (!segs || IS_ERR(segs))
228 __skb_push(skb, pkt_hlen);
229 skb_reset_mac_header(skb);
230 skb_set_network_header(skb, outer_l3_offset);
231 skb_set_transport_header(skb, outer_l4_offset);
234 memcpy(skb_network_header(skb), iph, pkt_hlen);
235 memcpy(skb->cb, cb, sizeof(cb));
237 skb->protocol = proto;
239 dst = dst_clone(dst);
241 skb_dst_set(skb, dst);
242 OVS_GSO_CB(skb)->fix_segment(skb);
251 static int output_ip(struct sk_buff *skb)
253 int ret = NETDEV_TX_OK;
256 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
259 err = ip_local_out(skb);
260 if (unlikely(net_xmit_eval(err)))
266 int rpl_ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
268 int ret = NETDEV_TX_OK;
271 if (!OVS_GSO_CB(skb)->fix_segment)
272 return output_ip(skb);
274 if (skb_is_gso(skb)) {
279 skb = tnl_skb_gso_segment(skb, 0, false, AF_INET);
280 if (!skb || IS_ERR(skb))
282 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
285 err = skb_checksum_help(skb);
291 struct sk_buff *next_skb = skb->next;
298 iph->id = htons(id++);
300 ret = output_ip(skb);
305 EXPORT_SYMBOL_GPL(rpl_ip_local_out);
307 static int output_ipv6(struct sk_buff *skb)
309 int ret = NETDEV_TX_OK;
312 memset(IP6CB(skb), 0, sizeof (*IP6CB(skb)));
314 err = ip6_local_out(skb);
315 if (unlikely(net_xmit_eval(err)))
321 int rpl_ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
323 int ret = NETDEV_TX_OK;
325 if (!OVS_GSO_CB(skb)->fix_segment)
326 return output_ipv6(skb);
328 if (skb_is_gso(skb)) {
329 skb = tnl_skb_gso_segment(skb, 0, false, AF_INET6);
330 if (!skb || IS_ERR(skb))
332 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
335 err = skb_checksum_help(skb);
341 struct sk_buff *next_skb = skb->next;
344 ret = output_ipv6(skb);
349 EXPORT_SYMBOL_GPL(rpl_ip6_local_out);