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>
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,12,0)
22 #include <linux/kconfig.h>
23 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
25 #include <linux/module.h>
27 #include <linux/if_tunnel.h>
28 #include <linux/icmp.h>
31 #include <linux/kernel.h>
32 #include <linux/kmod.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/spinlock.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
45 #ifndef HAVE_GRE_CISCO_REGISTER
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
49 #define GREPROTO_CISCO 0
50 #define GREPROTO_MAX 1
53 int (*handler)(struct sk_buff *skb);
55 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
57 static int gre_rcv(struct sk_buff *skb)
59 const struct gre_protocol *proto;
63 if (!pskb_may_pull(skb, 12))
66 ver = skb->data[1] & 0x7f;
67 if (ver >= GREPROTO_MAX)
71 proto = rcu_dereference(gre_proto[ver]);
72 if (!proto || !proto->handler)
74 ret = proto->handler(skb);
85 static const struct net_protocol net_gre_protocol = {
90 static int gre_add_protocol(const struct gre_protocol *proto, u8 version)
92 if (version >= GREPROTO_MAX)
95 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
96 pr_err("%s: cannot register gre protocol handler\n", __func__);
100 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
104 static int gre_del_protocol(const struct gre_protocol *proto, u8 version)
108 if (version >= GREPROTO_MAX)
111 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
119 ret = inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
128 static __sum16 check_checksum(struct sk_buff *skb)
132 switch (skb->ip_summed) {
133 case CHECKSUM_COMPLETE:
134 csum = csum_fold(skb->csum);
142 csum = __skb_checksum_complete(skb);
143 skb->ip_summed = CHECKSUM_COMPLETE;
150 static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
153 unsigned int ip_hlen = ip_hdrlen(skb);
154 struct gre_base_hdr *greh;
158 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
161 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
162 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
165 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
166 hdr_len = ip_gre_calc_hlen(tpi->flags);
168 if (!pskb_may_pull(skb, hdr_len))
171 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
172 tpi->proto = greh->protocol;
174 options = (__be32 *)(greh + 1);
175 if (greh->flags & GRE_CSUM) {
176 if (check_checksum(skb)) {
183 if (greh->flags & GRE_KEY) {
189 if (unlikely(greh->flags & GRE_SEQ)) {
195 /* WCCP version 1 and 2 protocol decoding.
196 * - Change protocol to IP
197 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
199 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
200 tpi->proto = htons(ETH_P_IP);
201 if ((*(u8 *)options & 0xF0) != 0x40) {
203 if (!pskb_may_pull(skb, hdr_len))
208 return iptunnel_pull_header(skb, hdr_len, tpi->proto);
211 static struct gre_cisco_protocol __rcu *gre_cisco_proto;
212 static int gre_cisco_rcv(struct sk_buff *skb)
214 struct tnl_ptk_info tpi;
215 bool csum_err = false;
216 struct gre_cisco_protocol *proto;
219 proto = rcu_dereference(gre_cisco_proto);
223 if (parse_gre_header(skb, &tpi, &csum_err) < 0)
225 proto->handler(skb, &tpi);
235 static const struct gre_protocol ipgre_protocol = {
236 .handler = gre_cisco_rcv,
239 int gre_cisco_register(struct gre_cisco_protocol *newp)
243 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
245 pr_warn("%s: cannot register gre_cisco protocol handler\n", __func__);
250 return (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, NULL, newp) == NULL) ?
254 int gre_cisco_unregister(struct gre_cisco_protocol *proto)
258 ret = (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, proto, NULL) == proto) ?
265 ret = gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
269 #endif /* !HAVE_GRE_CISCO_REGISTER */
271 #ifndef GRE_USE_KERNEL_GRE_HANDLE_OFFLOADS
274 static void gre_csum_fix(struct sk_buff *skb)
276 struct gre_base_hdr *greh;
278 int gre_offset = skb_transport_offset(skb);
280 greh = (struct gre_base_hdr *)skb_transport_header(skb);
281 options = ((__be32 *)greh + 1);
284 *(__sum16 *)options = csum_fold(skb_checksum(skb, gre_offset,
285 skb->len - gre_offset, 0));
288 struct sk_buff *gre_handle_offloads(struct sk_buff *skb, bool gre_csum)
292 skb_reset_inner_headers(skb);
294 if (skb_is_gso(skb)) {
296 OVS_GSO_CB(skb)->fix_segment = gre_csum_fix;
298 OVS_GSO_CB(skb)->fix_segment = NULL;
300 if (skb->ip_summed == CHECKSUM_PARTIAL && gre_csum) {
301 err = skb_checksum_help(skb);
305 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
306 skb->ip_summed = CHECKSUM_NONE;
314 static bool is_gre_gso(struct sk_buff *skb)
316 return skb_is_gso(skb);
319 void gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
322 struct gre_base_hdr *greh;
324 __skb_push(skb, hdr_len);
326 greh = (struct gre_base_hdr *)skb->data;
327 greh->flags = tnl_flags_to_gre_flags(tpi->flags);
328 greh->protocol = tpi->proto;
330 if (tpi->flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) {
331 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
333 if (tpi->flags & TUNNEL_SEQ) {
337 if (tpi->flags & TUNNEL_KEY) {
341 if (tpi->flags & TUNNEL_CSUM && !is_gre_gso(skb)) {
343 *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
350 #endif /* CONFIG_NET_IPGRE_DEMUX */