blob: 3752bd3e92ce9b2f636e0cc8f5a6e2f541683054 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
4 *
5 * Copyright (C)2003 USAGI/WIDE Project
6 *
7 * Author Mitsuru KANDA <mk@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09009/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * [Memo]
11 *
12 * Outbound:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090013 * The compression of IP datagram MUST be done before AH/ESP processing,
14 * fragmentation, and the addition of Hop-by-Hop/Routing header.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
16 * Inbound:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090017 * The decompression of IP datagram MUST be done after the reassembly,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * AH/ESP processing.
19 */
Joe Perchesf3213832012-05-15 14:11:53 +000020
21#define pr_fmt(fmt) "IPv6: " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <net/ip.h>
25#include <net/xfrm.h>
26#include <net/ipcomp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/crypto.h>
Herbert Xu4999f362007-11-07 02:21:47 -080028#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pfkeyv2.h>
30#include <linux/random.h>
31#include <linux/percpu.h>
32#include <linux/smp.h>
33#include <linux/list.h>
34#include <linux/vmalloc.h>
35#include <linux/rtnetlink.h>
David S. Miller81aded22012-06-15 14:54:11 -070036#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/icmp.h>
38#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020039#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ipv6.h>
41#include <linux/icmpv6.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Steffen Klassert59b84352014-03-14 07:28:07 +010044static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -070045 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Alexey Dobriyand74340d2010-01-25 10:39:09 +000047 struct net *net = dev_net(skb->dev);
Al Viroa94cfd12006-09-27 18:47:24 -070048 __be32 spi;
Eric Dumazetb71d1d42011-04-22 04:53:02 +000049 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
Herbert Xu87bdc482007-10-10 15:45:25 -070050 struct ip_comp_hdr *ipcomph =
51 (struct ip_comp_hdr *)(skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct xfrm_state *x;
53
Steffen Klassertb3b2b9e2013-09-10 13:43:09 +020054 if (type != ICMPV6_PKT_TOOBIG &&
David S. Millerec18d9a2012-07-12 00:25:15 -070055 type != NDISC_REDIRECT)
Steffen Klassert59b84352014-03-14 07:28:07 +010056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexey Dobriyan4195f812006-05-22 16:53:22 -070058 spi = htonl(ntohs(ipcomph->cpi));
Eric Dumazetb71d1d42011-04-22 04:53:02 +000059 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
60 spi, IPPROTO_COMP, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!x)
Steffen Klassert59b84352014-03-14 07:28:07 +010062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David S. Millerec18d9a2012-07-12 00:25:15 -070064 if (type == NDISC_REDIRECT)
Lorenzo Colittie2d118a2016-11-04 02:23:43 +090065 ip6_redirect(skb, net, skb->dev->ifindex, 0,
66 sock_net_uid(net, NULL));
David S. Millerec18d9a2012-07-12 00:25:15 -070067 else
Lorenzo Colittie2d118a2016-11-04 02:23:43 +090068 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 xfrm_state_put(x);
Steffen Klassert59b84352014-03-14 07:28:07 +010070
71 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
75{
Alexey Dobriyand74340d2010-01-25 10:39:09 +000076 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct xfrm_state *t = NULL;
78
Alexey Dobriyand74340d2010-01-25 10:39:09 +000079 t = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!t)
81 goto out;
82
83 t->id.proto = IPPROTO_IPV6;
Alexey Dobriyand74340d2010-01-25 10:39:09 +000084 t->id.spi = xfrm6_tunnel_alloc_spi(net, (xfrm_address_t *)&x->props.saddr);
Herbert Xu6abaaaa2006-03-26 17:37:54 -080085 if (!t->id.spi)
86 goto error;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
89 memcpy(&t->sel, &x->sel, sizeof(t->sel));
90 t->props.family = AF_INET6;
Herbert Xue40b3282007-11-13 21:39:08 -080091 t->props.mode = x->props.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080093 memcpy(&t->mark, &x->mark, sizeof(t->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Herbert Xu72cb6962005-06-20 13:18:08 -070095 if (xfrm_init_state(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 goto error;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 atomic_set(&t->tunnel_users, 1);
99
100out:
101 return t;
102
103error:
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800104 t->km.state = XFRM_STATE_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 xfrm_state_put(t);
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800106 t = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 goto out;
108}
109
110static int ipcomp6_tunnel_attach(struct xfrm_state *x)
111{
Alexey Dobriyand74340d2010-01-25 10:39:09 +0000112 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int err = 0;
114 struct xfrm_state *t = NULL;
Al Viroa94cfd12006-09-27 18:47:24 -0700115 __be32 spi;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800116 u32 mark = x->mark.m & x->mark.v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Alexey Dobriyand74340d2010-01-25 10:39:09 +0000118 spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800120 t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 spi, IPPROTO_IPV6, AF_INET6);
122 if (!t) {
123 t = ipcomp6_tunnel_create(x);
124 if (!t) {
125 err = -EINVAL;
126 goto out;
127 }
128 xfrm_state_insert(t);
129 xfrm_state_hold(t);
130 }
131 x->tunnel = t;
132 atomic_inc(&t->tunnel_users);
133
134out:
135 return err;
136}
137
Herbert Xu72cb6962005-06-20 13:18:08 -0700138static int ipcomp6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
David S. Miller2c3abab2008-07-27 03:59:24 -0700140 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 x->props.header_len = 0;
Herbert Xuca681452007-10-17 21:35:15 -0700143 switch (x->props.mode) {
Herbert Xuca681452007-10-17 21:35:15 -0700144 case XFRM_MODE_TRANSPORT:
145 break;
146 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 x->props.header_len += sizeof(struct ipv6hdr);
Herbert Xue40b3282007-11-13 21:39:08 -0800148 break;
Herbert Xuca681452007-10-17 21:35:15 -0700149 default:
Herbert Xue40b3282007-11-13 21:39:08 -0800150 goto out;
Herbert Xuca681452007-10-17 21:35:15 -0700151 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900152
Herbert Xu6fccab62008-07-25 02:54:40 -0700153 err = ipcomp_init_state(x);
154 if (err)
Herbert Xue40b3282007-11-13 21:39:08 -0800155 goto out;
156
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700157 if (x->props.mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 err = ipcomp6_tunnel_attach(x);
159 if (err)
Herbert Xu10e74542010-02-15 19:24:30 +0000160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = 0;
164out:
165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Steffen Klassert59b84352014-03-14 07:28:07 +0100168static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
169{
170 return 0;
171}
172
Ian Morriscc24bec2014-08-24 21:53:11 +0100173static const struct xfrm_type ipcomp6_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .description = "IPCOMP6",
175 .owner = THIS_MODULE,
176 .proto = IPPROTO_COMP,
177 .init_state = ipcomp6_init_state,
Herbert Xu6fccab62008-07-25 02:54:40 -0700178 .destructor = ipcomp_destroy,
179 .input = ipcomp_input,
180 .output = ipcomp_output,
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -0700181 .hdr_offset = xfrm6_find_1stfragopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Ian Morriscc24bec2014-08-24 21:53:11 +0100184static struct xfrm6_protocol ipcomp6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .handler = xfrm6_rcv,
Steffen Klassert59b84352014-03-14 07:28:07 +0100186 .cb_handler = ipcomp6_rcv_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .err_handler = ipcomp6_err,
Steffen Klassert59b84352014-03-14 07:28:07 +0100188 .priority = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191static int __init ipcomp6_init(void)
192{
193 if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +0000194 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EAGAIN;
196 }
Steffen Klassert59b84352014-03-14 07:28:07 +0100197 if (xfrm6_protocol_register(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +0000198 pr_info("%s: can't add protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 xfrm_unregister_type(&ipcomp6_type, AF_INET6);
200 return -EAGAIN;
201 }
202 return 0;
203}
204
205static void __exit ipcomp6_fini(void)
206{
Steffen Klassert59b84352014-03-14 07:28:07 +0100207 if (xfrm6_protocol_deregister(&ipcomp6_protocol, IPPROTO_COMP) < 0)
Joe Perchesf3213832012-05-15 14:11:53 +0000208 pr_info("%s: can't remove protocol\n", __func__);
Florian Westphal4f518e82019-05-03 17:46:19 +0200209 xfrm_unregister_type(&ipcomp6_type, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212module_init(ipcomp6_init);
213module_exit(ipcomp6_fini);
214MODULE_LICENSE("GPL");
215MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
216MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");
217
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700218MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_COMP);