blob: 05a5df2febc9d34ed22bdc8749b4a064c4550c14 [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
Joe Perchesafd465032012-03-12 07:03:32 +00006#define pr_fmt(fmt) "IPsec: " fmt
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/skbuff.h>
9#include <linux/module.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080010#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <net/xfrm.h>
12#include <net/ip.h>
13#include <net/protocol.h>
14
15static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
16{
Herbert Xu7b277b12007-10-10 15:44:06 -070017 skb_push(skb, -skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 return 0;
19}
20
Herbert Xue6956332006-04-01 00:52:46 -080021static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Herbert Xu04663d02007-10-17 21:28:06 -070023 return ip_hdr(skb)->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
Herbert Xu72cb6962005-06-20 13:18:08 -070026static int ipip_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070028 if (x->props.mode != XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return -EINVAL;
30
31 if (x->encap)
32 return -EINVAL;
33
34 x->props.header_len = sizeof(struct iphdr);
35
36 return 0;
37}
38
39static void ipip_destroy(struct xfrm_state *x)
40{
41}
42
Eric Dumazet533cb5b2008-01-30 19:11:50 -080043static const struct xfrm_type ipip_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .description = "IPIP",
45 .owner = THIS_MODULE,
46 .proto = IPPROTO_IPIP,
47 .init_state = ipip_init_state,
48 .destructor = ipip_destroy,
49 .input = ipip_xfrm_rcv,
50 .output = ipip_output
51};
52
Herbert Xuc4541b42007-10-17 21:28:53 -070053static int xfrm_tunnel_rcv(struct sk_buff *skb)
54{
Herbert Xub1641062008-01-30 21:48:24 -080055 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
Herbert Xuc4541b42007-10-17 21:28:53 -070056}
57
Herbert Xud2acc342006-03-28 01:12:13 -080058static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
59{
60 return -ENOENT;
61}
62
Eric Dumazet6dcd8142010-08-30 07:04:14 +000063static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070064 .handler = xfrm_tunnel_rcv,
Herbert Xud2acc342006-03-28 01:12:13 -080065 .err_handler = xfrm_tunnel_err,
66 .priority = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Eric Dumazetdfd56b82011-12-10 09:48:31 +000069#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet6dcd8142010-08-30 07:04:14 +000070static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070071 .handler = xfrm_tunnel_rcv,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080072 .err_handler = xfrm_tunnel_err,
73 .priority = 2,
74};
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int __init ipip_init(void)
78{
79 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +000080 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EAGAIN;
82 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080083
84 if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000085 pr_info("%s: can't add xfrm handler for AF_INET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 xfrm_unregister_type(&ipip_type, AF_INET);
87 return -EAGAIN;
88 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +000089#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080090 if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000091 pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080092 xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
93 xfrm_unregister_type(&ipip_type, AF_INET);
94 return -EAGAIN;
95 }
96#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return 0;
98}
99
100static void __exit ipip_fini(void)
101{
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000102#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800103 if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000104 pr_info("%s: can't remove xfrm handler for AF_INET6\n",
105 __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800106#endif
107 if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
Joe Perches058bd4d2012-03-11 18:36:11 +0000108 pr_info("%s: can't remove xfrm handler for AF_INET\n",
109 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +0000111 pr_info("%s: can't remove xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114module_init(ipip_init);
115module_exit(ipip_fini);
116MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700117MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);