blob: 2b31112c0856bd8136ea6283a601e4c392976726 [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 * Copyright (C)2003,2004 USAGI/WIDE Project
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors Mitsuru KANDA <mk@linux-ipv6.org>
Ian Morris67ba4152014-08-24 21:53:10 +01006 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on net/ipv4/xfrm4_tunnel.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/xfrm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000013#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/ip.h>
15#include <net/xfrm.h>
16#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ipv6.h>
18#include <linux/icmpv6.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080019#include <linux/mutex.h>
Alexey Dobriyana1664772010-01-25 10:37:54 +000020#include <net/netns/generic.h>
21
22#define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
23#define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
24
25#define XFRM6_TUNNEL_SPI_MIN 1
26#define XFRM6_TUNNEL_SPI_MAX 0xffffffff
27
28struct xfrm6_tunnel_net {
29 struct hlist_head spi_byaddr[XFRM6_TUNNEL_SPI_BYADDR_HSIZE];
30 struct hlist_head spi_byspi[XFRM6_TUNNEL_SPI_BYSPI_HSIZE];
31 u32 spi;
32};
33
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030034static unsigned int xfrm6_tunnel_net_id __read_mostly;
Alexey Dobriyana1664772010-01-25 10:37:54 +000035static inline struct xfrm6_tunnel_net *xfrm6_tunnel_pernet(struct net *net)
36{
37 return net_generic(net, xfrm6_tunnel_net_id);
38}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090041 * xfrm_tunnel_spi things are for allocating unique id ("spi")
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * per xfrm_address_t.
43 */
44struct xfrm6_tunnel_spi {
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000045 struct hlist_node list_byaddr;
46 struct hlist_node list_byspi;
47 xfrm_address_t addr;
48 u32 spi;
Reshetova, Elenad12f3822017-07-04 09:34:59 +030049 refcount_t refcnt;
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000050 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000053static DEFINE_SPINLOCK(xfrm6_tunnel_spi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Lametere18b8902006-12-06 20:33:20 -080055static struct kmem_cache *xfrm6_tunnel_spi_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Dumazet95c96172012-04-15 05:58:06 +000057static inline unsigned int xfrm6_tunnel_spi_hash_byaddr(const xfrm_address_t *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Eric Dumazet95c96172012-04-15 05:58:06 +000059 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
YOSHIFUJI Hideaki / 吉藤英明2b464f62013-01-13 05:02:38 +000061 h = ipv6_addr_hash((const struct in6_addr *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 h ^= h >> 16;
63 h ^= h >> 8;
64 h &= XFRM6_TUNNEL_SPI_BYADDR_HSIZE - 1;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return h;
67}
68
Eric Dumazet95c96172012-04-15 05:58:06 +000069static inline unsigned int xfrm6_tunnel_spi_hash_byspi(u32 spi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 return spi % XFRM6_TUNNEL_SPI_BYSPI_HSIZE;
72}
73
Eric Dumazetb71d1d42011-04-22 04:53:02 +000074static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Alexey Dobriyana1664772010-01-25 10:37:54 +000076 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct xfrm6_tunnel_spi *x6spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Sasha Levinb67bfe02013-02-27 17:06:00 -080079 hlist_for_each_entry_rcu(x6spi,
Alexey Dobriyana1664772010-01-25 10:37:54 +000080 &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
Madhuparna Bhowmikedf0d282020-02-21 21:54:47 +053081 list_byaddr, lockdep_is_held(&xfrm6_tunnel_spi_lock)) {
YOSHIFUJI Hideaki / 吉藤英明ff88b302013-01-29 12:48:31 +000082 if (xfrm6_addr_equal(&x6spi->addr, saddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return x6spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return NULL;
87}
88
Eric Dumazetb71d1d42011-04-22 04:53:02 +000089__be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct xfrm6_tunnel_spi *x6spi;
92 u32 spi;
93
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000094 rcu_read_lock_bh();
Alexey Dobriyana1664772010-01-25 10:37:54 +000095 x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 spi = x6spi ? x6spi->spi : 0;
Eric Dumazet91cc3bb2009-10-23 18:19:19 +000097 rcu_read_unlock_bh();
Al Viro5b122542006-11-01 15:28:58 -080098 return htonl(spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100EXPORT_SYMBOL(xfrm6_tunnel_spi_lookup);
101
Alexey Dobriyana1664772010-01-25 10:37:54 +0000102static int __xfrm6_tunnel_spi_check(struct net *net, u32 spi)
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900103{
Alexey Dobriyana1664772010-01-25 10:37:54 +0000104 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900105 struct xfrm6_tunnel_spi *x6spi;
106 int index = xfrm6_tunnel_spi_hash_byspi(spi);
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900107
Sasha Levinb67bfe02013-02-27 17:06:00 -0800108 hlist_for_each_entry(x6spi,
Alexey Dobriyana1664772010-01-25 10:37:54 +0000109 &xfrm6_tn->spi_byspi[index],
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900110 list_byspi) {
111 if (x6spi->spi == spi)
112 return -1;
113 }
114 return index;
115}
116
Alexey Dobriyana1664772010-01-25 10:37:54 +0000117static u32 __xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Alexey Dobriyana1664772010-01-25 10:37:54 +0000119 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 u32 spi;
121 struct xfrm6_tunnel_spi *x6spi;
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900122 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Alexey Dobriyana1664772010-01-25 10:37:54 +0000124 if (xfrm6_tn->spi < XFRM6_TUNNEL_SPI_MIN ||
125 xfrm6_tn->spi >= XFRM6_TUNNEL_SPI_MAX)
126 xfrm6_tn->spi = XFRM6_TUNNEL_SPI_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 else
Alexey Dobriyana1664772010-01-25 10:37:54 +0000128 xfrm6_tn->spi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Alexey Dobriyana1664772010-01-25 10:37:54 +0000130 for (spi = xfrm6_tn->spi; spi <= XFRM6_TUNNEL_SPI_MAX; spi++) {
131 index = __xfrm6_tunnel_spi_check(net, spi);
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900132 if (index >= 0)
133 goto alloc_spi;
YueHaibingfa89a452018-12-19 14:45:09 +0800134
135 if (spi == XFRM6_TUNNEL_SPI_MAX)
136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Alexey Dobriyana1664772010-01-25 10:37:54 +0000138 for (spi = XFRM6_TUNNEL_SPI_MIN; spi < xfrm6_tn->spi; spi++) {
139 index = __xfrm6_tunnel_spi_check(net, spi);
YOSHIFUJI Hideakidf8ea192008-02-19 22:54:00 +0900140 if (index >= 0)
141 goto alloc_spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 spi = 0;
144 goto out;
145alloc_spi:
Alexey Dobriyana1664772010-01-25 10:37:54 +0000146 xfrm6_tn->spi = spi;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800147 x6spi = kmem_cache_alloc(xfrm6_tunnel_spi_kmem, GFP_ATOMIC);
David S. Millera922ba52006-07-24 13:49:06 -0700148 if (!x6spi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out;
David S. Millera922ba52006-07-24 13:49:06 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 memcpy(&x6spi->addr, saddr, sizeof(x6spi->addr));
152 x6spi->spi = spi;
Reshetova, Elenad12f3822017-07-04 09:34:59 +0300153 refcount_set(&x6spi->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Alexey Dobriyana1664772010-01-25 10:37:54 +0000155 hlist_add_head_rcu(&x6spi->list_byspi, &xfrm6_tn->spi_byspi[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 index = xfrm6_tunnel_spi_hash_byaddr(saddr);
Alexey Dobriyana1664772010-01-25 10:37:54 +0000158 hlist_add_head_rcu(&x6spi->list_byaddr, &xfrm6_tn->spi_byaddr[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return spi;
161}
162
Alexey Dobriyana1664772010-01-25 10:37:54 +0000163__be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct xfrm6_tunnel_spi *x6spi;
166 u32 spi;
167
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000168 spin_lock_bh(&xfrm6_tunnel_spi_lock);
Alexey Dobriyana1664772010-01-25 10:37:54 +0000169 x6spi = __xfrm6_tunnel_spi_lookup(net, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (x6spi) {
Reshetova, Elenad12f3822017-07-04 09:34:59 +0300171 refcount_inc(&x6spi->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spi = x6spi->spi;
173 } else
Alexey Dobriyana1664772010-01-25 10:37:54 +0000174 spi = __xfrm6_tunnel_alloc_spi(net, saddr);
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000175 spin_unlock_bh(&xfrm6_tunnel_spi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Al Viro5b122542006-11-01 15:28:58 -0800177 return htonl(spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179EXPORT_SYMBOL(xfrm6_tunnel_alloc_spi);
180
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000181static void x6spi_destroy_rcu(struct rcu_head *head)
182{
183 kmem_cache_free(xfrm6_tunnel_spi_kmem,
184 container_of(head, struct xfrm6_tunnel_spi, rcu_head));
185}
186
stephen hemminger6f747ac2010-10-15 05:15:59 +0000187static void xfrm6_tunnel_free_spi(struct net *net, xfrm_address_t *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Alexey Dobriyana1664772010-01-25 10:37:54 +0000189 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct xfrm6_tunnel_spi *x6spi;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800191 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000193 spin_lock_bh(&xfrm6_tunnel_spi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Sasha Levinb67bfe02013-02-27 17:06:00 -0800195 hlist_for_each_entry_safe(x6spi, n,
Alexey Dobriyana1664772010-01-25 10:37:54 +0000196 &xfrm6_tn->spi_byaddr[xfrm6_tunnel_spi_hash_byaddr(saddr)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 list_byaddr)
198 {
YOSHIFUJI Hideaki / 吉藤英明ff88b302013-01-29 12:48:31 +0000199 if (xfrm6_addr_equal(&x6spi->addr, saddr)) {
Reshetova, Elenad12f3822017-07-04 09:34:59 +0300200 if (refcount_dec_and_test(&x6spi->refcnt)) {
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000201 hlist_del_rcu(&x6spi->list_byaddr);
202 hlist_del_rcu(&x6spi->list_byspi);
203 call_rcu(&x6spi->rcu_head, x6spi_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 }
206 }
207 }
Eric Dumazet91cc3bb2009-10-23 18:19:19 +0000208 spin_unlock_bh(&xfrm6_tunnel_spi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int xfrm6_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
212{
Herbert Xu7b277b12007-10-10 15:44:06 -0700213 skb_push(skb, -skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
Herbert Xue6956332006-04-01 00:52:46 -0800217static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Herbert Xu04663d02007-10-17 21:28:06 -0700219 return skb_network_header(skb)[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Herbert Xud2acc342006-03-28 01:12:13 -0800222static int xfrm6_tunnel_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Alexey Dobriyana1664772010-01-25 10:37:54 +0000224 struct net *net = dev_net(skb->dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000225 const struct ipv6hdr *iph = ipv6_hdr(skb);
Al Viroa252cc22006-09-27 18:48:18 -0700226 __be32 spi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000228 spi = xfrm6_tunnel_spi_lookup(net, (const xfrm_address_t *)&iph->saddr);
Nicolas Dichtel63c43782016-09-19 16:17:57 +0200229 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Herbert Xud2acc342006-03-28 01:12:13 -0800232static int xfrm6_tunnel_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700233 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* xfrm6_tunnel native err handling */
236 switch (type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900237 case ICMPV6_DEST_UNREACH:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (code) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900239 case ICMPV6_NOROUTE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case ICMPV6_ADM_PROHIBITED:
241 case ICMPV6_NOT_NEIGHBOUR:
242 case ICMPV6_ADDR_UNREACH:
243 case ICMPV6_PORT_UNREACH:
244 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 }
247 break;
248 case ICMPV6_PKT_TOOBIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 case ICMPV6_TIME_EXCEED:
251 switch (code) {
252 case ICMPV6_EXC_HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
254 case ICMPV6_EXC_FRAGTIME:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900255 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
258 break;
259 case ICMPV6_PARAMPROB:
260 switch (code) {
261 case ICMPV6_HDR_FIELD: break;
262 case ICMPV6_UNK_NEXTHDR: break;
263 case ICMPV6_UNK_OPTION: break;
264 }
265 break;
266 default:
267 break;
268 }
Herbert Xud2acc342006-03-28 01:12:13 -0800269
270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Herbert Xu72cb6962005-06-20 13:18:08 -0700273static int xfrm6_tunnel_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700275 if (x->props.mode != XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
277
278 if (x->encap)
279 return -EINVAL;
280
281 x->props.header_len = sizeof(struct ipv6hdr);
282
283 return 0;
284}
285
286static void xfrm6_tunnel_destroy(struct xfrm_state *x)
287{
Alexey Dobriyana1664772010-01-25 10:37:54 +0000288 struct net *net = xs_net(x);
289
290 xfrm6_tunnel_free_spi(net, (xfrm_address_t *)&x->props.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800293static const struct xfrm_type xfrm6_tunnel_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .owner = THIS_MODULE,
295 .proto = IPPROTO_IPV6,
296 .init_state = xfrm6_tunnel_init_state,
297 .destructor = xfrm6_tunnel_destroy,
298 .input = xfrm6_tunnel_input,
299 .output = xfrm6_tunnel_output,
300};
301
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +0000302static struct xfrm6_tunnel xfrm6_tunnel_handler __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .handler = xfrm6_tunnel_rcv,
Herbert Xud2acc342006-03-28 01:12:13 -0800304 .err_handler = xfrm6_tunnel_err,
Xin Long7fe94612020-10-08 16:13:24 +0800305 .priority = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +0000308static struct xfrm6_tunnel xfrm46_tunnel_handler __read_mostly = {
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800309 .handler = xfrm6_tunnel_rcv,
310 .err_handler = xfrm6_tunnel_err,
Xin Long7fe94612020-10-08 16:13:24 +0800311 .priority = 3,
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800312};
313
Alexey Dobriyana1664772010-01-25 10:37:54 +0000314static int __net_init xfrm6_tunnel_net_init(struct net *net)
315{
316 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
317 unsigned int i;
318
319 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
320 INIT_HLIST_HEAD(&xfrm6_tn->spi_byaddr[i]);
321 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
322 INIT_HLIST_HEAD(&xfrm6_tn->spi_byspi[i]);
323 xfrm6_tn->spi = 0;
324
325 return 0;
326}
327
328static void __net_exit xfrm6_tunnel_net_exit(struct net *net)
329{
Vasily Averinbaeb0db2017-11-12 22:34:03 +0300330 struct xfrm6_tunnel_net *xfrm6_tn = xfrm6_tunnel_pernet(net);
331 unsigned int i;
332
Steffen Klassertb48c05a2018-04-16 07:50:09 +0200333 xfrm_flush_gc();
Cong Wangdbb24832019-03-22 16:26:19 -0700334 xfrm_state_flush(net, 0, false, true);
Steffen Klassertb48c05a2018-04-16 07:50:09 +0200335
Vasily Averinbaeb0db2017-11-12 22:34:03 +0300336 for (i = 0; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE; i++)
337 WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byaddr[i]));
338
339 for (i = 0; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE; i++)
340 WARN_ON_ONCE(!hlist_empty(&xfrm6_tn->spi_byspi[i]));
Alexey Dobriyana1664772010-01-25 10:37:54 +0000341}
342
343static struct pernet_operations xfrm6_tunnel_net_ops = {
344 .init = xfrm6_tunnel_net_init,
345 .exit = xfrm6_tunnel_net_exit,
346 .id = &xfrm6_tunnel_net_id,
347 .size = sizeof(struct xfrm6_tunnel_net),
348};
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int __init xfrm6_tunnel_init(void)
351{
Alexey Dobriyane9249602010-01-25 10:28:21 +0000352 int rv;
353
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000354 xfrm6_tunnel_spi_kmem = kmem_cache_create("xfrm6_tunnel_spi",
355 sizeof(struct xfrm6_tunnel_spi),
356 0, SLAB_HWCACHE_ALIGN,
357 NULL);
358 if (!xfrm6_tunnel_spi_kmem)
359 return -ENOMEM;
Alexey Dobriyana1664772010-01-25 10:37:54 +0000360 rv = register_pernet_subsys(&xfrm6_tunnel_net_ops);
361 if (rv < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000362 goto out_pernet;
363 rv = xfrm_register_type(&xfrm6_tunnel_type, AF_INET6);
364 if (rv < 0)
365 goto out_type;
366 rv = xfrm6_tunnel_register(&xfrm6_tunnel_handler, AF_INET6);
367 if (rv < 0)
368 goto out_xfrm6;
369 rv = xfrm6_tunnel_register(&xfrm46_tunnel_handler, AF_INET);
370 if (rv < 0)
371 goto out_xfrm46;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
Ilpo Järvinen5ce1bbb2008-12-14 23:13:48 -0800373
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000374out_xfrm46:
Ilpo Järvinen5ce1bbb2008-12-14 23:13:48 -0800375 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000376out_xfrm6:
Ilpo Järvinen5ce1bbb2008-12-14 23:13:48 -0800377 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000378out_type:
379 unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
380out_pernet:
381 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
Alexey Dobriyane9249602010-01-25 10:28:21 +0000382 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static void __exit xfrm6_tunnel_fini(void)
386{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -0800387 xfrm6_tunnel_deregister(&xfrm46_tunnel_handler, AF_INET);
388 xfrm6_tunnel_deregister(&xfrm6_tunnel_handler, AF_INET6);
David S. Millera922ba52006-07-24 13:49:06 -0700389 xfrm_unregister_type(&xfrm6_tunnel_type, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000390 unregister_pernet_subsys(&xfrm6_tunnel_net_ops);
Su Yanjun6ee02a52019-03-14 14:59:42 +0800391 /* Someone maybe has gotten the xfrm6_tunnel_spi.
392 * So need to wait it.
393 */
394 rcu_barrier();
Alexey Dobriyand5aa4072010-02-16 09:05:04 +0000395 kmem_cache_destroy(xfrm6_tunnel_spi_kmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398module_init(xfrm6_tunnel_init);
399module_exit(xfrm6_tunnel_fini);
400MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700401MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_IPV6);