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/kconfig.h>
20 #if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
22 #include <linux/module.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/icmp.h>
28 #include <linux/kernel.h>
29 #include <linux/kmod.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
36 #include <net/protocol.h>
37 #include <net/route.h>
42 #ifndef HAVE_GRE_CISCO_REGISTER
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
46 #define GREPROTO_CISCO 0
47 #define GREPROTO_MAX 1
50 int (*handler)(struct sk_buff *skb);
52 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
54 static int gre_rcv(struct sk_buff *skb)
56 const struct gre_protocol *proto;
60 if (!pskb_may_pull(skb, 12))
63 ver = skb->data[1] & 0x7f;
64 if (ver >= GREPROTO_MAX)
68 proto = rcu_dereference(gre_proto[ver]);
69 if (!proto || !proto->handler)
71 ret = proto->handler(skb);
82 static const struct net_protocol net_gre_protocol = {
87 static int gre_add_protocol(const struct gre_protocol *proto, u8 version)
89 if (version >= GREPROTO_MAX)
92 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
93 pr_err("%s: cannot register gre protocol handler\n", __func__);
97 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
101 static int gre_del_protocol(const struct gre_protocol *proto, u8 version)
105 if (version >= GREPROTO_MAX)
108 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
116 ret = inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
125 static __sum16 check_checksum(struct sk_buff *skb)
129 switch (skb->ip_summed) {
130 case CHECKSUM_COMPLETE:
131 csum = csum_fold(skb->csum);
139 csum = __skb_checksum_complete(skb);
140 skb->ip_summed = CHECKSUM_COMPLETE;
147 static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
150 unsigned int ip_hlen = ip_hdrlen(skb);
151 struct gre_base_hdr *greh;
155 if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr))))
158 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
159 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
162 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
163 hdr_len = ip_gre_calc_hlen(tpi->flags);
165 if (!pskb_may_pull(skb, hdr_len))
168 greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen);
169 tpi->proto = greh->protocol;
171 options = (__be32 *)(greh + 1);
172 if (greh->flags & GRE_CSUM) {
173 if (check_checksum(skb)) {
180 if (greh->flags & GRE_KEY) {
186 if (unlikely(greh->flags & GRE_SEQ)) {
192 /* WCCP version 1 and 2 protocol decoding.
193 * - Change protocol to IP
194 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
196 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
197 tpi->proto = htons(ETH_P_IP);
198 if ((*(u8 *)options & 0xF0) != 0x40) {
200 if (!pskb_may_pull(skb, hdr_len))
205 return iptunnel_pull_header(skb, hdr_len, tpi->proto);
208 static struct gre_cisco_protocol __rcu *gre_cisco_proto;
209 static int gre_cisco_rcv(struct sk_buff *skb)
211 struct tnl_ptk_info tpi;
212 bool csum_err = false;
213 struct gre_cisco_protocol *proto;
216 proto = rcu_dereference(gre_cisco_proto);
220 if (parse_gre_header(skb, &tpi, &csum_err) < 0)
222 proto->handler(skb, &tpi);
232 static const struct gre_protocol ipgre_protocol = {
233 .handler = gre_cisco_rcv,
236 int gre_cisco_register(struct gre_cisco_protocol *newp)
240 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
242 pr_warn("%s: cannot register gre_cisco protocol handler\n", __func__);
247 return (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, NULL, newp) == NULL) ?
251 int gre_cisco_unregister(struct gre_cisco_protocol *proto)
255 ret = (cmpxchg((struct gre_cisco_protocol **)&gre_cisco_proto, proto, NULL) == proto) ?
262 ret = gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
266 #endif /* !HAVE_GRE_CISCO_REGISTER */
269 static void gre_csum_fix(struct sk_buff *skb)
271 struct gre_base_hdr *greh;
273 int gre_offset = skb_transport_offset(skb);
275 greh = (struct gre_base_hdr *)skb_transport_header(skb);
276 options = ((__be32 *)greh + 1);
279 *(__sum16 *)options = csum_fold(skb_checksum(skb, gre_offset,
280 skb->len - gre_offset, 0));
283 struct sk_buff *gre_handle_offloads(struct sk_buff *skb, bool gre_csum)
287 skb_reset_inner_headers(skb);
289 if (skb_is_gso(skb)) {
291 OVS_GSO_CB(skb)->fix_segment = gre_csum_fix;
293 if (skb->ip_summed == CHECKSUM_PARTIAL && gre_csum) {
294 err = skb_checksum_help(skb);
298 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
299 skb->ip_summed = CHECKSUM_NONE;
307 static bool is_gre_gso(struct sk_buff *skb)
309 return skb_is_gso(skb);
312 void gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
315 struct gre_base_hdr *greh;
317 __skb_push(skb, hdr_len);
319 greh = (struct gre_base_hdr *)skb->data;
320 greh->flags = tnl_flags_to_gre_flags(tpi->flags);
321 greh->protocol = tpi->proto;
323 if (tpi->flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) {
324 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
326 if (tpi->flags & TUNNEL_SEQ) {
330 if (tpi->flags & TUNNEL_KEY) {
334 if (tpi->flags & TUNNEL_CSUM && !is_gre_gso(skb)) {
336 *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
342 #endif /* CONFIG_NET_IPGRE_DEMUX */