1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/netdevice.h>
#include <linux/version.h>
#include <linux/spinlock.h>
#include <net/protocol.h>
#include "gre.h"
struct gre_protocol *gre_proto[GREPROTO_MAX] ____cacheline_aligned_in_smp;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
static rwlock_t gre_proto_lock=RW_LOCK_UNLOCKED;
#else
static DEFINE_SPINLOCK(gre_proto_lock);
#endif
int gre_add_protocol(struct gre_protocol *proto, u8 version)
{
int ret;
if (version >= GREPROTO_MAX)
return -EINVAL;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
write_lock_bh(&gre_proto_lock);
#else
spin_lock(&gre_proto_lock);
#endif
if (gre_proto[version]) {
ret = -EAGAIN;
} else {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
gre_proto[version] = proto;
#else
rcu_assign_pointer(gre_proto[version], proto);
#endif
ret = 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
write_unlock_bh(&gre_proto_lock);
#else
spin_unlock(&gre_proto_lock);
#endif
return ret;
}
int gre_del_protocol(struct gre_protocol *proto, u8 version)
{
if (version >= GREPROTO_MAX)
goto out_err;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
write_lock_bh(&gre_proto_lock);
#else
spin_lock(&gre_proto_lock);
#endif
if (gre_proto[version] == proto)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
gre_proto[version] = NULL;
#else
rcu_assign_pointer(gre_proto[version], NULL);
#endif
else
goto out_err_unlock;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
write_unlock_bh(&gre_proto_lock);
#else
spin_unlock(&gre_proto_lock);
synchronize_rcu();
#endif
return 0;
out_err_unlock:
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
write_unlock_bh(&gre_proto_lock);
#else
spin_unlock(&gre_proto_lock);
#endif
out_err:
return -EINVAL;
}
static int gre_rcv(struct sk_buff *skb)
{
u8 ver;
int ret;
struct gre_protocol *proto;
if (!pskb_may_pull(skb, 12))
goto drop_nolock;
ver = skb->data[1]&0x7f;
if (ver >= GREPROTO_MAX)
goto drop_nolock;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_lock(&gre_proto_lock);
proto = gre_proto[ver];
#else
rcu_read_lock();
proto = rcu_dereference(gre_proto[ver]);
#endif
if (!proto || !proto->handler)
goto drop;
ret = proto->handler(skb);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_unlock(&gre_proto_lock);
#else
rcu_read_unlock();
#endif
return ret;
drop:
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_unlock(&gre_proto_lock);
#else
rcu_read_unlock();
#endif
drop_nolock:
kfree_skb(skb);
return NET_RX_DROP;
}
static void gre_err(struct sk_buff *skb, u32 info)
{
u8 ver;
struct gre_protocol *proto;
if (!pskb_may_pull(skb, 12))
goto drop_nolock;
ver=skb->data[1]&0x7f;
if (ver>=GREPROTO_MAX)
goto drop_nolock;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_lock(&gre_proto_lock);
proto = gre_proto[ver];
#else
rcu_read_lock();
proto = rcu_dereference(gre_proto[ver]);
#endif
if (!proto || !proto->err_handler)
goto drop;
proto->err_handler(skb, info);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_unlock(&gre_proto_lock);
#else
rcu_read_unlock();
#endif
return;
drop:
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
read_unlock(&gre_proto_lock);
#else
rcu_read_unlock();
#endif
drop_nolock:
kfree_skb(skb);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
static struct inet_protocol net_gre_protocol = {
.handler = gre_rcv,
.err_handler = gre_err,
.protocol = IPPROTO_GRE,
.name = "GRE",
};
#else
static struct net_protocol net_gre_protocol = {
.handler = gre_rcv,
.err_handler = gre_err,
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,24)
.netns_ok=1,
#endif
};
#endif
static int __init gre_init(void)
{
printk(KERN_INFO "GRE over IPv4 demultiplexor driver");
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
inet_add_protocol(&net_gre_protocol);
#else
if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
printk(KERN_INFO "gre: can't add protocol\n");
return -EAGAIN;
}
#endif
return 0;
}
static void __exit gre_exit(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
inet_del_protocol(&net_gre_protocol);
#else
inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
#endif
}
module_init(gre_init);
module_exit(gre_exit);
MODULE_DESCRIPTION("GRE over IPv4 demultiplexor driver");
MODULE_AUTHOR("Kozlov D. (xeb@mail.ru)");
MODULE_LICENSE("GPL");
EXPORT_SYMBOL_GPL(gre_add_protocol);
EXPORT_SYMBOL_GPL(gre_del_protocol);
|