blob: 5d00e54cd3190e829e73479ad0126eca8979a24e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* xfrm4_tunnel.c: Generic IP tunnel transformer.
3 *
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
6
Joe Perchesafd465032012-03-12 07:03:32 +00007#define pr_fmt(fmt) "IPsec: " fmt
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/skbuff.h>
10#include <linux/module.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080011#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <net/xfrm.h>
13#include <net/ip.h>
14#include <net/protocol.h>
15
16static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
17{
Herbert Xu7b277b12007-10-10 15:44:06 -070018 skb_push(skb, -skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 return 0;
20}
21
Herbert Xue6956332006-04-01 00:52:46 -080022static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Herbert Xu04663d02007-10-17 21:28:06 -070024 return ip_hdr(skb)->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
Herbert Xu72cb6962005-06-20 13:18:08 -070027static int ipip_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070029 if (x->props.mode != XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return -EINVAL;
31
32 if (x->encap)
33 return -EINVAL;
34
35 x->props.header_len = sizeof(struct iphdr);
36
37 return 0;
38}
39
40static void ipip_destroy(struct xfrm_state *x)
41{
42}
43
Eric Dumazet533cb5b2008-01-30 19:11:50 -080044static const struct xfrm_type ipip_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .description = "IPIP",
46 .owner = THIS_MODULE,
47 .proto = IPPROTO_IPIP,
48 .init_state = ipip_init_state,
49 .destructor = ipip_destroy,
50 .input = ipip_xfrm_rcv,
51 .output = ipip_output
52};
53
Herbert Xuc4541b42007-10-17 21:28:53 -070054static int xfrm_tunnel_rcv(struct sk_buff *skb)
55{
Herbert Xub1641062008-01-30 21:48:24 -080056 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
Herbert Xuc4541b42007-10-17 21:28:53 -070057}
58
Herbert Xud2acc342006-03-28 01:12:13 -080059static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
60{
61 return -ENOENT;
62}
63
Eric Dumazet6dcd8142010-08-30 07:04:14 +000064static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070065 .handler = xfrm_tunnel_rcv,
Herbert Xud2acc342006-03-28 01:12:13 -080066 .err_handler = xfrm_tunnel_err,
Nicolas Dichtel32b8a8e2013-05-27 23:48:16 +000067 .priority = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Eric Dumazetdfd56b82011-12-10 09:48:31 +000070#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet6dcd8142010-08-30 07:04:14 +000071static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070072 .handler = xfrm_tunnel_rcv,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080073 .err_handler = xfrm_tunnel_err,
74 .priority = 2,
75};
76#endif
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int __init ipip_init(void)
79{
80 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +000081 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -EAGAIN;
83 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080084
85 if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000086 pr_info("%s: can't add xfrm handler for AF_INET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 xfrm_unregister_type(&ipip_type, AF_INET);
88 return -EAGAIN;
89 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +000090#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080091 if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000092 pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080093 xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
94 xfrm_unregister_type(&ipip_type, AF_INET);
95 return -EAGAIN;
96 }
97#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101static void __exit ipip_fini(void)
102{
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000103#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800104 if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000105 pr_info("%s: can't remove xfrm handler for AF_INET6\n",
106 __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800107#endif
108 if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
Joe Perches058bd4d2012-03-11 18:36:11 +0000109 pr_info("%s: can't remove xfrm handler for AF_INET\n",
110 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (xfrm_unregister_type(&ipip_type, AF_INET) < 0)
Joe Perches058bd4d2012-03-11 18:36:11 +0000112 pr_info("%s: can't remove xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115module_init(ipip_init);
116module_exit(ipip_fini);
117MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700118MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);