Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[pandora-kernel.git] / net / ipv4 / gre.c
1 /*
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>
18 #include <linux/netdevice.h>
19 #include <linux/spinlock.h>
20 #include <net/protocol.h>
21 #include <net/gre.h>
22
23
24 static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
25 static DEFINE_SPINLOCK(gre_proto_lock);
26
27 int gre_add_protocol(const struct gre_protocol *proto, u8 version)
28 {
29         if (version >= GREPROTO_MAX)
30                 goto err_out;
31
32         spin_lock(&gre_proto_lock);
33         if (gre_proto[version])
34                 goto err_out_unlock;
35
36         rcu_assign_pointer(gre_proto[version], proto);
37         spin_unlock(&gre_proto_lock);
38         return 0;
39
40 err_out_unlock:
41         spin_unlock(&gre_proto_lock);
42 err_out:
43         return -1;
44 }
45 EXPORT_SYMBOL_GPL(gre_add_protocol);
46
47 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
48 {
49         if (version >= GREPROTO_MAX)
50                 goto err_out;
51
52         spin_lock(&gre_proto_lock);
53         if (rcu_dereference_protected(gre_proto[version],
54                         lockdep_is_held(&gre_proto_lock)) != proto)
55                 goto err_out_unlock;
56         rcu_assign_pointer(gre_proto[version], NULL);
57         spin_unlock(&gre_proto_lock);
58         synchronize_rcu();
59         return 0;
60
61 err_out_unlock:
62         spin_unlock(&gre_proto_lock);
63 err_out:
64         return -1;
65 }
66 EXPORT_SYMBOL_GPL(gre_del_protocol);
67
68 static int gre_rcv(struct sk_buff *skb)
69 {
70         const struct gre_protocol *proto;
71         u8 ver;
72         int ret;
73
74         if (!pskb_may_pull(skb, 12))
75                 goto drop;
76
77         ver = skb->data[1]&0x7f;
78         if (ver >= GREPROTO_MAX)
79                 goto drop;
80
81         rcu_read_lock();
82         proto = rcu_dereference(gre_proto[ver]);
83         if (!proto || !proto->handler)
84                 goto drop_unlock;
85         ret = proto->handler(skb);
86         rcu_read_unlock();
87         return ret;
88
89 drop_unlock:
90         rcu_read_unlock();
91 drop:
92         kfree_skb(skb);
93         return NET_RX_DROP;
94 }
95
96 static void gre_err(struct sk_buff *skb, u32 info)
97 {
98         const struct gre_protocol *proto;
99         u8 ver;
100
101         if (!pskb_may_pull(skb, 12))
102                 goto drop;
103
104         ver = skb->data[1]&0x7f;
105         if (ver >= GREPROTO_MAX)
106                 goto drop;
107
108         rcu_read_lock();
109         proto = rcu_dereference(gre_proto[ver]);
110         if (!proto || !proto->err_handler)
111                 goto drop_unlock;
112         proto->err_handler(skb, info);
113         rcu_read_unlock();
114         return;
115
116 drop_unlock:
117         rcu_read_unlock();
118 drop:
119         kfree_skb(skb);
120 }
121
122 static const struct net_protocol net_gre_protocol = {
123         .handler     = gre_rcv,
124         .err_handler = gre_err,
125         .netns_ok    = 1,
126 };
127
128 static int __init gre_init(void)
129 {
130         pr_info("GRE over IPv4 demultiplexor driver");
131
132         if (inet_add_protocol(&net_gre_protocol, IPPROTO_GRE) < 0) {
133                 pr_err("gre: can't add protocol\n");
134                 return -EAGAIN;
135         }
136
137         return 0;
138 }
139
140 static void __exit gre_exit(void)
141 {
142         inet_del_protocol(&net_gre_protocol, IPPROTO_GRE);
143 }
144
145 module_init(gre_init);
146 module_exit(gre_exit);
147
148 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
149 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
150 MODULE_LICENSE("GPL");
151