blob: d2d5a99fba0983a4dead0909e30212c84f72154b [file] [log] [blame]
Dmitry Kozlov00959ad2010-08-21 23:05:39 -07001/*
2 * GRE over IPv4 demultiplexer driver
3 *
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
5 *
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.
10 *
11 */
12
Joe Perchesafd465032012-03-12 07:03:32 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/kmod.h>
18#include <linux/skbuff.h>
19#include <linux/in.h>
xeb@mail.ru559fafb2011-07-22 20:49:40 +000020#include <linux/ip.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070021#include <linux/netdevice.h>
Pravin B Shelar68c33162013-02-14 14:02:41 +000022#include <linux/if_tunnel.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070023#include <linux/spinlock.h>
24#include <net/protocol.h>
25#include <net/gre.h>
26
27
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000028static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070029static DEFINE_SPINLOCK(gre_proto_lock);
30
31int gre_add_protocol(const struct gre_protocol *proto, u8 version)
32{
33 if (version >= GREPROTO_MAX)
34 goto err_out;
35
36 spin_lock(&gre_proto_lock);
37 if (gre_proto[version])
38 goto err_out_unlock;
39
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000040 RCU_INIT_POINTER(gre_proto[version], proto);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070041 spin_unlock(&gre_proto_lock);
42 return 0;
43
44err_out_unlock:
45 spin_unlock(&gre_proto_lock);
46err_out:
47 return -1;
48}
49EXPORT_SYMBOL_GPL(gre_add_protocol);
50
51int gre_del_protocol(const struct gre_protocol *proto, u8 version)
52{
53 if (version >= GREPROTO_MAX)
54 goto err_out;
55
56 spin_lock(&gre_proto_lock);
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000057 if (rcu_dereference_protected(gre_proto[version],
58 lockdep_is_held(&gre_proto_lock)) != proto)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070059 goto err_out_unlock;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000060 RCU_INIT_POINTER(gre_proto[version], NULL);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070061 spin_unlock(&gre_proto_lock);
62 synchronize_rcu();
63 return 0;
64
65err_out_unlock:
66 spin_unlock(&gre_proto_lock);
67err_out:
68 return -1;
69}
70EXPORT_SYMBOL_GPL(gre_del_protocol);
71
72static int gre_rcv(struct sk_buff *skb)
73{
74 const struct gre_protocol *proto;
75 u8 ver;
76 int ret;
77
78 if (!pskb_may_pull(skb, 12))
79 goto drop;
80
81 ver = skb->data[1]&0x7f;
82 if (ver >= GREPROTO_MAX)
83 goto drop;
84
85 rcu_read_lock();
86 proto = rcu_dereference(gre_proto[ver]);
87 if (!proto || !proto->handler)
88 goto drop_unlock;
89 ret = proto->handler(skb);
90 rcu_read_unlock();
91 return ret;
92
93drop_unlock:
94 rcu_read_unlock();
95drop:
96 kfree_skb(skb);
97 return NET_RX_DROP;
98}
99
100static void gre_err(struct sk_buff *skb, u32 info)
101{
102 const struct gre_protocol *proto;
xeb@mail.ru559fafb2011-07-22 20:49:40 +0000103 const struct iphdr *iph = (const struct iphdr *)skb->data;
104 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700105
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700106 if (ver >= GREPROTO_MAX)
xeb@mail.ru559fafb2011-07-22 20:49:40 +0000107 return;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700108
109 rcu_read_lock();
110 proto = rcu_dereference(gre_proto[ver]);
xeb@mail.ru559fafb2011-07-22 20:49:40 +0000111 if (proto && proto->err_handler)
112 proto->err_handler(skb, info);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700113 rcu_read_unlock();
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700114}
115
Pravin B Shelar68c33162013-02-14 14:02:41 +0000116static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
117 netdev_features_t features)
118{
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;
Pravin B Shelar9cb690d2013-03-24 17:36:16 +0000124 int tnl_hlen;
Pravin B Shelar68c33162013-02-14 14:02:41 +0000125 bool csum;
126
127 if (unlikely(skb_shinfo(skb)->gso_type &
128 ~(SKB_GSO_TCPV4 |
129 SKB_GSO_TCPV6 |
130 SKB_GSO_UDP |
131 SKB_GSO_DODGY |
132 SKB_GSO_TCP_ECN |
133 SKB_GSO_GRE)))
134 goto out;
135
136 if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
137 goto out;
138
139 greh = (struct gre_base_hdr *)skb_transport_header(skb);
140
141 if (greh->flags & GRE_KEY)
142 ghl += GRE_HEADER_SECTION;
143 if (greh->flags & GRE_SEQ)
144 ghl += GRE_HEADER_SECTION;
145 if (greh->flags & GRE_CSUM) {
146 ghl += GRE_HEADER_SECTION;
147 csum = true;
148 } else
149 csum = false;
150
151 /* setup inner skb. */
152 if (greh->protocol == htons(ETH_P_TEB)) {
153 struct ethhdr *eth = eth_hdr(skb);
154 skb->protocol = eth->h_proto;
155 } else {
156 skb->protocol = greh->protocol;
157 }
158
159 skb->encapsulation = 0;
160
161 if (unlikely(!pskb_may_pull(skb, ghl)))
162 goto out;
163 __skb_pull(skb, ghl);
164 skb_reset_mac_header(skb);
165 skb_set_network_header(skb, skb_inner_network_offset(skb));
166 skb->mac_len = skb_inner_network_offset(skb);
167
168 /* segment inner packet. */
169 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
170 segs = skb_mac_gso_segment(skb, enc_features);
171 if (!segs || IS_ERR(segs))
172 goto out;
173
174 skb = segs;
175 tnl_hlen = skb_tnl_header_len(skb);
176 do {
177 __skb_push(skb, ghl);
178 if (csum) {
179 __be32 *pcsum;
180
181 if (skb_has_shared_frag(skb)) {
182 int err;
183
184 err = __skb_linearize(skb);
185 if (err) {
186 kfree_skb(segs);
187 segs = ERR_PTR(err);
188 goto out;
189 }
190 }
191
192 greh = (struct gre_base_hdr *)(skb->data);
193 pcsum = (__be32 *)(greh + 1);
194 *pcsum = 0;
195 *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
196 }
197 __skb_push(skb, tnl_hlen - ghl);
198
199 skb_reset_mac_header(skb);
200 skb_set_network_header(skb, mac_len);
201 skb->mac_len = mac_len;
202 } while ((skb = skb->next));
203out:
204 return segs;
205}
206
207static int gre_gso_send_check(struct sk_buff *skb)
208{
209 if (!skb->encapsulation)
210 return -EINVAL;
211 return 0;
212}
213
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700214static const struct net_protocol net_gre_protocol = {
215 .handler = gre_rcv,
216 .err_handler = gre_err,
217 .netns_ok = 1,
218};
219
Pravin B Shelar68c33162013-02-14 14:02:41 +0000220static const struct net_offload gre_offload = {
221 .callbacks = {
222 .gso_send_check = gre_gso_send_check,
223 .gso_segment = gre_gso_segment,
224 },
225};
226
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700227static int __init gre_init(void)
228{
Joe Perchesafd465032012-03-12 07:03:32 +0000229 pr_info("GRE over IPv4 demultiplexor driver\n");
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700230
231 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
Joe Perchesafd465032012-03-12 07:03:32 +0000232 pr_err("can't add protocol\n");
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700233 return -EAGAIN;
234 }
235
Pravin B Shelar68c33162013-02-14 14:02:41 +0000236 if (inet_add_offload(&gre_offload, IPPROTO_GRE)) {
237 pr_err("can't add protocol offload\n");
238 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
239 return -EAGAIN;
240 }
241
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700242 return 0;
243}
244
245static void __exit gre_exit(void)
246{
Pravin B Shelar68c33162013-02-14 14:02:41 +0000247 inet_del_offload(&gre_offload, IPPROTO_GRE);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700248 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
249}
250
251module_init(gre_init);
252module_exit(gre_exit);
253
254MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
255MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
256MODULE_LICENSE("GPL");
257