blob: b08d56b117f837e226cbf7085fb3badfd515064d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm4_tunnel.c: Generic IP tunnel transformer.
2 *
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4 */
5
6#include <linux/skbuff.h>
7#include <linux/module.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08008#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <net/xfrm.h>
10#include <net/ip.h>
11#include <net/protocol.h>
12
13static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
14{
15 struct iphdr *iph;
16
17 iph = skb->nh.iph;
18 iph->tot_len = htons(skb->len);
19 ip_send_check(iph);
20
21 return 0;
22}
23
24static int ipip_xfrm_rcv(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
25{
26 return 0;
27}
28
29static struct xfrm_tunnel *ipip_handler;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080030static DEFINE_MUTEX(xfrm4_tunnel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32int xfrm4_tunnel_register(struct xfrm_tunnel *handler)
33{
34 int ret;
35
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080036 mutex_lock(&xfrm4_tunnel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 ret = 0;
38 if (ipip_handler != NULL)
39 ret = -EINVAL;
40 if (!ret)
41 ipip_handler = handler;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042 mutex_unlock(&xfrm4_tunnel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 return ret;
45}
46
47EXPORT_SYMBOL(xfrm4_tunnel_register);
48
49int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler)
50{
51 int ret;
52
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080053 mutex_lock(&xfrm4_tunnel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ret = 0;
55 if (ipip_handler != handler)
56 ret = -EINVAL;
57 if (!ret)
58 ipip_handler = NULL;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080059 mutex_unlock(&xfrm4_tunnel_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 synchronize_net();
62
63 return ret;
64}
65
66EXPORT_SYMBOL(xfrm4_tunnel_deregister);
67
68static int ipip_rcv(struct sk_buff *skb)
69{
70 struct xfrm_tunnel *handler = ipip_handler;
71
72 /* Tunnel devices take precedence. */
73 if (handler && handler->handler(skb) == 0)
74 return 0;
75
76 return xfrm4_rcv(skb);
77}
78
79static void ipip_err(struct sk_buff *skb, u32 info)
80{
81 struct xfrm_tunnel *handler = ipip_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 if (handler)
Patrick McHardy03037702005-07-19 14:03:34 -070084 handler->err_handler(skb, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Herbert Xu72cb6962005-06-20 13:18:08 -070087static int ipip_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 if (!x->props.mode)
90 return -EINVAL;
91
92 if (x->encap)
93 return -EINVAL;
94
95 x->props.header_len = sizeof(struct iphdr);
96
97 return 0;
98}
99
100static void ipip_destroy(struct xfrm_state *x)
101{
102}
103
104static struct xfrm_type ipip_type = {
105 .description = "IPIP",
106 .owner = THIS_MODULE,
107 .proto = IPPROTO_IPIP,
108 .init_state = ipip_init_state,
109 .destructor = ipip_destroy,
110 .input = ipip_xfrm_rcv,
111 .output = ipip_output
112};
113
114static struct net_protocol ipip_protocol = {
115 .handler = ipip_rcv,
116 .err_handler = ipip_err,
117 .no_policy = 1,
118};
119
120static int __init ipip_init(void)
121{
122 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
123 printk(KERN_INFO "ipip init: can't add xfrm type\n");
124 return -EAGAIN;
125 }
126 if (inet_add_protocol(&ipip_protocol, IPPROTO_IPIP) < 0) {
127 printk(KERN_INFO "ipip init: can't add protocol\n");
128 xfrm_unregister_type(&ipip_type, AF_INET);
129 return -EAGAIN;
130 }
131 return 0;
132}
133
134static void __exit ipip_fini(void)
135{
136 if (inet_del_protocol(&ipip_protocol, IPPROTO_IPIP) < 0)
137 printk(KERN_INFO "ipip close: can't remove protocol\n");
138 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
139 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
140}
141
142module_init(ipip_init);
143module_exit(ipip_fini);
144MODULE_LICENSE("GPL");