1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c50cd357SDaniel Borkmann /* 3c50cd357SDaniel Borkmann * GRE over IPv4 demultiplexer driver 4c50cd357SDaniel Borkmann * 5c50cd357SDaniel Borkmann * Authors: Dmitry Kozlov (xeb@mail.ru) 6c50cd357SDaniel Borkmann */ 7c50cd357SDaniel Borkmann 8c50cd357SDaniel Borkmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9c50cd357SDaniel Borkmann 10c50cd357SDaniel Borkmann #include <linux/module.h> 11c50cd357SDaniel Borkmann #include <linux/if.h> 12c50cd357SDaniel Borkmann #include <linux/icmp.h> 13c50cd357SDaniel Borkmann #include <linux/kernel.h> 14c50cd357SDaniel Borkmann #include <linux/kmod.h> 15c50cd357SDaniel Borkmann #include <linux/skbuff.h> 16c50cd357SDaniel Borkmann #include <linux/in.h> 17c50cd357SDaniel Borkmann #include <linux/ip.h> 18c50cd357SDaniel Borkmann #include <linux/netdevice.h> 19c50cd357SDaniel Borkmann #include <linux/if_tunnel.h> 20c50cd357SDaniel Borkmann #include <linux/spinlock.h> 21c50cd357SDaniel Borkmann #include <net/protocol.h> 22c50cd357SDaniel Borkmann #include <net/gre.h> 23cb73ee40SLorenzo Bianconi #include <net/erspan.h> 24c50cd357SDaniel Borkmann 25c50cd357SDaniel Borkmann #include <net/icmp.h> 26c50cd357SDaniel Borkmann #include <net/route.h> 27c50cd357SDaniel Borkmann #include <net/xfrm.h> 28c50cd357SDaniel Borkmann 29c50cd357SDaniel Borkmann static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly; 30c50cd357SDaniel Borkmann 31c50cd357SDaniel Borkmann int gre_add_protocol(const struct gre_protocol *proto, u8 version) 32c50cd357SDaniel Borkmann { 33c50cd357SDaniel Borkmann if (version >= GREPROTO_MAX) 34c50cd357SDaniel Borkmann return -EINVAL; 35c50cd357SDaniel Borkmann 36c50cd357SDaniel Borkmann return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ? 37c50cd357SDaniel Borkmann 0 : -EBUSY; 38c50cd357SDaniel Borkmann } 39c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_add_protocol); 40c50cd357SDaniel Borkmann 41c50cd357SDaniel Borkmann int gre_del_protocol(const struct gre_protocol *proto, u8 version) 42c50cd357SDaniel Borkmann { 43c50cd357SDaniel Borkmann int ret; 44c50cd357SDaniel Borkmann 45c50cd357SDaniel Borkmann if (version >= GREPROTO_MAX) 46c50cd357SDaniel Borkmann return -EINVAL; 47c50cd357SDaniel Borkmann 48c50cd357SDaniel Borkmann ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ? 49c50cd357SDaniel Borkmann 0 : -EBUSY; 50c50cd357SDaniel Borkmann 51c50cd357SDaniel Borkmann if (ret) 52c50cd357SDaniel Borkmann return ret; 53c50cd357SDaniel Borkmann 54c50cd357SDaniel Borkmann synchronize_rcu(); 55c50cd357SDaniel Borkmann return 0; 56c50cd357SDaniel Borkmann } 57c50cd357SDaniel Borkmann EXPORT_SYMBOL_GPL(gre_del_protocol); 58c50cd357SDaniel Borkmann 59f132ae7cSJiri Benc /* Fills in tpi and returns header length to be pulled. */ 6095f5c64cSTom Herbert int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, 61e582615aSEric Dumazet bool *csum_err, __be16 proto, int nhs) 6295f5c64cSTom Herbert { 6395f5c64cSTom Herbert const struct gre_base_hdr *greh; 6495f5c64cSTom Herbert __be32 *options; 6595f5c64cSTom Herbert int hdr_len; 6695f5c64cSTom Herbert 67e582615aSEric Dumazet if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr)))) 6895f5c64cSTom Herbert return -EINVAL; 6995f5c64cSTom Herbert 70e582615aSEric Dumazet greh = (struct gre_base_hdr *)(skb->data + nhs); 7195f5c64cSTom Herbert if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) 7295f5c64cSTom Herbert return -EINVAL; 7395f5c64cSTom Herbert 7495f5c64cSTom Herbert tpi->flags = gre_flags_to_tnl_flags(greh->flags); 7595f5c64cSTom Herbert hdr_len = gre_calc_hlen(tpi->flags); 7695f5c64cSTom Herbert 77e582615aSEric Dumazet if (!pskb_may_pull(skb, nhs + hdr_len)) 7895f5c64cSTom Herbert return -EINVAL; 7995f5c64cSTom Herbert 80e582615aSEric Dumazet greh = (struct gre_base_hdr *)(skb->data + nhs); 8195f5c64cSTom Herbert tpi->proto = greh->protocol; 8295f5c64cSTom Herbert 8395f5c64cSTom Herbert options = (__be32 *)(greh + 1); 8495f5c64cSTom Herbert if (greh->flags & GRE_CSUM) { 85b0350d51SHaishuang Yan if (!skb_checksum_simple_validate(skb)) { 86b0350d51SHaishuang Yan skb_checksum_try_convert(skb, IPPROTO_GRE, 0, 87b0350d51SHaishuang Yan null_compute_pseudo); 88b0350d51SHaishuang Yan } else if (csum_err) { 8995f5c64cSTom Herbert *csum_err = true; 9095f5c64cSTom Herbert return -EINVAL; 9195f5c64cSTom Herbert } 9295f5c64cSTom Herbert 9395f5c64cSTom Herbert options++; 9495f5c64cSTom Herbert } 9595f5c64cSTom Herbert 9695f5c64cSTom Herbert if (greh->flags & GRE_KEY) { 9795f5c64cSTom Herbert tpi->key = *options; 9895f5c64cSTom Herbert options++; 9995f5c64cSTom Herbert } else { 10095f5c64cSTom Herbert tpi->key = 0; 10195f5c64cSTom Herbert } 10295f5c64cSTom Herbert if (unlikely(greh->flags & GRE_SEQ)) { 10395f5c64cSTom Herbert tpi->seq = *options; 10495f5c64cSTom Herbert options++; 10595f5c64cSTom Herbert } else { 10695f5c64cSTom Herbert tpi->seq = 0; 10795f5c64cSTom Herbert } 10895f5c64cSTom Herbert /* WCCP version 1 and 2 protocol decoding. 109da73b4e9SHaishuang Yan * - Change protocol to IPv4/IPv6 11095f5c64cSTom Herbert * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header 11195f5c64cSTom Herbert */ 11295f5c64cSTom Herbert if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { 113da73b4e9SHaishuang Yan tpi->proto = proto; 11400b20340SJiri Benc if ((*(u8 *)options & 0xF0) != 0x40) 11595f5c64cSTom Herbert hdr_len += 4; 11695f5c64cSTom Herbert } 1179b8c6d7bSEric Dumazet tpi->hdr_len = hdr_len; 118cb73ee40SLorenzo Bianconi 119cb73ee40SLorenzo Bianconi /* ERSPAN ver 1 and 2 protocol sets GRE key field 120cb73ee40SLorenzo Bianconi * to 0 and sets the configured key in the 121cb73ee40SLorenzo Bianconi * inner erspan header field 122cb73ee40SLorenzo Bianconi */ 123cb73ee40SLorenzo Bianconi if (greh->protocol == htons(ETH_P_ERSPAN) || 124cb73ee40SLorenzo Bianconi greh->protocol == htons(ETH_P_ERSPAN2)) { 125cb73ee40SLorenzo Bianconi struct erspan_base_hdr *ershdr; 126cb73ee40SLorenzo Bianconi 127cb73ee40SLorenzo Bianconi if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr))) 128cb73ee40SLorenzo Bianconi return -EINVAL; 129cb73ee40SLorenzo Bianconi 130cb73ee40SLorenzo Bianconi ershdr = (struct erspan_base_hdr *)options; 131cb73ee40SLorenzo Bianconi tpi->key = cpu_to_be32(get_session_id(ershdr)); 132cb73ee40SLorenzo Bianconi } 133cb73ee40SLorenzo Bianconi 134f132ae7cSJiri Benc return hdr_len; 13595f5c64cSTom Herbert } 13695f5c64cSTom Herbert EXPORT_SYMBOL(gre_parse_header); 13795f5c64cSTom Herbert 138c50cd357SDaniel Borkmann static int gre_rcv(struct sk_buff *skb) 139c50cd357SDaniel Borkmann { 140c50cd357SDaniel Borkmann const struct gre_protocol *proto; 141c50cd357SDaniel Borkmann u8 ver; 142c50cd357SDaniel Borkmann int ret; 143c50cd357SDaniel Borkmann 144c50cd357SDaniel Borkmann if (!pskb_may_pull(skb, 12)) 145c50cd357SDaniel Borkmann goto drop; 146c50cd357SDaniel Borkmann 147c50cd357SDaniel Borkmann ver = skb->data[1]&0x7f; 148c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 149c50cd357SDaniel Borkmann goto drop; 150c50cd357SDaniel Borkmann 151c50cd357SDaniel Borkmann rcu_read_lock(); 152c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 153c50cd357SDaniel Borkmann if (!proto || !proto->handler) 154c50cd357SDaniel Borkmann goto drop_unlock; 155c50cd357SDaniel Borkmann ret = proto->handler(skb); 156c50cd357SDaniel Borkmann rcu_read_unlock(); 157c50cd357SDaniel Borkmann return ret; 158c50cd357SDaniel Borkmann 159c50cd357SDaniel Borkmann drop_unlock: 160c50cd357SDaniel Borkmann rcu_read_unlock(); 161c50cd357SDaniel Borkmann drop: 162c50cd357SDaniel Borkmann kfree_skb(skb); 163c50cd357SDaniel Borkmann return NET_RX_DROP; 164c50cd357SDaniel Borkmann } 165c50cd357SDaniel Borkmann 16632bbd879SStefano Brivio static int gre_err(struct sk_buff *skb, u32 info) 167c50cd357SDaniel Borkmann { 168c50cd357SDaniel Borkmann const struct gre_protocol *proto; 169c50cd357SDaniel Borkmann const struct iphdr *iph = (const struct iphdr *)skb->data; 170c50cd357SDaniel Borkmann u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f; 17132bbd879SStefano Brivio int err = 0; 172c50cd357SDaniel Borkmann 173c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 17432bbd879SStefano Brivio return -EINVAL; 175c50cd357SDaniel Borkmann 176c50cd357SDaniel Borkmann rcu_read_lock(); 177c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 178c50cd357SDaniel Borkmann if (proto && proto->err_handler) 179c50cd357SDaniel Borkmann proto->err_handler(skb, info); 18032bbd879SStefano Brivio else 18132bbd879SStefano Brivio err = -EPROTONOSUPPORT; 182c50cd357SDaniel Borkmann rcu_read_unlock(); 18332bbd879SStefano Brivio 18432bbd879SStefano Brivio return err; 185c50cd357SDaniel Borkmann } 186c50cd357SDaniel Borkmann 187c50cd357SDaniel Borkmann static const struct net_protocol net_gre_protocol = { 188c50cd357SDaniel Borkmann .handler = gre_rcv, 189c50cd357SDaniel Borkmann .err_handler = gre_err, 190c50cd357SDaniel Borkmann .netns_ok = 1, 191c50cd357SDaniel Borkmann }; 192c50cd357SDaniel Borkmann 193c50cd357SDaniel Borkmann static int __init gre_init(void) 194c50cd357SDaniel Borkmann { 195c50cd357SDaniel Borkmann pr_info("GRE over IPv4 demultiplexor driver\n"); 196c50cd357SDaniel Borkmann 197c50cd357SDaniel Borkmann if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) { 198c50cd357SDaniel Borkmann pr_err("can't add protocol\n"); 199c50cd357SDaniel Borkmann return -EAGAIN; 200c50cd357SDaniel Borkmann } 2019f57c67cSPravin B Shelar return 0; 2029f57c67cSPravin B Shelar } 203c50cd357SDaniel Borkmann 204c50cd357SDaniel Borkmann static void __exit gre_exit(void) 205c50cd357SDaniel Borkmann { 206c50cd357SDaniel Borkmann inet_del_protocol(&net_gre_protocol, IPPROTO_GRE); 207c50cd357SDaniel Borkmann } 208c50cd357SDaniel Borkmann 209c50cd357SDaniel Borkmann module_init(gre_init); 210c50cd357SDaniel Borkmann module_exit(gre_exit); 211c50cd357SDaniel Borkmann 212c50cd357SDaniel Borkmann MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver"); 213c50cd357SDaniel Borkmann MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); 214c50cd357SDaniel Borkmann MODULE_LICENSE("GPL"); 215