summaryrefslogtreecommitdiff
path: root/driver/gre.c
blob: cda0576be52f5d8ba4c4669b1235f3c735bc6835 (plain)
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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kmod.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;
static DEFINE_RWLOCK(gre_proto_lock);

int gre_add_protocol(struct gre_protocol *proto, u8 version)
{
	int ret;

	if (version >= GREPROTO_MAX)
		return -1;
	
	write_lock_bh(&gre_proto_lock);
	if (gre_proto[version]) {
		ret = -1;
	} else {
		gre_proto[version]=proto;
		ret = 0;
	}
	write_unlock_bh(&gre_proto_lock);

	return ret;
}
int gre_del_protocol(struct gre_protocol *proto, u8 version)
{
	int ret;

	if (version >= GREPROTO_MAX)
		return -1;

	write_lock_bh(&gre_proto_lock);
	if (gre_proto[version] == proto) {
		gre_proto[version] = NULL;
		ret = 0;
	} else {
		ret = -1;
	}
	write_unlock_bh(&gre_proto_lock);

	return ret;
}
static int gre_rcv(struct sk_buff *skb)
{
	u8 ver;
	int ret;

	if (!pskb_may_pull(skb, 12))
		goto drop_nolock;

	ver = skb->data[1]&0x7f;
	if (ver >= GREPROTO_MAX)
		goto drop_nolock;
	
	read_lock(&gre_proto_lock);
	if (!gre_proto[ver] || !gre_proto[ver]->handler)
		goto drop;

	ret = gre_proto[ver]->handler(skb);
	read_unlock(&gre_proto_lock);

	return ret;

drop:
	read_unlock(&gre_proto_lock);
drop_nolock:
	kfree_skb(skb);
	return NET_RX_DROP;
}
static void gre_err(struct sk_buff *skb, u32 info)
{
	u8 ver;

	printk("err\n");

	if (!pskb_may_pull(skb, 12))
		goto drop_nolock;

	ver=skb->data[1];
	if (ver>=GREPROTO_MAX)
		goto drop_nolock;
		
	read_lock(&gre_proto_lock);
	if (!gre_proto[ver] || !gre_proto[ver]->err_handler)
		goto drop;

	gre_proto[ver]->err_handler(skb,info);
	read_unlock(&gre_proto_lock);

	return;

drop:
	read_unlock(&gre_proto_lock);
drop_nolock:
	kfree_skb(skb);
}


static struct net_protocol net_gre_protocol = {
	.handler	= gre_rcv,
	.err_handler	=	gre_err,
//	.netns_ok=1,
};

static int __init gre_init(void)
{
	printk(KERN_INFO "GRE over IPv4 demultiplexor driver");
	
	if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
		printk(KERN_INFO "gre: can't add protocol\n");
		return -EAGAIN;
	}

	return 0;
}

static void __exit gre_exit(void)
{
	inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
}

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);