blob: 9d4f418f1bf81655a6a16647c032f78539b7e1e1 [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>
11#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <net/protocol.h>
13
14static int ipip_output(struct xfrm_state *x, struct sk_buff *skb)
15{
Herbert Xu7b277b12007-10-10 15:44:06 -070016 skb_push(skb, -skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 return 0;
18}
19
Herbert Xue6956332006-04-01 00:52:46 -080020static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Herbert Xu04663d02007-10-17 21:28:06 -070022 return ip_hdr(skb)->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
Herbert Xu72cb6962005-06-20 13:18:08 -070025static int ipip_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -070027 if (x->props.mode != XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return -EINVAL;
29
30 if (x->encap)
31 return -EINVAL;
32
33 x->props.header_len = sizeof(struct iphdr);
34
35 return 0;
36}
37
38static void ipip_destroy(struct xfrm_state *x)
39{
40}
41
Eric Dumazet533cb5b2008-01-30 19:11:50 -080042static const struct xfrm_type ipip_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .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 Xuc4541b42007-10-17 21:28:53 -070051static int xfrm_tunnel_rcv(struct sk_buff *skb)
52{
Herbert Xub1641062008-01-30 21:48:24 -080053 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
Herbert Xuc4541b42007-10-17 21:28:53 -070054}
55
Herbert Xud2acc342006-03-28 01:12:13 -080056static int xfrm_tunnel_err(struct sk_buff *skb, u32 info)
57{
58 return -ENOENT;
59}
60
Eric Dumazet6dcd8142010-08-30 07:04:14 +000061static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070062 .handler = xfrm_tunnel_rcv,
Herbert Xud2acc342006-03-28 01:12:13 -080063 .err_handler = xfrm_tunnel_err,
Xin Long7fe94612020-10-08 16:13:24 +080064 .priority = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Eric Dumazetdfd56b82011-12-10 09:48:31 +000067#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet6dcd8142010-08-30 07:04:14 +000068static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = {
Herbert Xuc4541b42007-10-17 21:28:53 -070069 .handler = xfrm_tunnel_rcv,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080070 .err_handler = xfrm_tunnel_err,
Xin Long7fe94612020-10-08 16:13:24 +080071 .priority = 3,
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080072};
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int __init ipip_init(void)
76{
77 if (xfrm_register_type(&ipip_type, AF_INET) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +000078 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EAGAIN;
80 }
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080081
82 if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000083 pr_info("%s: can't add xfrm handler for AF_INET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 xfrm_unregister_type(&ipip_type, AF_INET);
85 return -EAGAIN;
86 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +000087#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080088 if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) {
Joe Perches058bd4d2012-03-11 18:36:11 +000089 pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -080090 xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET);
91 xfrm_unregister_type(&ipip_type, AF_INET);
92 return -EAGAIN;
93 }
94#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return 0;
96}
97
98static void __exit ipip_fini(void)
99{
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000100#if IS_ENABLED(CONFIG_IPV6)
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800101 if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6))
Joe Perches058bd4d2012-03-11 18:36:11 +0000102 pr_info("%s: can't remove xfrm handler for AF_INET6\n",
103 __func__);
Kazunori MIYAZAWAc0d56402007-02-13 12:54:47 -0800104#endif
105 if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET))
Joe Perches058bd4d2012-03-11 18:36:11 +0000106 pr_info("%s: can't remove xfrm handler for AF_INET\n",
107 __func__);
Florian Westphal4f518e82019-05-03 17:46:19 +0200108 xfrm_unregister_type(&ipip_type, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111module_init(ipip_init);
112module_exit(ipip_fini);
113MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700114MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP);