]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - net/netlabel/netlabel_domainhash.c
netlabel: Replace protocol/NetLabel linking with refrerence counts
[linux-2.6-omap-h63xx.git] / net / netlabel / netlabel_domainhash.c
index 9a8ea0195c4fbc2f08bb8c55a5535ae43889c809..0243f0c57b4157037b7d64d191b6652758472c7d 100644 (file)
@@ -30,8 +30,7 @@
  */
 
 #include <linux/types.h>
-#include <linux/rcupdate.h>
-#include <linux/list.h>
+#include <linux/rculist.h>
 #include <linux/skbuff.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
@@ -116,13 +115,13 @@ static u32 netlbl_domhsh_hash(const char *key)
 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
 {
        u32 bkt;
+       struct list_head *bkt_list;
        struct netlbl_dom_map *iter;
 
        if (domain != NULL) {
                bkt = netlbl_domhsh_hash(domain);
-               list_for_each_entry_rcu(iter,
-                                    &rcu_dereference(netlbl_domhsh)->tbl[bkt],
-                                    list)
+               bkt_list = &rcu_dereference(netlbl_domhsh)->tbl[bkt];
+               list_for_each_entry_rcu(iter, bkt_list, list)
                        if (iter->valid && strcmp(iter->domain, domain) == 0)
                                return iter;
        }
@@ -150,11 +149,11 @@ static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
        entry = netlbl_domhsh_search(domain);
        if (entry == NULL) {
                entry = rcu_dereference(netlbl_domhsh_def);
-               if (entry != NULL && entry->valid)
-                       return entry;
+               if (entry != NULL && !entry->valid)
+                       entry = NULL;
        }
 
-       return NULL;
+       return entry;
 }
 
 /*
@@ -171,7 +170,7 @@ static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
  * values on error.
  *
  */
-int netlbl_domhsh_init(u32 size)
+int __init netlbl_domhsh_init(u32 size)
 {
        u32 iter;
        struct netlbl_domhsh_tbl *hsh_tbl;
@@ -218,20 +217,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
        u32 bkt;
        struct audit_buffer *audit_buf;
 
-       switch (entry->type) {
-       case NETLBL_NLTYPE_UNLABELED:
-               ret_val = 0;
-               break;
-       case NETLBL_NLTYPE_CIPSOV4:
-               ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
-                                                 entry->domain);
-               break;
-       default:
-               return -EINVAL;
-       }
-       if (ret_val != 0)
-               return ret_val;
-
        entry->valid = 1;
        INIT_RCU_HEAD(&entry->rcu);
 
@@ -272,16 +257,6 @@ int netlbl_domhsh_add(struct netlbl_dom_map *entry,
        }
        rcu_read_unlock();
 
-       if (ret_val != 0) {
-               switch (entry->type) {
-               case NETLBL_NLTYPE_CIPSOV4:
-                       if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
-                                                      entry->domain) != 0)
-                               BUG();
-                       break;
-               }
-       }
-
        return ret_val;
 }
 
@@ -303,35 +278,26 @@ int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
 }
 
 /**
- * netlbl_domhsh_remove - Removes an entry from the domain hash table
- * @domain: the domain to remove
+ * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
+ * @entry: the entry to remove
  * @audit_info: NetLabel audit information
  *
  * Description:
  * Removes an entry from the domain hash table and handles any updates to the
- * lower level protocol handler (i.e. CIPSO).  Returns zero on success,
- * negative on failure.
+ * lower level protocol handler (i.e. CIPSO).  Caller is responsible for
+ * ensuring that the RCU read lock is held.  Returns zero on success, negative
+ * on failure.
  *
  */
-int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
+int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
+                              struct netlbl_audit *audit_info)
 {
-       int ret_val = -ENOENT;
-       struct netlbl_dom_map *entry;
+       int ret_val = 0;
        struct audit_buffer *audit_buf;
 
-       rcu_read_lock();
-       if (domain)
-               entry = netlbl_domhsh_search(domain);
-       else
-               entry = netlbl_domhsh_search_def(domain);
        if (entry == NULL)
-               goto remove_return;
-       switch (entry->type) {
-       case NETLBL_NLTYPE_CIPSOV4:
-               cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
-                                          entry->domain);
-               break;
-       }
+               return -ENOENT;
+
        spin_lock(&netlbl_domhsh_lock);
        if (entry->valid) {
                entry->valid = 0;
@@ -339,8 +305,8 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
                        list_del_rcu(&entry->list);
                else
                        rcu_assign_pointer(netlbl_domhsh_def, NULL);
-               ret_val = 0;
-       }
+       } else
+               ret_val = -ENOENT;
        spin_unlock(&netlbl_domhsh_lock);
 
        audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
@@ -352,10 +318,42 @@ int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
                audit_log_end(audit_buf);
        }
 
-remove_return:
-       rcu_read_unlock();
-       if (ret_val == 0)
+       if (ret_val == 0) {
+               switch (entry->type) {
+               case NETLBL_NLTYPE_CIPSOV4:
+                       cipso_v4_doi_putdef(entry->type_def.cipsov4);
+                       break;
+               }
                call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
+       }
+
+       return ret_val;
+}
+
+/**
+ * netlbl_domhsh_remove - Removes an entry from the domain hash table
+ * @domain: the domain to remove
+ * @audit_info: NetLabel audit information
+ *
+ * Description:
+ * Removes an entry from the domain hash table and handles any updates to the
+ * lower level protocol handler (i.e. CIPSO).  Returns zero on success,
+ * negative on failure.
+ *
+ */
+int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
+{
+       int ret_val;
+       struct netlbl_dom_map *entry;
+
+       rcu_read_lock();
+       if (domain)
+               entry = netlbl_domhsh_search(domain);
+       else
+               entry = netlbl_domhsh_search_def(domain);
+       ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
+       rcu_read_unlock();
+
        return ret_val;
 }
 
@@ -411,6 +409,7 @@ int netlbl_domhsh_walk(u32 *skip_bkt,
 {
        int ret_val = -ENOENT;
        u32 iter_bkt;
+       struct list_head *iter_list;
        struct netlbl_dom_map *iter_entry;
        u32 chain_cnt = 0;
 
@@ -418,9 +417,8 @@ int netlbl_domhsh_walk(u32 *skip_bkt,
        for (iter_bkt = *skip_bkt;
             iter_bkt < rcu_dereference(netlbl_domhsh)->size;
             iter_bkt++, chain_cnt = 0) {
-               list_for_each_entry_rcu(iter_entry,
-                               &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt],
-                               list)
+               iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
+               list_for_each_entry_rcu(iter_entry, iter_list, list)
                        if (iter_entry->valid) {
                                if (chain_cnt++ < *skip_chain)
                                        continue;