Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* xfrm4_tunnel.c: Generic IP tunnel transformer. |
3 | * | |
4 | * Copyright (C) 2003 David S. Miller (davem@redhat.com) | |
5 | */ | |
6 | ||
afd46503 JP |
7 | #define pr_fmt(fmt) "IPsec: " fmt |
8 | ||
1da177e4 LT |
9 | #include <linux/skbuff.h> |
10 | #include <linux/module.h> | |
4a3e2f71 | 11 | #include <linux/mutex.h> |
1da177e4 LT |
12 | #include <net/xfrm.h> |
13 | #include <net/ip.h> | |
14 | #include <net/protocol.h> | |
15 | ||
16 | static int ipip_output(struct xfrm_state *x, struct sk_buff *skb) | |
17 | { | |
7b277b1a | 18 | skb_push(skb, -skb_network_offset(skb)); |
1da177e4 LT |
19 | return 0; |
20 | } | |
21 | ||
e695633e | 22 | static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb) |
1da177e4 | 23 | { |
04663d0b | 24 | return ip_hdr(skb)->protocol; |
1da177e4 LT |
25 | } |
26 | ||
72cb6962 | 27 | static int ipip_init_state(struct xfrm_state *x) |
1da177e4 | 28 | { |
7e49e6de | 29 | if (x->props.mode != XFRM_MODE_TUNNEL) |
1da177e4 LT |
30 | 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 | ||
40 | static void ipip_destroy(struct xfrm_state *x) | |
41 | { | |
42 | } | |
43 | ||
533cb5b0 | 44 | static const struct xfrm_type ipip_type = { |
1da177e4 LT |
45 | .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 | ||
c4541b41 HX |
54 | static int xfrm_tunnel_rcv(struct sk_buff *skb) |
55 | { | |
b1641064 | 56 | return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr); |
c4541b41 HX |
57 | } |
58 | ||
d2acc347 HX |
59 | static int xfrm_tunnel_err(struct sk_buff *skb, u32 info) |
60 | { | |
61 | return -ENOENT; | |
62 | } | |
63 | ||
6dcd814b | 64 | static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = { |
c4541b41 | 65 | .handler = xfrm_tunnel_rcv, |
d2acc347 | 66 | .err_handler = xfrm_tunnel_err, |
7fe94612 | 67 | .priority = 4, |
1da177e4 LT |
68 | }; |
69 | ||
dfd56b8b | 70 | #if IS_ENABLED(CONFIG_IPV6) |
6dcd814b | 71 | static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = { |
c4541b41 | 72 | .handler = xfrm_tunnel_rcv, |
c0d56408 | 73 | .err_handler = xfrm_tunnel_err, |
7fe94612 | 74 | .priority = 3, |
c0d56408 KM |
75 | }; |
76 | #endif | |
77 | ||
1da177e4 LT |
78 | static int __init ipip_init(void) |
79 | { | |
80 | if (xfrm_register_type(&ipip_type, AF_INET) < 0) { | |
058bd4d2 | 81 | pr_info("%s: can't add xfrm type\n", __func__); |
1da177e4 LT |
82 | return -EAGAIN; |
83 | } | |
c0d56408 KM |
84 | |
85 | if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) { | |
058bd4d2 | 86 | pr_info("%s: can't add xfrm handler for AF_INET\n", __func__); |
c0d56408 KM |
87 | xfrm_unregister_type(&ipip_type, AF_INET); |
88 | return -EAGAIN; | |
89 | } | |
dfd56b8b | 90 | #if IS_ENABLED(CONFIG_IPV6) |
c0d56408 | 91 | if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) { |
058bd4d2 | 92 | pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__); |
c0d56408 | 93 | xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET); |
1da177e4 LT |
94 | xfrm_unregister_type(&ipip_type, AF_INET); |
95 | return -EAGAIN; | |
96 | } | |
c0d56408 | 97 | #endif |
1da177e4 LT |
98 | return 0; |
99 | } | |
100 | ||
101 | static void __exit ipip_fini(void) | |
102 | { | |
dfd56b8b | 103 | #if IS_ENABLED(CONFIG_IPV6) |
c0d56408 | 104 | if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6)) |
058bd4d2 JP |
105 | pr_info("%s: can't remove xfrm handler for AF_INET6\n", |
106 | __func__); | |
c0d56408 KM |
107 | #endif |
108 | if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET)) | |
058bd4d2 JP |
109 | pr_info("%s: can't remove xfrm handler for AF_INET\n", |
110 | __func__); | |
4f518e80 | 111 | xfrm_unregister_type(&ipip_type, AF_INET); |
1da177e4 LT |
112 | } |
113 | ||
114 | module_init(ipip_init); | |
115 | module_exit(ipip_fini); | |
116 | MODULE_LICENSE("GPL"); | |
d3d6dd3a | 117 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP); |