1c50cd357SDaniel Borkmann /* 2c50cd357SDaniel Borkmann * GRE over IPv4 demultiplexer driver 3c50cd357SDaniel Borkmann * 4c50cd357SDaniel Borkmann * Authors: Dmitry Kozlov (xeb@mail.ru) 5c50cd357SDaniel Borkmann * 6c50cd357SDaniel Borkmann * This program is free software; you can redistribute it and/or 7c50cd357SDaniel Borkmann * modify it under the terms of the GNU General Public License 8c50cd357SDaniel Borkmann * as published by the Free Software Foundation; either version 9c50cd357SDaniel Borkmann * 2 of the License, or (at your option) any later version. 10c50cd357SDaniel Borkmann * 11c50cd357SDaniel Borkmann */ 12c50cd357SDaniel Borkmann 13c50cd357SDaniel Borkmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14c50cd357SDaniel Borkmann 15c50cd357SDaniel Borkmann #include <linux/module.h> 16c50cd357SDaniel Borkmann #include <linux/if.h> 17c50cd357SDaniel Borkmann #include <linux/icmp.h> 18c50cd357SDaniel Borkmann #include <linux/kernel.h> 19c50cd357SDaniel Borkmann #include <linux/kmod.h> 20c50cd357SDaniel Borkmann #include <linux/skbuff.h> 21c50cd357SDaniel Borkmann #include <linux/in.h> 22c50cd357SDaniel Borkmann #include <linux/ip.h> 23c50cd357SDaniel Borkmann #include <linux/netdevice.h> 24c50cd357SDaniel Borkmann #include <linux/if_tunnel.h> 25c50cd357SDaniel Borkmann #include <linux/spinlock.h> 26c50cd357SDaniel Borkmann #include <net/protocol.h> 27c50cd357SDaniel Borkmann #include <net/gre.h> 28c50cd357SDaniel Borkmann 29c50cd357SDaniel Borkmann #include <net/icmp.h> 30c50cd357SDaniel Borkmann #include <net/route.h> 31c50cd357SDaniel Borkmann #include <net/xfrm.h> 32c50cd357SDaniel Borkmann 33c50cd357SDaniel Borkmann static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly; 34c50cd357SDaniel Borkmann 35c50cd357SDaniel Borkmann int gre_add_protocol(const struct gre_protocol *proto, u8 version) 36c50cd357SDaniel Borkmann { 37c50cd357SDaniel Borkmann if (version >= GREPROTO_MAX) 38c50cd357SDaniel Borkmann return -EINVAL; 39c50cd357SDaniel Borkmann 40c50cd357SDaniel Borkmann return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ? 41c50cd357SDaniel Borkmann 0 : -EBUSY; 42c50cd357SDaniel Borkmann } 43c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_add_protocol); 44c50cd357SDaniel Borkmann 45c50cd357SDaniel Borkmann int gre_del_protocol(const struct gre_protocol *proto, u8 version) 46c50cd357SDaniel Borkmann { 47c50cd357SDaniel Borkmann int ret; 48c50cd357SDaniel Borkmann 49c50cd357SDaniel Borkmann if (version >= GREPROTO_MAX) 50c50cd357SDaniel Borkmann return -EINVAL; 51c50cd357SDaniel Borkmann 52c50cd357SDaniel Borkmann ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ? 53c50cd357SDaniel Borkmann 0 : -EBUSY; 54c50cd357SDaniel Borkmann 55c50cd357SDaniel Borkmann if (ret) 56c50cd357SDaniel Borkmann return ret; 57c50cd357SDaniel Borkmann 58c50cd357SDaniel Borkmann synchronize_rcu(); 59c50cd357SDaniel Borkmann return 0; 60c50cd357SDaniel Borkmann } 61c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_del_protocol); 62c50cd357SDaniel Borkmann 63f132ae7cSJiri Benc /* Fills in tpi and returns header length to be pulled. */ 6495f5c64cSTom Herbert int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, 65da73b4e9SHaishuang Yan bool *csum_err, __be16 proto) 6695f5c64cSTom Herbert { 6795f5c64cSTom Herbert const struct gre_base_hdr *greh; 6895f5c64cSTom Herbert __be32 *options; 6995f5c64cSTom Herbert int hdr_len; 7095f5c64cSTom Herbert 7195f5c64cSTom Herbert if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr)))) 7295f5c64cSTom Herbert return -EINVAL; 7395f5c64cSTom Herbert 7495f5c64cSTom Herbert greh = (struct gre_base_hdr *)skb_transport_header(skb); 7595f5c64cSTom Herbert if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) 7695f5c64cSTom Herbert return -EINVAL; 7795f5c64cSTom Herbert 7895f5c64cSTom Herbert tpi->flags = gre_flags_to_tnl_flags(greh->flags); 7995f5c64cSTom Herbert hdr_len = gre_calc_hlen(tpi->flags); 8095f5c64cSTom Herbert 8195f5c64cSTom Herbert if (!pskb_may_pull(skb, hdr_len)) 8295f5c64cSTom Herbert return -EINVAL; 8395f5c64cSTom Herbert 8495f5c64cSTom Herbert greh = (struct gre_base_hdr *)skb_transport_header(skb); 8595f5c64cSTom Herbert tpi->proto = greh->protocol; 8695f5c64cSTom Herbert 8795f5c64cSTom Herbert options = (__be32 *)(greh + 1); 8895f5c64cSTom Herbert if (greh->flags & GRE_CSUM) { 8995f5c64cSTom Herbert if (skb_checksum_simple_validate(skb)) { 9095f5c64cSTom Herbert *csum_err = true; 9195f5c64cSTom Herbert return -EINVAL; 9295f5c64cSTom Herbert } 9395f5c64cSTom Herbert 9495f5c64cSTom Herbert skb_checksum_try_convert(skb, IPPROTO_GRE, 0, 9595f5c64cSTom Herbert null_compute_pseudo); 9695f5c64cSTom Herbert options++; 9795f5c64cSTom Herbert } 9895f5c64cSTom Herbert 9995f5c64cSTom Herbert if (greh->flags & GRE_KEY) { 10095f5c64cSTom Herbert tpi->key = *options; 10195f5c64cSTom Herbert options++; 10295f5c64cSTom Herbert } else { 10395f5c64cSTom Herbert tpi->key = 0; 10495f5c64cSTom Herbert } 10595f5c64cSTom Herbert if (unlikely(greh->flags & GRE_SEQ)) { 10695f5c64cSTom Herbert tpi->seq = *options; 10795f5c64cSTom Herbert options++; 10895f5c64cSTom Herbert } else { 10995f5c64cSTom Herbert tpi->seq = 0; 11095f5c64cSTom Herbert } 11195f5c64cSTom Herbert /* WCCP version 1 and 2 protocol decoding. 112da73b4e9SHaishuang Yan * - Change protocol to IPv4/IPv6 11395f5c64cSTom Herbert * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header 11495f5c64cSTom Herbert */ 11595f5c64cSTom Herbert if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { 116da73b4e9SHaishuang Yan tpi->proto = proto; 11700b20340SJiri Benc if ((*(u8 *)options & 0xF0) != 0x40) 11895f5c64cSTom Herbert hdr_len += 4; 11995f5c64cSTom Herbert } 120*9b8c6d7bSEric Dumazet tpi->hdr_len = hdr_len; 121f132ae7cSJiri Benc return hdr_len; 12295f5c64cSTom Herbert } 12395f5c64cSTom Herbert EXPORT_SYMBOL(gre_parse_header); 12495f5c64cSTom Herbert 125c50cd357SDaniel Borkmann static int gre_rcv(struct sk_buff *skb) 126c50cd357SDaniel Borkmann { 127c50cd357SDaniel Borkmann const struct gre_protocol *proto; 128c50cd357SDaniel Borkmann u8 ver; 129c50cd357SDaniel Borkmann int ret; 130c50cd357SDaniel Borkmann 131c50cd357SDaniel Borkmann if (!pskb_may_pull(skb, 12)) 132c50cd357SDaniel Borkmann goto drop; 133c50cd357SDaniel Borkmann 134c50cd357SDaniel Borkmann ver = skb->data[1]&0x7f; 135c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 136c50cd357SDaniel Borkmann goto drop; 137c50cd357SDaniel Borkmann 138c50cd357SDaniel Borkmann rcu_read_lock(); 139c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 140c50cd357SDaniel Borkmann if (!proto || !proto->handler) 141c50cd357SDaniel Borkmann goto drop_unlock; 142c50cd357SDaniel Borkmann ret = proto->handler(skb); 143c50cd357SDaniel Borkmann rcu_read_unlock(); 144c50cd357SDaniel Borkmann return ret; 145c50cd357SDaniel Borkmann 146c50cd357SDaniel Borkmann drop_unlock: 147c50cd357SDaniel Borkmann rcu_read_unlock(); 148c50cd357SDaniel Borkmann drop: 149c50cd357SDaniel Borkmann kfree_skb(skb); 150c50cd357SDaniel Borkmann return NET_RX_DROP; 151c50cd357SDaniel Borkmann } 152c50cd357SDaniel Borkmann 153c50cd357SDaniel Borkmann static void gre_err(struct sk_buff *skb, u32 info) 154c50cd357SDaniel Borkmann { 155c50cd357SDaniel Borkmann const struct gre_protocol *proto; 156c50cd357SDaniel Borkmann const struct iphdr *iph = (const struct iphdr *)skb->data; 157c50cd357SDaniel Borkmann u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f; 158c50cd357SDaniel Borkmann 159c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 160c50cd357SDaniel Borkmann return; 161c50cd357SDaniel Borkmann 162c50cd357SDaniel Borkmann rcu_read_lock(); 163c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 164c50cd357SDaniel Borkmann if (proto && proto->err_handler) 165c50cd357SDaniel Borkmann proto->err_handler(skb, info); 166c50cd357SDaniel Borkmann rcu_read_unlock(); 167c50cd357SDaniel Borkmann } 168c50cd357SDaniel Borkmann 169c50cd357SDaniel Borkmann static const struct net_protocol net_gre_protocol = { 170c50cd357SDaniel Borkmann .handler = gre_rcv, 171c50cd357SDaniel Borkmann .err_handler = gre_err, 172c50cd357SDaniel Borkmann .netns_ok = 1, 173c50cd357SDaniel Borkmann }; 174c50cd357SDaniel Borkmann 175c50cd357SDaniel Borkmann static int __init gre_init(void) 176c50cd357SDaniel Borkmann { 177c50cd357SDaniel Borkmann pr_info("GRE over IPv4 demultiplexor driver\n"); 178c50cd357SDaniel Borkmann 179c50cd357SDaniel Borkmann if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) { 180c50cd357SDaniel Borkmann pr_err("can't add protocol\n"); 181c50cd357SDaniel Borkmann return -EAGAIN; 182c50cd357SDaniel Borkmann } 1839f57c67cSPravin B Shelar return 0; 1849f57c67cSPravin B Shelar } 185c50cd357SDaniel Borkmann 186c50cd357SDaniel Borkmann static void __exit gre_exit(void) 187c50cd357SDaniel Borkmann { 188c50cd357SDaniel Borkmann inet_del_protocol(&net_gre_protocol, IPPROTO_GRE); 189c50cd357SDaniel Borkmann } 190c50cd357SDaniel Borkmann 191c50cd357SDaniel Borkmann module_init(gre_init); 192c50cd357SDaniel Borkmann module_exit(gre_exit); 193c50cd357SDaniel Borkmann 194c50cd357SDaniel Borkmann MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver"); 195c50cd357SDaniel Borkmann MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); 196c50cd357SDaniel Borkmann MODULE_LICENSE("GPL"); 197