xfs: fix acl count validation in xfs_acl_from_disk()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / gre.c
blob3e3f75d96be514df535d9ea9b25f2af12b6fce4e
1 /*
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>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/netdevice.h>
20 #include <linux/version.h>
21 #include <linux/spinlock.h>
22 #include <net/protocol.h>
23 #include <net/gre.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)
32 goto err_out;
34 spin_lock(&gre_proto_lock);
35 if (gre_proto[version])
36 goto err_out_unlock;
38 rcu_assign_pointer(gre_proto[version], proto);
39 spin_unlock(&gre_proto_lock);
40 return 0;
42 err_out_unlock:
43 spin_unlock(&gre_proto_lock);
44 err_out:
45 return -1;
47 EXPORT_SYMBOL_GPL(gre_add_protocol);
49 int gre_del_protocol(const struct gre_protocol *proto, u8 version)
51 if (version >= GREPROTO_MAX)
52 goto err_out;
54 spin_lock(&gre_proto_lock);
55 if (rcu_dereference_protected(gre_proto[version],
56 lockdep_is_held(&gre_proto_lock)) != proto)
57 goto err_out_unlock;
58 rcu_assign_pointer(gre_proto[version], NULL);
59 spin_unlock(&gre_proto_lock);
60 synchronize_rcu();
61 return 0;
63 err_out_unlock:
64 spin_unlock(&gre_proto_lock);
65 err_out:
66 return -1;
68 EXPORT_SYMBOL_GPL(gre_del_protocol);
70 static int gre_rcv(struct sk_buff *skb)
72 const struct gre_protocol *proto;
73 u8 ver;
74 int ret;
76 if (!pskb_may_pull(skb, 12))
77 goto drop;
79 ver = skb->data[1]&0x7f;
80 if (ver >= GREPROTO_MAX)
81 goto drop;
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;
91 drop_unlock:
92 rcu_read_unlock();
93 drop:
94 kfree_skb(skb);
95 return NET_RX_DROP;
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)
105 return;
107 rcu_read_lock();
108 proto = rcu_dereference(gre_proto[ver]);
109 if (proto && proto->err_handler)
110 proto->err_handler(skb, info);
111 rcu_read_unlock();
114 static const struct net_protocol net_gre_protocol = {
115 .handler = gre_rcv,
116 .err_handler = gre_err,
117 .netns_ok = 1,
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");
126 return -EAGAIN;
129 return 0;
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");