blob: 15f984be35705ab7c316e47985f89fd258335c22 [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));
Xin Longd5a7a502020-07-06 20:01:36 +080094 t->if_id = x->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Herbert Xu72cb6962005-06-20 13:18:08 -070096 if (xfrm_init_state(t))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 goto error;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 atomic_set(&t->tunnel_users, 1);
100
101out:
102 return t;
103
104error:
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800105 t->km.state = XFRM_STATE_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 xfrm_state_put(t);
Herbert Xu6abaaaa2006-03-26 17:37:54 -0800107 t = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out;
109}
110
111static int ipcomp6_tunnel_attach(struct xfrm_state *x)
112{
Alexey Dobriyand74340d2010-01-25 10:39:09 +0000113 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int err = 0;
115 struct xfrm_state *t = NULL;
Al Viroa94cfd12006-09-27 18:47:24 -0700116 __be32 spi;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800117 u32 mark = x->mark.m & x->mark.v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Alexey Dobriyand74340d2010-01-25 10:39:09 +0000119 spi = xfrm6_tunnel_spi_lookup(net, (xfrm_address_t *)&x->props.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800121 t = xfrm_state_lookup(net, mark, (xfrm_address_t *)&x->id.daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 spi, IPPROTO_IPV6, AF_INET6);
123 if (!t) {
124 t = ipcomp6_tunnel_create(x);
125 if (!t) {
126 err = -EINVAL;
127 goto out;
128 }
129 xfrm_state_insert(t);
130 xfrm_state_hold(t);
131 }
132 x->tunnel = t;
133 atomic_inc(&t->tunnel_users);
134
135out:
136 return err;
137}
138
Herbert Xu72cb6962005-06-20 13:18:08 -0700139static int ipcomp6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
David S. Miller2c3abab2008-07-27 03:59:24 -0700141 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 x->props.header_len = 0;
Herbert Xuca681452007-10-17 21:35:15 -0700144 switch (x->props.mode) {
Herbert Xuca681452007-10-17 21:35:15 -0700145 case XFRM_MODE_TRANSPORT:
146 break;
147 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 x->props.header_len += sizeof(struct ipv6hdr);
Herbert Xue40b3282007-11-13 21:39:08 -0800149 break;
Herbert Xuca681452007-10-17 21:35:15 -0700150 default:
Herbert Xue40b3282007-11-13 21:39:08 -0800151 goto out;
Herbert Xuca681452007-10-17 21:35:15 -0700152 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900153
Herbert Xu6fccab62008-07-25 02:54:40 -0700154 err = ipcomp_init_state(x);
155 if (err)
Herbert Xue40b3282007-11-13 21:39:08 -0800156 goto out;
157
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700158 if (x->props.mode == XFRM_MODE_TUNNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 err = ipcomp6_tunnel_attach(x);
160 if (err)
Herbert Xu10e74542010-02-15 19:24:30 +0000161 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 err = 0;
165out:
166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Steffen Klassert59b84352014-03-14 07:28:07 +0100169static int ipcomp6_rcv_cb(struct sk_buff *skb, int err)
170{
171 return 0;
172}
173
Ian Morriscc24bec2014-08-24 21:53:11 +0100174static const struct xfrm_type ipcomp6_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Ian Morriscc24bec2014-08-24 21:53:11 +0100183static struct xfrm6_protocol ipcomp6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200185 .input_handler = xfrm_input,
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);