2626
2727static LIST_HEAD (ip_set_type_list ); /* all registered set types */
2828static DEFINE_MUTEX (ip_set_type_mutex ); /* protects ip_set_type_list */
29+ static DEFINE_RWLOCK (ip_set_ref_lock ); /* protects the set refs */
2930
3031static struct ip_set * * ip_set_list ; /* all individual sets */
3132static ip_set_id_t ip_set_max = CONFIG_IP_SET_MAX ; /* max number of sets */
@@ -301,13 +302,18 @@ EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
301302static inline void
302303__ip_set_get (ip_set_id_t index )
303304{
304- atomic_inc (& ip_set_list [index ]-> ref );
305+ write_lock_bh (& ip_set_ref_lock );
306+ ip_set_list [index ]-> ref ++ ;
307+ write_unlock_bh (& ip_set_ref_lock );
305308}
306309
307310static inline void
308311__ip_set_put (ip_set_id_t index )
309312{
310- atomic_dec (& ip_set_list [index ]-> ref );
313+ write_lock_bh (& ip_set_ref_lock );
314+ BUG_ON (ip_set_list [index ]-> ref == 0 );
315+ ip_set_list [index ]-> ref -- ;
316+ write_unlock_bh (& ip_set_ref_lock );
311317}
312318
313319/*
@@ -324,7 +330,7 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
324330 struct ip_set * set = ip_set_list [index ];
325331 int ret = 0 ;
326332
327- BUG_ON (set == NULL || atomic_read ( & set -> ref ) == 0 );
333+ BUG_ON (set == NULL );
328334 pr_debug ("set %s, index %u\n" , set -> name , index );
329335
330336 if (dim < set -> type -> dimension ||
@@ -356,7 +362,7 @@ ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
356362 struct ip_set * set = ip_set_list [index ];
357363 int ret ;
358364
359- BUG_ON (set == NULL || atomic_read ( & set -> ref ) == 0 );
365+ BUG_ON (set == NULL );
360366 pr_debug ("set %s, index %u\n" , set -> name , index );
361367
362368 if (dim < set -> type -> dimension ||
@@ -378,7 +384,7 @@ ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
378384 struct ip_set * set = ip_set_list [index ];
379385 int ret = 0 ;
380386
381- BUG_ON (set == NULL || atomic_read ( & set -> ref ) == 0 );
387+ BUG_ON (set == NULL );
382388 pr_debug ("set %s, index %u\n" , set -> name , index );
383389
384390 if (dim < set -> type -> dimension ||
@@ -397,7 +403,6 @@ EXPORT_SYMBOL_GPL(ip_set_del);
397403 * Find set by name, reference it once. The reference makes sure the
398404 * thing pointed to, does not go away under our feet.
399405 *
400- * The nfnl mutex must already be activated.
401406 */
402407ip_set_id_t
403408ip_set_get_byname (const char * name , struct ip_set * * set )
@@ -423,15 +428,12 @@ EXPORT_SYMBOL_GPL(ip_set_get_byname);
423428 * reference count by 1. The caller shall not assume the index
424429 * to be valid, after calling this function.
425430 *
426- * The nfnl mutex must already be activated.
427431 */
428432void
429433ip_set_put_byindex (ip_set_id_t index )
430434{
431- if (ip_set_list [index ] != NULL ) {
432- BUG_ON (atomic_read (& ip_set_list [index ]-> ref ) == 0 );
435+ if (ip_set_list [index ] != NULL )
433436 __ip_set_put (index );
434- }
435437}
436438EXPORT_SYMBOL_GPL (ip_set_put_byindex );
437439
@@ -441,15 +443,14 @@ EXPORT_SYMBOL_GPL(ip_set_put_byindex);
441443 * can't be destroyed. The set cannot be renamed due to
442444 * the referencing either.
443445 *
444- * The nfnl mutex must already be activated.
445446 */
446447const char *
447448ip_set_name_byindex (ip_set_id_t index )
448449{
449450 const struct ip_set * set = ip_set_list [index ];
450451
451452 BUG_ON (set == NULL );
452- BUG_ON (atomic_read ( & set -> ref ) == 0 );
453+ BUG_ON (set -> ref == 0 );
453454
454455 /* Referenced, so it's safe */
455456 return set -> name ;
@@ -515,18 +516,15 @@ void
515516ip_set_nfnl_put (ip_set_id_t index )
516517{
517518 nfnl_lock ();
518- if (ip_set_list [index ] != NULL ) {
519- BUG_ON (atomic_read (& ip_set_list [index ]-> ref ) == 0 );
520- __ip_set_put (index );
521- }
519+ ip_set_put_byindex (index );
522520 nfnl_unlock ();
523521}
524522EXPORT_SYMBOL_GPL (ip_set_nfnl_put );
525523
526524/*
527525 * Communication protocol with userspace over netlink.
528526 *
529- * We already locked by nfnl_lock .
527+ * The commands are serialized by the nfnl mutex .
530528 */
531529
532530static inline bool
@@ -657,7 +655,6 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
657655 return - ENOMEM ;
658656 rwlock_init (& set -> lock );
659657 strlcpy (set -> name , name , IPSET_MAXNAMELEN );
660- atomic_set (& set -> ref , 0 );
661658 set -> family = family ;
662659
663660 /*
@@ -690,8 +687,8 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
690687
691688 /*
692689 * Here, we have a valid, constructed set and we are protected
693- * by nfnl_lock . Find the first free index in ip_set_list and
694- * check clashing.
690+ * by the nfnl mutex . Find the first free index in ip_set_list
691+ * and check clashing.
695692 */
696693 if ((ret = find_free_id (set -> name , & index , & clash )) != 0 ) {
697694 /* If this is the same set and requested, ignore error */
@@ -751,31 +748,51 @@ ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
751748 const struct nlattr * const attr [])
752749{
753750 ip_set_id_t i ;
751+ int ret = 0 ;
754752
755753 if (unlikely (protocol_failed (attr )))
756754 return - IPSET_ERR_PROTOCOL ;
757755
758- /* References are protected by the nfnl mutex */
756+ /* Commands are serialized and references are
757+ * protected by the ip_set_ref_lock.
758+ * External systems (i.e. xt_set) must call
759+ * ip_set_put|get_nfnl_* functions, that way we
760+ * can safely check references here.
761+ *
762+ * list:set timer can only decrement the reference
763+ * counter, so if it's already zero, we can proceed
764+ * without holding the lock.
765+ */
766+ read_lock_bh (& ip_set_ref_lock );
759767 if (!attr [IPSET_ATTR_SETNAME ]) {
760768 for (i = 0 ; i < ip_set_max ; i ++ ) {
761- if (ip_set_list [i ] != NULL &&
762- (atomic_read (& ip_set_list [i ]-> ref )))
763- return - IPSET_ERR_BUSY ;
769+ if (ip_set_list [i ] != NULL && ip_set_list [i ]-> ref ) {
770+ ret = IPSET_ERR_BUSY ;
771+ goto out ;
772+ }
764773 }
774+ read_unlock_bh (& ip_set_ref_lock );
765775 for (i = 0 ; i < ip_set_max ; i ++ ) {
766776 if (ip_set_list [i ] != NULL )
767777 ip_set_destroy_set (i );
768778 }
769779 } else {
770780 i = find_set_id (nla_data (attr [IPSET_ATTR_SETNAME ]));
771- if (i == IPSET_INVALID_ID )
772- return - ENOENT ;
773- else if (atomic_read (& ip_set_list [i ]-> ref ))
774- return - IPSET_ERR_BUSY ;
781+ if (i == IPSET_INVALID_ID ) {
782+ ret = - ENOENT ;
783+ goto out ;
784+ } else if (ip_set_list [i ]-> ref ) {
785+ ret = - IPSET_ERR_BUSY ;
786+ goto out ;
787+ }
788+ read_unlock_bh (& ip_set_ref_lock );
775789
776790 ip_set_destroy_set (i );
777791 }
778792 return 0 ;
793+ out :
794+ read_unlock_bh (& ip_set_ref_lock );
795+ return ret ;
779796}
780797
781798/* Flush sets */
@@ -834,6 +851,7 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
834851 struct ip_set * set ;
835852 const char * name2 ;
836853 ip_set_id_t i ;
854+ int ret = 0 ;
837855
838856 if (unlikely (protocol_failed (attr ) ||
839857 attr [IPSET_ATTR_SETNAME ] == NULL ||
@@ -843,25 +861,33 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
843861 set = find_set (nla_data (attr [IPSET_ATTR_SETNAME ]));
844862 if (set == NULL )
845863 return - ENOENT ;
846- if (atomic_read (& set -> ref ) != 0 )
847- return - IPSET_ERR_REFERENCED ;
864+
865+ read_lock_bh (& ip_set_ref_lock );
866+ if (set -> ref != 0 ) {
867+ ret = - IPSET_ERR_REFERENCED ;
868+ goto out ;
869+ }
848870
849871 name2 = nla_data (attr [IPSET_ATTR_SETNAME2 ]);
850872 for (i = 0 ; i < ip_set_max ; i ++ ) {
851873 if (ip_set_list [i ] != NULL &&
852- STREQ (ip_set_list [i ]-> name , name2 ))
853- return - IPSET_ERR_EXIST_SETNAME2 ;
874+ STREQ (ip_set_list [i ]-> name , name2 )) {
875+ ret = - IPSET_ERR_EXIST_SETNAME2 ;
876+ goto out ;
877+ }
854878 }
855879 strncpy (set -> name , name2 , IPSET_MAXNAMELEN );
856880
857- return 0 ;
881+ out :
882+ read_unlock_bh (& ip_set_ref_lock );
883+ return ret ;
858884}
859885
860886/* Swap two sets so that name/index points to the other.
861887 * References and set names are also swapped.
862888 *
863- * We are protected by the nfnl mutex and references are
864- * manipulated only by holding the mutex . The kernel interfaces
889+ * The commands are serialized by the nfnl mutex and references are
890+ * protected by the ip_set_ref_lock . The kernel interfaces
865891 * do not hold the mutex but the pointer settings are atomic
866892 * so the ip_set_list always contains valid pointers to the sets.
867893 */
@@ -874,7 +900,6 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
874900 struct ip_set * from , * to ;
875901 ip_set_id_t from_id , to_id ;
876902 char from_name [IPSET_MAXNAMELEN ];
877- u32 from_ref ;
878903
879904 if (unlikely (protocol_failed (attr ) ||
880905 attr [IPSET_ATTR_SETNAME ] == NULL ||
@@ -899,17 +924,15 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
899924 from -> type -> family == to -> type -> family ))
900925 return - IPSET_ERR_TYPE_MISMATCH ;
901926
902- /* No magic here: ref munging protected by the nfnl_lock */
903927 strncpy (from_name , from -> name , IPSET_MAXNAMELEN );
904- from_ref = atomic_read (& from -> ref );
905-
906928 strncpy (from -> name , to -> name , IPSET_MAXNAMELEN );
907- atomic_set (& from -> ref , atomic_read (& to -> ref ));
908929 strncpy (to -> name , from_name , IPSET_MAXNAMELEN );
909- atomic_set (& to -> ref , from_ref );
910930
931+ write_lock_bh (& ip_set_ref_lock );
932+ swap (from -> ref , to -> ref );
911933 ip_set_list [from_id ] = to ;
912934 ip_set_list [to_id ] = from ;
935+ write_unlock_bh (& ip_set_ref_lock );
913936
914937 return 0 ;
915938}
@@ -926,7 +949,7 @@ ip_set_dump_done(struct netlink_callback *cb)
926949{
927950 if (cb -> args [2 ]) {
928951 pr_debug ("release set %s\n" , ip_set_list [cb -> args [1 ]]-> name );
929- __ip_set_put ((ip_set_id_t ) cb -> args [1 ]);
952+ ip_set_put_byindex ((ip_set_id_t ) cb -> args [1 ]);
930953 }
931954 return 0 ;
932955}
@@ -1068,7 +1091,7 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
10681091 /* If there was an error or set is done, release set */
10691092 if (ret || !cb -> args [2 ]) {
10701093 pr_debug ("release set %s\n" , ip_set_list [index ]-> name );
1071- __ip_set_put (index );
1094+ ip_set_put_byindex (index );
10721095 }
10731096
10741097 /* If we dump all sets, continue with dumping last ones */
0 commit comments