2 * GRE over IPv4 demultiplexer driver
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
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.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/kmod.h>
16 #include <linux/skbuff.h>
19 #include <linux/netdevice.h>
20 #include <linux/version.h>
21 #include <linux/spinlock.h>
22 #include <net/protocol.h>
26 static const struct gre_protocol __rcu
*gre_proto
[GREPROTO_MAX
] __read_mostly
;
27 static DEFINE_SPINLOCK(gre_proto_lock
);
29 int gre_add_protocol(const struct gre_protocol
*proto
, u8 version
)
31 if (version
>= GREPROTO_MAX
)
34 spin_lock(&gre_proto_lock
);
35 if (gre_proto
[version
])
38 rcu_assign_pointer(gre_proto
[version
], proto
);
39 spin_unlock(&gre_proto_lock
);
43 spin_unlock(&gre_proto_lock
);
47 EXPORT_SYMBOL_GPL(gre_add_protocol
);
49 int gre_del_protocol(const struct gre_protocol
*proto
, u8 version
)
51 if (version
>= GREPROTO_MAX
)
54 spin_lock(&gre_proto_lock
);
55 if (rcu_dereference_protected(gre_proto
[version
],
56 lockdep_is_held(&gre_proto_lock
)) != proto
)
58 rcu_assign_pointer(gre_proto
[version
], NULL
);
59 spin_unlock(&gre_proto_lock
);
64 spin_unlock(&gre_proto_lock
);
68 EXPORT_SYMBOL_GPL(gre_del_protocol
);
70 static int gre_rcv(struct sk_buff
*skb
)
72 const struct gre_protocol
*proto
;
76 if (!pskb_may_pull(skb
, 12))
79 ver
= skb
->data
[1]&0x7f;
80 if (ver
>= GREPROTO_MAX
)
84 proto
= rcu_dereference(gre_proto
[ver
]);
85 if (!proto
|| !proto
->handler
)
87 ret
= proto
->handler(skb
);
98 static void gre_err(struct sk_buff
*skb
, u32 info
)
100 const struct gre_protocol
*proto
;
101 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
102 u8 ver
= skb
->data
[(iph
->ihl
<<2) + 1]&0x7f;
104 if (ver
>= GREPROTO_MAX
)
108 proto
= rcu_dereference(gre_proto
[ver
]);
109 if (proto
&& proto
->err_handler
)
110 proto
->err_handler(skb
, info
);
114 static const struct net_protocol net_gre_protocol
= {
116 .err_handler
= gre_err
,
120 static int __init
gre_init(void)
122 pr_info("GRE over IPv4 demultiplexor driver");
124 if (inet_add_protocol(&net_gre_protocol
, IPPROTO_GRE
) < 0) {
125 pr_err("gre: can't add protocol\n");
132 static void __exit
gre_exit(void)
134 inet_del_protocol(&net_gre_protocol
, IPPROTO_GRE
);
137 module_init(gre_init
);
138 module_exit(gre_exit
);
140 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
141 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
142 MODULE_LICENSE("GPL");