1414#include <linux/slab.h>
1515#include <net/gen_stats.h>
1616#include <net/netlink.h>
17+ #include <net/netns/generic.h>
1718
1819#include <linux/netfilter/x_tables.h>
1920#include <linux/netfilter/xt_RATEEST.h>
2021#include <net/netfilter/xt_rateest.h>
2122
22- static DEFINE_MUTEX (xt_rateest_mutex );
23-
2423#define RATEEST_HSIZE 16
25- static struct hlist_head rateest_hash [RATEEST_HSIZE ] __read_mostly ;
24+
25+ struct xt_rateest_net {
26+ struct mutex hash_lock ;
27+ struct hlist_head hash [RATEEST_HSIZE ];
28+ };
29+
30+ static unsigned int xt_rateest_id ;
31+
2632static unsigned int jhash_rnd __read_mostly ;
2733
2834static unsigned int xt_rateest_hash (const char * name )
@@ -31,21 +37,23 @@ static unsigned int xt_rateest_hash(const char *name)
3137 (RATEEST_HSIZE - 1 );
3238}
3339
34- static void xt_rateest_hash_insert (struct xt_rateest * est )
40+ static void xt_rateest_hash_insert (struct xt_rateest_net * xn ,
41+ struct xt_rateest * est )
3542{
3643 unsigned int h ;
3744
3845 h = xt_rateest_hash (est -> name );
39- hlist_add_head (& est -> list , & rateest_hash [h ]);
46+ hlist_add_head (& est -> list , & xn -> hash [h ]);
4047}
4148
42- static struct xt_rateest * __xt_rateest_lookup (const char * name )
49+ static struct xt_rateest * __xt_rateest_lookup (struct xt_rateest_net * xn ,
50+ const char * name )
4351{
4452 struct xt_rateest * est ;
4553 unsigned int h ;
4654
4755 h = xt_rateest_hash (name );
48- hlist_for_each_entry (est , & rateest_hash [h ], list ) {
56+ hlist_for_each_entry (est , & xn -> hash [h ], list ) {
4957 if (strcmp (est -> name , name ) == 0 ) {
5058 est -> refcnt ++ ;
5159 return est ;
@@ -55,20 +63,23 @@ static struct xt_rateest *__xt_rateest_lookup(const char *name)
5563 return NULL ;
5664}
5765
58- struct xt_rateest * xt_rateest_lookup (const char * name )
66+ struct xt_rateest * xt_rateest_lookup (struct net * net , const char * name )
5967{
68+ struct xt_rateest_net * xn = net_generic (net , xt_rateest_id );
6069 struct xt_rateest * est ;
6170
62- mutex_lock (& xt_rateest_mutex );
63- est = __xt_rateest_lookup (name );
64- mutex_unlock (& xt_rateest_mutex );
71+ mutex_lock (& xn -> hash_lock );
72+ est = __xt_rateest_lookup (xn , name );
73+ mutex_unlock (& xn -> hash_lock );
6574 return est ;
6675}
6776EXPORT_SYMBOL_GPL (xt_rateest_lookup );
6877
69- void xt_rateest_put (struct xt_rateest * est )
78+ void xt_rateest_put (struct net * net , struct xt_rateest * est )
7079{
71- mutex_lock (& xt_rateest_mutex );
80+ struct xt_rateest_net * xn = net_generic (net , xt_rateest_id );
81+
82+ mutex_lock (& xn -> hash_lock );
7283 if (-- est -> refcnt == 0 ) {
7384 hlist_del (& est -> list );
7485 gen_kill_estimator (& est -> rate_est );
@@ -78,7 +89,7 @@ void xt_rateest_put(struct xt_rateest *est)
7889 */
7990 kfree_rcu (est , rcu );
8091 }
81- mutex_unlock (& xt_rateest_mutex );
92+ mutex_unlock (& xn -> hash_lock );
8293}
8394EXPORT_SYMBOL_GPL (xt_rateest_put );
8495
@@ -98,6 +109,7 @@ xt_rateest_tg(struct sk_buff *skb, const struct xt_action_param *par)
98109
99110static int xt_rateest_tg_checkentry (const struct xt_tgchk_param * par )
100111{
112+ struct xt_rateest_net * xn = net_generic (par -> net , xt_rateest_id );
101113 struct xt_rateest_target_info * info = par -> targinfo ;
102114 struct xt_rateest * est ;
103115 struct {
@@ -108,18 +120,18 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
108120
109121 net_get_random_once (& jhash_rnd , sizeof (jhash_rnd ));
110122
111- mutex_lock (& xt_rateest_mutex );
112- est = __xt_rateest_lookup (info -> name );
123+ mutex_lock (& xn -> hash_lock );
124+ est = __xt_rateest_lookup (xn , info -> name );
113125 if (est ) {
114- mutex_unlock (& xt_rateest_mutex );
126+ mutex_unlock (& xn -> hash_lock );
115127 /*
116128 * If estimator parameters are specified, they must match the
117129 * existing estimator.
118130 */
119131 if ((!info -> interval && !info -> ewma_log ) ||
120132 (info -> interval != est -> params .interval ||
121133 info -> ewma_log != est -> params .ewma_log )) {
122- xt_rateest_put (est );
134+ xt_rateest_put (par -> net , est );
123135 return - EINVAL ;
124136 }
125137 info -> est = est ;
@@ -148,22 +160,22 @@ static int xt_rateest_tg_checkentry(const struct xt_tgchk_param *par)
148160 goto err2 ;
149161
150162 info -> est = est ;
151- xt_rateest_hash_insert (est );
152- mutex_unlock (& xt_rateest_mutex );
163+ xt_rateest_hash_insert (xn , est );
164+ mutex_unlock (& xn -> hash_lock );
153165 return 0 ;
154166
155167err2 :
156168 kfree (est );
157169err1 :
158- mutex_unlock (& xt_rateest_mutex );
170+ mutex_unlock (& xn -> hash_lock );
159171 return ret ;
160172}
161173
162174static void xt_rateest_tg_destroy (const struct xt_tgdtor_param * par )
163175{
164176 struct xt_rateest_target_info * info = par -> targinfo ;
165177
166- xt_rateest_put (info -> est );
178+ xt_rateest_put (par -> net , info -> est );
167179}
168180
169181static struct xt_target xt_rateest_tg_reg __read_mostly = {
@@ -178,19 +190,46 @@ static struct xt_target xt_rateest_tg_reg __read_mostly = {
178190 .me = THIS_MODULE ,
179191};
180192
181- static int __init xt_rateest_tg_init (void )
193+ static __net_init int xt_rateest_net_init (struct net * net )
194+ {
195+ struct xt_rateest_net * xn = net_generic (net , xt_rateest_id );
196+ int i ;
197+
198+ mutex_init (& xn -> hash_lock );
199+ for (i = 0 ; i < ARRAY_SIZE (xn -> hash ); i ++ )
200+ INIT_HLIST_HEAD (& xn -> hash [i ]);
201+ return 0 ;
202+ }
203+
204+ static void __net_exit xt_rateest_net_exit (struct net * net )
182205{
183- unsigned int i ;
206+ struct xt_rateest_net * xn = net_generic (net , xt_rateest_id );
207+ int i ;
208+
209+ for (i = 0 ; i < ARRAY_SIZE (xn -> hash ); i ++ )
210+ WARN_ON_ONCE (!hlist_empty (& xn -> hash [i ]));
211+ }
184212
185- for (i = 0 ; i < ARRAY_SIZE (rateest_hash ); i ++ )
186- INIT_HLIST_HEAD (& rateest_hash [i ]);
213+ static struct pernet_operations xt_rateest_net_ops = {
214+ .init = xt_rateest_net_init ,
215+ .exit = xt_rateest_net_exit ,
216+ .id = & xt_rateest_id ,
217+ .size = sizeof (struct xt_rateest_net ),
218+ };
219+
220+ static int __init xt_rateest_tg_init (void )
221+ {
222+ int err = register_pernet_subsys (& xt_rateest_net_ops );
187223
224+ if (err )
225+ return err ;
188226 return xt_register_target (& xt_rateest_tg_reg );
189227}
190228
191229static void __exit xt_rateest_tg_fini (void )
192230{
193231 xt_unregister_target (& xt_rateest_tg_reg );
232+ unregister_pernet_subsys (& xt_rateest_net_ops );
194233}
195234
196235
0 commit comments