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