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>
18 #include <linux/netdevice.h>
19 #include <linux/version.h>
20 #include <linux/spinlock.h>
21 #include <net/protocol.h>
25 static const struct gre_protocol
*gre_proto
[GREPROTO_MAX
] __read_mostly
;
26 static DEFINE_SPINLOCK(gre_proto_lock
);
28 int gre_add_protocol(const struct gre_protocol
*proto
, u8 version
)
30 if (version
>= GREPROTO_MAX
)
33 spin_lock(&gre_proto_lock
);
34 if (gre_proto
[version
])
37 rcu_assign_pointer(gre_proto
[version
], proto
);
38 spin_unlock(&gre_proto_lock
);
42 spin_unlock(&gre_proto_lock
);
46 EXPORT_SYMBOL_GPL(gre_add_protocol
);
48 int gre_del_protocol(const struct gre_protocol
*proto
, u8 version
)
50 if (version
>= GREPROTO_MAX
)
53 spin_lock(&gre_proto_lock
);
54 if (gre_proto
[version
] != proto
)
56 rcu_assign_pointer(gre_proto
[version
], NULL
);
57 spin_unlock(&gre_proto_lock
);
62 spin_unlock(&gre_proto_lock
);
66 EXPORT_SYMBOL_GPL(gre_del_protocol
);
68 static int gre_rcv(struct sk_buff
*skb
)
70 const struct gre_protocol
*proto
;
74 if (!pskb_may_pull(skb
, 12))
77 ver
= skb
->data
[1]&0x7f;
78 if (ver
>= GREPROTO_MAX
)
82 proto
= rcu_dereference(gre_proto
[ver
]);
83 if (!proto
|| !proto
->handler
)
85 ret
= proto
->handler(skb
);
96 static void gre_err(struct sk_buff
*skb
, u32 info
)
98 const struct gre_protocol
*proto
;
101 if (!pskb_may_pull(skb
, 12))
104 ver
= skb
->data
[1]&0x7f;
105 if (ver
>= GREPROTO_MAX
)
109 proto
= rcu_dereference(gre_proto
[ver
]);
110 if (!proto
|| !proto
->err_handler
)
112 proto
->err_handler(skb
, info
);
122 static const struct net_protocol net_gre_protocol
= {
124 .err_handler
= gre_err
,
128 static int __init
gre_init(void)
130 pr_info("GRE over IPv4 demultiplexor driver");
132 if (inet_add_protocol(&net_gre_protocol
, IPPROTO_GRE
) < 0) {
133 pr_err("gre: can't add protocol\n");
140 static void __exit
gre_exit(void)
142 inet_del_protocol(&net_gre_protocol
, IPPROTO_GRE
);
145 module_init(gre_init
);
146 module_exit(gre_exit
);
148 MODULE_DESCRIPTION("GRE over IPv4 demultiplexer driver");
149 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
150 MODULE_LICENSE("GPL");