Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* xfrm4_tunnel.c: Generic IP tunnel transformer. |
| 3 | * |
| 4 | * Copyright (C) 2003 David S. Miller (davem@redhat.com) |
| 5 | */ |
| 6 | |
Joe Perches | afd46503 | 2012-03-12 07:03:32 +0000 | [diff] [blame] | 7 | #define pr_fmt(fmt) "IPsec: " fmt |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/skbuff.h> |
| 10 | #include <linux/module.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 18 | skb_push(skb, -skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | return 0; |
| 20 | } |
| 21 | |
Herbert Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 22 | static int ipip_xfrm_rcv(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Herbert Xu | 04663d0 | 2007-10-17 21:28:06 -0700 | [diff] [blame] | 24 | return ip_hdr(skb)->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 27 | static int ipip_init_state(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 29 | if (x->props.mode != XFRM_MODE_TUNNEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 44 | static const struct xfrm_type ipip_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 54 | static int xfrm_tunnel_rcv(struct sk_buff *skb) |
| 55 | { |
Herbert Xu | b164106 | 2008-01-30 21:48:24 -0800 | [diff] [blame] | 56 | return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr); |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 59 | static int xfrm_tunnel_err(struct sk_buff *skb, u32 info) |
| 60 | { |
| 61 | return -ENOENT; |
| 62 | } |
| 63 | |
Eric Dumazet | 6dcd814 | 2010-08-30 07:04:14 +0000 | [diff] [blame] | 64 | static struct xfrm_tunnel xfrm_tunnel_handler __read_mostly = { |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 65 | .handler = xfrm_tunnel_rcv, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 66 | .err_handler = xfrm_tunnel_err, |
Nicolas Dichtel | 32b8a8e | 2013-05-27 23:48:16 +0000 | [diff] [blame] | 67 | .priority = 3, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 70 | #if IS_ENABLED(CONFIG_IPV6) |
Eric Dumazet | 6dcd814 | 2010-08-30 07:04:14 +0000 | [diff] [blame] | 71 | static struct xfrm_tunnel xfrm64_tunnel_handler __read_mostly = { |
Herbert Xu | c4541b4 | 2007-10-17 21:28:53 -0700 | [diff] [blame] | 72 | .handler = xfrm_tunnel_rcv, |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 73 | .err_handler = xfrm_tunnel_err, |
| 74 | .priority = 2, |
| 75 | }; |
| 76 | #endif |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static int __init ipip_init(void) |
| 79 | { |
| 80 | if (xfrm_register_type(&ipip_type, AF_INET) < 0) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 81 | pr_info("%s: can't add xfrm type\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return -EAGAIN; |
| 83 | } |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 84 | |
| 85 | if (xfrm4_tunnel_register(&xfrm_tunnel_handler, AF_INET)) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 86 | pr_info("%s: can't add xfrm handler for AF_INET\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | xfrm_unregister_type(&ipip_type, AF_INET); |
| 88 | return -EAGAIN; |
| 89 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 90 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 91 | if (xfrm4_tunnel_register(&xfrm64_tunnel_handler, AF_INET6)) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 92 | pr_info("%s: can't add xfrm handler for AF_INET6\n", __func__); |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 93 | xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET); |
| 94 | xfrm_unregister_type(&ipip_type, AF_INET); |
| 95 | return -EAGAIN; |
| 96 | } |
| 97 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void __exit ipip_fini(void) |
| 102 | { |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 103 | #if IS_ENABLED(CONFIG_IPV6) |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 104 | if (xfrm4_tunnel_deregister(&xfrm64_tunnel_handler, AF_INET6)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 105 | pr_info("%s: can't remove xfrm handler for AF_INET6\n", |
| 106 | __func__); |
Kazunori MIYAZAWA | c0d5640 | 2007-02-13 12:54:47 -0800 | [diff] [blame] | 107 | #endif |
| 108 | if (xfrm4_tunnel_deregister(&xfrm_tunnel_handler, AF_INET)) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 109 | pr_info("%s: can't remove xfrm handler for AF_INET\n", |
| 110 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (xfrm_unregister_type(&ipip_type, AF_INET) < 0) |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 112 | pr_info("%s: can't remove xfrm type\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | module_init(ipip_init); |
| 116 | module_exit(ipip_fini); |
| 117 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 118 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_IPIP); |