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);
34 #define GRE_HEADER_SECTION 4
36 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
38 if (version >= GREPROTO_MAX)
41 spin_lock(&gre_proto_lock);
42 if (gre_proto[version])
45 RCU_INIT_POINTER(gre_proto[version], proto);
46 spin_unlock(&gre_proto_lock);
50 spin_unlock(&gre_proto_lock);
54 EXPORT_SYMBOL_GPL(gre_add_protocol);
56 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
58 if (version >= GREPROTO_MAX)
61 spin_lock(&gre_proto_lock);
62 if (rcu_dereference_protected(gre_proto[version],
63 lockdep_is_held(&gre_proto_lock)) != proto)
65 RCU_INIT_POINTER(gre_proto[version], NULL);
66 spin_unlock(&gre_proto_lock);
71 spin_unlock(&gre_proto_lock);
75 EXPORT_SYMBOL_GPL(gre_del_protocol);
77 static int gre_rcv(struct sk_buff *skb)
79 const struct gre_protocol *proto;
83 if (!pskb_may_pull(skb, 12))
86 ver = skb->data[1]&0x7f;
87 if (ver >= GREPROTO_MAX)
91 proto = rcu_dereference(gre_proto[ver]);
92 if (!proto || !proto->handler)
94 ret = proto->handler(skb);
105 static void gre_err(struct sk_buff *skb, u32 info)
107 const struct gre_protocol *proto;
108 const struct iphdr *iph = (const struct iphdr *)skb->data;
109 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
111 if (ver >= GREPROTO_MAX)
115 proto = rcu_dereference(gre_proto[ver]);
116 if (proto && proto->err_handler)
117 proto->err_handler(skb, info);
121 static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
122 netdev_features_t features)
124 struct sk_buff *segs = ERR_PTR(-EINVAL);
125 netdev_features_t enc_features;
126 int ghl = GRE_HEADER_SECTION;
127 struct gre_base_hdr *greh;
128 int mac_len = skb->mac_len;
132 if (unlikely(skb_shinfo(skb)->gso_type &
141 if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
144 greh = (struct gre_base_hdr *)skb_transport_header(skb);
146 if (greh->flags & GRE_KEY)
147 ghl += GRE_HEADER_SECTION;
148 if (greh->flags & GRE_SEQ)
149 ghl += GRE_HEADER_SECTION;
150 if (greh->flags & GRE_CSUM) {
151 ghl += GRE_HEADER_SECTION;
156 /* setup inner skb. */
157 if (greh->protocol == htons(ETH_P_TEB)) {
158 struct ethhdr *eth = eth_hdr(skb);
159 skb->protocol = eth->h_proto;
161 skb->protocol = greh->protocol;
164 skb->encapsulation = 0;
166 if (unlikely(!pskb_may_pull(skb, ghl)))
168 __skb_pull(skb, ghl);
169 skb_reset_mac_header(skb);
170 skb_set_network_header(skb, skb_inner_network_offset(skb));
171 skb->mac_len = skb_inner_network_offset(skb);
173 /* segment inner packet. */
174 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
175 segs = skb_mac_gso_segment(skb, enc_features);
176 if (!segs || IS_ERR(segs))
180 tnl_hlen = skb_tnl_header_len(skb);
182 __skb_push(skb, ghl);
186 if (skb_has_shared_frag(skb)) {
189 err = __skb_linearize(skb);
197 greh = (struct gre_base_hdr *)(skb->data);
198 pcsum = (__be32 *)(greh + 1);
200 *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
202 __skb_push(skb, tnl_hlen - ghl);
204 skb_reset_mac_header(skb);
205 skb_set_network_header(skb, mac_len);
206 skb->mac_len = mac_len;
207 } while ((skb = skb->next));
212 static int gre_gso_send_check(struct sk_buff *skb)
214 if (!skb->encapsulation)
219 static const struct net_protocol net_gre_protocol = {
221 .err_handler = gre_err,
225 static const struct net_offload gre_offload = {
227 .gso_send_check = gre_gso_send_check,
228 .gso_segment = gre_gso_segment,
232 static int __init gre_init(void)
234 pr_info("GRE over IPv4 demultiplexor driver\n");
236 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
237 pr_err("can't add protocol\n");
241 if (inet_add_offload(&gre_offload, IPPROTO_GRE)) {
242 pr_err("can't add protocol offload\n");
243 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
250 static void __exit gre_exit(void)
252 inet_del_offload(&gre_offload, IPPROTO_GRE);
253 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
256 module_init(gre_init);
257 module_exit(gre_exit);
259 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
260 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
261 MODULE_LICENSE("GPL");