blob: 3e3f75d96be514df535d9ea9b25f2af12b6fce4e [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
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/kmod.h>
16#include <linux/skbuff.h>
17#include <linux/in.h>
xeb@mail.ru06e67262011-07-22 20:49:40 +000018#include <linux/ip.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070019#include <linux/netdevice.h>
20#include <linux/version.h>
21#include <linux/spinlock.h>
22#include <net/protocol.h>
23#include <net/gre.h>
24
25
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000026static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070027static DEFINE_SPINLOCK(gre_proto_lock);
28
29int gre_add_protocol(const struct gre_protocol *proto, u8 version)
30{
31 if (version >= GREPROTO_MAX)
32 goto err_out;
33
34 spin_lock(&gre_proto_lock);
35 if (gre_proto[version])
36 goto err_out_unlock;
37
38 rcu_assign_pointer(gre_proto[version], proto);
39 spin_unlock(&gre_proto_lock);
40 return 0;
41
42err_out_unlock:
43 spin_unlock(&gre_proto_lock);
44err_out:
45 return -1;
46}
47EXPORT_SYMBOL_GPL(gre_add_protocol);
48
49int gre_del_protocol(const struct gre_protocol *proto, u8 version)
50{
51 if (version >= GREPROTO_MAX)
52 goto err_out;
53
54 spin_lock(&gre_proto_lock);
Eric Dumazet6f0bcf12010-10-24 21:33:16 +000055 if (rcu_dereference_protected(gre_proto[version],
56 lockdep_is_held(&gre_proto_lock)) != proto)
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070057 goto err_out_unlock;
58 rcu_assign_pointer(gre_proto[version], NULL);
59 spin_unlock(&gre_proto_lock);
60 synchronize_rcu();
61 return 0;
62
63err_out_unlock:
64 spin_unlock(&gre_proto_lock);
65err_out:
66 return -1;
67}
68EXPORT_SYMBOL_GPL(gre_del_protocol);
69
70static int gre_rcv(struct sk_buff *skb)
71{
72 const struct gre_protocol *proto;
73 u8 ver;
74 int ret;
75
76 if (!pskb_may_pull(skb, 12))
77 goto drop;
78
79 ver = skb->data[1]&0x7f;
80 if (ver >= GREPROTO_MAX)
81 goto drop;
82
83 rcu_read_lock();
84 proto = rcu_dereference(gre_proto[ver]);
85 if (!proto || !proto->handler)
86 goto drop_unlock;
87 ret = proto->handler(skb);
88 rcu_read_unlock();
89 return ret;
90
91drop_unlock:
92 rcu_read_unlock();
93drop:
94 kfree_skb(skb);
95 return NET_RX_DROP;
96}
97
98static void gre_err(struct sk_buff *skb, u32 info)
99{
100 const struct gre_protocol *proto;
xeb@mail.ru06e67262011-07-22 20:49:40 +0000101 const struct iphdr *iph = (const struct iphdr *)skb->data;
102 u8 ver = skb->data[(iph->ihl<<2) + 1]&0x7f;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700103
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700104 if (ver >= GREPROTO_MAX)
xeb@mail.ru06e67262011-07-22 20:49:40 +0000105 return;
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700106
107 rcu_read_lock();
108 proto = rcu_dereference(gre_proto[ver]);
xeb@mail.ru06e67262011-07-22 20:49:40 +0000109 if (proto && proto->err_handler)
110 proto->err_handler(skb, info);
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700111 rcu_read_unlock();
Dmitry Kozlov00959ad2010-08-21 23:05:39 -0700112}
113
114static const struct net_protocol net_gre_protocol = {
115 .handler = gre_rcv,
116 .err_handler = gre_err,
117 .netns_ok = 1,
118};
119
120static int __init gre_init(void)
121{
122 pr_info("GRE over IPv4 demultiplexor driver");
123
124 if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
125 pr_err("gre: can't add protocol\n");
126 return -EAGAIN;
127 }
128
129 return 0;
130}
131
132static void __exit gre_exit(void)
133{
134 inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
135}
136
137module_init(gre_init);
138module_exit(gre_exit);
139
140MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
141MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
142MODULE_LICENSE("GPL");
143