inet_diag: fix inet_diag_bc_audit()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / tunnel4.c
blob3b3813cc80b9cb7e1c5afcf2f7acfb9dce32f50b
1 /* tunnel4.c: Generic IP tunnel transformer.
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4 */
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/slab.h>
12 #include <net/icmp.h>
13 #include <net/ip.h>
14 #include <net/protocol.h>
15 #include <net/xfrm.h>
17 static struct xfrm_tunnel *tunnel4_handlers;
18 static struct xfrm_tunnel *tunnel64_handlers;
19 static DEFINE_MUTEX(tunnel4_mutex);
21 static inline struct xfrm_tunnel **fam_handlers(unsigned short family)
23 return (family == AF_INET) ? &tunnel4_handlers : &tunnel64_handlers;
26 int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family)
28 struct xfrm_tunnel **pprev;
29 int ret = -EEXIST;
30 int priority = handler->priority;
32 mutex_lock(&tunnel4_mutex);
34 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
35 if ((*pprev)->priority > priority)
36 break;
37 if ((*pprev)->priority == priority)
38 goto err;
41 handler->next = *pprev;
42 *pprev = handler;
44 ret = 0;
46 err:
47 mutex_unlock(&tunnel4_mutex);
49 return ret;
52 EXPORT_SYMBOL(xfrm4_tunnel_register);
54 int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family)
56 struct xfrm_tunnel **pprev;
57 int ret = -ENOENT;
59 mutex_lock(&tunnel4_mutex);
61 for (pprev = fam_handlers(family); *pprev; pprev = &(*pprev)->next) {
62 if (*pprev == handler) {
63 *pprev = handler->next;
64 ret = 0;
65 break;
69 mutex_unlock(&tunnel4_mutex);
71 synchronize_net();
73 return ret;
76 EXPORT_SYMBOL(xfrm4_tunnel_deregister);
78 static int tunnel4_rcv(struct sk_buff *skb)
80 struct xfrm_tunnel *handler;
82 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
83 goto drop;
85 for (handler = tunnel4_handlers; handler; handler = handler->next)
86 if (!handler->handler(skb))
87 return 0;
89 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
91 drop:
92 kfree_skb(skb);
93 return 0;
96 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
97 static int tunnel64_rcv(struct sk_buff *skb)
99 struct xfrm_tunnel *handler;
101 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
102 goto drop;
104 for (handler = tunnel64_handlers; handler; handler = handler->next)
105 if (!handler->handler(skb))
106 return 0;
108 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
110 drop:
111 kfree_skb(skb);
112 return 0;
114 #endif
116 static void tunnel4_err(struct sk_buff *skb, u32 info)
118 struct xfrm_tunnel *handler;
120 for (handler = tunnel4_handlers; handler; handler = handler->next)
121 if (!handler->err_handler(skb, info))
122 break;
125 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
126 static void tunnel64_err(struct sk_buff *skb, u32 info)
128 struct xfrm_tunnel *handler;
130 for (handler = tunnel64_handlers; handler; handler = handler->next)
131 if (!handler->err_handler(skb, info))
132 break;
134 #endif
136 static const struct net_protocol tunnel4_protocol = {
137 .handler = tunnel4_rcv,
138 .err_handler = tunnel4_err,
139 .no_policy = 1,
140 .netns_ok = 1,
143 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
144 static const struct net_protocol tunnel64_protocol = {
145 .handler = tunnel64_rcv,
146 .err_handler = tunnel64_err,
147 .no_policy = 1,
148 .netns_ok = 1,
150 #endif
152 static int __init tunnel4_init(void)
154 if (inet_add_protocol(&tunnel4_protocol, IPPROTO_IPIP)) {
155 printk(KERN_ERR "tunnel4 init: can't add protocol\n");
156 return -EAGAIN;
158 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
159 if (inet_add_protocol(&tunnel64_protocol, IPPROTO_IPV6)) {
160 printk(KERN_ERR "tunnel64 init: can't add protocol\n");
161 inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP);
162 return -EAGAIN;
164 #endif
165 return 0;
168 static void __exit tunnel4_fini(void)
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 if (inet_del_protocol(&tunnel64_protocol, IPPROTO_IPV6))
172 printk(KERN_ERR "tunnel64 close: can't remove protocol\n");
173 #endif
174 if (inet_del_protocol(&tunnel4_protocol, IPPROTO_IPIP))
175 printk(KERN_ERR "tunnel4 close: can't remove protocol\n");
178 module_init(tunnel4_init);
179 module_exit(tunnel4_fini);
180 MODULE_LICENSE("GPL");