2 * GRE over IPv4 demultiplexer driver
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/kmod.h>
18 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/if_tunnel.h>
23 #include <linux/spinlock.h>
24 #include <net/protocol.h>
28 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
29 static DEFINE_SPINLOCK(gre_proto_lock);
31 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
33 if (version >= GREPROTO_MAX)
36 spin_lock(&gre_proto_lock);
37 if (gre_proto[version])
40 RCU_INIT_POINTER(gre_proto[version], proto);
41 spin_unlock(&gre_proto_lock);
45 spin_unlock(&gre_proto_lock);
49 EXPORT_SYMBOL_GPL(gre_add_protocol);
51 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
53 if (version >= GREPROTO_MAX)
56 spin_lock(&gre_proto_lock);
57 if (rcu_dereference_protected(gre_proto[version],
58 lockdep_is_held(&gre_proto_lock)) != proto)
60 RCU_INIT_POINTER(gre_proto[version], NULL);
61 spin_unlock(&gre_proto_lock);
66 spin_unlock(&gre_proto_lock);
70 EXPORT_SYMBOL_GPL(gre_del_protocol);
72 static int gre_rcv(struct sk_buff *skb)
74 const struct gre_protocol *proto;
78 if (!pskb_may_pull(skb, 12))
81 ver = skb->data[1]&0x7f;
82 if (ver >= GREPROTO_MAX)
86 proto = rcu_dereference(gre_proto[ver]);
87 if (!proto || !proto->handler)
89 ret = proto->handler(skb);
100 static void gre_err(struct sk_buff *skb, u32 info)
102 const struct gre_protocol *proto;
103 const struct iphdr *iph = (const struct iphdr *)skb->data;
104 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
106 if (ver >= GREPROTO_MAX)
110 proto = rcu_dereference(gre_proto[ver]);
111 if (proto && proto->err_handler)
112 proto->err_handler(skb, info);
116 static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
117 netdev_features_t features)
119 struct sk_buff *segs = ERR_PTR(-EINVAL);
120 netdev_features_t enc_features;
121 int ghl = GRE_HEADER_SECTION;
122 struct gre_base_hdr *greh;
123 int mac_len = skb->mac_len;
124 __be16 protocol = skb->protocol;
128 if (unlikely(skb_shinfo(skb)->gso_type &
137 if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
140 greh = (struct gre_base_hdr *)skb_transport_header(skb);
142 if (greh->flags & GRE_KEY)
143 ghl += GRE_HEADER_SECTION;
144 if (greh->flags & GRE_SEQ)
145 ghl += GRE_HEADER_SECTION;
146 if (greh->flags & GRE_CSUM) {
147 ghl += GRE_HEADER_SECTION;
152 /* setup inner skb. */
153 if (greh->protocol == htons(ETH_P_TEB)) {
154 struct ethhdr *eth = (struct ethhdr *)skb_inner_mac_header(skb);
155 skb->protocol = eth->h_proto;
157 skb->protocol = greh->protocol;
160 skb->encapsulation = 0;
162 if (unlikely(!pskb_may_pull(skb, ghl)))
164 __skb_pull(skb, ghl);
165 skb_reset_mac_header(skb);
166 skb_set_network_header(skb, skb_inner_network_offset(skb));
167 skb->mac_len = skb_inner_network_offset(skb);
169 /* segment inner packet. */
170 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
171 segs = skb_mac_gso_segment(skb, enc_features);
172 if (!segs || IS_ERR(segs))
176 tnl_hlen = skb_tnl_header_len(skb);
178 __skb_push(skb, ghl);
182 if (skb_has_shared_frag(skb)) {
185 err = __skb_linearize(skb);
193 greh = (struct gre_base_hdr *)(skb->data);
194 pcsum = (__be32 *)(greh + 1);
196 *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
198 __skb_push(skb, tnl_hlen - ghl);
200 skb_reset_mac_header(skb);
201 skb_set_network_header(skb, mac_len);
202 skb->mac_len = mac_len;
203 skb->protocol = protocol;
204 } while ((skb = skb->next));
209 static int gre_gso_send_check(struct sk_buff *skb)
211 if (!skb->encapsulation)
216 static const struct net_protocol net_gre_protocol = {
218 .err_handler = gre_err,
222 static const struct net_offload gre_offload = {
224 .gso_send_check = gre_gso_send_check,
225 .gso_segment = gre_gso_segment,
229 static int __init gre_init(void)
231 pr_info("GRE over IPv4 demultiplexor driver\n");
233 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
234 pr_err("can't add protocol\n");
238 if (inet_add_offload(&gre_offload, IPPROTO_GRE)) {
239 pr_err("can't add protocol offload\n");
240 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
247 static void __exit gre_exit(void)
249 inet_del_offload(&gre_offload, IPPROTO_GRE);
250 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
253 module_init(gre_init);
254 module_exit(gre_exit);
256 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
257 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
258 MODULE_LICENSE("GPL");