3030#include <linux/ipv6.h>
3131#include <linux/icmpv6.h>
3232#include <linux/mutex.h>
33+ #include <net/netns/generic.h>
34+
35+ #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
36+ #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
37+
38+ #define XFRM6_TUNNEL_SPI_MIN 1
39+ #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
40+
41+ struct xfrm6_tunnel_net {
42+ struct hlist_head spi_byaddr [XFRM6_TUNNEL_SPI_BYADDR_HSIZE ];
43+ struct hlist_head spi_byspi [XFRM6_TUNNEL_SPI_BYSPI_HSIZE ];
44+ u32 spi ;
45+ };
46+
47+ static int xfrm6_tunnel_net_id __read_mostly ;
48+ static inline struct xfrm6_tunnel_net * xfrm6_tunnel_pernet (struct net * net )
49+ {
50+ return net_generic (net , xfrm6_tunnel_net_id );
51+ }
3352
3453/*
3554 * xfrm_tunnel_spi things are for allocating unique id ("spi")
@@ -46,19 +65,8 @@ struct xfrm6_tunnel_spi {
4665
4766static DEFINE_SPINLOCK (xfrm6_tunnel_spi_lock );
4867
49- static u32 xfrm6_tunnel_spi ;
50-
51- #define XFRM6_TUNNEL_SPI_MIN 1
52- #define XFRM6_TUNNEL_SPI_MAX 0xffffffff
53-
5468static struct kmem_cache * xfrm6_tunnel_spi_kmem __read_mostly ;
5569
56- #define XFRM6_TUNNEL_SPI_BYADDR_HSIZE 256
57- #define XFRM6_TUNNEL_SPI_BYSPI_HSIZE 256
58-
59- static struct hlist_head xfrm6_tunnel_spi_byaddr [XFRM6_TUNNEL_SPI_BYADDR_HSIZE ];
60- static struct hlist_head xfrm6_tunnel_spi_byspi [XFRM6_TUNNEL_SPI_BYSPI_HSIZE ];
61-
6270static inline unsigned xfrm6_tunnel_spi_hash_byaddr (xfrm_address_t * addr )
6371{
6472 unsigned h ;
@@ -77,49 +85,30 @@ static inline unsigned xfrm6_tunnel_spi_hash_byspi(u32 spi)
7785}
7886
7987
80- static int xfrm6_tunnel_spi_init (void )
88+ static int __init xfrm6_tunnel_spi_init (void )
8189{
82- int i ;
83-
84- xfrm6_tunnel_spi = 0 ;
8590 xfrm6_tunnel_spi_kmem = kmem_cache_create ("xfrm6_tunnel_spi" ,
8691 sizeof (struct xfrm6_tunnel_spi ),
8792 0 , SLAB_HWCACHE_ALIGN ,
8893 NULL );
8994 if (!xfrm6_tunnel_spi_kmem )
9095 return - ENOMEM ;
91-
92- for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE ; i ++ )
93- INIT_HLIST_HEAD (& xfrm6_tunnel_spi_byaddr [i ]);
94- for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE ; i ++ )
95- INIT_HLIST_HEAD (& xfrm6_tunnel_spi_byspi [i ]);
9696 return 0 ;
9797}
9898
9999static void xfrm6_tunnel_spi_fini (void )
100100{
101- int i ;
102-
103- for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE ; i ++ ) {
104- if (!hlist_empty (& xfrm6_tunnel_spi_byaddr [i ]))
105- return ;
106- }
107- for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE ; i ++ ) {
108- if (!hlist_empty (& xfrm6_tunnel_spi_byspi [i ]))
109- return ;
110- }
111- rcu_barrier ();
112101 kmem_cache_destroy (xfrm6_tunnel_spi_kmem );
113- xfrm6_tunnel_spi_kmem = NULL ;
114102}
115103
116- static struct xfrm6_tunnel_spi * __xfrm6_tunnel_spi_lookup (xfrm_address_t * saddr )
104+ static struct xfrm6_tunnel_spi * __xfrm6_tunnel_spi_lookup (struct net * net , xfrm_address_t * saddr )
117105{
106+ struct xfrm6_tunnel_net * xfrm6_tn = xfrm6_tunnel_pernet (net );
118107 struct xfrm6_tunnel_spi * x6spi ;
119108 struct hlist_node * pos ;
120109
121110 hlist_for_each_entry_rcu (x6spi , pos ,
122- & xfrm6_tunnel_spi_byaddr [xfrm6_tunnel_spi_hash_byaddr (saddr )],
111+ & xfrm6_tn -> spi_byaddr [xfrm6_tunnel_spi_hash_byaddr (saddr )],
123112 list_byaddr ) {
124113 if (memcmp (& x6spi -> addr , saddr , sizeof (x6spi -> addr )) == 0 )
125114 return x6spi ;
@@ -128,61 +117,63 @@ static struct xfrm6_tunnel_spi *__xfrm6_tunnel_spi_lookup(xfrm_address_t *saddr)
128117 return NULL ;
129118}
130119
131- __be32 xfrm6_tunnel_spi_lookup (xfrm_address_t * saddr )
120+ __be32 xfrm6_tunnel_spi_lookup (struct net * net , xfrm_address_t * saddr )
132121{
133122 struct xfrm6_tunnel_spi * x6spi ;
134123 u32 spi ;
135124
136125 rcu_read_lock_bh ();
137- x6spi = __xfrm6_tunnel_spi_lookup (saddr );
126+ x6spi = __xfrm6_tunnel_spi_lookup (net , saddr );
138127 spi = x6spi ? x6spi -> spi : 0 ;
139128 rcu_read_unlock_bh ();
140129 return htonl (spi );
141130}
142131
143132EXPORT_SYMBOL (xfrm6_tunnel_spi_lookup );
144133
145- static int __xfrm6_tunnel_spi_check (u32 spi )
134+ static int __xfrm6_tunnel_spi_check (struct net * net , u32 spi )
146135{
136+ struct xfrm6_tunnel_net * xfrm6_tn = xfrm6_tunnel_pernet (net );
147137 struct xfrm6_tunnel_spi * x6spi ;
148138 int index = xfrm6_tunnel_spi_hash_byspi (spi );
149139 struct hlist_node * pos ;
150140
151141 hlist_for_each_entry (x6spi , pos ,
152- & xfrm6_tunnel_spi_byspi [index ],
142+ & xfrm6_tn -> spi_byspi [index ],
153143 list_byspi ) {
154144 if (x6spi -> spi == spi )
155145 return -1 ;
156146 }
157147 return index ;
158148}
159149
160- static u32 __xfrm6_tunnel_alloc_spi (xfrm_address_t * saddr )
150+ static u32 __xfrm6_tunnel_alloc_spi (struct net * net , xfrm_address_t * saddr )
161151{
152+ struct xfrm6_tunnel_net * xfrm6_tn = xfrm6_tunnel_pernet (net );
162153 u32 spi ;
163154 struct xfrm6_tunnel_spi * x6spi ;
164155 int index ;
165156
166- if (xfrm6_tunnel_spi < XFRM6_TUNNEL_SPI_MIN ||
167- xfrm6_tunnel_spi >= XFRM6_TUNNEL_SPI_MAX )
168- xfrm6_tunnel_spi = XFRM6_TUNNEL_SPI_MIN ;
157+ if (xfrm6_tn -> spi < XFRM6_TUNNEL_SPI_MIN ||
158+ xfrm6_tn -> spi >= XFRM6_TUNNEL_SPI_MAX )
159+ xfrm6_tn -> spi = XFRM6_TUNNEL_SPI_MIN ;
169160 else
170- xfrm6_tunnel_spi ++ ;
161+ xfrm6_tn -> spi ++ ;
171162
172- for (spi = xfrm6_tunnel_spi ; spi <= XFRM6_TUNNEL_SPI_MAX ; spi ++ ) {
173- index = __xfrm6_tunnel_spi_check (spi );
163+ for (spi = xfrm6_tn -> spi ; spi <= XFRM6_TUNNEL_SPI_MAX ; spi ++ ) {
164+ index = __xfrm6_tunnel_spi_check (net , spi );
174165 if (index >= 0 )
175166 goto alloc_spi ;
176167 }
177- for (spi = XFRM6_TUNNEL_SPI_MIN ; spi < xfrm6_tunnel_spi ; spi ++ ) {
178- index = __xfrm6_tunnel_spi_check (spi );
168+ for (spi = XFRM6_TUNNEL_SPI_MIN ; spi < xfrm6_tn -> spi ; spi ++ ) {
169+ index = __xfrm6_tunnel_spi_check (net , spi );
179170 if (index >= 0 )
180171 goto alloc_spi ;
181172 }
182173 spi = 0 ;
183174 goto out ;
184175alloc_spi :
185- xfrm6_tunnel_spi = spi ;
176+ xfrm6_tn -> spi = spi ;
186177 x6spi = kmem_cache_alloc (xfrm6_tunnel_spi_kmem , GFP_ATOMIC );
187178 if (!x6spi )
188179 goto out ;
@@ -192,26 +183,26 @@ static u32 __xfrm6_tunnel_alloc_spi(xfrm_address_t *saddr)
192183 x6spi -> spi = spi ;
193184 atomic_set (& x6spi -> refcnt , 1 );
194185
195- hlist_add_head_rcu (& x6spi -> list_byspi , & xfrm6_tunnel_spi_byspi [index ]);
186+ hlist_add_head_rcu (& x6spi -> list_byspi , & xfrm6_tn -> spi_byspi [index ]);
196187
197188 index = xfrm6_tunnel_spi_hash_byaddr (saddr );
198- hlist_add_head_rcu (& x6spi -> list_byaddr , & xfrm6_tunnel_spi_byaddr [index ]);
189+ hlist_add_head_rcu (& x6spi -> list_byaddr , & xfrm6_tn -> spi_byaddr [index ]);
199190out :
200191 return spi ;
201192}
202193
203- __be32 xfrm6_tunnel_alloc_spi (xfrm_address_t * saddr )
194+ __be32 xfrm6_tunnel_alloc_spi (struct net * net , xfrm_address_t * saddr )
204195{
205196 struct xfrm6_tunnel_spi * x6spi ;
206197 u32 spi ;
207198
208199 spin_lock_bh (& xfrm6_tunnel_spi_lock );
209- x6spi = __xfrm6_tunnel_spi_lookup (saddr );
200+ x6spi = __xfrm6_tunnel_spi_lookup (net , saddr );
210201 if (x6spi ) {
211202 atomic_inc (& x6spi -> refcnt );
212203 spi = x6spi -> spi ;
213204 } else
214- spi = __xfrm6_tunnel_alloc_spi (saddr );
205+ spi = __xfrm6_tunnel_alloc_spi (net , saddr );
215206 spin_unlock_bh (& xfrm6_tunnel_spi_lock );
216207
217208 return htonl (spi );
@@ -225,15 +216,16 @@ static void x6spi_destroy_rcu(struct rcu_head *head)
225216 container_of (head , struct xfrm6_tunnel_spi , rcu_head ));
226217}
227218
228- void xfrm6_tunnel_free_spi (xfrm_address_t * saddr )
219+ void xfrm6_tunnel_free_spi (struct net * net , xfrm_address_t * saddr )
229220{
221+ struct xfrm6_tunnel_net * xfrm6_tn = xfrm6_tunnel_pernet (net );
230222 struct xfrm6_tunnel_spi * x6spi ;
231223 struct hlist_node * pos , * n ;
232224
233225 spin_lock_bh (& xfrm6_tunnel_spi_lock );
234226
235227 hlist_for_each_entry_safe (x6spi , pos , n ,
236- & xfrm6_tunnel_spi_byaddr [xfrm6_tunnel_spi_hash_byaddr (saddr )],
228+ & xfrm6_tn -> spi_byaddr [xfrm6_tunnel_spi_hash_byaddr (saddr )],
237229 list_byaddr )
238230 {
239231 if (memcmp (& x6spi -> addr , saddr , sizeof (x6spi -> addr )) == 0 ) {
@@ -263,10 +255,11 @@ static int xfrm6_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
263255
264256static int xfrm6_tunnel_rcv (struct sk_buff * skb )
265257{
258+ struct net * net = dev_net (skb -> dev );
266259 struct ipv6hdr * iph = ipv6_hdr (skb );
267260 __be32 spi ;
268261
269- spi = xfrm6_tunnel_spi_lookup ((xfrm_address_t * )& iph -> saddr );
262+ spi = xfrm6_tunnel_spi_lookup (net , (xfrm_address_t * )& iph -> saddr );
270263 return xfrm6_rcv_spi (skb , IPPROTO_IPV6 , spi ) > 0 ? : 0 ;
271264}
272265
@@ -326,7 +319,9 @@ static int xfrm6_tunnel_init_state(struct xfrm_state *x)
326319
327320static void xfrm6_tunnel_destroy (struct xfrm_state * x )
328321{
329- xfrm6_tunnel_free_spi ((xfrm_address_t * )& x -> props .saddr );
322+ struct net * net = xs_net (x );
323+
324+ xfrm6_tunnel_free_spi (net , (xfrm_address_t * )& x -> props .saddr );
330325}
331326
332327static const struct xfrm_type xfrm6_tunnel_type = {
@@ -351,6 +346,31 @@ static struct xfrm6_tunnel xfrm46_tunnel_handler = {
351346 .priority = 2 ,
352347};
353348
349+ static int __net_init xfrm6_tunnel_net_init (struct net * net )
350+ {
351+ struct xfrm6_tunnel_net * xfrm6_tn = xfrm6_tunnel_pernet (net );
352+ unsigned int i ;
353+
354+ for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYADDR_HSIZE ; i ++ )
355+ INIT_HLIST_HEAD (& xfrm6_tn -> spi_byaddr [i ]);
356+ for (i = 0 ; i < XFRM6_TUNNEL_SPI_BYSPI_HSIZE ; i ++ )
357+ INIT_HLIST_HEAD (& xfrm6_tn -> spi_byspi [i ]);
358+ xfrm6_tn -> spi = 0 ;
359+
360+ return 0 ;
361+ }
362+
363+ static void __net_exit xfrm6_tunnel_net_exit (struct net * net )
364+ {
365+ }
366+
367+ static struct pernet_operations xfrm6_tunnel_net_ops = {
368+ .init = xfrm6_tunnel_net_init ,
369+ .exit = xfrm6_tunnel_net_exit ,
370+ .id = & xfrm6_tunnel_net_id ,
371+ .size = sizeof (struct xfrm6_tunnel_net ),
372+ };
373+
354374static int __init xfrm6_tunnel_init (void )
355375{
356376 int rv ;
@@ -367,8 +387,13 @@ static int __init xfrm6_tunnel_init(void)
367387 rv = xfrm6_tunnel_spi_init ();
368388 if (rv < 0 )
369389 goto dereg46 ;
390+ rv = register_pernet_subsys (& xfrm6_tunnel_net_ops );
391+ if (rv < 0 )
392+ goto deregspi ;
370393 return 0 ;
371394
395+ deregspi :
396+ xfrm6_tunnel_spi_fini ();
372397dereg46 :
373398 xfrm6_tunnel_deregister (& xfrm46_tunnel_handler , AF_INET );
374399dereg6 :
@@ -381,6 +406,7 @@ static int __init xfrm6_tunnel_init(void)
381406
382407static void __exit xfrm6_tunnel_fini (void )
383408{
409+ unregister_pernet_subsys (& xfrm6_tunnel_net_ops );
384410 xfrm6_tunnel_spi_fini ();
385411 xfrm6_tunnel_deregister (& xfrm46_tunnel_handler , AF_INET );
386412 xfrm6_tunnel_deregister (& xfrm6_tunnel_handler , AF_INET6 );
0 commit comments