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 #include <linux/kconfig.h>
21 #include <linux/module.h>
23 #include <linux/if_tunnel.h>
24 #include <linux/icmp.h>
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/netdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
35 #include <net/protocol.h>
36 #include <net/route.h>
41 #ifndef HAVE_METADATA_DST
42 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
44 #ifndef HAVE_GRE_HANDLE_OFFLOADS
46 #ifndef HAVE_GRE_CISCO_REGISTER
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
50 #define GREPROTO_CISCO 0
51 #define GREPROTO_MAX 1
54 int (*handler)(struct sk_buff *skb);
56 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
58 static int gre_rcv(struct sk_buff *skb)
60 const struct gre_protocol *proto;
64 if (!pskb_may_pull(skb, 12))
67 ver = skb->data[1] & 0x7f;
68 if (ver >= GREPROTO_MAX)
72 proto = rcu_dereference(gre_proto[ver]);
73 if (!proto || !proto->handler)
75 ret = proto->handler(skb);
86 static const struct net_protocol net_gre_protocol = {
91 static int gre_add_protocol(const struct gre_protocol *proto, u8 version)
93 if (version >= GREPROTO_MAX)
96 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
97 pr_err("%s: cannot register gre protocol handler\n", __func__);
101 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
105 static int gre_del_protocol(const struct gre_protocol *proto, u8 version)
109 if (version >= GREPROTO_MAX)
112 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
120 ret = inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
129 static __sum16 check_checksum(struct sk_buff *skb)
133 switch (skb->ip_summed) {
134 case CHECKSUM_COMPLETE:
135 csum = csum_fold(skb->csum);
143 csum = __skb_checksum_complete(skb);
144 skb->ip_summed = CHECKSUM_COMPLETE;
151 #define ip_gre_calc_hlen rpl_ip_gre_calc_hlen
152 static int ip_gre_calc_hlen(__be16 o_flags)
156 if (o_flags & TUNNEL_CSUM)
158 if (o_flags & TUNNEL_KEY)
160 if (o_flags & TUNNEL_SEQ)
165 #define gre_flags_to_tnl_flags rpl_gre_flags_to_tnl_flags
166 static __be16 gre_flags_to_tnl_flags(__be16 flags)
170 if (flags & GRE_CSUM)
171 tflags |= TUNNEL_CSUM;
172 if (flags & GRE_ROUTING)
173 tflags |= TUNNEL_ROUTING;
175 tflags |= TUNNEL_KEY;
177 tflags |= TUNNEL_SEQ;
178 if (flags & GRE_STRICT)
179 tflags |= TUNNEL_STRICT;
181 tflags |= TUNNEL_REC;
182 if (flags & GRE_VERSION)
183 tflags |= TUNNEL_VERSION;
188 static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
191 unsigned int ip_hlen = ip_hdrlen(skb);
192 struct gre_base_hdr *greh;
196 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
199 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
200 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
203 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
204 hdr_len = ip_gre_calc_hlen(tpi->flags);
206 if (!pskb_may_pull(skb, hdr_len))
209 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
210 tpi->proto = greh->protocol;
212 options = (__be32 *)(greh + 1);
213 if (greh->flags & GRE_CSUM) {
214 if (check_checksum(skb)) {
221 if (greh->flags & GRE_KEY) {
227 if (unlikely(greh->flags & GRE_SEQ)) {
233 /* WCCP version 1 and 2 protocol decoding.
234 * - Change protocol to IP
235 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
237 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
238 tpi->proto = htons(ETH_P_IP);
239 if ((*(u8 *)options & 0xF0) != 0x40) {
241 if (!pskb_may_pull(skb, hdr_len))
246 return iptunnel_pull_header(skb, hdr_len, tpi->proto);
249 static struct gre_cisco_protocol __rcu *gre_cisco_proto;
250 static int gre_cisco_rcv(struct sk_buff *skb)
252 struct tnl_ptk_info tpi;
253 bool csum_err = false;
254 struct gre_cisco_protocol *proto;
257 proto = rcu_dereference(gre_cisco_proto);
261 if (parse_gre_header(skb, &tpi, &csum_err) < 0)
263 proto->handler(skb, &tpi);
273 static const struct gre_protocol ipgre_protocol = {
274 .handler = gre_cisco_rcv,
277 int rpl_gre_cisco_register(struct gre_cisco_protocol *newp)
281 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
283 pr_warn("%s: cannot register gre_cisco protocol handler\n", __func__);
288 return (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, NULL, newp) == NULL) ?
291 EXPORT_SYMBOL_GPL(rpl_gre_cisco_register);
293 int rpl_gre_cisco_unregister(struct gre_cisco_protocol *proto)
297 ret = (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, proto, NULL) == proto) ?
304 ret = gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
307 EXPORT_SYMBOL_GPL(rpl_gre_cisco_unregister);
309 #endif /* !HAVE_GRE_CISCO_REGISTER */
312 #endif /* CONFIG_NET_IPGRE_DEMUX */
313 #endif /* HAVE_METADATA_DST */