blob: 1312417608e2a34ba7c6e05746d605b2930d05bf [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{
Herbert Xu7b277b12007-10-10 15:44:06 -070015 skb_push(skb, -skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 return 0;
17}
18
Herbert Xue6956332006-04-01 00:52:46 -080019static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Herbert Xu631a6692007-10-10 15:46:21 -070021 return IPPROTO_IP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022}
23
Herbert Xu72cb6962005-06-20 13:18:08 -070024static int ipip_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070026 if (x->props.mode != XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return -EINVAL;
28
29 if (x->encap)
30 return -EINVAL;
31
32 x->props.header_len = sizeof(struct iphdr);
33
34 return 0;
35}
36
37static void ipip_destroy(struct xfrm_state *x)
38{
39}
40
41static struct xfrm_type ipip_type = {
42 .description = "IPIP",
43 .owner = THIS_MODULE,
44 .proto = IPPROTO_IPIP,
45 .init_state = ipip_init_state,
46 .destructor = ipip_destroy,
47 .input = ipip_xfrm_rcv,
48 .output = ipip_output
49};
50
Herbert Xud2acc342006-03-28 01:12:13 -080051static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
52{
53 return -ENOENT;
54}
55
56static struct xfrm_tunnel xfrm_tunnel_handler = {
57 .handler = xfrm4_rcv,
58 .err_handler = xfrm_tunnel_err,
59 .priority = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080062#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
63static struct xfrm_tunnel xfrm64_tunnel_handler = {
64 .handler = xfrm4_rcv,
65 .err_handler = xfrm_tunnel_err,
66 .priority = 2,
67};
68#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int __init ipip_init(void)
71{
72 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
73 printk(KERN_INFO "ipip init: can't add xfrm type\n");
74 return -EAGAIN;
75 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080076
77 if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
78 printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 xfrm_unregister_type(&ipip_type, AF_INET);
80 return -EAGAIN;
81 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080082#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
83 if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
84 printk(KERN_INFO "ipip init: can't add xfrm handler for AF_INET6\n");
85 xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
86 xfrm_unregister_type(&ipip_type, AF_INET);
87 return -EAGAIN;
88 }
89#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91}
92
93static void __exit ipip_fini(void)
94{
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080095#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
96 if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
97 printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET6\n");
98#endif
99 if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
100 printk(KERN_INFO "ipip close: can't remove xfrm handler for AF_INET\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
102 printk(KERN_INFO "ipip close: can't remove xfrm type\n");
103}
104
105module_init(ipip_init);
106module_exit(ipip_fini);
107MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700108MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);