12874c5fdSThomas 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 5917c25cafSEric Dumazet /* Fills in tpi and returns header length to be pulled. 6017c25cafSEric Dumazet * Note that caller must use pskb_may_pull() before pulling GRE header. 6117c25cafSEric Dumazet */ 6295f5c64cSTom Herbert int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, 63e582615aSEric Dumazet bool *csum_err, __be16 proto, int nhs) 6495f5c64cSTom Herbert { 6595f5c64cSTom Herbert const struct gre_base_hdr *greh; 6695f5c64cSTom Herbert __be32 *options; 6795f5c64cSTom Herbert int hdr_len; 6895f5c64cSTom Herbert 69e582615aSEric Dumazet if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr)))) 7095f5c64cSTom Herbert return -EINVAL; 7195f5c64cSTom Herbert 72e582615aSEric Dumazet greh = (struct gre_base_hdr *)(skb->data + nhs); 7395f5c64cSTom Herbert if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) 7495f5c64cSTom Herbert return -EINVAL; 7595f5c64cSTom Herbert 76*5832c4a7SAlexander Lobakin gre_flags_to_tnl_flags(tpi->flags, greh->flags); 7795f5c64cSTom Herbert hdr_len = gre_calc_hlen(tpi->flags); 7895f5c64cSTom Herbert 79e582615aSEric Dumazet if (!pskb_may_pull(skb, nhs + hdr_len)) 8095f5c64cSTom Herbert return -EINVAL; 8195f5c64cSTom Herbert 82e582615aSEric Dumazet greh = (struct gre_base_hdr *)(skb->data + nhs); 8395f5c64cSTom Herbert tpi->proto = greh->protocol; 8495f5c64cSTom Herbert 8595f5c64cSTom Herbert options = (__be32 *)(greh + 1); 8695f5c64cSTom Herbert if (greh->flags & GRE_CSUM) { 87b0350d51SHaishuang Yan if (!skb_checksum_simple_validate(skb)) { 88e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_GRE, 89b0350d51SHaishuang Yan null_compute_pseudo); 90b0350d51SHaishuang Yan } else if (csum_err) { 9195f5c64cSTom Herbert *csum_err = true; 9295f5c64cSTom Herbert return -EINVAL; 9395f5c64cSTom Herbert } 9495f5c64cSTom Herbert 9595f5c64cSTom Herbert options++; 9695f5c64cSTom Herbert } 9795f5c64cSTom Herbert 9895f5c64cSTom Herbert if (greh->flags & GRE_KEY) { 9995f5c64cSTom Herbert tpi->key = *options; 10095f5c64cSTom Herbert options++; 10195f5c64cSTom Herbert } else { 10295f5c64cSTom Herbert tpi->key = 0; 10395f5c64cSTom Herbert } 10495f5c64cSTom Herbert if (unlikely(greh->flags & GRE_SEQ)) { 10595f5c64cSTom Herbert tpi->seq = *options; 10695f5c64cSTom Herbert options++; 10795f5c64cSTom Herbert } else { 10895f5c64cSTom Herbert tpi->seq = 0; 10995f5c64cSTom Herbert } 11095f5c64cSTom Herbert /* WCCP version 1 and 2 protocol decoding. 111da73b4e9SHaishuang Yan * - Change protocol to IPv4/IPv6 11295f5c64cSTom Herbert * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header 11395f5c64cSTom Herbert */ 11495f5c64cSTom Herbert if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { 11517c25cafSEric Dumazet u8 _val, *val; 11617c25cafSEric Dumazet 11717c25cafSEric Dumazet val = skb_header_pointer(skb, nhs + hdr_len, 11817c25cafSEric Dumazet sizeof(_val), &_val); 11917c25cafSEric Dumazet if (!val) 12017c25cafSEric Dumazet return -EINVAL; 121da73b4e9SHaishuang Yan tpi->proto = proto; 12217c25cafSEric Dumazet if ((*val & 0xF0) != 0x40) 12395f5c64cSTom Herbert hdr_len += 4; 12495f5c64cSTom Herbert } 1259b8c6d7bSEric Dumazet tpi->hdr_len = hdr_len; 126cb73ee40SLorenzo Bianconi 127cb73ee40SLorenzo Bianconi /* ERSPAN ver 1 and 2 protocol sets GRE key field 128cb73ee40SLorenzo Bianconi * to 0 and sets the configured key in the 129cb73ee40SLorenzo Bianconi * inner erspan header field 130cb73ee40SLorenzo Bianconi */ 131085c7c4eSCong Wang if ((greh->protocol == htons(ETH_P_ERSPAN) && hdr_len != 4) || 132cb73ee40SLorenzo Bianconi greh->protocol == htons(ETH_P_ERSPAN2)) { 133cb73ee40SLorenzo Bianconi struct erspan_base_hdr *ershdr; 134cb73ee40SLorenzo Bianconi 135cb73ee40SLorenzo Bianconi if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr))) 136cb73ee40SLorenzo Bianconi return -EINVAL; 137cb73ee40SLorenzo Bianconi 1380e494092SCong Wang ershdr = (struct erspan_base_hdr *)(skb->data + nhs + hdr_len); 139cb73ee40SLorenzo Bianconi tpi->key = cpu_to_be32(get_session_id(ershdr)); 140cb73ee40SLorenzo Bianconi } 141cb73ee40SLorenzo Bianconi 142f132ae7cSJiri Benc return hdr_len; 14395f5c64cSTom Herbert } 14495f5c64cSTom Herbert EXPORT_SYMBOL(gre_parse_header); 14595f5c64cSTom Herbert 146c50cd357SDaniel Borkmann static int gre_rcv(struct sk_buff *skb) 147c50cd357SDaniel Borkmann { 148c50cd357SDaniel Borkmann const struct gre_protocol *proto; 149c50cd357SDaniel Borkmann u8 ver; 150c50cd357SDaniel Borkmann int ret; 151c50cd357SDaniel Borkmann 152c50cd357SDaniel Borkmann if (!pskb_may_pull(skb, 12)) 153c50cd357SDaniel Borkmann goto drop; 154c50cd357SDaniel Borkmann 155c50cd357SDaniel Borkmann ver = skb->data[1]&0x7f; 156c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 157c50cd357SDaniel Borkmann goto drop; 158c50cd357SDaniel Borkmann 159c50cd357SDaniel Borkmann rcu_read_lock(); 160c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 161c50cd357SDaniel Borkmann if (!proto || !proto->handler) 162c50cd357SDaniel Borkmann goto drop_unlock; 163c50cd357SDaniel Borkmann ret = proto->handler(skb); 164c50cd357SDaniel Borkmann rcu_read_unlock(); 165c50cd357SDaniel Borkmann return ret; 166c50cd357SDaniel Borkmann 167c50cd357SDaniel Borkmann drop_unlock: 168c50cd357SDaniel Borkmann rcu_read_unlock(); 169c50cd357SDaniel Borkmann drop: 170c50cd357SDaniel Borkmann kfree_skb(skb); 171c50cd357SDaniel Borkmann return NET_RX_DROP; 172c50cd357SDaniel Borkmann } 173c50cd357SDaniel Borkmann 17432bbd879SStefano Brivio static int gre_err(struct sk_buff *skb, u32 info) 175c50cd357SDaniel Borkmann { 176c50cd357SDaniel Borkmann const struct gre_protocol *proto; 177c50cd357SDaniel Borkmann const struct iphdr *iph = (const struct iphdr *)skb->data; 178c50cd357SDaniel Borkmann u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f; 17932bbd879SStefano Brivio int err = 0; 180c50cd357SDaniel Borkmann 181c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 18232bbd879SStefano Brivio return -EINVAL; 183c50cd357SDaniel Borkmann 184c50cd357SDaniel Borkmann rcu_read_lock(); 185c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 186c50cd357SDaniel Borkmann if (proto && proto->err_handler) 187c50cd357SDaniel Borkmann proto->err_handler(skb, info); 18832bbd879SStefano Brivio else 18932bbd879SStefano Brivio err = -EPROTONOSUPPORT; 190c50cd357SDaniel Borkmann rcu_read_unlock(); 19132bbd879SStefano Brivio 19232bbd879SStefano Brivio return err; 193c50cd357SDaniel Borkmann } 194c50cd357SDaniel Borkmann 195c50cd357SDaniel Borkmann static const struct net_protocol net_gre_protocol = { 196c50cd357SDaniel Borkmann .handler = gre_rcv, 197c50cd357SDaniel Borkmann .err_handler = gre_err, 198c50cd357SDaniel Borkmann }; 199c50cd357SDaniel Borkmann 200c50cd357SDaniel Borkmann static int __init gre_init(void) 201c50cd357SDaniel Borkmann { 202c50cd357SDaniel Borkmann pr_info("GRE over IPv4 demultiplexor driver\n"); 203c50cd357SDaniel Borkmann 204c50cd357SDaniel Borkmann if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) { 205c50cd357SDaniel Borkmann pr_err("can't add protocol\n"); 206c50cd357SDaniel Borkmann return -EAGAIN; 207c50cd357SDaniel Borkmann } 2089f57c67cSPravin B Shelar return 0; 2099f57c67cSPravin B Shelar } 210c50cd357SDaniel Borkmann 211c50cd357SDaniel Borkmann static void __exit gre_exit(void) 212c50cd357SDaniel Borkmann { 213c50cd357SDaniel Borkmann inet_del_protocol(&net_gre_protocol, IPPROTO_GRE); 214c50cd357SDaniel Borkmann } 215c50cd357SDaniel Borkmann 216c50cd357SDaniel Borkmann module_init(gre_init); 217c50cd357SDaniel Borkmann module_exit(gre_exit); 218c50cd357SDaniel Borkmann 219c50cd357SDaniel Borkmann MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver"); 2206a57a219SAhelenia Ziemiańska MODULE_AUTHOR("D. Kozlov <xeb@mail.ru>"); 221c50cd357SDaniel Borkmann MODULE_LICENSE("GPL"); 222