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 63*95f5c64cSTom Herbert int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, 64*95f5c64cSTom Herbert bool *csum_err, int *ret_hdr_len) 65*95f5c64cSTom Herbert { 66*95f5c64cSTom Herbert const struct gre_base_hdr *greh; 67*95f5c64cSTom Herbert __be32 *options; 68*95f5c64cSTom Herbert int hdr_len; 69*95f5c64cSTom Herbert 70*95f5c64cSTom Herbert if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr)))) 71*95f5c64cSTom Herbert return -EINVAL; 72*95f5c64cSTom Herbert 73*95f5c64cSTom Herbert greh = (struct gre_base_hdr *)skb_transport_header(skb); 74*95f5c64cSTom Herbert if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) 75*95f5c64cSTom Herbert return -EINVAL; 76*95f5c64cSTom Herbert 77*95f5c64cSTom Herbert tpi->flags = gre_flags_to_tnl_flags(greh->flags); 78*95f5c64cSTom Herbert hdr_len = gre_calc_hlen(tpi->flags); 79*95f5c64cSTom Herbert 80*95f5c64cSTom Herbert if (!pskb_may_pull(skb, hdr_len)) 81*95f5c64cSTom Herbert return -EINVAL; 82*95f5c64cSTom Herbert 83*95f5c64cSTom Herbert greh = (struct gre_base_hdr *)skb_transport_header(skb); 84*95f5c64cSTom Herbert tpi->proto = greh->protocol; 85*95f5c64cSTom Herbert 86*95f5c64cSTom Herbert options = (__be32 *)(greh + 1); 87*95f5c64cSTom Herbert if (greh->flags & GRE_CSUM) { 88*95f5c64cSTom Herbert if (skb_checksum_simple_validate(skb)) { 89*95f5c64cSTom Herbert *csum_err = true; 90*95f5c64cSTom Herbert return -EINVAL; 91*95f5c64cSTom Herbert } 92*95f5c64cSTom Herbert 93*95f5c64cSTom Herbert skb_checksum_try_convert(skb, IPPROTO_GRE, 0, 94*95f5c64cSTom Herbert null_compute_pseudo); 95*95f5c64cSTom Herbert options++; 96*95f5c64cSTom Herbert } 97*95f5c64cSTom Herbert 98*95f5c64cSTom Herbert if (greh->flags & GRE_KEY) { 99*95f5c64cSTom Herbert tpi->key = *options; 100*95f5c64cSTom Herbert options++; 101*95f5c64cSTom Herbert } else { 102*95f5c64cSTom Herbert tpi->key = 0; 103*95f5c64cSTom Herbert } 104*95f5c64cSTom Herbert if (unlikely(greh->flags & GRE_SEQ)) { 105*95f5c64cSTom Herbert tpi->seq = *options; 106*95f5c64cSTom Herbert options++; 107*95f5c64cSTom Herbert } else { 108*95f5c64cSTom Herbert tpi->seq = 0; 109*95f5c64cSTom Herbert } 110*95f5c64cSTom Herbert /* WCCP version 1 and 2 protocol decoding. 111*95f5c64cSTom Herbert * - Change protocol to IP 112*95f5c64cSTom Herbert * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header 113*95f5c64cSTom Herbert */ 114*95f5c64cSTom Herbert if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { 115*95f5c64cSTom Herbert tpi->proto = htons(ETH_P_IP); 116*95f5c64cSTom Herbert if ((*(u8 *)options & 0xF0) != 0x40) { 117*95f5c64cSTom Herbert hdr_len += 4; 118*95f5c64cSTom Herbert if (!pskb_may_pull(skb, hdr_len)) 119*95f5c64cSTom Herbert return -EINVAL; 120*95f5c64cSTom Herbert } 121*95f5c64cSTom Herbert } 122*95f5c64cSTom Herbert *ret_hdr_len = hdr_len; 123*95f5c64cSTom Herbert return 0; 124*95f5c64cSTom Herbert } 125*95f5c64cSTom Herbert EXPORT_SYMBOL(gre_parse_header); 126*95f5c64cSTom Herbert 127c50cd357SDaniel Borkmann static int gre_rcv(struct sk_buff *skb) 128c50cd357SDaniel Borkmann { 129c50cd357SDaniel Borkmann const struct gre_protocol *proto; 130c50cd357SDaniel Borkmann u8 ver; 131c50cd357SDaniel Borkmann int ret; 132c50cd357SDaniel Borkmann 133c50cd357SDaniel Borkmann if (!pskb_may_pull(skb, 12)) 134c50cd357SDaniel Borkmann goto drop; 135c50cd357SDaniel Borkmann 136c50cd357SDaniel Borkmann ver = skb->data[1]&0x7f; 137c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 138c50cd357SDaniel Borkmann goto drop; 139c50cd357SDaniel Borkmann 140c50cd357SDaniel Borkmann rcu_read_lock(); 141c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 142c50cd357SDaniel Borkmann if (!proto || !proto->handler) 143c50cd357SDaniel Borkmann goto drop_unlock; 144c50cd357SDaniel Borkmann ret = proto->handler(skb); 145c50cd357SDaniel Borkmann rcu_read_unlock(); 146c50cd357SDaniel Borkmann return ret; 147c50cd357SDaniel Borkmann 148c50cd357SDaniel Borkmann drop_unlock: 149c50cd357SDaniel Borkmann rcu_read_unlock(); 150c50cd357SDaniel Borkmann drop: 151c50cd357SDaniel Borkmann kfree_skb(skb); 152c50cd357SDaniel Borkmann return NET_RX_DROP; 153c50cd357SDaniel Borkmann } 154c50cd357SDaniel Borkmann 155c50cd357SDaniel Borkmann static void gre_err(struct sk_buff *skb, u32 info) 156c50cd357SDaniel Borkmann { 157c50cd357SDaniel Borkmann const struct gre_protocol *proto; 158c50cd357SDaniel Borkmann const struct iphdr *iph = (const struct iphdr *)skb->data; 159c50cd357SDaniel Borkmann u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f; 160c50cd357SDaniel Borkmann 161c50cd357SDaniel Borkmann if (ver >= GREPROTO_MAX) 162c50cd357SDaniel Borkmann return; 163c50cd357SDaniel Borkmann 164c50cd357SDaniel Borkmann rcu_read_lock(); 165c50cd357SDaniel Borkmann proto = rcu_dereference(gre_proto[ver]); 166c50cd357SDaniel Borkmann if (proto && proto->err_handler) 167c50cd357SDaniel Borkmann proto->err_handler(skb, info); 168c50cd357SDaniel Borkmann rcu_read_unlock(); 169c50cd357SDaniel Borkmann } 170c50cd357SDaniel Borkmann 171c50cd357SDaniel Borkmann static const struct net_protocol net_gre_protocol = { 172c50cd357SDaniel Borkmann .handler = gre_rcv, 173c50cd357SDaniel Borkmann .err_handler = gre_err, 174c50cd357SDaniel Borkmann .netns_ok = 1, 175c50cd357SDaniel Borkmann }; 176c50cd357SDaniel Borkmann 177c50cd357SDaniel Borkmann static int __init gre_init(void) 178c50cd357SDaniel Borkmann { 179c50cd357SDaniel Borkmann pr_info("GRE over IPv4 demultiplexor driver\n"); 180c50cd357SDaniel Borkmann 181c50cd357SDaniel Borkmann if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) { 182c50cd357SDaniel Borkmann pr_err("can't add protocol\n"); 183c50cd357SDaniel Borkmann return -EAGAIN; 184c50cd357SDaniel Borkmann } 1859f57c67cSPravin B Shelar return 0; 1869f57c67cSPravin B Shelar } 187c50cd357SDaniel Borkmann 188c50cd357SDaniel Borkmann static void __exit gre_exit(void) 189c50cd357SDaniel Borkmann { 190c50cd357SDaniel Borkmann inet_del_protocol(&net_gre_protocol, IPPROTO_GRE); 191c50cd357SDaniel Borkmann } 192c50cd357SDaniel Borkmann 193c50cd357SDaniel Borkmann module_init(gre_init); 194c50cd357SDaniel Borkmann module_exit(gre_exit); 195c50cd357SDaniel Borkmann 196c50cd357SDaniel Borkmann MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver"); 197c50cd357SDaniel Borkmann MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); 198c50cd357SDaniel Borkmann MODULE_LICENSE("GPL"); 199