0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/if.h>
0012 #include <linux/icmp.h>
0013 #include <linux/kernel.h>
0014 #include <linux/kmod.h>
0015 #include <linux/skbuff.h>
0016 #include <linux/in.h>
0017 #include <linux/ip.h>
0018 #include <linux/netdevice.h>
0019 #include <linux/if_tunnel.h>
0020 #include <linux/spinlock.h>
0021 #include <net/protocol.h>
0022 #include <net/gre.h>
0023 #include <net/erspan.h>
0024
0025 #include <net/icmp.h>
0026 #include <net/route.h>
0027 #include <net/xfrm.h>
0028
0029 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
0030
0031 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
0032 {
0033 if (version >= GREPROTO_MAX)
0034 return -EINVAL;
0035
0036 return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
0037 0 : -EBUSY;
0038 }
0039 EXPORT_SYMBOL_GPL(gre_add_protocol);
0040
0041 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
0042 {
0043 int ret;
0044
0045 if (version >= GREPROTO_MAX)
0046 return -EINVAL;
0047
0048 ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
0049 0 : -EBUSY;
0050
0051 if (ret)
0052 return ret;
0053
0054 synchronize_rcu();
0055 return 0;
0056 }
0057 EXPORT_SYMBOL_GPL(gre_del_protocol);
0058
0059
0060
0061
0062 int gre_parse_header(struct sk_buff *skb, struct tnl_ptk_info *tpi,
0063 bool *csum_err, __be16 proto, int nhs)
0064 {
0065 const struct gre_base_hdr *greh;
0066 __be32 *options;
0067 int hdr_len;
0068
0069 if (unlikely(!pskb_may_pull(skb, nhs + sizeof(struct gre_base_hdr))))
0070 return -EINVAL;
0071
0072 greh = (struct gre_base_hdr *)(skb->data + nhs);
0073 if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING)))
0074 return -EINVAL;
0075
0076 tpi->flags = gre_flags_to_tnl_flags(greh->flags);
0077 hdr_len = gre_calc_hlen(tpi->flags);
0078
0079 if (!pskb_may_pull(skb, nhs + hdr_len))
0080 return -EINVAL;
0081
0082 greh = (struct gre_base_hdr *)(skb->data + nhs);
0083 tpi->proto = greh->protocol;
0084
0085 options = (__be32 *)(greh + 1);
0086 if (greh->flags & GRE_CSUM) {
0087 if (!skb_checksum_simple_validate(skb)) {
0088 skb_checksum_try_convert(skb, IPPROTO_GRE,
0089 null_compute_pseudo);
0090 } else if (csum_err) {
0091 *csum_err = true;
0092 return -EINVAL;
0093 }
0094
0095 options++;
0096 }
0097
0098 if (greh->flags & GRE_KEY) {
0099 tpi->key = *options;
0100 options++;
0101 } else {
0102 tpi->key = 0;
0103 }
0104 if (unlikely(greh->flags & GRE_SEQ)) {
0105 tpi->seq = *options;
0106 options++;
0107 } else {
0108 tpi->seq = 0;
0109 }
0110
0111
0112
0113
0114 if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) {
0115 u8 _val, *val;
0116
0117 val = skb_header_pointer(skb, nhs + hdr_len,
0118 sizeof(_val), &_val);
0119 if (!val)
0120 return -EINVAL;
0121 tpi->proto = proto;
0122 if ((*val & 0xF0) != 0x40)
0123 hdr_len += 4;
0124 }
0125 tpi->hdr_len = hdr_len;
0126
0127
0128
0129
0130
0131 if ((greh->protocol == htons(ETH_P_ERSPAN) && hdr_len != 4) ||
0132 greh->protocol == htons(ETH_P_ERSPAN2)) {
0133 struct erspan_base_hdr *ershdr;
0134
0135 if (!pskb_may_pull(skb, nhs + hdr_len + sizeof(*ershdr)))
0136 return -EINVAL;
0137
0138 ershdr = (struct erspan_base_hdr *)(skb->data + nhs + hdr_len);
0139 tpi->key = cpu_to_be32(get_session_id(ershdr));
0140 }
0141
0142 return hdr_len;
0143 }
0144 EXPORT_SYMBOL(gre_parse_header);
0145
0146 static int gre_rcv(struct sk_buff *skb)
0147 {
0148 const struct gre_protocol *proto;
0149 u8 ver;
0150 int ret;
0151
0152 if (!pskb_may_pull(skb, 12))
0153 goto drop;
0154
0155 ver = skb->data[1]&0x7f;
0156 if (ver >= GREPROTO_MAX)
0157 goto drop;
0158
0159 rcu_read_lock();
0160 proto = rcu_dereference(gre_proto[ver]);
0161 if (!proto || !proto->handler)
0162 goto drop_unlock;
0163 ret = proto->handler(skb);
0164 rcu_read_unlock();
0165 return ret;
0166
0167 drop_unlock:
0168 rcu_read_unlock();
0169 drop:
0170 kfree_skb(skb);
0171 return NET_RX_DROP;
0172 }
0173
0174 static int gre_err(struct sk_buff *skb, u32 info)
0175 {
0176 const struct gre_protocol *proto;
0177 const struct iphdr *iph = (const struct iphdr *)skb->data;
0178 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
0179 int err = 0;
0180
0181 if (ver >= GREPROTO_MAX)
0182 return -EINVAL;
0183
0184 rcu_read_lock();
0185 proto = rcu_dereference(gre_proto[ver]);
0186 if (proto && proto->err_handler)
0187 proto->err_handler(skb, info);
0188 else
0189 err = -EPROTONOSUPPORT;
0190 rcu_read_unlock();
0191
0192 return err;
0193 }
0194
0195 static const struct net_protocol net_gre_protocol = {
0196 .handler = gre_rcv,
0197 .err_handler = gre_err,
0198 };
0199
0200 static int __init gre_init(void)
0201 {
0202 pr_info("GRE over IPv4 demultiplexor driver\n");
0203
0204 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
0205 pr_err("can't add protocol\n");
0206 return -EAGAIN;
0207 }
0208 return 0;
0209 }
0210
0211 static void __exit gre_exit(void)
0212 {
0213 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
0214 }
0215
0216 module_init(gre_init);
0217 module_exit(gre_exit);
0218
0219 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
0220 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
0221 MODULE_LICENSE("GPL");